methods to check elements without deserialization
This commit is contained in:
parent
08daad48ad
commit
6ff22c9377
1
cache.h
1
cache.h
@ -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;
|
||||
|
32
cache.hpp
32
cache.hpp
@ -137,11 +137,41 @@ V DataBase::Cache<K, V>::getRecord(const K& key) const {
|
||||
handleMode();
|
||||
return value;
|
||||
} catch (const NotFound& error) {
|
||||
abscent->insert(key);
|
||||
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) {
|
||||
|
1
table.h
1
table.h
@ -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;
|
||||
|
||||
|
26
table.hpp
26
table.hpp
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user