Loading...
Searching...
No Matches
memory_db.h
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
20#pragma once
21
22#include <list>
23#include <map>
24#include <memory>
25#include <unordered_map>
26
28
29namespace resdb {
30namespace storage {
31
32// Key Value Storage supporting two types of interfaces:
33// Non-version:
34// It provides set and get function to set a value to a specific key
35// Values will be set directly.
36// Version:
37// It provides set and get function to set a value to a specific key
38// with a version.
39// The version inside setting function is to support OCC verification.
40// The version value should be obtained before setting
41// and returned back when setting a new value. If the version is not
42// the same as the old one, return failure.
43// If the value of a specific version does not exist or providing
44// version 0 as parameter when accessing get
45// it returns the current value along its version.
46//
47// Note: Only one type of interface are allowed to be used.
48//
49
50std::unique_ptr<Storage> NewMemoryDB();
51
52class MemoryDB : public Storage {
53 public:
54 MemoryDB();
55
56 int SetValue(const std::string& key, const std::string& value);
57 std::string GetValue(const std::string& key);
58
59 std::string GetAllValues() override;
60 std::string GetRange(const std::string& min_key,
61 const std::string& max_key) override;
62
63 int SetValueWithVersion(const std::string& key, const std::string& value,
64 int version) override;
65 std::pair<std::string, int> GetValueWithVersion(const std::string& key,
66 int version) override;
67
68 // Return a map of <key, <value, version>>
69 std::map<std::string, std::pair<std::string, int>> GetAllItems() override;
70 std::map<std::string, std::pair<std::string, int>> GetKeyRange(
71 const std::string& min_key, const std::string& max_key) override;
72
73 // Return a list of <value, version>
74 std::vector<std::pair<std::string, int>> GetHistory(const std::string& key,
75 int min_version,
76 int max_version) override;
77
78 std::vector<std::pair<std::string, int>> GetTopHistory(const std::string& key,
79 int number) override;
80
81 private:
82 std::unordered_map<std::string, std::string> kv_map_;
83 std::unordered_map<std::string, std::list<std::pair<std::string, int>>>
84 kv_map_with_v_;
85};
86
87} // namespace storage
88} // 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