1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
6 #include "include/buffer.h"
11 #include "include/memory.h"
12 #include <boost/scoped_ptr.hpp>
13 #include "include/encoding.h"
14 #include "common/Formatter.h"
15 #include "common/perf_counters.h"
19 * Defines virtual interface to be implemented by key value store
21 * Kyoto Cabinet or LevelDB should implement this
25 class TransactionImpl {
29 const std::string &prefix, ///< [in] Prefix for keys
30 const std::map<std::string, bufferlist> &to_set ///< [in] keys/values to set
32 std::map<std::string, bufferlist>::const_iterator it;
33 for (it = to_set.begin(); it != to_set.end(); ++it)
34 set(prefix, it->first, it->second);
37 /// Set Keys (via encoded bufferlist)
39 const std::string &prefix, ///< [in] prefix
40 bufferlist& to_set_bl ///< [in] encoded key/values to set
42 bufferlist::iterator p = to_set_bl.begin();
50 set(prefix, key, value);
56 const std::string &prefix, ///< [in] Prefix for the key
57 const std::string &k, ///< [in] Key to set
58 const bufferlist &bl ///< [in] Value to set
61 const std::string &prefix,
64 const bufferlist& bl) {
65 set(prefix, string(k, keylen), bl);
68 /// Removes Keys (via encoded bufferlist)
70 const std::string &prefix, ///< [in] Prefix to search for
71 bufferlist &keys_bl ///< [in] Keys to remove
73 bufferlist::iterator p = keys_bl.begin();
85 const std::string &prefix, ///< [in] Prefix to search for
86 const std::set<std::string> &keys ///< [in] Keys to remove
88 std::set<std::string>::const_iterator it;
89 for (it = keys.begin(); it != keys.end(); ++it)
95 const std::string &prefix, ///< [in] Prefix to search for
96 const std::string &k ///< [in] Key to remove
99 const std::string &prefix, ///< [in] Prefix to search for
100 const char *k, ///< [in] Key to remove
103 rmkey(prefix, string(k, keylen));
106 /// Remove Single Key which exists and was not overwritten.
107 /// This API is only related to performance optimization, and should only be
108 /// re-implemented by log-insert-merge tree based keyvalue stores(such as RocksDB).
109 /// If a key is overwritten (by calling set multiple times), then the result
110 /// of calling rm_single_key on this key is undefined.
111 virtual void rm_single_key(
112 const std::string &prefix, ///< [in] Prefix to search for
113 const std::string &k ///< [in] Key to remove
114 ) { return rmkey(prefix, k);}
116 /// Removes keys beginning with prefix
117 virtual void rmkeys_by_prefix(
118 const std::string &prefix ///< [in] Prefix by which to remove keys
121 virtual void rm_range_keys(
122 const string &prefix, ///< [in] Prefix by which to remove keys
123 const string &start, ///< [in] The start bound of remove keys
124 const string &end ///< [in] The start bound of remove keys
127 /// Merge value into key
129 const std::string &prefix, ///< [in] Prefix ==> MUST match some established merge operator
130 const std::string &key, ///< [in] Key to be merged
131 const bufferlist &value ///< [in] value to be merged into key
132 ) { assert(0 == "Not implemented"); }
134 virtual ~TransactionImpl() {}
136 typedef ceph::shared_ptr< TransactionImpl > Transaction;
138 /// create a new instance
139 static KeyValueDB *create(CephContext *cct, const std::string& type,
140 const std::string& dir,
143 /// test whether we can successfully initialize; may have side effects (e.g., create)
144 static int test_init(const std::string& type, const std::string& dir);
145 virtual int init(string option_str="") = 0;
146 virtual int open(std::ostream &out) = 0;
147 virtual int create_and_open(std::ostream &out) = 0;
148 virtual void close() { }
150 virtual Transaction get_transaction() = 0;
151 virtual int submit_transaction(Transaction) = 0;
152 virtual int submit_transaction_sync(Transaction t) {
153 return submit_transaction(t);
158 const std::string &prefix, ///< [in] Prefix for key
159 const std::set<std::string> &key, ///< [in] Key to retrieve
160 std::map<std::string, bufferlist> *out ///< [out] Key value retrieved
162 virtual int get(const std::string &prefix, ///< [in] prefix
163 const std::string &key, ///< [in] key
164 bufferlist *value) { ///< [out] value
165 std::set<std::string> ks;
167 std::map<std::string,bufferlist> om;
168 int r = get(prefix, ks, &om);
169 if (om.find(key) != om.end()) {
172 *value = bufferlist();
177 virtual int get(const string &prefix,
178 const char *key, size_t keylen,
180 return get(prefix, string(key, keylen), value);
183 class GenericIteratorImpl {
185 virtual int seek_to_first() = 0;
186 virtual int upper_bound(const std::string &after) = 0;
187 virtual int lower_bound(const std::string &to) = 0;
188 virtual bool valid() = 0;
189 virtual int next(bool validate=true) = 0;
190 virtual std::string key() = 0;
191 virtual bufferlist value() = 0;
192 virtual int status() = 0;
193 virtual ~GenericIteratorImpl() {}
196 class WholeSpaceIteratorImpl {
198 virtual int seek_to_first() = 0;
199 virtual int seek_to_first(const std::string &prefix) = 0;
200 virtual int seek_to_last() = 0;
201 virtual int seek_to_last(const std::string &prefix) = 0;
202 virtual int upper_bound(const std::string &prefix, const std::string &after) = 0;
203 virtual int lower_bound(const std::string &prefix, const std::string &to) = 0;
204 virtual bool valid() = 0;
205 virtual int next() = 0;
206 virtual int prev() = 0;
207 virtual std::string key() = 0;
208 virtual std::pair<std::string,std::string> raw_key() = 0;
209 virtual bool raw_key_is_prefixed(const std::string &prefix) = 0;
210 virtual bufferlist value() = 0;
211 virtual bufferptr value_as_ptr() {
212 bufferlist bl = value();
214 return *bl.buffers().begin();
219 virtual int status() = 0;
220 virtual size_t key_size() {
223 virtual size_t value_size() {
226 virtual ~WholeSpaceIteratorImpl() { }
228 typedef ceph::shared_ptr< WholeSpaceIteratorImpl > WholeSpaceIterator;
230 class IteratorImpl : public GenericIteratorImpl {
231 const std::string prefix;
232 WholeSpaceIterator generic_iter;
234 IteratorImpl(const std::string &prefix, WholeSpaceIterator iter) :
235 prefix(prefix), generic_iter(iter) { }
236 ~IteratorImpl() override { }
238 int seek_to_first() override {
239 return generic_iter->seek_to_first(prefix);
242 return generic_iter->seek_to_last(prefix);
244 int upper_bound(const std::string &after) override {
245 return generic_iter->upper_bound(prefix, after);
247 int lower_bound(const std::string &to) override {
248 return generic_iter->lower_bound(prefix, to);
250 bool valid() override {
251 if (!generic_iter->valid())
253 return generic_iter->raw_key_is_prefixed(prefix);
255 // Note that next() and prev() shouldn't validate iters,
256 // it's responsibility of caller to ensure they're valid.
257 int next(bool validate=true) override {
260 return generic_iter->next();
263 return generic_iter->next();
267 int prev(bool validate=true) {
270 return generic_iter->prev();
273 return generic_iter->prev();
276 std::string key() override {
277 return generic_iter->key();
279 std::pair<std::string, std::string> raw_key() {
280 return generic_iter->raw_key();
282 bufferlist value() override {
283 return generic_iter->value();
285 bufferptr value_as_ptr() {
286 return generic_iter->value_as_ptr();
288 int status() override {
289 return generic_iter->status();
293 typedef ceph::shared_ptr< IteratorImpl > Iterator;
295 WholeSpaceIterator get_iterator() {
296 return _get_iterator();
299 Iterator get_iterator(const std::string &prefix) {
300 return std::make_shared<IteratorImpl>(prefix, get_iterator());
303 virtual uint64_t get_estimated_size(std::map<std::string,uint64_t> &extra) = 0;
304 virtual int get_statfs(struct store_statfs_t *buf) {
308 virtual int set_cache_size(uint64_t) {
312 virtual ~KeyValueDB() {}
314 /// compact the underlying store
315 virtual void compact() {}
317 /// compact db for all keys with a given prefix
318 virtual void compact_prefix(const std::string& prefix) {}
319 /// compact db for all keys with a given prefix, async
320 virtual void compact_prefix_async(const std::string& prefix) {}
321 virtual void compact_range(const std::string& prefix,
322 const std::string& start, const std::string& end) {}
323 virtual void compact_range_async(const std::string& prefix,
324 const std::string& start, const std::string& end) {}
326 // See RocksDB merge operator definition, we support the basic
327 // associative merge only right now.
328 class MergeOperator {
330 /// Merge into a key that doesn't exist
331 virtual void merge_nonexistent(
332 const char *rdata, size_t rlen,
333 std::string *new_value) = 0;
334 /// Merge into a key that does exist
336 const char *ldata, size_t llen,
337 const char *rdata, size_t rlen,
338 std::string *new_value) = 0;
339 /// We use each operator name and each prefix to construct the overall RocksDB operator name for consistency check at open time.
340 virtual string name() const = 0;
342 virtual ~MergeOperator() {}
345 /// Setup one or more operators, this needs to be done BEFORE the DB is opened.
346 virtual int set_merge_operator(const std::string& prefix,
347 std::shared_ptr<MergeOperator> mop) {
351 virtual void get_statistics(Formatter *f) {
356 * Return your perf counters if you have any. Subclasses are not
357 * required to implement this, and callers must respect a null return
360 virtual PerfCounters *get_perf_counters() {
364 /// List of matching prefixes and merge operators
365 std::vector<std::pair<std::string,
366 std::shared_ptr<MergeOperator> > > merge_ops;
368 virtual WholeSpaceIterator _get_iterator() = 0;