Here is an example shows that:
Get() returns NotFound even if Put() successfully with write_options.sync=true.
This is an interesting case.
Pay attention that I release disk space(not the data in the db) when io error(No space left on device) occurs, then the error disappears.
The test code is as follow:
#include <string>
#include <unistd.h>
#include "leveldb/db.h"
int main() {
const char* dbname = "./trash/ldb_data";
leveldb::DB* db = NULL;
leveldb::Options opts;
opts.create_if_missing = true;
leveldb::WriteOptions wopts;
leveldb::Status s;
long long count = 0;
s = leveldb::DB::Open(opts, dbname, &db);
assert(db != NULL);
printf("Put until io error(No space left on device) occurs.\n");
while (true) {
std::string key = std::to_string(count++);
s = db->Put(wopts, key, "hello world");
if (!s.ok()) {
printf("Put status %s.\n", s.ToString().c_str());
break;
}
}
printf("Release disk space(not the data in the db).\n");
sleep(20);
{
wopts.sync = true;
std::string key = std::to_string(count);
s = db->Put(wopts, key, std::string(32 * 1024, 'x'));
printf("Put key %s status %s.\n", key.c_str(), s.ToString().c_str());
assert(s.ok());
}
printf("Re-open the leveldb.\n");
{
delete db;
db = NULL;
s = leveldb::DB::Open(opts, dbname, &db);
assert(db != NULL);
}
printf("Read the key put successfully with wopts.sync=true.\n");
{
std::string key = std::to_string(count);
std::string value;
s = db->Get(leveldb::ReadOptions(), key, &value);
printf("Get key %s status %s.\n", key.c_str(), s.ToString().c_str());
assert(!s.IsNotFound()); // assert happen !!!
}
return 0;
}
Run the code and the output:
|