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 <boost/scoped_ptr.hpp>
18 #include "include/encoding.h"
19 #include "include/btree_map.h"
20 #include "KeyValueDB.h"
21 #include "osd/osd_types.h"
24 #define KEY_DELIM '\0'
29 l_memdb_first
= 34440,
33 l_memdb_submit_latency
,
37 class MemDB
: public KeyValueDB
39 typedef std::pair
<std::pair
<std::string
, std::string
>, bufferlist
> ms_op_t
;
41 uint64_t m_total_bytes
;
42 uint64_t m_allocated_bytes
;
44 typedef std::map
<std::string
, bufferptr
> mdb_map_t
;
45 typedef mdb_map_t::iterator mdb_iter_t
;
56 int transaction_rollback(KeyValueDB::Transaction t
);
57 int _open(ostream
&out
);
58 void close() override
;
59 bool _get(const string
&prefix
, const string
&k
, bufferlist
*out
);
60 bool _get_locked(const string
&prefix
, const string
&k
, bufferlist
*out
);
61 std::string
_get_data_fn();
62 void _encode(mdb_iter_t iter
, bufferlist
&bl
);
65 uint64_t iterator_seq_no
;
68 MemDB(CephContext
*c
, const string
&path
, void *p
) :
69 m_total_bytes(0), m_allocated_bytes(0), m_using_btree(false),
70 m_cct(c
), logger(NULL
), m_priv(p
), m_db_path(path
), iterator_seq_no(1)
76 int set_merge_operator(const std::string
& prefix
,
77 std::shared_ptr
<MergeOperator
> mop
) override
;
79 std::shared_ptr
<MergeOperator
> _find_merge_op(const std::string
&prefix
);
82 int _test_init(const string
& dir
) { return 0; };
84 class MDBTransactionImpl
: public KeyValueDB::TransactionImpl
{
86 enum op_type
{ WRITE
= 1, MERGE
= 2, DELETE
= 3};
89 std::vector
<std::pair
<op_type
, ms_op_t
>> ops
;
92 bool key_is_prefixed(const string
&prefix
, const string
& full_key
);
94 const std::vector
<std::pair
<op_type
, ms_op_t
>>&
95 get_ops() { return ops
; };
97 void set(const std::string
&prefix
, const std::string
&key
,
98 const bufferlist
&val
) override
;
99 using KeyValueDB::TransactionImpl::set
;
100 void rmkey(const std::string
&prefix
, const std::string
&k
) override
;
101 using KeyValueDB::TransactionImpl::rmkey
;
102 void rmkeys_by_prefix(const std::string
&prefix
) override
;
104 const string
&prefix
,
106 const string
&end
) override
;
108 void merge(const std::string
&prefix
, const std::string
&key
, const bufferlist
&value
) override
;
112 explicit MDBTransactionImpl(MemDB
* _db
) :m_db(_db
)
116 ~MDBTransactionImpl() override
{};
122 * Transaction states.
124 int _merge(const std::string
&k
, bufferptr
&bl
);
125 int _merge(ms_op_t
&op
);
126 int _setkey(ms_op_t
&op
);
127 int _rmkey(ms_op_t
&op
);
131 int init(string option_str
="") override
{ m_options
= option_str
; return 0; }
132 int _init(bool format
);
134 int do_open(ostream
&out
, bool create
);
135 int open(ostream
&out
, const std::vector
<ColumnFamily
>&) override
;
136 int create_and_open(ostream
&out
, const std::vector
<ColumnFamily
>&) override
;
137 using KeyValueDB::create_and_open
;
139 KeyValueDB::Transaction
get_transaction() override
{
140 return std::shared_ptr
<MDBTransactionImpl
>(new MDBTransactionImpl(this));
143 int submit_transaction(Transaction
) override
;
144 int submit_transaction_sync(Transaction
) override
;
146 int get(const std::string
&prefix
, const std::set
<std::string
> &key
,
147 std::map
<std::string
, bufferlist
> *out
) override
;
149 int get(const std::string
&prefix
, const std::string
&key
,
150 bufferlist
*out
) override
;
152 using KeyValueDB::get
;
154 class MDBWholeSpaceIteratorImpl
: public KeyValueDB::WholeSpaceIteratorImpl
{
157 std::pair
<string
, bufferlist
> m_key_value
;
159 std::mutex
*m_map_lock_p
;
160 uint64_t *global_seq_no
;
161 uint64_t this_seq_no
;
165 MDBWholeSpaceIteratorImpl(mdb_map_t
*btree_p
, std::mutex
*btree_lock_p
,
166 uint64_t *iterator_seq_no
, bool using_btree
) {
168 m_map_lock_p
= btree_lock_p
;
169 std::lock_guard
<std::mutex
> l(*m_map_lock_p
);
170 global_seq_no
= iterator_seq_no
;
171 this_seq_no
= *iterator_seq_no
;
172 m_using_btree
= using_btree
;
179 int seek_to_first(const std::string
&k
) override
;
180 int seek_to_last(const std::string
&k
) override
;
182 int seek_to_first() override
{ return seek_to_first(std::string()); };
183 int seek_to_last() override
{ return seek_to_last(std::string()); };
185 int upper_bound(const std::string
&prefix
, const std::string
&after
) override
;
186 int lower_bound(const std::string
&prefix
, const std::string
&to
) override
;
187 bool valid() override
;
188 bool iterator_validate();
192 int status() override
{ return 0; };
194 std::string
key() override
;
195 std::pair
<std::string
,std::string
> raw_key() override
;
196 bool raw_key_is_prefixed(const std::string
&prefix
) override
;
197 bufferlist
value() override
;
198 ~MDBWholeSpaceIteratorImpl() override
;
201 uint64_t get_estimated_size(std::map
<std::string
,uint64_t> &extra
) override
{
202 std::lock_guard
<std::mutex
> l(m_lock
);
203 return m_allocated_bytes
;
206 int get_statfs(struct store_statfs_t
*buf
) override
{
207 std::lock_guard
<std::mutex
> l(m_lock
);
209 buf
->total
= m_total_bytes
;
210 buf
->allocated
= m_allocated_bytes
;
211 buf
->data_stored
= m_total_bytes
;
215 WholeSpaceIterator
get_wholespace_iterator() override
{
216 return std::shared_ptr
<KeyValueDB::WholeSpaceIteratorImpl
>(
217 new MDBWholeSpaceIteratorImpl(&m_map
, &m_lock
, &iterator_seq_no
, m_using_btree
));