]> git.proxmox.com Git - ceph.git/blob - ceph/src/kv/MemDB.h
add subtree-ish sources for 12.0.3
[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 "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"
25
26 using std::string;
27 #define KEY_DELIM '\0'
28
29 class MemDB : public KeyValueDB
30 {
31 typedef std::pair<std::pair<std::string, std::string>, bufferlist> ms_op_t;
32 std::mutex m_lock;
33 uint64_t m_total_bytes;
34 uint64_t m_allocated_bytes;
35
36 typedef std::map<std::string, bufferptr> mdb_map_t;
37 typedef mdb_map_t::iterator mdb_iter_t;
38 bool m_using_btree;
39
40 mdb_map_t m_map;
41
42 CephContext *m_cct;
43 void* m_priv;
44 string m_options;
45 string m_db_path;
46
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);
54 void _save();
55 int _load();
56 uint64_t iterator_seq_no;
57
58 public:
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)
61 {
62 //Nothing as of now
63 }
64
65 ~MemDB() override;
66 int set_merge_operator(const std::string& prefix,
67 std::shared_ptr<MergeOperator> mop) override;
68
69 std::shared_ptr<MergeOperator> _find_merge_op(std::string prefix);
70
71 static
72 int _test_init(const string& dir) { return 0; };
73
74 class MDBTransactionImpl : public KeyValueDB::TransactionImpl {
75 public:
76 enum op_type { WRITE = 1, MERGE = 2, DELETE = 3};
77 private:
78
79 std::vector<std::pair<op_type, ms_op_t>> ops;
80 MemDB *m_db;
81
82 bool key_is_prefixed(const string &prefix, const string& full_key);
83 public:
84 const std::vector<std::pair<op_type, ms_op_t>>&
85 get_ops() { return ops; };
86
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;
93 void rm_range_keys(
94 const string &prefix,
95 const string &start,
96 const string &end) override;
97
98 void merge(const std::string &prefix, const std::string &key, const bufferlist &value) override;
99 void clear() {
100 ops.clear();
101 }
102 MDBTransactionImpl(MemDB* _db) :m_db(_db)
103 {
104 ops.clear();
105 }
106 ~MDBTransactionImpl() override {};
107 };
108
109 private:
110
111 /*
112 * Transaction states.
113 */
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);
118
119 public:
120
121 int init(string option_str="") override { m_options = option_str; return 0; }
122 int _init(bool format);
123
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); }
127
128 KeyValueDB::Transaction get_transaction() override {
129 return std::shared_ptr<MDBTransactionImpl>(new MDBTransactionImpl(this));
130 }
131
132 int submit_transaction(Transaction) override;
133 int submit_transaction_sync(Transaction) override;
134
135 int get(const std::string &prefix, const std::set<std::string> &key,
136 std::map<std::string, bufferlist> *out) override;
137
138 int get(const std::string &prefix, const std::string &key,
139 bufferlist *out) override;
140
141 using KeyValueDB::get;
142
143 class MDBWholeSpaceIteratorImpl : public KeyValueDB::WholeSpaceIteratorImpl {
144
145 mdb_iter_t m_iter;
146 std::pair<string, bufferlist> m_key_value;
147 mdb_map_t *m_map_p;
148 std::mutex *m_map_lock_p;
149 uint64_t *global_seq_no;
150 uint64_t this_seq_no;
151 bool m_using_btree;
152
153 public:
154 MDBWholeSpaceIteratorImpl(mdb_map_t *btree_p, std::mutex *btree_lock_p,
155 uint64_t *iterator_seq_no, bool using_btree) {
156 m_map_p = btree_p;
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;
162 }
163
164 void fill_current();
165 void free_last();
166
167
168 int seek_to_first(const std::string &k) override;
169 int seek_to_last(const std::string &k) override;
170
171 int seek_to_first() override { return seek_to_first(std::string()); };
172 int seek_to_last() override { return seek_to_last(std::string()); };
173
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();
178
179 int next() override;
180 int prev() override;
181 int status() override { return 0; };
182
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;
188 };
189
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;
193 };
194
195 int get_statfs(struct store_statfs_t *buf) override {
196 std::lock_guard<std::mutex> l(m_lock);
197 buf->reset();
198 buf->total = m_total_bytes;
199 buf->allocated = m_allocated_bytes;
200 buf->stored = m_total_bytes;
201 return 0;
202 }
203
204 protected:
205
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));
209 }
210 };
211
212 #endif
213