Loading...
Searching...
No Matches
leveldb.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 <memory>
23#include <optional>
24#include <string>
25
26#include "chain/storage/proto/leveldb_config.pb.h"
28#include "leveldb/db.h"
29#include "leveldb/write_batch.h"
30
31namespace resdb {
32namespace storage {
33
34std::unique_ptr<Storage> NewResLevelDB(
35 const std::string& path, std::optional<LevelDBInfo> config = std::nullopt);
36std::unique_ptr<Storage> NewResLevelDB(
37 std::optional<LevelDBInfo> config = std::nullopt);
38
39class ResLevelDB : public Storage {
40 public:
41 ResLevelDB(std::optional<LevelDBInfo> config_data = std::nullopt);
42
43 virtual ~ResLevelDB();
44 int SetValue(const std::string& key, const std::string& value) override;
45 std::string GetValue(const std::string& key) override;
46 std::string GetAllValues(void) override;
47 std::string GetRange(const std::string& min_key,
48 const std::string& max_key) override;
49
50 int SetValueWithVersion(const std::string& key, const std::string& value,
51 int version) override;
52 std::pair<std::string, int> GetValueWithVersion(const std::string& key,
53 int version) override;
54
55 // Return a map of <key, <value, version>>
56 std::map<std::string, std::pair<std::string, int>> GetAllItems() override;
57 std::map<std::string, std::pair<std::string, int>> GetKeyRange(
58 const std::string& min_key, const std::string& max_key) override;
59
60 // Return a list of <value, version>
61 std::vector<std::pair<std::string, int>> GetHistory(const std::string& key,
62 int min_version,
63 int max_version) override;
64
65 std::vector<std::pair<std::string, int>> GetTopHistory(
66 const std::string& key, int top_number) override;
67
68 bool Flush() override;
69
70 private:
71 void CreateDB(const std::string& path);
72
73 private:
74 std::unique_ptr<leveldb::DB> db_ = nullptr;
75 ::leveldb::WriteBatch batch_;
76 unsigned int write_buffer_size_ = 64 << 20;
77 unsigned int write_batch_size_ = 1;
78};
79
80} // namespace storage
81} // namespace resdb
std::map< std::string, std::pair< std::string, int > > GetKeyRange(const std::string &min_key, const std::string &max_key) override
Definition leveldb.cpp:223
std::string GetAllValues(void) override
Definition leveldb.cpp:105
std::map< std::string, std::pair< std::string, int > > GetAllItems() override
Definition leveldb.cpp:203
std::vector< std::pair< std::string, int > > GetTopHistory(const std::string &key, int top_number) override
Definition leveldb.cpp:270
std::vector< std::pair< std::string, int > > GetHistory(const std::string &key, int min_version, int max_version) override
Definition leveldb.cpp:246
std::pair< std::string, int > GetValueWithVersion(const std::string &key, int version) override
Definition leveldb.cpp:175
int SetValue(const std::string &key, const std::string &value) override
Definition leveldb.cpp:79
std::string GetRange(const std::string &min_key, const std::string &max_key) override
Definition leveldb.cpp:120
int SetValueWithVersion(const std::string &key, const std::string &value, int version) override
Definition leveldb.cpp:147
std::string GetValue(const std::string &key) override
Definition leveldb.cpp:95
bool Flush() override
Definition leveldb.cpp:137
std::unique_ptr< Storage > storage
std::unique_ptr< Storage > NewResLevelDB(const std::string &path, std::optional< LevelDBInfo > config)
Definition leveldb.cpp:29