5 #include "abstract_db.h"
6 #include "../typed_txn_btree.h"
8 #include "../txn_impl.h"
9 #include "../txn_proto2_impl.h"
11 template <template <typename> class Transaction, typename Schema>
12 class ndb_index : public abstract_ordered_index,
13 public typed_txn_btree<Transaction, Schema> {
14 typedef typed_txn_btree<Transaction, Schema> super_type;
17 ndb_index(size_t value_size_hint,
19 const std::string &name)
20 : super_type(value_size_hint, mostly_append, name),
27 return this->size_estimate();
30 virtual std::map<std::string, uint64_t>
33 #ifdef TXN_BTREE_DUMP_PURGE_STATS
34 std::cerr << "purging txn index: " << name << std::endl;
36 return this->unsafe_purge(true);
45 template <enum abstract_db::TxnProfileHint> struct ndb_txn_type {};
47 struct default_traits : public default_transaction_traits {
48 typedef str_arena StringAllocator;
53 struct hint_kv_get_put_traits {
54 static const size_t read_set_expected_size = 1;
55 static const size_t write_set_expected_size = 1;
56 static const size_t absent_set_expected_size = 1;
57 static const bool stable_input_memory = true;
58 static const bool hard_expected_sizes = true;
59 static const bool read_own_writes = false;
60 typedef str_arena StringAllocator;
63 struct hint_kv_rmw_traits : public hint_kv_get_put_traits {};
65 struct hint_kv_scan_traits {
66 static const size_t read_set_expected_size = 100;
67 static const size_t write_set_expected_size = 1;
68 static const size_t absent_set_expected_size = read_set_expected_size / 7 + 1;
69 static const bool stable_input_memory = true;
70 static const bool hard_expected_sizes = false;
71 static const bool read_own_writes = false;
72 typedef str_arena StringAllocator;
77 struct hint_read_only_traits {
78 static const size_t read_set_expected_size = 1;
79 static const size_t write_set_expected_size = 1;
80 static const size_t absent_set_expected_size = 1;
81 static const bool stable_input_memory = true;
82 static const bool hard_expected_sizes = true;
83 static const bool read_own_writes = false;
84 typedef str_arena StringAllocator;
87 struct hint_tpcc_new_order_traits {
88 static const size_t read_set_expected_size = 35;
89 static const size_t write_set_expected_size = 35;
90 static const size_t absent_set_expected_size = 1;
91 static const bool stable_input_memory = true;
92 static const bool hard_expected_sizes = true;
93 static const bool read_own_writes = false;
94 typedef str_arena StringAllocator;
97 struct hint_tpcc_payment_traits {
98 static const size_t read_set_expected_size = 85;
99 static const size_t write_set_expected_size = 10;
100 static const size_t absent_set_expected_size = 15;
101 static const bool stable_input_memory = true;
102 static const bool hard_expected_sizes = false;
103 static const bool read_own_writes = false;
104 typedef str_arena StringAllocator;
107 struct hint_tpcc_delivery_traits {
108 static const size_t read_set_expected_size = 175;
109 static const size_t write_set_expected_size = 175;
110 static const size_t absent_set_expected_size = 35;
111 static const bool stable_input_memory = true;
112 static const bool hard_expected_sizes = false;
113 static const bool read_own_writes = false;
114 typedef str_arena StringAllocator;
117 struct hint_tpcc_order_status_traits {
118 static const size_t read_set_expected_size = 95;
119 static const size_t write_set_expected_size = 1;
120 static const size_t absent_set_expected_size = 25;
121 static const bool stable_input_memory = true;
122 static const bool hard_expected_sizes = false;
123 static const bool read_own_writes = false;
124 typedef str_arena StringAllocator;
127 struct hint_tpcc_order_status_read_only_traits : public hint_read_only_traits {};
129 struct hint_tpcc_stock_level_traits {
130 static const size_t read_set_expected_size = 500;
131 static const size_t write_set_expected_size = 1;
132 static const size_t absent_set_expected_size = 25;
133 static const bool stable_input_memory = true;
134 static const bool hard_expected_sizes = false;
135 static const bool read_own_writes = false;
136 typedef str_arena StringAllocator;
139 struct hint_tpcc_stock_level_read_only_traits : public hint_read_only_traits {};
141 #define TXN_PROFILE_HINT_OP(x) \
142 x(abstract_db::HINT_DEFAULT, default_traits) \
143 x(abstract_db::HINT_KV_GET_PUT, hint_kv_get_put_traits) \
144 x(abstract_db::HINT_KV_RMW, hint_kv_rmw_traits) \
145 x(abstract_db::HINT_KV_SCAN, hint_kv_scan_traits) \
146 x(abstract_db::HINT_TPCC_NEW_ORDER, hint_tpcc_new_order_traits) \
147 x(abstract_db::HINT_TPCC_PAYMENT, hint_tpcc_payment_traits) \
148 x(abstract_db::HINT_TPCC_DELIVERY, hint_tpcc_delivery_traits) \
149 x(abstract_db::HINT_TPCC_ORDER_STATUS, hint_tpcc_order_status_traits) \
150 x(abstract_db::HINT_TPCC_ORDER_STATUS_READ_ONLY, hint_tpcc_order_status_read_only_traits) \
151 x(abstract_db::HINT_TPCC_STOCK_LEVEL, hint_tpcc_stock_level_traits) \
152 x(abstract_db::HINT_TPCC_STOCK_LEVEL_READ_ONLY, hint_tpcc_stock_level_read_only_traits)
154 #define SPECIALIZE_OP_HINTS_X(hint, traitstype) \
155 template <> struct ndb_txn_type< hint > { \
156 typedef traitstype type; \
159 TXN_PROFILE_HINT_OP(SPECIALIZE_OP_HINTS_X)
161 #undef SPECIALIZE_OP_HINTS_X
164 template <template <typename> class Transaction>
165 class ndb_database : public abstract_db {
168 template <typename Schema>
170 typedef ndb_index<Transaction, Schema> type;
171 typedef std::shared_ptr<type> ptr_type;
174 template <enum abstract_db::TxnProfileHint hint>
175 struct TransactionType
177 typedef Transaction<typename private_::ndb_txn_type<hint>::type> type;
178 typedef std::shared_ptr<type> ptr_type;
181 template <enum abstract_db::TxnProfileHint hint>
182 inline typename TransactionType<hint>::ptr_type
183 new_txn(uint64_t txn_flags, str_arena &arena) const
185 return std::make_shared<typename TransactionType<hint>::type>(txn_flags, arena);
188 typedef transaction_abort_exception abort_exception_type;
190 ssize_t txn_max_batch_size() const OVERRIDE { return 100; }
193 do_txn_epoch_sync() const OVERRIDE
195 txn_epoch_sync<Transaction>::sync();
199 do_txn_finish() const OVERRIDE
201 txn_epoch_sync<Transaction>::finish();
205 thread_init(bool loader) OVERRIDE
207 txn_epoch_sync<Transaction>::thread_init(loader);
211 thread_end() OVERRIDE
213 txn_epoch_sync<Transaction>::thread_end();
216 std::tuple<uint64_t, uint64_t, double>
217 get_ntxn_persisted() const OVERRIDE
219 return txn_epoch_sync<Transaction>::compute_ntxn_persisted();
223 reset_ntxn_persisted() OVERRIDE
225 txn_epoch_sync<Transaction>::reset_ntxn_persisted();
228 template <typename Schema>
229 inline typename IndexType<Schema>::ptr_type
230 open_index(const std::string &name,
231 size_t value_size_hint,
234 return std::make_shared<typename IndexType<Schema>::type>(
235 value_size_hint, mostly_append, name);