]> git.proxmox.com Git - ceph.git/blob - ceph/src/kv/MemDB.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / kv / MemDB.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * In-memory crash non-safe keyvalue db
5 * Author: Ramesh Chander, Ramesh.Chander@sandisk.com
6 */
7
8 #ifndef CEPH_OS_BLUESTORE_MEMDB_H
9 #define CEPH_OS_BLUESTORE_MEMDB_H
10
11 #include "include/buffer.h"
12 #include <ostream>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <memory>
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"
22
23 using std::string;
24 #define KEY_DELIM '\0'
25
26 class PerfCounters;
27
28 enum {
29 l_memdb_first = 34440,
30 l_memdb_gets,
31 l_memdb_txns,
32 l_memdb_get_latency,
33 l_memdb_submit_latency,
34 l_memdb_last,
35 };
36
37 class MemDB : public KeyValueDB
38 {
39 typedef std::pair<std::pair<std::string, std::string>, bufferlist> ms_op_t;
40 std::mutex m_lock;
41 uint64_t m_total_bytes;
42 uint64_t m_allocated_bytes;
43
44 typedef std::map<std::string, bufferptr> mdb_map_t;
45 typedef mdb_map_t::iterator mdb_iter_t;
46 bool m_using_btree;
47
48 mdb_map_t m_map;
49
50 CephContext *m_cct;
51 PerfCounters *logger;
52 void* m_priv;
53 string m_options;
54 string m_db_path;
55
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);
63 void _save();
64 int _load();
65 uint64_t iterator_seq_no;
66
67 public:
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)
71 {
72 //Nothing as of now
73 }
74
75 ~MemDB() override;
76 int set_merge_operator(const std::string& prefix,
77 std::shared_ptr<MergeOperator> mop) override;
78
79 std::shared_ptr<MergeOperator> _find_merge_op(const std::string &prefix);
80
81 static
82 int _test_init(const string& dir) { return 0; };
83
84 class MDBTransactionImpl : public KeyValueDB::TransactionImpl {
85 public:
86 enum op_type { WRITE = 1, MERGE = 2, DELETE = 3};
87 private:
88
89 std::vector<std::pair<op_type, ms_op_t>> ops;
90 MemDB *m_db;
91
92 bool key_is_prefixed(const string &prefix, const string& full_key);
93 public:
94 const std::vector<std::pair<op_type, ms_op_t>>&
95 get_ops() { return ops; };
96
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;
103 void rm_range_keys(
104 const string &prefix,
105 const string &start,
106 const string &end) override;
107
108 void merge(const std::string &prefix, const std::string &key, const bufferlist &value) override;
109 void clear() {
110 ops.clear();
111 }
112 explicit MDBTransactionImpl(MemDB* _db) :m_db(_db)
113 {
114 ops.clear();
115 }
116 ~MDBTransactionImpl() override {};
117 };
118
119 private:
120
121 /*
122 * Transaction states.
123 */
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);
128
129 public:
130
131 int init(string option_str="") override { m_options = option_str; return 0; }
132 int _init(bool format);
133
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;
138
139 KeyValueDB::Transaction get_transaction() override {
140 return std::shared_ptr<MDBTransactionImpl>(new MDBTransactionImpl(this));
141 }
142
143 int submit_transaction(Transaction) override;
144 int submit_transaction_sync(Transaction) override;
145
146 int get(const std::string &prefix, const std::set<std::string> &key,
147 std::map<std::string, bufferlist> *out) override;
148
149 int get(const std::string &prefix, const std::string &key,
150 bufferlist *out) override;
151
152 using KeyValueDB::get;
153
154 class MDBWholeSpaceIteratorImpl : public KeyValueDB::WholeSpaceIteratorImpl {
155
156 mdb_iter_t m_iter;
157 std::pair<string, bufferlist> m_key_value;
158 mdb_map_t *m_map_p;
159 std::mutex *m_map_lock_p;
160 uint64_t *global_seq_no;
161 uint64_t this_seq_no;
162 bool m_using_btree;
163
164 public:
165 MDBWholeSpaceIteratorImpl(mdb_map_t *btree_p, std::mutex *btree_lock_p,
166 uint64_t *iterator_seq_no, bool using_btree) {
167 m_map_p = btree_p;
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;
173 }
174
175 void fill_current();
176 void free_last();
177
178
179 int seek_to_first(const std::string &k) override;
180 int seek_to_last(const std::string &k) override;
181
182 int seek_to_first() override { return seek_to_first(std::string()); };
183 int seek_to_last() override { return seek_to_last(std::string()); };
184
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();
189
190 int next() override;
191 int prev() override;
192 int status() override { return 0; };
193
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;
199 };
200
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;
204 };
205
206 int get_statfs(struct store_statfs_t *buf) override {
207 std::lock_guard<std::mutex> l(m_lock);
208 buf->reset();
209 buf->total = m_total_bytes;
210 buf->allocated = m_allocated_bytes;
211 buf->data_stored = m_total_bytes;
212 return 0;
213 }
214
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));
218 }
219 };
220
221 #endif
222