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 <ft/ft.h>
42
43#include "ydb-internal.h"
44#include "ydb_txn.h"
45
46#include <memory>
47
48typedef enum {
49 YDB_LAYER_DIRECTORY_WRITE_LOCKS = 0, /* total directory write locks taken */
50 YDB_LAYER_DIRECTORY_WRITE_LOCKS_FAIL, /* total directory write locks unable to be taken */
51 YDB_LAYER_LOGSUPPRESS, /* number of times logs are suppressed for empty table (2440) */
52 YDB_LAYER_LOGSUPPRESS_FAIL, /* number of times unable to suppress logs for empty table (2440) */
53 YDB_DB_LAYER_STATUS_NUM_ROWS /* number of rows in this status array */
54} ydb_db_lock_layer_status_entry;
55
56typedef struct {
57 bool initialized;
58 TOKU_ENGINE_STATUS_ROW_S status[YDB_DB_LAYER_STATUS_NUM_ROWS];
59} YDB_DB_LAYER_STATUS_S, *YDB_DB_LAYER_STATUS;
60
61void ydb_db_layer_get_status(YDB_DB_LAYER_STATUS statp);
62
63//
64// export the following locktree create/destroy callbacks so
65// the environment can pass them to the locktree manager.
66//
67struct lt_on_create_callback_extra {
68 DB_TXN *txn;
69 FT_HANDLE ft_handle;
70};
71int toku_db_lt_on_create_callback(toku::locktree *lt, void *extra);
72void toku_db_lt_on_destroy_callback(toku::locktree *lt);
73
74/* db methods */
75static inline int db_opened(DB *db) {
76 return db->i->opened != 0;
77}
78
79static inline const toku::comparator &toku_db_get_comparator(DB *db) {
80 return toku_ft_get_comparator(db->i->ft_handle);
81}
82
83int toku_db_use_builtin_key_cmp(DB *db);
84int toku_db_pre_acquire_fileops_lock(DB *db, DB_TXN *txn);
85int toku_db_open_iname(DB * db, DB_TXN * txn, const char *iname, uint32_t flags, int mode);
86int toku_db_pre_acquire_table_lock(DB *db, DB_TXN *txn);
87int toku_db_get (DB * db, DB_TXN * txn, DBT * key, DBT * data, uint32_t flags);
88int toku_db_create(DB ** db, DB_ENV * env, uint32_t flags);
89int toku_db_close(DB * db);
90int toku_setup_db_internal (DB **dbp, DB_ENV *env, uint32_t flags, FT_HANDLE ft_handle, bool is_open);
91int db_getf_set(DB *db, DB_TXN *txn, uint32_t flags, DBT *key, YDB_CALLBACK_FUNCTION f, void *extra);
92int autotxn_db_get(DB* db, DB_TXN* txn, DBT* key, DBT* data, uint32_t flags);
93
94//TODO: DB_AUTO_COMMIT.
95//TODO: Nowait only conditionally?
96//TODO: NOSYNC change to SYNC if DB_ENV has something in set_flags
97static inline int
98toku_db_construct_autotxn(DB* db, DB_TXN **txn, bool* changed, bool force_auto_commit) {
99 assert(db && txn && changed);
100 DB_ENV* env = db->dbenv;
101 if (*txn || !(env->i->open_flags & DB_INIT_TXN)) {
102 *changed = false;
103 return 0;
104 }
105 bool nosync = (bool)(!force_auto_commit && !(env->i->open_flags & DB_AUTO_COMMIT));
106 uint32_t txn_flags = DB_TXN_NOWAIT | (nosync ? DB_TXN_NOSYNC : 0);
107 int r = toku_txn_begin(env, NULL, txn, txn_flags);
108 if (r!=0) return r;
109 *changed = true;
110 return 0;
111}
112
113static inline int
114toku_db_destruct_autotxn(DB_TXN *txn, int r, bool changed) {
115 if (!changed) return r;
116 if (r==0) {
117 r = locked_txn_commit(txn, 0);
118 }
119 else {
120 locked_txn_abort(txn);
121 }
122 return r;
123}
124
125void create_iname_hint(DB_ENV *env, const char *dname, char *hint);
126char *create_iname(DB_ENV *env,
127 uint64_t id1,
128 uint64_t id2,
129 char *hint,
130 const char *mark,
131 int n);
132std::unique_ptr<char[], decltype(&toku_free)> generate_iname_for_rename_or_open(
133 DB_ENV *env,
134 DB_TXN *txn,
135 const char *dname,
136 bool is_open);
137