methods to check elements without deserialization

This commit is contained in:
Blue 2022-12-15 20:29:49 +03:00
parent 08daad48ad
commit 6ff22c9377
Signed by: blue
GPG Key ID: 9B203B252A63EE38
4 changed files with 59 additions and 1 deletions

View File

@ -42,6 +42,7 @@ public:
virtual bool forceRecord(const K& key, const V& value) override;
virtual void changeRecord(const K& key, const V& value) override;
virtual void removeRecord(const K& key) override;
virtual bool checkRecord(const K& key) const override;
virtual V getRecord(const K& key) const override;
virtual uint32_t count() const override;
virtual int drop(MDB_txn * transaction) override;

View File

@ -137,11 +137,41 @@ V DataBase::Cache<K, V>::getRecord(const K& key) const {
handleMode();
return value;
} catch (const NotFound& error) {
if (*mode != Mode::full) {
abscent->insert(key);
}
throw error;
}
}
template<class K, class V>
bool DataBase::Cache<K, V>::checkRecord(const K& key) const {
if (!DataBase::Table<K, V>::db->opened) {
throw Closed("checkRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
}
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end()) {
return true;
}
if (*mode == Mode::full || abscent->count(key) != 0) {
return false;
}
try {
V value = Table<K, V>::getRecord(key);
cache->insert(std::make_pair(key, value));
handleMode();
return true;
} catch (const NotFound& error) {
if (*mode != Mode::full) {
abscent->insert(key);
}
return false;
}
}
template<class K, class V>
std::map<K, V> DataBase::Cache<K, V>::readAll() const {
if (!DataBase::Table<K, V>::db->opened) {

View File

@ -58,6 +58,7 @@ public:
virtual bool forceRecord(const K& key, const V& value); //returns true if there was addition, false if change
virtual void changeRecord(const K& key, const V& value);
virtual void removeRecord(const K& key);
virtual bool checkRecord(const K& key) const; //checks if there is a record with given key
virtual V getRecord(const K& key) const;
virtual std::map<K, V> readAll() const;

View File

@ -141,6 +141,32 @@ V DataBase::Table<K, V>::getRecord(const K& key) const {
}
}
template<class K, class V>
bool DataBase::Table<K, V>::checkRecord(const K& key) const {
if (!db->opened) {
throw Closed("checkRecord", db->name, name);
}
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbData;
MDB_txn *txn;
int rc;
mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn);
rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
mdb_txn_abort(txn);
if (rc) {
if (rc == MDB_NOTFOUND) {
return false;
} else {
throw Unknown(db->name, mdb_strerror(rc), name);
}
} else {
return true;
}
}
template<class K, class V>
std::map<K, V> DataBase::Table<K, V>::readAll() const {
if (!db->opened) {