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"
18 * Defines virtual interface to be implemented by key value store
20 * Kyoto Cabinet or LevelDB should implement this
24 class TransactionImpl
{
28 const std::string
&prefix
, ///< [in] Prefix for keys
29 const std::map
<std::string
, bufferlist
> &to_set
///< [in] keys/values to set
31 std::map
<std::string
, bufferlist
>::const_iterator it
;
32 for (it
= to_set
.begin(); it
!= to_set
.end(); ++it
)
33 set(prefix
, it
->first
, it
->second
);
36 /// Set Keys (via encoded bufferlist)
38 const std::string
&prefix
, ///< [in] prefix
39 bufferlist
& to_set_bl
///< [in] encoded key/values to set
41 bufferlist::iterator p
= to_set_bl
.begin();
49 set(prefix
, key
, value
);
55 const std::string
&prefix
, ///< [in] Prefix for the key
56 const std::string
&k
, ///< [in] Key to set
57 const bufferlist
&bl
///< [in] Value to set
60 const std::string
&prefix
,
63 const bufferlist
& bl
) {
64 set(prefix
, string(k
, keylen
), bl
);
67 /// Removes Keys (via encoded bufferlist)
69 const std::string
&prefix
, ///< [in] Prefix to search for
70 bufferlist
&keys_bl
///< [in] Keys to remove
72 bufferlist::iterator p
= keys_bl
.begin();
84 const std::string
&prefix
, ///< [in] Prefix to search for
85 const std::set
<std::string
> &keys
///< [in] Keys to remove
87 std::set
<std::string
>::const_iterator it
;
88 for (it
= keys
.begin(); it
!= keys
.end(); ++it
)
94 const std::string
&prefix
, ///< [in] Prefix to search for
95 const std::string
&k
///< [in] Key to remove
98 const std::string
&prefix
, ///< [in] Prefix to search for
99 const char *k
, ///< [in] Key to remove
102 rmkey(prefix
, string(k
, keylen
));
105 /// Remove Single Key which exists and was not overwritten.
106 /// This API is only related to performance optimization, and should only be
107 /// re-implemented by log-insert-merge tree based keyvalue stores(such as RocksDB).
108 /// If a key is overwritten (by calling set multiple times), then the result
109 /// of calling rm_single_key on this key is undefined.
110 virtual void rm_single_key(
111 const std::string
&prefix
, ///< [in] Prefix to search for
112 const std::string
&k
///< [in] Key to remove
113 ) { return rmkey(prefix
, k
);}
115 /// Removes keys beginning with prefix
116 virtual void rmkeys_by_prefix(
117 const std::string
&prefix
///< [in] Prefix by which to remove keys
120 virtual void rm_range_keys(
121 const string
&prefix
, ///< [in] Prefix by which to remove keys
122 const string
&start
, ///< [in] The start bound of remove keys
123 const string
&end
///< [in] The start bound of remove keys
126 /// Merge value into key
128 const std::string
&prefix
, ///< [in] Prefix ==> MUST match some established merge operator
129 const std::string
&key
, ///< [in] Key to be merged
130 const bufferlist
&value
///< [in] value to be merged into key
131 ) { assert(0 == "Not implemented"); }
133 virtual ~TransactionImpl() {}
135 typedef ceph::shared_ptr
< TransactionImpl
> Transaction
;
137 /// create a new instance
138 static KeyValueDB
*create(CephContext
*cct
, const std::string
& type
,
139 const std::string
& dir
,
142 /// test whether we can successfully initialize; may have side effects (e.g., create)
143 static int test_init(const std::string
& type
, const std::string
& dir
);
144 virtual int init(string option_str
="") = 0;
145 virtual int open(std::ostream
&out
) = 0;
146 virtual int create_and_open(std::ostream
&out
) = 0;
147 virtual void close() { }
149 virtual Transaction
get_transaction() = 0;
150 virtual int submit_transaction(Transaction
) = 0;
151 virtual int submit_transaction_sync(Transaction t
) {
152 return submit_transaction(t
);
157 const std::string
&prefix
, ///< [in] Prefix for key
158 const std::set
<std::string
> &key
, ///< [in] Key to retrieve
159 std::map
<std::string
, bufferlist
> *out
///< [out] Key value retrieved
161 virtual int get(const std::string
&prefix
, ///< [in] prefix
162 const std::string
&key
, ///< [in] key
163 bufferlist
*value
) { ///< [out] value
164 std::set
<std::string
> ks
;
166 std::map
<std::string
,bufferlist
> om
;
167 int r
= get(prefix
, ks
, &om
);
168 if (om
.find(key
) != om
.end()) {
171 *value
= bufferlist();
176 virtual int get(const string
&prefix
,
177 const char *key
, size_t keylen
,
179 return get(prefix
, string(key
, keylen
), value
);
182 class GenericIteratorImpl
{
184 virtual int seek_to_first() = 0;
185 virtual int upper_bound(const std::string
&after
) = 0;
186 virtual int lower_bound(const std::string
&to
) = 0;
187 virtual bool valid() = 0;
188 virtual int next(bool validate
=true) = 0;
189 virtual std::string
key() = 0;
190 virtual bufferlist
value() = 0;
191 virtual int status() = 0;
192 virtual ~GenericIteratorImpl() {}
195 class WholeSpaceIteratorImpl
{
197 virtual int seek_to_first() = 0;
198 virtual int seek_to_first(const std::string
&prefix
) = 0;
199 virtual int seek_to_last() = 0;
200 virtual int seek_to_last(const std::string
&prefix
) = 0;
201 virtual int upper_bound(const std::string
&prefix
, const std::string
&after
) = 0;
202 virtual int lower_bound(const std::string
&prefix
, const std::string
&to
) = 0;
203 virtual bool valid() = 0;
204 virtual int next() = 0;
205 virtual int prev() = 0;
206 virtual std::string
key() = 0;
207 virtual std::pair
<std::string
,std::string
> raw_key() = 0;
208 virtual bool raw_key_is_prefixed(const std::string
&prefix
) = 0;
209 virtual bufferlist
value() = 0;
210 virtual bufferptr
value_as_ptr() {
211 bufferlist bl
= value();
213 return *bl
.buffers().begin();
218 virtual int status() = 0;
219 virtual size_t key_size() {
222 virtual size_t value_size() {
225 virtual ~WholeSpaceIteratorImpl() { }
227 typedef ceph::shared_ptr
< WholeSpaceIteratorImpl
> WholeSpaceIterator
;
229 class IteratorImpl
: public GenericIteratorImpl
{
230 const std::string prefix
;
231 WholeSpaceIterator generic_iter
;
233 IteratorImpl(const std::string
&prefix
, WholeSpaceIterator iter
) :
234 prefix(prefix
), generic_iter(iter
) { }
235 ~IteratorImpl() override
{ }
237 int seek_to_first() override
{
238 return generic_iter
->seek_to_first(prefix
);
241 return generic_iter
->seek_to_last(prefix
);
243 int upper_bound(const std::string
&after
) override
{
244 return generic_iter
->upper_bound(prefix
, after
);
246 int lower_bound(const std::string
&to
) override
{
247 return generic_iter
->lower_bound(prefix
, to
);
249 bool valid() override
{
250 if (!generic_iter
->valid())
252 return generic_iter
->raw_key_is_prefixed(prefix
);
254 // Note that next() and prev() shouldn't validate iters,
255 // it's responsibility of caller to ensure they're valid.
256 int next(bool validate
=true) override
{
259 return generic_iter
->next();
262 return generic_iter
->next();
266 int prev(bool validate
=true) {
269 return generic_iter
->prev();
272 return generic_iter
->prev();
275 std::string
key() override
{
276 return generic_iter
->key();
278 std::pair
<std::string
, std::string
> raw_key() {
279 return generic_iter
->raw_key();
281 bufferlist
value() override
{
282 return generic_iter
->value();
284 bufferptr
value_as_ptr() {
285 return generic_iter
->value_as_ptr();
287 int status() override
{
288 return generic_iter
->status();
292 typedef ceph::shared_ptr
< IteratorImpl
> Iterator
;
294 WholeSpaceIterator
get_iterator() {
295 return _get_iterator();
298 Iterator
get_iterator(const std::string
&prefix
) {
299 return std::make_shared
<IteratorImpl
>(prefix
, get_iterator());
302 virtual uint64_t get_estimated_size(std::map
<std::string
,uint64_t> &extra
) = 0;
303 virtual int get_statfs(struct store_statfs_t
*buf
) {
307 virtual ~KeyValueDB() {}
309 /// compact the underlying store
310 virtual void compact() {}
312 /// compact db for all keys with a given prefix
313 virtual void compact_prefix(const std::string
& prefix
) {}
314 /// compact db for all keys with a given prefix, async
315 virtual void compact_prefix_async(const std::string
& prefix
) {}
316 virtual void compact_range(const std::string
& prefix
,
317 const std::string
& start
, const std::string
& end
) {}
318 virtual void compact_range_async(const std::string
& prefix
,
319 const std::string
& start
, const std::string
& end
) {}
321 // See RocksDB merge operator definition, we support the basic
322 // associative merge only right now.
323 class MergeOperator
{
325 /// Merge into a key that doesn't exist
326 virtual void merge_nonexistent(
327 const char *rdata
, size_t rlen
,
328 std::string
*new_value
) = 0;
329 /// Merge into a key that does exist
331 const char *ldata
, size_t llen
,
332 const char *rdata
, size_t rlen
,
333 std::string
*new_value
) = 0;
334 /// We use each operator name and each prefix to construct the overall RocksDB operator name for consistency check at open time.
335 virtual string
name() const = 0;
337 virtual ~MergeOperator() {}
340 /// Setup one or more operators, this needs to be done BEFORE the DB is opened.
341 virtual int set_merge_operator(const std::string
& prefix
,
342 std::shared_ptr
<MergeOperator
> mop
) {
346 virtual void get_statistics(Formatter
*f
) {
350 /// List of matching prefixes and merge operators
351 std::vector
<std::pair
<std::string
,
352 std::shared_ptr
<MergeOperator
> > > merge_ops
;
354 virtual WholeSpaceIterator
_get_iterator() = 0;