forked from blue/lmdbal
methods for addition with overwrite
This commit is contained in:
parent
928558539c
commit
08daad48ad
1
cache.h
1
cache.h
@ -39,6 +39,7 @@ private:
|
||||
|
||||
public:
|
||||
virtual void addRecord(const K& key, const V& value) override;
|
||||
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 V getRecord(const K& key) const override;
|
||||
|
27
cache.hpp
27
cache.hpp
@ -52,8 +52,35 @@ void DataBase::Cache<K, V>::addRecord(const K& key, const V& value) {
|
||||
|
||||
Table<K, V>::addRecord(key, value);
|
||||
cache->insert(std::make_pair(key, value));
|
||||
|
||||
if (*mode != Mode::full) {
|
||||
abscent->erase(key);
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
bool DataBase::Cache<K, V>::forceRecord(const K& key, const V& value) {
|
||||
if (!DataBase::Table<K, V>::db->opened) {
|
||||
throw Closed("forceRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
|
||||
bool added = Table<K, V>::forceRecord(key, value);
|
||||
if (*mode == Mode::full) {
|
||||
(*cache)[key] = value;
|
||||
} else {
|
||||
if (added) {
|
||||
abscent->erase(key);
|
||||
}
|
||||
std::pair<typename std::map<K, V>::iterator, bool> result = cache->insert(std::make_pair(key, value));
|
||||
if (!result.second) {
|
||||
result.first->second = value;
|
||||
} else if (!added) { //this way database had value but cache didn't, so, need to decrease sizeDifference
|
||||
handleMode();
|
||||
}
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
|
||||
|
1
table.h
1
table.h
@ -55,6 +55,7 @@ protected:
|
||||
|
||||
public:
|
||||
virtual void addRecord(const K& key, const V& value);
|
||||
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 V getRecord(const K& key) const;
|
||||
|
31
table.hpp
31
table.hpp
@ -58,7 +58,38 @@ void DataBase::Table<K, V>::addRecord(const K& key, const V& value) {
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
bool DataBase::Table<K, V>::forceRecord(const K& key, const V& value) {
|
||||
if (!db->opened) {
|
||||
throw Closed("forceRecord", db->name, name);
|
||||
}
|
||||
|
||||
bool added;
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
MDB_val lmdbData;
|
||||
MDB_txn *txn;
|
||||
mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
int rc;
|
||||
rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
||||
if (rc == 0) {
|
||||
added = false;
|
||||
} else if (rc == MDB_NOTFOUND) {
|
||||
added = true;
|
||||
} else {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
lmdbData = valueSerializer->setData(value);
|
||||
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
|
||||
if (rc != 0) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
} else {
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Table<K, V>::changeRecord(const K& key, const V& value) {
|
||||
|
Loading…
Reference in New Issue
Block a user