-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMemoryDB.h
More file actions
52 lines (42 loc) · 1.46 KB
/
MemoryDB.h
File metadata and controls
52 lines (42 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Aleth: Ethereum C++ client, tools and libraries.
// Copyright 2017-2019 Aleth Authors.
// Licensed under the GNU General Public License, Version 3.
#pragma once
#include "Common.h"
#include "Guards.h"
#include "db.h"
namespace dev
{
namespace db
{
class MemoryDBWriteBatch : public WriteBatchFace
{
public:
void insert(Slice _key, Slice _value) override;
void kill(Slice _key) override;
std::unordered_map<std::string, std::string>& writeBatch() { return m_batch; }
size_t size() { return m_batch.size(); }
private:
std::unordered_map<std::string, std::string> m_batch;
};
class MemoryDB : public DatabaseFace
{
public:
std::string lookup(Slice _key) const override;
bool exists(Slice _key) const override;
void insert(Slice _key, Slice _value) override;
void kill(Slice _key) override;
std::unique_ptr<WriteBatchFace> createWriteBatch() const override;
void commit(std::unique_ptr<WriteBatchFace> _batch) override;
// A database must implement the `forEach` method that allows the caller
// to pass in a function `f`, which will be called with the key and value
// of each record in the database. If `f` returns false, the `forEach`
// method must return immediately.
void forEach(std::function<bool(Slice, Slice)> _f) const override;
size_t size() const { return m_db.size(); }
private:
std::unordered_map<std::string, std::string> m_db;
mutable Mutex m_mutex;
};
} // namespace db
} // namespace dev