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#pragma once
40
41#include <db.h>
42
43#include "ft/cachetable/cachetable.h"
44#include "ft/ft-ops.h"
45#include "ft/logger/log.h"
46#include "util/dbt.h"
47
48typedef struct ft *FT;
49typedef struct ft_options *FT_OPTIONS;
50
51// unlink a ft from the filesystem with or without a txn.
52// if with a txn, then the unlink happens on commit.
53void toku_ft_unlink(FT_HANDLE handle);
54void toku_ft_unlink_on_commit(FT_HANDLE handle, TOKUTXN txn);
55
56int toku_ft_rename_iname(DB_TXN *txn,
57 const char *data_dir,
58 const char *old_iname,
59 const char *new_iname,
60 CACHETABLE ct);
61
62void toku_ft_init_reflock(FT ft);
63void toku_ft_destroy_reflock(FT ft);
64void toku_ft_grab_reflock(FT ft);
65void toku_ft_release_reflock(FT ft);
66
67void toku_ft_lock(struct ft *ft);
68void toku_ft_unlock(struct ft *ft);
69
70void toku_ft_create(FT *ftp, FT_OPTIONS options, CACHEFILE cf, TOKUTXN txn);
71void toku_ft_free (FT ft);
72
73int toku_read_ft_and_store_in_cachefile (FT_HANDLE ft_h, CACHEFILE cf, LSN max_acceptable_lsn, FT *header);
74void toku_ft_note_ft_handle_open(FT ft, FT_HANDLE live);
75
76bool toku_ft_needed_unlocked(FT ft);
77bool toku_ft_has_one_reference_unlocked(FT ft);
78
79// evict a ft from memory by closing its cachefile. any future work
80// will have to read in the ft in a new cachefile and new FT object.
81void toku_ft_evict_from_memory(FT ft, bool oplsn_valid, LSN oplsn);
82
83FT_HANDLE toku_ft_get_only_existing_ft_handle(FT ft);
84
85void toku_ft_note_hot_begin(FT_HANDLE ft_h);
86void toku_ft_note_hot_complete(FT_HANDLE ft_h, bool success, MSN msn_at_start_of_hot);
87
88void
89toku_ft_init(
90 FT ft,
91 BLOCKNUM root_blocknum_on_disk,
92 LSN checkpoint_lsn,
93 TXNID root_xid_that_created,
94 uint32_t target_nodesize,
95 uint32_t target_basementnodesize,
96 enum toku_compression_method compression_method,
97 uint32_t fanout
98 );
99
100int toku_dictionary_redirect_abort(FT old_h, FT new_h, TOKUTXN txn) __attribute__ ((warn_unused_result));
101int toku_dictionary_redirect (const char *dst_fname_in_env, FT_HANDLE old_ft, TOKUTXN txn);
102void toku_reset_root_xid_that_created(FT ft, TXNID new_root_xid_that_created);
103// Reset the root_xid_that_created field to the given value.
104// This redefines which xid created the dictionary.
105
106void toku_ft_add_txn_ref(FT ft);
107void toku_ft_remove_txn_ref(FT ft);
108
109void toku_calculate_root_offset_pointer (FT ft, CACHEKEY* root_key, uint32_t *roothash);
110void toku_ft_set_new_root_blocknum(FT ft, CACHEKEY new_root_key);
111LSN toku_ft_checkpoint_lsn(FT ft) __attribute__ ((warn_unused_result));
112void toku_ft_stat64 (FT ft, struct ftstat64_s *s);
113void toku_ft_get_fractal_tree_info64 (FT ft, struct ftinfo64 *s);
114int toku_ft_iterate_fractal_tree_block_map(FT ft, int (*iter)(uint64_t,int64_t,int64_t,int64_t,int64_t,void*), void *iter_extra);
115
116// unconditionally set the descriptor for an open FT. can't do this when
117// any operation has already occurred on the ft.
118// see toku_ft_change_descriptor(), which is the transactional version
119// used by the ydb layer. it better describes the client contract.
120void toku_ft_update_descriptor(FT ft, DESCRIPTOR desc);
121// use this version if the FT is not fully user-opened with a valid cachefile.
122// this is a clean hack to get deserialization code to update a descriptor
123// while the FT and cf are in the process of opening, for upgrade purposes
124void toku_ft_update_descriptor_with_fd(FT ft, DESCRIPTOR desc, int fd);
125void toku_ft_update_cmp_descriptor(FT ft);
126
127// get the descriptor for a ft. safe to read as long as clients honor the
128// strict contract put forth by toku_ft_update_descriptor/toku_ft_change_descriptor
129// essentially, there should never be a reader while there is a writer, enforced
130// by the client, not the FT.
131DESCRIPTOR toku_ft_get_descriptor(FT_HANDLE ft_handle);
132DESCRIPTOR toku_ft_get_cmp_descriptor(FT_HANDLE ft_handle);
133
134typedef struct {
135 // delta versions in basements could be negative
136 // These represent the physical leaf entries and do not account
137 // for pending deletes or other in-flight messages that have not been
138 // applied to a leaf entry.
139 int64_t numrows;
140 int64_t numbytes;
141} STAT64INFO_S, *STAT64INFO;
142static const STAT64INFO_S ZEROSTATS = { .numrows = 0, .numbytes = 0 };
143
144void toku_ft_update_stats(STAT64INFO headerstats, STAT64INFO_S delta);
145void toku_ft_decrease_stats(STAT64INFO headerstats, STAT64INFO_S delta);
146void toku_ft_adjust_logical_row_count(FT ft, int64_t delta);
147
148typedef void (*remove_ft_ref_callback)(FT ft, void *extra);
149void toku_ft_remove_reference(FT ft,
150 bool oplsn_valid, LSN oplsn,
151 remove_ft_ref_callback remove_ref, void *extra);
152
153void toku_ft_set_nodesize(FT ft, unsigned int nodesize);
154void toku_ft_get_nodesize(FT ft, unsigned int *nodesize);
155void toku_ft_set_basementnodesize(FT ft, unsigned int basementnodesize);
156void toku_ft_get_basementnodesize(FT ft, unsigned int *basementnodesize);
157void toku_ft_set_compression_method(FT ft, enum toku_compression_method method);
158void toku_ft_get_compression_method(FT ft, enum toku_compression_method *methodp);
159void toku_ft_set_fanout(FT ft, unsigned int fanout);
160void toku_ft_get_fanout(FT ft, unsigned int *fanout);
161
162// mark the ft as a blackhole. any message injections will be a no op.
163void toku_ft_set_blackhole(FT_HANDLE ft_handle);
164
165// Effect: Calculates the total space and used space for a FT's leaf data.
166// The difference between the two is MVCC garbage.
167void toku_ft_get_garbage(FT ft, uint64_t *total_space, uint64_t *used_space);
168
169// TODO: Should be in portability
170int get_num_cores(void);
171
172// TODO: Use the cachetable's worker pool instead of something managed by the FT...
173struct toku_thread_pool *get_ft_pool(void);
174
175// TODO: Should be in portability
176int toku_single_process_lock(const char *lock_dir, const char *which, int *lockfd);
177int toku_single_process_unlock(int *lockfd);
178
179void tokuft_update_product_name_strings(void);
180#define TOKU_MAX_PRODUCT_NAME_LENGTH (256)
181extern char toku_product_name[TOKU_MAX_PRODUCT_NAME_LENGTH];
182
183struct toku_product_name_strings_struct {
184 char db_version[sizeof(toku_product_name) + sizeof("1.2.3 build ") + 256];
185 char environmentdictionary[sizeof(toku_product_name) + sizeof(".environment")];
186 char fileopsdirectory[sizeof(toku_product_name) + sizeof(".directory")];
187 char single_process_lock[sizeof(toku_product_name) + sizeof("___lock_dont_delete_me")];
188 char rollback_cachefile[sizeof(toku_product_name) + sizeof(".rollback")];
189};
190
191extern struct toku_product_name_strings_struct toku_product_name_strings;
192extern int tokuft_num_envs;
193