1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-12 19:22:37 +00:00
ton/tddb/td/db/RocksDb.h
birydrad 72020c04c4
celldb in-memory mode, stats for actors, perf counters, minor fix in rldp2 (#1164)
* getactorstats query for validator-engine-console

* celldb in-memory mode (--celldb-in-memory option)

* rldp2: bugfix - do not estimate speed while nothing is sent

* add simple ed25519 benchmark

* fix compilation errors of different platforms and move to c++20

* fix some warnings

* turn on TON_USE_ABSEIL for glibc 2.27 nix build

---------

Co-authored-by: birydrad <>
2024-09-23 17:34:37 +03:00

127 lines
3.8 KiB
C++

/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#pragma once
#if !TDDB_USE_ROCKSDB
#error "RocksDb is not supported"
#endif
#include "td/db/KeyValue.h"
#include "td/utils/Span.h"
#include "td/utils/Status.h"
#include "td/utils/optional.h"
#include "td/utils/Time.h"
#include <map>
#include <mutex>
#include <set>
#include <functional>
namespace rocksdb {
class Cache;
class OptimisticTransactionDB;
class Transaction;
class WriteBatch;
class Snapshot;
class Statistics;
} // namespace rocksdb
namespace td {
struct RocksDbSnapshotStatistics {
void begin_snapshot(const rocksdb::Snapshot *snapshot);
void end_snapshot(const rocksdb::Snapshot *snapshot);
td::Timestamp oldest_snapshot_timestamp() const;
std::string to_string() const;
private:
mutable std::mutex mutex_;
std::map<std::uintptr_t, double> id_to_ts_;
std::set<std::pair<double, std::uintptr_t>> by_ts_;
};
struct RocksDbOptions {
std::shared_ptr<rocksdb::Statistics> statistics = nullptr;
std::shared_ptr<rocksdb::Cache> block_cache; // Default - one 1G cache for all RocksDb
std::shared_ptr<RocksDbSnapshotStatistics> snapshot_statistics = nullptr;
bool use_direct_reads = false;
bool no_block_cache = false;
};
class RocksDb : public KeyValue {
public:
static Status destroy(Slice path);
RocksDb clone() const;
static Result<RocksDb> open(std::string path, RocksDbOptions options = {});
Result<GetStatus> get(Slice key, std::string &value) override;
Status set(Slice key, Slice value) override;
Status erase(Slice key) override;
Result<size_t> count(Slice prefix) override;
Status for_each(std::function<Status(Slice, Slice)> f) override;
Status for_each_in_range (Slice begin, Slice end, std::function<Status(Slice, Slice)> f) override;
Status begin_write_batch() override;
Status commit_write_batch() override;
Status abort_write_batch() override;
Status begin_transaction() override;
Status commit_transaction() override;
Status abort_transaction() override;
Status flush() override;
Status begin_snapshot();
Status end_snapshot();
std::unique_ptr<KeyValueReader> snapshot() override;
std::string stats() const override;
static std::shared_ptr<rocksdb::Statistics> create_statistics();
static std::string statistics_to_string(const std::shared_ptr<rocksdb::Statistics> statistics);
static void reset_statistics(const std::shared_ptr<rocksdb::Statistics> statistics);
static std::shared_ptr<rocksdb::Cache> create_cache(size_t capacity);
RocksDb(RocksDb &&);
RocksDb &operator=(RocksDb &&);
~RocksDb();
std::shared_ptr<rocksdb::OptimisticTransactionDB> raw_db() const {
return db_;
};
private:
std::shared_ptr<rocksdb::OptimisticTransactionDB> db_;
RocksDbOptions options_;
std::unique_ptr<rocksdb::Transaction> transaction_;
std::unique_ptr<rocksdb::WriteBatch> write_batch_;
class UnreachableDeleter {
public:
template <class T>
void operator()(T *) {
UNREACHABLE();
}
};
std::unique_ptr<const rocksdb::Snapshot, UnreachableDeleter> snapshot_;
explicit RocksDb(std::shared_ptr<rocksdb::OptimisticTransactionDB> db, RocksDbOptions options);
};
} // namespace td