Loading...
Searching...
No Matches
kv_executor.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 {
25
26KVExecutor::KVExecutor(std::unique_ptr<Storage> storage)
27 : storage_(std::move(storage)) {}
28
29std::unique_ptr<std::string> KVExecutor::ExecuteData(
30 const std::string& request) {
31 KVRequest kv_request;
32 KVResponse kv_response;
33
34 if (!kv_request.ParseFromString(request)) {
35 LOG(ERROR) << "parse data fail";
36 return nullptr;
37 }
38
39 if (kv_request.cmd() == KVRequest::SET) {
40 Set(kv_request.key(), kv_request.value());
41 } else if (kv_request.cmd() == KVRequest::GET) {
42 kv_response.set_value(Get(kv_request.key()));
43 } else if (kv_request.cmd() == KVRequest::GETALLVALUES) {
44 kv_response.set_value(GetAllValues());
45 } else if (kv_request.cmd() == KVRequest::GETRANGE) {
46 kv_response.set_value(GetRange(kv_request.key(), kv_request.value()));
47 } else if (kv_request.cmd() == KVRequest::SET_WITH_VERSION) {
48 SetWithVersion(kv_request.key(), kv_request.value(), kv_request.version());
49 } else if (kv_request.cmd() == KVRequest::GET_WITH_VERSION) {
50 GetWithVersion(kv_request.key(), kv_request.version(),
51 kv_response.mutable_value_info());
52 } else if (kv_request.cmd() == KVRequest::GET_ALL_ITEMS) {
53 GetAllItems(kv_response.mutable_items());
54 } else if (kv_request.cmd() == KVRequest::GET_KEY_RANGE) {
55 GetKeyRange(kv_request.min_key(), kv_request.max_key(),
56 kv_response.mutable_items());
57 } else if (kv_request.cmd() == KVRequest::GET_HISTORY) {
58 GetHistory(kv_request.key(), kv_request.min_version(),
59 kv_request.max_version(), kv_response.mutable_items());
60 } else if (kv_request.cmd() == KVRequest::GET_TOP) {
61 GetTopHistory(kv_request.key(), kv_request.top_number(),
62 kv_response.mutable_items());
63 }
64
65 std::unique_ptr<std::string> resp_str = std::make_unique<std::string>();
66 if (!kv_response.SerializeToString(resp_str.get())) {
67 return nullptr;
68 }
69 return resp_str;
70}
71
72void KVExecutor::Set(const std::string& key, const std::string& value) {
73 storage_->SetValue(key, value);
74}
75
76std::string KVExecutor::Get(const std::string& key) {
77 return storage_->GetValue(key);
78}
79
80std::string KVExecutor::GetAllValues() { return storage_->GetAllValues(); }
81
82// Get values on a range of keys
83std::string KVExecutor::GetRange(const std::string& min_key,
84 const std::string& max_key) {
85 return storage_->GetRange(min_key, max_key);
86}
87
88void KVExecutor::SetWithVersion(const std::string& key,
89 const std::string& value, int version) {
90 storage_->SetValueWithVersion(key, value, version);
91}
92
93void KVExecutor::GetWithVersion(const std::string& key, int version,
94 ValueInfo* info) {
95 std::pair<std::string, int> ret = storage_->GetValueWithVersion(key, version);
96 info->set_value(ret.first);
97 info->set_version(ret.second);
98}
99
100void KVExecutor::GetAllItems(Items* items) {
101 const std::map<std::string, std::pair<std::string, int>>& ret =
102 storage_->GetAllItems();
103 for (auto it : ret) {
104 Item* item = items->add_item();
105 item->set_key(it.first);
106 item->mutable_value_info()->set_value(it.second.first);
107 item->mutable_value_info()->set_version(it.second.second);
108 }
109}
110
111void KVExecutor::GetKeyRange(const std::string& min_key,
112 const std::string& max_key, Items* items) {
113 const std::map<std::string, std::pair<std::string, int>>& ret =
114 storage_->GetKeyRange(min_key, max_key);
115 for (auto it : ret) {
116 Item* item = items->add_item();
117 item->set_key(it.first);
118 item->mutable_value_info()->set_value(it.second.first);
119 item->mutable_value_info()->set_version(it.second.second);
120 }
121}
122
123void KVExecutor::GetHistory(const std::string& key, int min_version,
124 int max_version, Items* items) {
125 const std::vector<std::pair<std::string, int>>& ret =
126 storage_->GetHistory(key, min_version, max_version);
127 for (auto it : ret) {
128 Item* item = items->add_item();
129 item->set_key(key);
130 item->mutable_value_info()->set_value(it.first);
131 item->mutable_value_info()->set_version(it.second);
132 }
133}
134
135void KVExecutor::GetTopHistory(const std::string& key, int top_number,
136 Items* items) {
137 const std::vector<std::pair<std::string, int>>& ret =
138 storage_->GetTopHistory(key, top_number);
139 for (auto it : ret) {
140 Item* item = items->add_item();
141 item->set_key(key);
142 item->mutable_value_info()->set_value(it.first);
143 item->mutable_value_info()->set_version(it.second);
144 }
145}
146
147} // namespace resdb
KVExecutor(std::unique_ptr< Storage > storage)
virtual void Set(const std::string &key, const std::string &value)
std::unique_ptr< std::string > ExecuteData(const std::string &request) override
void GetTopHistory(const std::string &key, int top_number, Items *items)
void GetAllItems(Items *items)
void GetKeyRange(const std::string &min_key, const std::string &max_key, Items *items)
std::string Get(const std::string &key)
void GetHistory(const std::string &key, int min_key, int max_key, Items *items)
void GetWithVersion(const std::string &key, int version, ValueInfo *info)
std::string GetAllValues()
std::string GetRange(const std::string &min_key, const std::string &max_key)
void SetWithVersion(const std::string &key, const std::string &value, int version)
std::unique_ptr< Storage > storage