Loading...
Searching...
No Matches
rocksdb.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/rocksdb_config.pb.h"
28#include "rocksdb/db.h"
29#include "rocksdb/write_batch.h"
30
31namespace resdb {
32namespace storage {
33
34std::unique_ptr<Storage> NewResRocksDB(
35 const std::string& path, std::optional<RocksDBInfo> config = std::nullopt);
36std::unique_ptr<Storage> NewResRocksDB(
37 std::optional<RocksDBInfo> config = std::nullopt);
38
39class ResRocksDB : public Storage {
40 public:
41 ResRocksDB(std::optional<RocksDBInfo> config_data = std::nullopt);
42 virtual ~ResRocksDB();
43 int SetValue(const std::string& key, const std::string& value) override;
44 std::string GetValue(const std::string& key) override;
45 std::string GetAllValues(void) override;
46 std::string GetRange(const std::string& min_key,
47 const std::string& max_key) override;
48
49 int SetValueWithVersion(const std::string& key, const std::string& value,
50 int version) override;
51 std::pair<std::string, int> GetValueWithVersion(const std::string& key,
52 int version) override;
53
54 // Return a map of <key, <value, version>>
55 std::map<std::string, std::pair<std::string, int>> GetAllItems() override;
56 std::map<std::string, std::pair<std::string, int>> GetKeyRange(
57 const std::string& min_key, const std::string& max_key) override;
58
59 // Return a list of <value, version>
60 std::vector<std::pair<std::string, int>> GetHistory(const std::string& key,
61 int min_version,
62 int max_version) override;
63 std::vector<std::pair<std::string, int>> GetTopHistory(
64 const std::string& key, int top_number) override;
65
66 bool Flush() override;
67
68 private:
69 void CreateDB(const std::string& path);
70
71 private:
72 std::unique_ptr<::rocksdb::DB> db_ = nullptr;
73 ::rocksdb::WriteBatch batch_;
74 unsigned int num_threads_ = 1;
75 unsigned int write_buffer_size_ = 64 << 20;
76 unsigned int write_batch_size_ = 1;
77};
78
79} // namespace storage
80} // namespace resdb
std::pair< std::string, int > GetValueWithVersion(const std::string &key, int version) override
Definition rocksdb.cpp:176
int SetValue(const std::string &key, const std::string &value) override
Definition rocksdb.cpp:82
std::map< std::string, std::pair< std::string, int > > GetKeyRange(const std::string &min_key, const std::string &max_key) override
Definition rocksdb.cpp:224
std::vector< std::pair< std::string, int > > GetHistory(const std::string &key, int min_version, int max_version) override
Definition rocksdb.cpp:247
int SetValueWithVersion(const std::string &key, const std::string &value, int version) override
Definition rocksdb.cpp:148
bool Flush() override
Definition rocksdb.cpp:139
std::string GetValue(const std::string &key) override
Definition rocksdb.cpp:97
std::string GetAllValues(void) override
Definition rocksdb.cpp:107
std::vector< std::pair< std::string, int > > GetTopHistory(const std::string &key, int top_number) override
Definition rocksdb.cpp:271
std::string GetRange(const std::string &min_key, const std::string &max_key) override
Definition rocksdb.cpp:122
std::map< std::string, std::pair< std::string, int > > GetAllItems() override
Definition rocksdb.cpp:204
std::unique_ptr< Storage > storage
std::unique_ptr< Storage > NewResRocksDB(const std::string &path, std::optional< RocksDBInfo > config)
Definition rocksdb.cpp:29