1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * In-memory crash non-safe keyvalue db
5 * Author: Ramesh Chander, Ramesh.Chander@sandisk.com
8 #ifndef CEPH_OS_BLUESTORE_MEMDB_H
9 #define CEPH_OS_BLUESTORE_MEMDB_H
11 #include "include/buffer.h"
17 #include "include/memory.h"
18 #include <boost/scoped_ptr.hpp>
19 #include "include/encoding.h"
20 #include "include/cpp-btree/btree.h"
21 #include "include/cpp-btree/btree_map.h"
22 #include "include/encoding_btree.h"
23 #include "KeyValueDB.h"
24 #include "osd/osd_types.h"
27 #define KEY_DELIM '\0'
29 class MemDB
: public KeyValueDB
31 typedef std::pair
<std::pair
<std::string
, std::string
>, bufferlist
> ms_op_t
;
33 uint64_t m_total_bytes
;
34 uint64_t m_allocated_bytes
;
36 typedef std::map
<std::string
, bufferptr
> mdb_map_t
;
37 typedef mdb_map_t::iterator mdb_iter_t
;
47 int transaction_rollback(KeyValueDB::Transaction t
);
48 int _open(ostream
&out
);
49 void close() override
;
50 bool _get(const string
&prefix
, const string
&k
, bufferlist
*out
);
51 bool _get_locked(const string
&prefix
, const string
&k
, bufferlist
*out
);
52 std::string
_get_data_fn();
53 void _encode(mdb_iter_t iter
, bufferlist
&bl
);
56 uint64_t iterator_seq_no
;
59 MemDB(CephContext
*c
, const string
&path
, void *p
) :
60 m_using_btree(false), m_cct(c
), m_priv(p
), m_db_path(path
), iterator_seq_no(1)
66 int set_merge_operator(const std::string
& prefix
,
67 std::shared_ptr
<MergeOperator
> mop
) override
;
69 std::shared_ptr
<MergeOperator
> _find_merge_op(std::string prefix
);
72 int _test_init(const string
& dir
) { return 0; };
74 class MDBTransactionImpl
: public KeyValueDB::TransactionImpl
{
76 enum op_type
{ WRITE
= 1, MERGE
= 2, DELETE
= 3};
79 std::vector
<std::pair
<op_type
, ms_op_t
>> ops
;
82 bool key_is_prefixed(const string
&prefix
, const string
& full_key
);
84 const std::vector
<std::pair
<op_type
, ms_op_t
>>&
85 get_ops() { return ops
; };
87 void set(const std::string
&prefix
, const std::string
&key
,
88 const bufferlist
&val
) override
;
89 using KeyValueDB::TransactionImpl::set
;
90 void rmkey(const std::string
&prefix
, const std::string
&k
) override
;
91 using KeyValueDB::TransactionImpl::rmkey
;
92 void rmkeys_by_prefix(const std::string
&prefix
) override
;
96 const string
&end
) override
;
98 void merge(const std::string
&prefix
, const std::string
&key
, const bufferlist
&value
) override
;
102 MDBTransactionImpl(MemDB
* _db
) :m_db(_db
)
106 ~MDBTransactionImpl() override
{};
112 * Transaction states.
114 int _merge(const std::string
&k
, bufferptr
&bl
);
115 int _merge(ms_op_t
&op
);
116 int _setkey(ms_op_t
&op
);
117 int _rmkey(ms_op_t
&op
);
121 int init(string option_str
="") override
{ m_options
= option_str
; return 0; }
122 int _init(bool format
);
124 int do_open(ostream
&out
, bool create
);
125 int open(ostream
&out
) override
{ return do_open(out
, false); }
126 int create_and_open(ostream
&out
) override
{ return do_open(out
, true); }
128 KeyValueDB::Transaction
get_transaction() override
{
129 return std::shared_ptr
<MDBTransactionImpl
>(new MDBTransactionImpl(this));
132 int submit_transaction(Transaction
) override
;
133 int submit_transaction_sync(Transaction
) override
;
135 int get(const std::string
&prefix
, const std::set
<std::string
> &key
,
136 std::map
<std::string
, bufferlist
> *out
) override
;
138 int get(const std::string
&prefix
, const std::string
&key
,
139 bufferlist
*out
) override
;
141 using KeyValueDB::get
;
143 class MDBWholeSpaceIteratorImpl
: public KeyValueDB::WholeSpaceIteratorImpl
{
146 std::pair
<string
, bufferlist
> m_key_value
;
148 std::mutex
*m_map_lock_p
;
149 uint64_t *global_seq_no
;
150 uint64_t this_seq_no
;
154 MDBWholeSpaceIteratorImpl(mdb_map_t
*btree_p
, std::mutex
*btree_lock_p
,
155 uint64_t *iterator_seq_no
, bool using_btree
) {
157 m_map_lock_p
= btree_lock_p
;
158 std::lock_guard
<std::mutex
> l(*m_map_lock_p
);
159 global_seq_no
= iterator_seq_no
;
160 this_seq_no
= *iterator_seq_no
;
161 m_using_btree
= using_btree
;
168 int seek_to_first(const std::string
&k
) override
;
169 int seek_to_last(const std::string
&k
) override
;
171 int seek_to_first() override
{ return seek_to_first(std::string()); };
172 int seek_to_last() override
{ return seek_to_last(std::string()); };
174 int upper_bound(const std::string
&prefix
, const std::string
&after
) override
;
175 int lower_bound(const std::string
&prefix
, const std::string
&to
) override
;
176 bool valid() override
;
177 bool iterator_validate();
181 int status() override
{ return 0; };
183 std::string
key() override
;
184 std::pair
<std::string
,std::string
> raw_key() override
;
185 bool raw_key_is_prefixed(const std::string
&prefix
) override
;
186 bufferlist
value() override
;
187 ~MDBWholeSpaceIteratorImpl() override
;
190 uint64_t get_estimated_size(std::map
<std::string
,uint64_t> &extra
) override
{
191 std::lock_guard
<std::mutex
> l(m_lock
);
192 return m_allocated_bytes
;
195 int get_statfs(struct store_statfs_t
*buf
) override
{
196 std::lock_guard
<std::mutex
> l(m_lock
);
198 buf
->total
= m_total_bytes
;
199 buf
->allocated
= m_allocated_bytes
;
200 buf
->stored
= m_total_bytes
;
206 WholeSpaceIterator
_get_iterator() override
{
207 return std::shared_ptr
<KeyValueDB::WholeSpaceIteratorImpl
>(
208 new MDBWholeSpaceIteratorImpl(&m_map
, &m_lock
, &iterator_seq_no
, m_using_btree
));