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