benchmark silo added
[c11concurrency-benchmarks.git] / silo / benchmarks / ndb_wrapper.h
1 #ifndef _NDB_WRAPPER_H_
2 #define _NDB_WRAPPER_H_
3
4 #include "abstract_db.h"
5 #include "../txn_btree.h"
6
7 namespace private_ {
8   struct ndbtxn {
9     abstract_db::TxnProfileHint hint;
10     char buf[0];
11   } PACKED;
12
13   // XXX: doesn't check to make sure you are passing in an ndbtx
14   // of the right hint
15   template <template <typename> class Transaction, typename Traits>
16   struct cast_base {
17     typedef Transaction<Traits> type;
18     inline ALWAYS_INLINE type *
19     operator()(struct ndbtxn *p) const
20     {
21       return reinterpret_cast<type *>(&p->buf[0]);
22     }
23   };
24
25   STATIC_COUNTER_DECL(scopedperf::tsc_ctr, ndb_get_probe0, ndb_get_probe0_cg)
26   STATIC_COUNTER_DECL(scopedperf::tsc_ctr, ndb_put_probe0, ndb_put_probe0_cg)
27   STATIC_COUNTER_DECL(scopedperf::tsc_ctr, ndb_insert_probe0, ndb_insert_probe0_cg)
28   STATIC_COUNTER_DECL(scopedperf::tsc_ctr, ndb_scan_probe0, ndb_scan_probe0_cg)
29   STATIC_COUNTER_DECL(scopedperf::tsc_ctr, ndb_remove_probe0, ndb_remove_probe0_cg)
30   STATIC_COUNTER_DECL(scopedperf::tsc_ctr, ndb_dtor_probe0, ndb_dtor_probe0_cg)
31 }
32
33 template <template <typename> class Transaction>
34 class ndb_wrapper : public abstract_db {
35 protected:
36   typedef private_::ndbtxn ndbtxn;
37   template <typename Traits>
38     using cast = private_::cast_base<Transaction, Traits>;
39
40 public:
41
42   ndb_wrapper(
43       const std::vector<std::string> &logfiles,
44       const std::vector<std::vector<unsigned>> &assignments_given,
45       bool call_fsync,
46       bool use_compression,
47       bool fake_writes);
48
49   virtual ssize_t txn_max_batch_size() const OVERRIDE { return 100; }
50
51   virtual void
52   do_txn_epoch_sync() const
53   {
54     txn_epoch_sync<Transaction>::sync();
55   }
56
57   virtual void
58   do_txn_finish() const
59   {
60     txn_epoch_sync<Transaction>::finish();
61   }
62
63   virtual void
64   thread_init(bool loader)
65   {
66     txn_epoch_sync<Transaction>::thread_init(loader);
67   }
68
69   virtual void
70   thread_end()
71   {
72     txn_epoch_sync<Transaction>::thread_end();
73   }
74
75   virtual std::tuple<uint64_t, uint64_t, double>
76   get_ntxn_persisted() const
77   {
78     return txn_epoch_sync<Transaction>::compute_ntxn_persisted();
79   }
80
81   virtual void
82   reset_ntxn_persisted()
83   {
84     txn_epoch_sync<Transaction>::reset_ntxn_persisted();
85   }
86
87   virtual size_t
88   sizeof_txn_object(uint64_t txn_flags) const;
89
90   virtual void *new_txn(
91       uint64_t txn_flags,
92       str_arena &arena,
93       void *buf,
94       TxnProfileHint hint);
95   virtual bool commit_txn(void *txn);
96   virtual void abort_txn(void *txn);
97   virtual void print_txn_debug(void *txn) const;
98   virtual std::map<std::string, uint64_t> get_txn_counters(void *txn) const;
99
100   virtual abstract_ordered_index *
101   open_index(const std::string &name,
102              size_t value_size_hint,
103              bool mostly_append);
104
105   virtual void
106   close_index(abstract_ordered_index *idx);
107
108 };
109
110 template <template <typename> class Transaction>
111 class ndb_ordered_index : public abstract_ordered_index {
112 protected:
113   typedef private_::ndbtxn ndbtxn;
114   template <typename Traits>
115     using cast = private_::cast_base<Transaction, Traits>;
116
117 public:
118   ndb_ordered_index(const std::string &name, size_t value_size_hint, bool mostly_append);
119   virtual bool get(
120       void *txn,
121       const std::string &key,
122       std::string &value, size_t max_bytes_read);
123   virtual const char * put(
124       void *txn,
125       const std::string &key,
126       const std::string &value);
127   virtual const char * put(
128       void *txn,
129       std::string &&key,
130       std::string &&value);
131   virtual const char *
132   insert(void *txn,
133          const std::string &key,
134          const std::string &value);
135   virtual const char *
136   insert(void *txn,
137          std::string &&key,
138          std::string &&value);
139   virtual void scan(
140       void *txn,
141       const std::string &start_key,
142       const std::string *end_key,
143       scan_callback &callback,
144       str_arena *arena);
145   virtual void rscan(
146       void *txn,
147       const std::string &start_key,
148       const std::string *end_key,
149       scan_callback &callback,
150       str_arena *arena);
151   virtual void remove(
152       void *txn,
153       const std::string &key);
154   virtual void remove(
155       void *txn,
156       std::string &&key);
157   virtual size_t size() const;
158   virtual std::map<std::string, uint64_t> clear();
159 private:
160   std::string name;
161   txn_btree<Transaction> btr;
162 };
163
164 #endif /* _NDB_WRAPPER_H_ */