Key count is innacurate after overwrite

Former-commit-id: 180c5a8e401415cede36548ab8d01f3e4ff8fb7f
This commit is contained in:
John Sully 2019-12-22 19:41:36 -05:00
parent 1b56e36309
commit 033eda6016
3 changed files with 19 additions and 4 deletions

View File

@ -12,12 +12,15 @@ RocksDBStorageProvider::RocksDBStorageProvider(std::shared_ptr<rocksdb::DB> &spd
void RocksDBStorageProvider::insert(const char *key, size_t cchKey, void *data, size_t cb) void RocksDBStorageProvider::insert(const char *key, size_t cchKey, void *data, size_t cb)
{ {
rocksdb::Status status; rocksdb::Status status;
bool fOverwrite = FKeyExists(key, cchKey);
if (m_spbatch != nullptr) if (m_spbatch != nullptr)
status = m_spbatch->Put(m_spcolfamily.get(), rocksdb::Slice(key, cchKey), rocksdb::Slice((const char*)data, cb)); status = m_spbatch->Put(m_spcolfamily.get(), rocksdb::Slice(key, cchKey), rocksdb::Slice((const char*)data, cb));
else else
status = m_spdb->Put(WriteOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cchKey), rocksdb::Slice((const char*)data, cb)); status = m_spdb->Put(WriteOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cchKey), rocksdb::Slice((const char*)data, cb));
if (!status.ok()) if (!status.ok())
throw status.ToString(); throw status.ToString();
if (!fOverwrite)
++m_count; ++m_count;
} }
@ -30,8 +33,7 @@ bool RocksDBStorageProvider::erase(const char *key, size_t cchKey)
} }
else else
{ {
std::string strT; if (!FKeyExists(key, cchKey))
if (!m_spdb->Get(ReadOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cchKey), &strT).ok())
return false; return false;
status = m_spdb->Delete(WriteOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cchKey)); status = m_spdb->Delete(WriteOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cchKey));
} }
@ -124,3 +126,9 @@ void RocksDBStorageProvider::flush()
{ {
m_spdb->SyncWAL(); m_spdb->SyncWAL();
} }
bool RocksDBStorageProvider::FKeyExists(const char *key, size_t cch) const
{
std::string strT;
return m_spdb->Get(ReadOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cch), &strT).ok();
}

View File

@ -33,6 +33,7 @@ public:
size_t count() const; size_t count() const;
protected: protected:
bool FKeyExists(const char *key, size_t cchKey) const;
const rocksdb::ReadOptions &ReadOptions() const { return m_readOptionsTemplate; } const rocksdb::ReadOptions &ReadOptions() const { return m_readOptionsTemplate; }
rocksdb::WriteOptions WriteOptions() const; rocksdb::WriteOptions WriteOptions() const;

View File

@ -7,13 +7,19 @@ start_server {tags {"flash"} overrides {"storage-provider flash ./rocks.db"}} {
} }
test { DEL of nonexistant key returns 0 } { test { DEL of nonexistant key returns 0 } {
r flushall
assert_equal {0} [r del foobar] assert_equal {0} [r del foobar]
assert_equal {0} [r dbsize] "Key count is accurate after non-existant delete"
} }
test { SET of existing but flushed key works } { test { SET of existing but flushed key works } {
r flushall
r set testkey foo r set testkey foo
assert_equal {1} [r dbsize] "Only one key after first insert"
r flushall cache r flushall cache
assert_equal {1} [r dbsize] "Only one key after flushall cache"
r set testkey bar r set testkey bar
assert_equal {1} [r dbsize] "Only one key after overwrite"
assert_equal {bar} [r get testkey] assert_equal {bar} [r get testkey]
} }