Loading...
Searching...
No Matches
memory_db.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23
24namespace resdb {
25namespace storage {
26
27std::unique_ptr<Storage> NewMemoryDB() { return std::make_unique<MemoryDB>(); }
28
30
31int MemoryDB::SetValue(const std::string& key, const std::string& value) {
32 kv_map_[key] = value;
33 return 0;
34}
35
36std::string MemoryDB::GetAllValues(void) {
37 std::string values = "[";
38 bool first_iteration = true;
39 for (auto kv : kv_map_) {
40 if (!first_iteration) values.append(",");
41 first_iteration = false;
42 values.append(kv.second);
43 }
44 values.append("]");
45 return values;
46}
47
48std::string MemoryDB::GetRange(const std::string& min_key,
49 const std::string& max_key) {
50 std::string values = "[";
51 bool first_iteration = true;
52 for (auto kv : kv_map_) {
53 if (kv.first >= min_key && kv.first <= max_key) {
54 if (!first_iteration) values.append(",");
55 first_iteration = false;
56 values.append(kv.second);
57 }
58 }
59 values.append("]");
60 return values;
61}
62
63std::string MemoryDB::GetValue(const std::string& key) {
64 auto search = kv_map_.find(key);
65 if (search != kv_map_.end())
66 return search->second;
67 else {
68 return "";
69 }
70}
71
72int MemoryDB::SetValueWithVersion(const std::string& key,
73 const std::string& value, int version) {
74 auto it = kv_map_with_v_.find(key);
75 if ((it == kv_map_with_v_.end() && version != 0) ||
76 (it != kv_map_with_v_.end() && it->second.back().second != version)) {
77 LOG(ERROR) << " value version not match. key:" << key << " db version:"
78 << (it == kv_map_with_v_.end() ? 0 : it->second.back().second)
79 << " user version:" << version;
80 return -2;
81 }
82 kv_map_with_v_[key].push_back(std::make_pair(value, version + 1));
83 return 0;
84}
85
86std::pair<std::string, int> MemoryDB::GetValueWithVersion(
87 const std::string& key, int version) {
88 auto search_it = kv_map_with_v_.find(key);
89 if (search_it != kv_map_with_v_.end() && search_it->second.size()) {
90 auto it = search_it->second.end();
91 do {
92 --it;
93 if (it->second == version) {
94 return *it;
95 }
96 if (it->second < version) {
97 break;
98 }
99 } while (it != search_it->second.begin());
100 it = --search_it->second.end();
101 LOG(ERROR) << " key:" << key << " no version:" << version
102 << " return max:" << it->second;
103 return *it;
104 }
105 return std::make_pair("", 0);
106}
107
108std::map<std::string, std::pair<std::string, int>> MemoryDB::GetAllItems() {
109 std::map<std::string, std::pair<std::string, int>> resp;
110
111 for (const auto& it : kv_map_with_v_) {
112 resp.insert(std::make_pair(it.first, it.second.back()));
113 }
114 return resp;
115}
116
117std::map<std::string, std::pair<std::string, int>> MemoryDB::GetKeyRange(
118 const std::string& min_key, const std::string& max_key) {
119 LOG(ERROR) << "min key:" << min_key << " max key:" << max_key;
120 std::map<std::string, std::pair<std::string, int>> resp;
121 for (const auto& it : kv_map_with_v_) {
122 if (it.first >= min_key && it.first <= max_key) {
123 resp.insert(std::make_pair(it.first, it.second.back()));
124 }
125 }
126 return resp;
127}
128
129std::vector<std::pair<std::string, int>> MemoryDB::GetHistory(
130 const std::string& key, int min_version, int max_version) {
131 std::vector<std::pair<std::string, int>> resp;
132 auto search_it = kv_map_with_v_.find(key);
133 if (search_it == kv_map_with_v_.end()) {
134 return resp;
135 }
136
137 auto it = search_it->second.end();
138 do {
139 --it;
140 if (it->second < min_version) {
141 break;
142 }
143 if (it->second <= max_version) {
144 resp.push_back(*it);
145 }
146 } while (it != search_it->second.begin());
147 return resp;
148}
149
150std::vector<std::pair<std::string, int>> MemoryDB::GetTopHistory(
151 const std::string& key, int top_number) {
152 std::vector<std::pair<std::string, int>> resp;
153 auto search_it = kv_map_with_v_.find(key);
154 if (search_it == kv_map_with_v_.end()) {
155 return resp;
156 }
157
158 auto it = search_it->second.end();
159 do {
160 --it;
161 resp.push_back(*it);
162 if (resp.size() >= static_cast<size_t>(top_number)) {
163 break;
164 }
165 } while (it != search_it->second.begin());
166 return resp;
167}
168
169} // namespace storage
170} // namespace resdb
std::vector< std::pair< std::string, int > > GetTopHistory(const std::string &key, int number) override
std::string GetAllValues() override
Definition memory_db.cpp:36
std::vector< std::pair< std::string, int > > GetHistory(const std::string &key, int min_version, int max_version) override
std::string GetRange(const std::string &min_key, const std::string &max_key) override
Definition memory_db.cpp:48
std::string GetValue(const std::string &key)
Definition memory_db.cpp:63
std::map< std::string, std::pair< std::string, int > > GetAllItems() override
int SetValueWithVersion(const std::string &key, const std::string &value, int version) override
Definition memory_db.cpp:72
int SetValue(const std::string &key, const std::string &value)
Definition memory_db.cpp:31
std::pair< std::string, int > GetValueWithVersion(const std::string &key, int version) override
Definition memory_db.cpp:86
std::map< std::string, std::pair< std::string, int > > GetKeyRange(const std::string &min_key, const std::string &max_key) override
std::unique_ptr< Storage > storage
std::unique_ptr< Storage > NewMemoryDB()
Definition memory_db.cpp:27