Merge branch 'master' of /home/git/concurrency-benchmarks
[c11concurrency-benchmarks.git] / silo / benchmarks / abstract_ordered_index.h
1 #ifndef _ABSTRACT_ORDERED_INDEX_H_
2 #define _ABSTRACT_ORDERED_INDEX_H_
3
4 #include <stdint.h>
5 #include <string>
6 #include <utility>
7 #include <map>
8
9 #include "../macros.h"
10 #include "../str_arena.h"
11
12 /**
13  * The underlying index manages memory for keys/values, but
14  * may choose to expose the underlying memory to callers
15  * (see put() and inesrt()).
16  */
17 class abstract_ordered_index {
18 public:
19
20   virtual ~abstract_ordered_index() {}
21
22   /**
23    * Get a key of length keylen. The underlying DB does not manage
24    * the memory associated with key. Returns true if found, false otherwise
25    */
26   virtual bool get(
27       void *txn,
28       const std::string &key,
29       std::string &value,
30       size_t max_bytes_read = std::string::npos) = 0;
31
32   class scan_callback {
33   public:
34     virtual ~scan_callback() {}
35     // XXX(stephentu): key is passed as (const char *, size_t) pair
36     // because it really should be the string_type of the underlying
37     // tree, but since abstract_ordered_index is not templated we can't
38     // really do better than this for now
39     //
40     // we keep value as std::string b/c we have more control over how those
41     // strings are generated
42     virtual bool invoke(const char *keyp, size_t keylen,
43                         const std::string &value) = 0;
44   };
45
46   /**
47    * Search [start_key, *end_key) if end_key is not null, otherwise
48    * search [start_key, +infty)
49    */
50   virtual void scan(
51       void *txn,
52       const std::string &start_key,
53       const std::string *end_key,
54       scan_callback &callback,
55       str_arena *arena = nullptr) = 0;
56
57   /**
58    * Search (*end_key, start_key] if end_key is not null, otherwise
59    * search (-infty, start_key] (starting at start_key and traversing
60    * backwards)
61    */
62   virtual void rscan(
63       void *txn,
64       const std::string &start_key,
65       const std::string *end_key,
66       scan_callback &callback,
67       str_arena *arena = nullptr) = 0;
68
69   /**
70    * Put a key of length keylen, with mapping of length valuelen.
71    * The underlying DB does not manage the memory pointed to by key or value
72    * (a copy is made).
73    *
74    * If a record with key k exists, overwrites. Otherwise, inserts.
75    *
76    * If the return value is not NULL, then it points to the actual stable
77    * location in memory where the value is located. Thus, [ret, ret+valuelen)
78    * will be valid memory, bytewise equal to [value, value+valuelen), since the
79    * implementations have immutable values for the time being. The value
80    * returned is guaranteed to be valid memory until the key associated with
81    * value is overriden.
82    */
83   virtual const char *
84   put(void *txn,
85       const std::string &key,
86       const std::string &value) = 0;
87
88   virtual const char *
89   put(void *txn,
90       std::string &&key,
91       std::string &&value)
92   {
93     return put(txn, static_cast<const std::string &>(key),
94                     static_cast<const std::string &>(value));
95   }
96
97   /**
98    * Insert a key of length keylen.
99    *
100    * If a record with key k exists, behavior is unspecified- this function
101    * is only to be used when you can guarantee no such key exists (ie in loading phase)
102    *
103    * Default implementation calls put(). See put() for meaning of return value.
104    */
105   virtual const char *
106   insert(void *txn,
107          const std::string &key,
108          const std::string &value)
109   {
110     return put(txn, key, value);
111   }
112
113   virtual const char *
114   insert(void *txn,
115          std::string &&key,
116          std::string &&value)
117   {
118     return insert(txn, static_cast<const std::string &>(key),
119                        static_cast<const std::string &>(value));
120   }
121
122   /**
123    * Default implementation calls put() with NULL (zero-length) value
124    */
125   virtual void remove(
126       void *txn,
127       const std::string &key)
128   {
129     put(txn, key, "");
130   }
131
132   virtual void remove(
133       void *txn,
134       std::string &&key)
135   {
136     remove(txn, static_cast<const std::string &>(key));
137   }
138
139   /**
140    * Only an estimate, not transactional!
141    */
142   virtual size_t size() const = 0;
143
144   /**
145    * Not thread safe for now
146    */
147   virtual std::map<std::string, uint64_t> clear() = 0;
148 };
149
150 #endif /* _ABSTRACT_ORDERED_INDEX_H_ */