]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | #include <map> | |
4 | #include <set> | |
5 | #include <string> | |
7c673cae FG |
6 | |
7 | #include "kv/KeyValueDB.h" | |
8 | #include "include/buffer.h" | |
9 | #include "include/Context.h" | |
10 | ||
11 | using std::string; | |
12 | ||
13 | class KeyValueDBMemory : public KeyValueDB { | |
14 | public: | |
15 | std::map<std::pair<string,string>,bufferlist> db; | |
16 | ||
17 | KeyValueDBMemory() { } | |
18 | explicit KeyValueDBMemory(KeyValueDBMemory *db) : db(db->db) { } | |
19 | ~KeyValueDBMemory() override { } | |
20 | ||
21 | int init(string _opt) override { | |
22 | return 0; | |
23 | } | |
11fdf7f2 | 24 | int open(std::ostream &out, const vector<ColumnFamily>& cfs = {}) override { |
7c673cae FG |
25 | return 0; |
26 | } | |
11fdf7f2 | 27 | int create_and_open(ostream &out, const vector<ColumnFamily>& cfs = {}) override { |
7c673cae FG |
28 | return 0; |
29 | } | |
30 | ||
31 | int get( | |
32 | const string &prefix, | |
33 | const std::set<string> &key, | |
34 | std::map<string, bufferlist> *out | |
35 | ) override; | |
36 | using KeyValueDB::get; | |
37 | ||
38 | int get_keys( | |
39 | const string &prefix, | |
40 | const std::set<string> &key, | |
41 | std::set<string> *out | |
42 | ); | |
43 | ||
44 | int set( | |
45 | const string &prefix, | |
46 | const string &key, | |
47 | const bufferlist &bl | |
48 | ); | |
49 | ||
50 | int rmkey( | |
51 | const string &prefix, | |
52 | const string &key | |
53 | ); | |
54 | ||
55 | int rmkeys_by_prefix( | |
56 | const string &prefix | |
57 | ); | |
58 | ||
59 | int rm_range_keys( | |
60 | const string &prefix, | |
61 | const string &start, | |
62 | const string &end | |
63 | ); | |
64 | ||
65 | class TransactionImpl_ : public TransactionImpl { | |
66 | public: | |
67 | list<Context *> on_commit; | |
68 | KeyValueDBMemory *db; | |
69 | ||
70 | explicit TransactionImpl_(KeyValueDBMemory *db) : db(db) {} | |
71 | ||
72 | ||
73 | struct SetOp : public Context { | |
74 | KeyValueDBMemory *db; | |
75 | std::pair<string,string> key; | |
76 | bufferlist value; | |
77 | SetOp(KeyValueDBMemory *db, | |
78 | const std::pair<string,string> &key, | |
79 | const bufferlist &value) | |
80 | : db(db), key(key), value(value) {} | |
81 | void finish(int r) override { | |
82 | db->set(key.first, key.second, value); | |
83 | } | |
84 | }; | |
85 | ||
86 | void set(const string &prefix, const string &k, const bufferlist& bl) override { | |
87 | on_commit.push_back(new SetOp(db, std::make_pair(prefix, k), bl)); | |
88 | } | |
89 | ||
90 | struct RmKeysOp : public Context { | |
91 | KeyValueDBMemory *db; | |
92 | std::pair<string,string> key; | |
93 | RmKeysOp(KeyValueDBMemory *db, | |
94 | const std::pair<string,string> &key) | |
95 | : db(db), key(key) {} | |
96 | void finish(int r) override { | |
97 | db->rmkey(key.first, key.second); | |
98 | } | |
99 | }; | |
100 | ||
11fdf7f2 TL |
101 | using KeyValueDB::TransactionImpl::rmkey; |
102 | using KeyValueDB::TransactionImpl::set; | |
7c673cae FG |
103 | void rmkey(const string &prefix, const string &key) override { |
104 | on_commit.push_back(new RmKeysOp(db, std::make_pair(prefix, key))); | |
105 | } | |
106 | ||
107 | struct RmKeysByPrefixOp : public Context { | |
108 | KeyValueDBMemory *db; | |
109 | string prefix; | |
110 | RmKeysByPrefixOp(KeyValueDBMemory *db, | |
111 | const string &prefix) | |
112 | : db(db), prefix(prefix) {} | |
113 | void finish(int r) override { | |
114 | db->rmkeys_by_prefix(prefix); | |
115 | } | |
116 | }; | |
117 | void rmkeys_by_prefix(const string &prefix) override { | |
118 | on_commit.push_back(new RmKeysByPrefixOp(db, prefix)); | |
119 | } | |
120 | ||
121 | struct RmRangeKeys: public Context { | |
122 | KeyValueDBMemory *db; | |
123 | string prefix, start, end; | |
124 | RmRangeKeys(KeyValueDBMemory *db, const string &prefix, const string &s, const string &e) | |
125 | : db(db), prefix(prefix), start(s), end(e) {} | |
126 | void finish(int r) { | |
127 | db->rm_range_keys(prefix, start, end); | |
128 | } | |
129 | }; | |
130 | ||
131 | void rm_range_keys(const string &prefix, const string &start, const string &end) { | |
132 | on_commit.push_back(new RmRangeKeys(db, prefix, start, end)); | |
133 | } | |
134 | ||
135 | int complete() { | |
136 | for (list<Context *>::iterator i = on_commit.begin(); | |
137 | i != on_commit.end(); | |
138 | on_commit.erase(i++)) { | |
139 | (*i)->complete(0); | |
140 | } | |
141 | return 0; | |
142 | } | |
143 | ||
144 | ~TransactionImpl_() override { | |
145 | for (list<Context *>::iterator i = on_commit.begin(); | |
146 | i != on_commit.end(); | |
147 | on_commit.erase(i++)) { | |
148 | delete *i; | |
149 | } | |
150 | } | |
151 | }; | |
152 | ||
153 | Transaction get_transaction() override { | |
154 | return Transaction(new TransactionImpl_(this)); | |
155 | } | |
156 | ||
157 | int submit_transaction(Transaction trans) override { | |
158 | return static_cast<TransactionImpl_*>(trans.get())->complete(); | |
159 | } | |
160 | ||
161 | uint64_t get_estimated_size(map<string,uint64_t> &extras) override { | |
162 | uint64_t total_size = 0; | |
163 | ||
164 | for (map<pair<string,string>,bufferlist>::iterator p = db.begin(); | |
165 | p != db.end(); ++p) { | |
166 | string prefix = p->first.first; | |
167 | bufferlist &bl = p->second; | |
168 | ||
169 | uint64_t sz = bl.length(); | |
170 | total_size += sz; | |
171 | if (extras.count(prefix) == 0) | |
172 | extras[prefix] = 0; | |
173 | extras[prefix] += sz; | |
174 | } | |
175 | ||
176 | return total_size; | |
177 | } | |
178 | ||
179 | private: | |
180 | bool exists_prefix(const string &prefix) { | |
181 | std::map<std::pair<string,string>,bufferlist>::iterator it; | |
182 | it = db.lower_bound(std::make_pair(prefix, "")); | |
183 | return ((it != db.end()) && ((*it).first.first == prefix)); | |
184 | } | |
185 | ||
186 | friend class WholeSpaceMemIterator; | |
187 | ||
11fdf7f2 TL |
188 | public: |
189 | WholeSpaceIterator get_wholespace_iterator() override; | |
7c673cae | 190 | }; |