]> git.proxmox.com Git - ceph.git/blob - ceph/src/kv/MemDB.h
import quincy beta 17.1.0
[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/common_fwd.h"
19 #include "include/encoding.h"
20 #include "include/btree_map.h"
21 #include "KeyValueDB.h"
22 #include "osd/osd_types.h"
23
24 #define KEY_DELIM '\0'
25
26
27 enum {
28 l_memdb_first = 34440,
29 l_memdb_gets,
30 l_memdb_txns,
31 l_memdb_get_latency,
32 l_memdb_submit_latency,
33 l_memdb_last,
34 };
35
36 class MemDB : public KeyValueDB
37 {
38 typedef std::pair<std::pair<std::string, std::string>, ceph::bufferlist> ms_op_t;
39 std::mutex m_lock;
40 uint64_t m_total_bytes;
41 uint64_t m_allocated_bytes;
42
43 typedef std::map<std::string, ceph::bufferptr> mdb_map_t;
44 typedef mdb_map_t::iterator mdb_iter_t;
45 bool m_using_btree;
46
47 mdb_map_t m_map;
48
49 CephContext *m_cct;
50 PerfCounters *logger;
51 void* m_priv;
52 std::string m_options;
53 std::string m_db_path;
54
55 int transaction_rollback(KeyValueDB::Transaction t);
56 int _open(std::ostream &out);
57 void close() override;
58 bool _get(const std::string &prefix, const std::string &k, ceph::bufferlist *out);
59 bool _get_locked(const std::string &prefix, const std::string &k, ceph::bufferlist *out);
60 std::string _get_data_fn();
61 void _encode(mdb_iter_t iter, ceph::bufferlist &bl);
62 void _save();
63 int _load();
64 uint64_t iterator_seq_no;
65
66 public:
67 MemDB(CephContext *c, const std::string &path, void *p) :
68 m_total_bytes(0), m_allocated_bytes(0), m_using_btree(false),
69 m_cct(c), logger(NULL), m_priv(p), m_db_path(path), iterator_seq_no(1)
70 {
71 //Nothing as of now
72 }
73
74 ~MemDB() override;
75 int set_merge_operator(const std::string& prefix,
76 std::shared_ptr<MergeOperator> mop) override;
77
78 std::shared_ptr<MergeOperator> _find_merge_op(const std::string &prefix);
79
80 static
81 int _test_init(const std::string& dir) { return 0; };
82
83 class MDBTransactionImpl : public KeyValueDB::TransactionImpl {
84 public:
85 enum op_type { WRITE = 1, MERGE = 2, DELETE = 3};
86 private:
87
88 std::vector<std::pair<op_type, ms_op_t>> ops;
89 MemDB *m_db;
90
91 bool key_is_prefixed(const std::string &prefix, const std::string& full_key);
92 public:
93 const std::vector<std::pair<op_type, ms_op_t>>&
94 get_ops() { return ops; };
95
96 void set(const std::string &prefix, const std::string &key,
97 const ceph::bufferlist &val) override;
98 using KeyValueDB::TransactionImpl::set;
99 void rmkey(const std::string &prefix, const std::string &k) override;
100 using KeyValueDB::TransactionImpl::rmkey;
101 void rmkeys_by_prefix(const std::string &prefix) override;
102 void rm_range_keys(
103 const std::string &prefix,
104 const std::string &start,
105 const std::string &end) override;
106
107 void merge(const std::string &prefix, const std::string &key,
108 const ceph::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, ceph::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(std::string option_str="") override { m_options = option_str; return 0; }
132 int _init(bool format);
133
134 int do_open(std::ostream &out, bool create);
135 int open(std::ostream &out, const std::string& cfs="") override;
136 int create_and_open(std::ostream &out, const std::string& cfs="") 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, ceph::bufferlist> *out) override;
148
149 int get(const std::string &prefix, const std::string &key,
150 ceph::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<std::string, ceph::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 ceph::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(IteratorOpts opts = 0) 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