Merge branch 'master' of /home/git/concurrency-benchmarks
[c11concurrency-benchmarks.git] / silo / benchmarks / abstract_db.h
1 #ifndef _ABSTRACT_DB_H_
2 #define _ABSTRACT_DB_H_
3
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <sys/types.h>
7
8 #include <map>
9 #include <string>
10
11 #include "abstract_ordered_index.h"
12 #include "../str_arena.h"
13
14 /**
15  * Abstract interface for a DB. This is to facilitate writing
16  * benchmarks for different systems, making each system present
17  * a unified interface
18  */
19 class abstract_db {
20 public:
21
22   /**
23    * both get() and put() can throw abstract_abort_exception. If thrown,
24    * abort_txn() must be called (calling commit_txn() will result in undefined
25    * behavior).  Also if thrown, subsequently calling get()/put() will also
26    * result in undefined behavior)
27    */
28   class abstract_abort_exception {};
29
30   // ctor should open db
31   abstract_db() {}
32
33   // dtor should close db
34   virtual ~abstract_db() {}
35
36   /**
37    * an approximate max batch size for updates in a transaction.
38    *
39    * A return value of -1 indicates no maximum
40    */
41   virtual ssize_t txn_max_batch_size() const { return -1; }
42
43   virtual bool index_has_stable_put_memory() const { return false; }
44
45   // XXX(stephentu): laziness
46   virtual size_t
47   sizeof_txn_object(uint64_t txn_flags) const { NDB_UNIMPLEMENTED("sizeof_txn_object"); };
48
49   /**
50    * XXX(stephentu): hack
51    */
52   virtual void do_txn_epoch_sync() const {}
53
54   /**
55    * XXX(stephentu): hack
56    */
57   virtual void do_txn_finish() const {}
58
59   /** loader should be used as a performance hint, not for correctness */
60   virtual void thread_init(bool loader) {}
61
62   virtual void thread_end() {}
63
64   // [ntxns_persisted, ntxns_committed, avg latency]
65   virtual std::tuple<uint64_t, uint64_t, double>
66     get_ntxn_persisted() const { return std::make_tuple(0, 0, 0.0); }
67
68   virtual void reset_ntxn_persisted() { }
69
70   enum TxnProfileHint {
71     HINT_DEFAULT,
72
73     // ycsb profiles
74     HINT_KV_GET_PUT, // KV workloads over a single key
75     HINT_KV_RMW, // get/put over a single key
76     HINT_KV_SCAN, // KV scan workloads (~100 keys)
77
78     // tpcc profiles
79     HINT_TPCC_NEW_ORDER,
80     HINT_TPCC_PAYMENT,
81     HINT_TPCC_DELIVERY,
82     HINT_TPCC_ORDER_STATUS,
83     HINT_TPCC_ORDER_STATUS_READ_ONLY,
84     HINT_TPCC_STOCK_LEVEL,
85     HINT_TPCC_STOCK_LEVEL_READ_ONLY,
86   };
87
88   /**
89    * Initializes a new txn object the space pointed to by buf
90    *
91    * Flags is only for the ndb protocol for now
92    *
93    * [buf, buf + sizeof_txn_object(txn_flags)) is a valid ptr
94    */
95   virtual void *new_txn(
96       uint64_t txn_flags,
97       str_arena &arena,
98       void *buf,
99       TxnProfileHint hint = HINT_DEFAULT) = 0;
100
101   typedef std::map<std::string, uint64_t> counter_map;
102   typedef std::map<std::string, counter_map> txn_counter_map;
103
104   /**
105    * Reports things like read/write set sizes
106    */
107   virtual counter_map
108   get_txn_counters(void *txn) const
109   {
110     return counter_map();
111   }
112
113   /**
114    * Returns true on successful commit.
115    *
116    * On failure, can either throw abstract_abort_exception, or
117    * return false- caller should be prepared to deal with both cases
118    */
119   virtual bool commit_txn(void *txn) = 0;
120
121   /**
122    * XXX
123    */
124   virtual void abort_txn(void *txn) = 0;
125
126   virtual void print_txn_debug(void *txn) const {}
127
128   virtual abstract_ordered_index *
129   open_index(const std::string &name,
130              size_t value_size_hint,
131              bool mostly_append = false) = 0;
132
133   virtual void
134   close_index(abstract_ordered_index *idx) = 0;
135 };
136
137 #endif /* _ABSTRACT_DB_H_ */