LMDBAL 0.6.0
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
cache.h
1/*
2 * LMDB Abstraction Layer.
3 * Copyright (C) 2023 Yury Gubich <blue@macaw.me>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <map>
22#include <set>
23#include <tuple>
24
25#include "storage.h"
26
27namespace LMDBAL {
28
29template <class K, class V>
30class Cache : public Storage<K, V> {
31 friend class Base;
32 enum class Mode {
33 nothing,
34 size,
35 full
36 };
37
38 enum class Operation {
39 add,
40 remove,
41 change,
42 force,
43 drop,
44 replace,
45 addMany
46 };
47
48 typedef std::pair<Operation, void*> Entry;
49 typedef std::list<Entry> Queue;
50 typedef std::map<TransactionID, Queue> TransactionCache;
51
52protected:
53 Cache(Base* parent, const std::string& name, bool duplicates = false);
54 ~Cache() override;
55
56 virtual void handleDrop() override;
57 virtual void transactionStarted(TransactionID txn, bool readOnly) const override;
58 virtual void transactionCommited(TransactionID txn) override;
59 virtual void transactionAborted(TransactionID txn) const override;
60
61 virtual void discoveredRecord(const K& key, const V& value) const override;
62 virtual void discoveredRecord(const K& key, const V& value, TransactionID txn) const override;
63
64 virtual SizeType count(TransactionID txn) const override;
65 virtual void addRecord(const K& key, const V& value, TransactionID txn) override;
66 virtual bool forceRecord(const K& key, const V& value, TransactionID txn) override;
67 virtual void changeRecord(const K& key, const V& value, TransactionID txn) override;
68 virtual void removeRecord(const K& key, TransactionID txn) override;
69 virtual bool checkRecord(const K& key, TransactionID txn) const override;
70 virtual void getRecord(const K& key, V& out, TransactionID txn) const override;
71 virtual V getRecord(const K& key, TransactionID txn) const override;
72 virtual std::map<K, V> readAll(TransactionID txn) const override;
73 virtual void readAll(std::map<K, V>& out, TransactionID txn) const override;
74 virtual void replaceAll(const std::map<K, V>& data, TransactionID txn) override;
75 virtual SizeType addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite = false) override;
76
77private:
78 void handleMode() const;
79
80 void handleTransactionEntry(const Entry& entry);
81 void destroyTransactionEntry(const Entry& entry) const;
82
83 void handleAddRecord(const K& key, const V& value);
84 void handleRemoveRecord(const K& key);
85 void handleChangeRecord(const K& key, const V& value);
86 void handleForceRecord(const K& key, const V& value, bool added);
87 void handleReplaceAll(std::map<K, V>* data);
88 void handleAddRecords(const std::map<K, V>& data, bool overwrite, SizeType newSize);
89 void appendToCache(const K& key, const V& value) const;
90
91public:
92 using Storage<K, V>::drop;
93 using Storage<K, V>::addRecord;
94 using Storage<K, V>::forceRecord;
95 using Storage<K, V>::changeRecord;
96 using Storage<K, V>::removeRecord;
97 using Storage<K, V>::checkRecord;
98 using Storage<K, V>::getRecord;
99 using Storage<K, V>::readAll;
100 using Storage<K, V>::replaceAll;
101 using Storage<K, V>::addRecords;
102 using Storage<K, V>::count;
103 virtual int drop(const WriteTransaction& transaction) override;
104 virtual void addRecord(const K& key, const V& value) override;
105 virtual bool forceRecord(const K& key, const V& value) override;
106 virtual void changeRecord(const K& key, const V& value) override;
107 virtual void removeRecord(const K& key) override;
108 virtual bool checkRecord(const K& key) const override;
109 virtual void getRecord(const K& key, V& out) const override;
110 virtual V getRecord(const K& key) const override;
111 virtual SizeType count() const override;
112
113 virtual std::map<K, V> readAll() const override;
114 virtual void readAll(std::map<K, V>& out) const override;
115 virtual void replaceAll(const std::map<K, V>& data) override;
116 virtual SizeType addRecords(const std::map<K, V>& data, bool overwrite = false) override;
117
118protected:
126 mutable Mode mode;
127 std::map<K, V>* cache;
128 std::set<K>* abscent;
129 mutable SizeType sizeDifference;
130 TransactionCache* transactionCache;
131};
132
133}
134
135#include "cache.hpp"
SizeType sizeDifference
Difference of size between cached data and amount of records in the lmdb storage.
Definition cache.h:129
Mode mode
Cache mode.
Definition cache.h:126
virtual bool forceRecord(const K &key, const V &value, TransactionID txn) override
Adds a key-value record to the storage, overwrites if it already exists (private transaction variant)
Definition cache.hpp:108
virtual std::map< K, V > readAll(TransactionID txn) const override
Reads whole storage into a map (private transaction variant)
Definition cache.hpp:486
virtual void transactionAborted(TransactionID txn) const override
called on abortion of public transaction
Definition cache.hpp:825
virtual std::map< K, V > readAll() const override
Reads whole storage into a map.
Definition cache.hpp:459
virtual bool checkRecord(const K &key, TransactionID txn) const override
Chechs if storage has value (private transaction variant)
Definition cache.hpp:378
virtual void discoveredRecord(const K &key, const V &value) const override
A private virtual method that cursor calls when he reads a record, does nothing here but populates th...
Definition cache.hpp:342
std::map< K, V > * cache
Cached data.
Definition cache.h:127
virtual int drop(const WriteTransaction &transaction) override
Drops content of a storage interface (public transaction variant)
Definition cache.hpp:783
Cache(Base *parent, const std::string &name, bool duplicates=false)
Creates a cache.
Definition cache.hpp:46
TransactionCache * transactionCache
All changes made under under uncommited transactions.
Definition cache.h:130
virtual void replaceAll(const std::map< K, V > &data, TransactionID txn) override
Replaces the content of the whole storage with the given (private transaction variant)
Definition cache.hpp:572
virtual void transactionCommited(TransactionID txn) override
called on commitment of public transaction
Definition cache.hpp:813
virtual void changeRecord(const K &key, const V &value, TransactionID txn) override
Changes key-value record to the storage (private transaction variant)
Definition cache.hpp:168
virtual SizeType addRecords(const std::map< K, V > &data, TransactionID txn, bool overwrite=false) override
Adds records in bulk (private transaction variant)
Definition cache.hpp:604
virtual void getRecord(const K &key, V &out, TransactionID txn) const override
Gets the record from the database (private transaction, reference variant)
Definition cache.hpp:250
virtual SizeType count() const override
Storage size.
Definition cache.hpp:688
virtual SizeType count(TransactionID txn) const override
Storage size (private transaction variant)
Definition cache.hpp:712
virtual void transactionStarted(TransactionID txn, bool readOnly) const override
called on beginning of public transaction
Definition cache.hpp:807
virtual void handleDrop() override
A method where database additionally handles drop.
Definition cache.hpp:799
virtual void removeRecord(const K &key, TransactionID txn) override
Removes one of the records (private transaction variant)
Definition cache.hpp:661
std::set< K > * abscent
Set of keys that are definitely not in the cache.
Definition cache.h:128
~Cache() override
Destroys a cache.
Definition cache.hpp:58
virtual void addRecord(const K &key, const V &value, TransactionID txn) override
Adds a key-value record to the storage (private transaction variant)
Definition cache.hpp:76
const std::string name
this storage name
Definition storagecommon.h:111
const bool duplicates
true if storage supports duplicates
Definition storagecommon.h:112
Storage(Base *parent, const std::string &name, bool duplicates=false)
Creates a storage.
Definition storage.hpp:48
Public writable transaction.
Definition transaction.h:54