diff --git a/src/storage/rocksdb.cpp b/src/storage/rocksdb.cpp index 96333195f..9ecac8ed8 100644 --- a/src/storage/rocksdb.cpp +++ b/src/storage/rocksdb.cpp @@ -12,13 +12,16 @@ RocksDBStorageProvider::RocksDBStorageProvider(std::shared_ptr &spd void RocksDBStorageProvider::insert(const char *key, size_t cchKey, void *data, size_t cb) { rocksdb::Status status; + bool fOverwrite = FKeyExists(key, cchKey); if (m_spbatch != nullptr) status = m_spbatch->Put(m_spcolfamily.get(), rocksdb::Slice(key, cchKey), rocksdb::Slice((const char*)data, cb)); else status = m_spdb->Put(WriteOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cchKey), rocksdb::Slice((const char*)data, cb)); if (!status.ok()) throw status.ToString(); - ++m_count; + + if (!fOverwrite) + ++m_count; } bool RocksDBStorageProvider::erase(const char *key, size_t cchKey) @@ -30,13 +33,12 @@ bool RocksDBStorageProvider::erase(const char *key, size_t cchKey) } else { - std::string strT; - if (!m_spdb->Get(ReadOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cchKey), &strT).ok()) + if (!FKeyExists(key, cchKey)) return false; status = m_spdb->Delete(WriteOptions(), m_spcolfamily.get(), rocksdb::Slice(key, cchKey)); } if (status.ok()) - -- m_count; + --m_count; return status.ok(); } @@ -123,4 +125,10 @@ void RocksDBStorageProvider::endWriteBatch() void RocksDBStorageProvider::flush() { 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(); } \ No newline at end of file diff --git a/src/storage/rocksdb.h b/src/storage/rocksdb.h index 4362d89d4..8b7f6d549 100644 --- a/src/storage/rocksdb.h +++ b/src/storage/rocksdb.h @@ -33,6 +33,7 @@ public: size_t count() const; protected: + bool FKeyExists(const char *key, size_t cchKey) const; const rocksdb::ReadOptions &ReadOptions() const { return m_readOptionsTemplate; } rocksdb::WriteOptions WriteOptions() const; diff --git a/tests/unit/flash.tcl b/tests/unit/flash.tcl index 54940aa9d..b8ed35a32 100644 --- a/tests/unit/flash.tcl +++ b/tests/unit/flash.tcl @@ -7,13 +7,19 @@ start_server {tags {"flash"} overrides {"storage-provider flash ./rocks.db"}} { } test { DEL of nonexistant key returns 0 } { + r flushall 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 } { + r flushall r set testkey foo + assert_equal {1} [r dbsize] "Only one key after first insert" r flushall cache + assert_equal {1} [r dbsize] "Only one key after flushall cache" r set testkey bar + assert_equal {1} [r dbsize] "Only one key after overwrite" assert_equal {bar} [r get testkey] }