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/btree_map.h"
21 #include "KeyValueDB.h"
22 #include "osd/osd_types.h"
25 #define KEY_DELIM '\0'
27 class MemDB
: public KeyValueDB
29 typedef std::pair
<std::pair
<std::string
, std::string
>, bufferlist
> ms_op_t
;
31 uint64_t m_total_bytes
;
32 uint64_t m_allocated_bytes
;
34 typedef std::map
<std::string
, bufferptr
> mdb_map_t
;
35 typedef mdb_map_t::iterator mdb_iter_t
;
45 int transaction_rollback(KeyValueDB::Transaction t
);
46 int _open(ostream
&out
);
47 void close() override
;
48 bool _get(const string
&prefix
, const string
&k
, bufferlist
*out
);
49 bool _get_locked(const string
&prefix
, const string
&k
, bufferlist
*out
);
50 std::string
_get_data_fn();
51 void _encode(mdb_iter_t iter
, bufferlist
&bl
);
54 uint64_t iterator_seq_no
;
57 MemDB(CephContext
*c
, const string
&path
, void *p
) :
58 m_using_btree(false), m_cct(c
), m_priv(p
), m_db_path(path
), iterator_seq_no(1)
64 int set_merge_operator(const std::string
& prefix
,
65 std::shared_ptr
<MergeOperator
> mop
) override
;
67 std::shared_ptr
<MergeOperator
> _find_merge_op(std::string prefix
);
70 int _test_init(const string
& dir
) { return 0; };
72 class MDBTransactionImpl
: public KeyValueDB::TransactionImpl
{
74 enum op_type
{ WRITE
= 1, MERGE
= 2, DELETE
= 3};
77 std::vector
<std::pair
<op_type
, ms_op_t
>> ops
;
80 bool key_is_prefixed(const string
&prefix
, const string
& full_key
);
82 const std::vector
<std::pair
<op_type
, ms_op_t
>>&
83 get_ops() { return ops
; };
85 void set(const std::string
&prefix
, const std::string
&key
,
86 const bufferlist
&val
) override
;
87 using KeyValueDB::TransactionImpl::set
;
88 void rmkey(const std::string
&prefix
, const std::string
&k
) override
;
89 using KeyValueDB::TransactionImpl::rmkey
;
90 void rmkeys_by_prefix(const std::string
&prefix
) override
;
94 const string
&end
) override
;
96 void merge(const std::string
&prefix
, const std::string
&key
, const bufferlist
&value
) override
;
100 MDBTransactionImpl(MemDB
* _db
) :m_db(_db
)
104 ~MDBTransactionImpl() override
{};
110 * Transaction states.
112 int _merge(const std::string
&k
, bufferptr
&bl
);
113 int _merge(ms_op_t
&op
);
114 int _setkey(ms_op_t
&op
);
115 int _rmkey(ms_op_t
&op
);
119 int init(string option_str
="") override
{ m_options
= option_str
; return 0; }
120 int _init(bool format
);
122 int do_open(ostream
&out
, bool create
);
123 int open(ostream
&out
) override
{ return do_open(out
, false); }
124 int create_and_open(ostream
&out
) override
{ return do_open(out
, true); }
126 KeyValueDB::Transaction
get_transaction() override
{
127 return std::shared_ptr
<MDBTransactionImpl
>(new MDBTransactionImpl(this));
130 int submit_transaction(Transaction
) override
;
131 int submit_transaction_sync(Transaction
) override
;
133 int get(const std::string
&prefix
, const std::set
<std::string
> &key
,
134 std::map
<std::string
, bufferlist
> *out
) override
;
136 int get(const std::string
&prefix
, const std::string
&key
,
137 bufferlist
*out
) override
;
139 using KeyValueDB::get
;
141 class MDBWholeSpaceIteratorImpl
: public KeyValueDB::WholeSpaceIteratorImpl
{
144 std::pair
<string
, bufferlist
> m_key_value
;
146 std::mutex
*m_map_lock_p
;
147 uint64_t *global_seq_no
;
148 uint64_t this_seq_no
;
152 MDBWholeSpaceIteratorImpl(mdb_map_t
*btree_p
, std::mutex
*btree_lock_p
,
153 uint64_t *iterator_seq_no
, bool using_btree
) {
155 m_map_lock_p
= btree_lock_p
;
156 std::lock_guard
<std::mutex
> l(*m_map_lock_p
);
157 global_seq_no
= iterator_seq_no
;
158 this_seq_no
= *iterator_seq_no
;
159 m_using_btree
= using_btree
;
166 int seek_to_first(const std::string
&k
) override
;
167 int seek_to_last(const std::string
&k
) override
;
169 int seek_to_first() override
{ return seek_to_first(std::string()); };
170 int seek_to_last() override
{ return seek_to_last(std::string()); };
172 int upper_bound(const std::string
&prefix
, const std::string
&after
) override
;
173 int lower_bound(const std::string
&prefix
, const std::string
&to
) override
;
174 bool valid() override
;
175 bool iterator_validate();
179 int status() override
{ return 0; };
181 std::string
key() override
;
182 std::pair
<std::string
,std::string
> raw_key() override
;
183 bool raw_key_is_prefixed(const std::string
&prefix
) override
;
184 bufferlist
value() override
;
185 ~MDBWholeSpaceIteratorImpl() override
;
188 uint64_t get_estimated_size(std::map
<std::string
,uint64_t> &extra
) override
{
189 std::lock_guard
<std::mutex
> l(m_lock
);
190 return m_allocated_bytes
;
193 int get_statfs(struct store_statfs_t
*buf
) override
{
194 std::lock_guard
<std::mutex
> l(m_lock
);
196 buf
->total
= m_total_bytes
;
197 buf
->allocated
= m_allocated_bytes
;
198 buf
->stored
= m_total_bytes
;
204 WholeSpaceIterator
_get_iterator() override
{
205 return std::shared_ptr
<KeyValueDB::WholeSpaceIteratorImpl
>(
206 new MDBWholeSpaceIteratorImpl(&m_map
, &m_lock
, &iterator_seq_no
, m_using_btree
));