modify build setup
[c11concurrency-benchmarks.git] / silo / new-benchmarks / ndb_database.h
1 #pragma once
2
3 #include <memory>
4
5 #include "abstract_db.h"
6 #include "../typed_txn_btree.h"
7 #include "../txn.h"
8 #include "../txn_impl.h"
9 #include "../txn_proto2_impl.h"
10
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;
15 public:
16
17   ndb_index(size_t value_size_hint,
18             bool mostly_append,
19             const std::string &name)
20     : super_type(value_size_hint, mostly_append, name),
21       name(name)
22   {}
23
24   virtual size_t
25   size() const OVERRIDE
26   {
27     return this->size_estimate();
28   }
29
30   virtual std::map<std::string, uint64_t>
31   clear() OVERRIDE
32   {
33 #ifdef TXN_BTREE_DUMP_PURGE_STATS
34     std::cerr << "purging txn index: " << name << std::endl;
35 #endif
36     return this->unsafe_purge(true);
37   }
38
39 private:
40   std::string name;
41 };
42
43 namespace private_ {
44
45   template <enum abstract_db::TxnProfileHint> struct ndb_txn_type {};
46
47   struct default_traits : public default_transaction_traits {
48     typedef str_arena StringAllocator;
49   };
50
51   // ycsb profiles
52
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;
61   };
62
63   struct hint_kv_rmw_traits : public hint_kv_get_put_traits {};
64
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;
73   };
74
75   // tpcc profiles
76
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;
85   };
86
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;
95   };
96
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;
105   };
106
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;
115   };
116
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;
125   };
126
127   struct hint_tpcc_order_status_read_only_traits : public hint_read_only_traits {};
128
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;
137   };
138
139   struct hint_tpcc_stock_level_read_only_traits : public hint_read_only_traits {};
140
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)
153
154 #define SPECIALIZE_OP_HINTS_X(hint, traitstype) \
155   template <> struct ndb_txn_type< hint > { \
156     typedef traitstype type; \
157   };
158
159 TXN_PROFILE_HINT_OP(SPECIALIZE_OP_HINTS_X)
160
161 #undef SPECIALIZE_OP_HINTS_X
162 }
163
164 template <template <typename> class Transaction>
165 class ndb_database : public abstract_db {
166 public:
167
168   template <typename Schema>
169   struct IndexType {
170     typedef ndb_index<Transaction, Schema> type;
171     typedef std::shared_ptr<type> ptr_type;
172   };
173
174   template <enum abstract_db::TxnProfileHint hint>
175   struct TransactionType
176   {
177     typedef Transaction<typename private_::ndb_txn_type<hint>::type> type;
178     typedef std::shared_ptr<type> ptr_type;
179   };
180
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
184   {
185     return std::make_shared<typename TransactionType<hint>::type>(txn_flags, arena);
186   }
187
188   typedef transaction_abort_exception abort_exception_type;
189
190   ssize_t txn_max_batch_size() const OVERRIDE { return 100; }
191
192   void
193   do_txn_epoch_sync() const OVERRIDE
194   {
195     txn_epoch_sync<Transaction>::sync();
196   }
197
198   void
199   do_txn_finish() const OVERRIDE
200   {
201     txn_epoch_sync<Transaction>::finish();
202   }
203
204   void
205   thread_init(bool loader) OVERRIDE
206   {
207     txn_epoch_sync<Transaction>::thread_init(loader);
208   }
209
210   void
211   thread_end() OVERRIDE
212   {
213     txn_epoch_sync<Transaction>::thread_end();
214   }
215
216   std::tuple<uint64_t, uint64_t, double>
217   get_ntxn_persisted() const OVERRIDE
218   {
219     return txn_epoch_sync<Transaction>::compute_ntxn_persisted();
220   }
221
222   void
223   reset_ntxn_persisted() OVERRIDE
224   {
225     txn_epoch_sync<Transaction>::reset_ntxn_persisted();
226   }
227
228   template <typename Schema>
229   inline typename IndexType<Schema>::ptr_type
230   open_index(const std::string &name,
231              size_t value_size_hint,
232              bool mostly_append)
233   {
234     return std::make_shared<typename IndexType<Schema>::type>(
235         value_size_hint, mostly_append, name);
236   }
237 };