1/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3#ident "$Id$"
4/*======
5This file is part of PerconaFT.
6
7
8Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35======= */
36
37#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39#include "ft/serialize/block_table.h"
40#include "ft/ft.h"
41#include "ft/ft-internal.h"
42#include "ft/cursor.h"
43
44struct recount_rows_extra_t {
45 int (*_progress_callback)(
46 uint64_t count,
47 uint64_t deleted,
48 void* progress_extra);
49 void* _progress_extra;
50 uint64_t _keys;
51 bool _cancelled;
52};
53
54static int recount_rows_found(
55 uint32_t UU(keylen),
56 const void* key,
57 uint32_t UU(vallen),
58 const void* UU(val),
59 void* extra,
60 bool UU(lock_only)) {
61
62 recount_rows_extra_t* rre = (recount_rows_extra_t*)extra;
63
64 if (FT_LIKELY(key != nullptr)) {
65 rre->_keys++;
66 }
67 return rre->_cancelled
68 = rre->_progress_callback(rre->_keys, 0, rre->_progress_extra);
69}
70static bool recount_rows_interrupt(void* extra, uint64_t deleted_rows) {
71 recount_rows_extra_t* rre = (recount_rows_extra_t*)extra;
72
73 return rre->_cancelled =
74 rre->_progress_callback(rre->_keys, deleted_rows, rre->_progress_extra);
75}
76int toku_ft_recount_rows(FT_HANDLE ft,
77 int (*progress_callback)(uint64_t count,
78 uint64_t deleted,
79 void* progress_extra),
80 void* progress_extra) {
81 int ret = 0;
82 recount_rows_extra_t rre = {progress_callback, progress_extra, 0, false};
83
84 ft_cursor c;
85 ret = toku_ft_cursor_create(ft, &c, nullptr, C_READ_ANY, false, false);
86 if (ret)
87 return ret;
88
89 toku_ft_cursor_set_check_interrupt_cb(&c, recount_rows_interrupt, &rre);
90
91 ret = toku_ft_cursor_first(&c, recount_rows_found, &rre);
92 while (FT_LIKELY(ret == 0)) {
93 ret = toku_ft_cursor_next(&c, recount_rows_found, &rre);
94 }
95
96 toku_ft_cursor_destroy(&c);
97
98 if (rre._cancelled == false) {
99 // update ft count
100 toku_unsafe_set(&ft->ft->in_memory_logical_rows, rre._keys);
101 ft->ft->h->dirty = 1;
102 ret = 0;
103 }
104
105 return ret;
106}
107