Skip to content

Commit

Permalink
perf: faster db_clear with DeleteRange
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed May 10, 2022
1 parent f3548ae commit ce739f0
Showing 1 changed file with 54 additions and 28 deletions.
82 changes: 54 additions & 28 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,46 +911,72 @@ NAPI_METHOD(db_clear) {
const auto lte = StringProperty(env, argv[1], "lte");
const auto gt = StringProperty(env, argv[1], "gt");
const auto gte = StringProperty(env, argv[1], "gte");

if (!reverse && limit == -1 && (lte || lt)) {
rocksdb::Slice begin;
if (gte) {
begin = *gte;
} else if (gt) {
begin = *gt + '\0';
}

// TODO (perf): Use DeleteRange.
rocksdb::Slice end;
if (lte) {
end = *lte + '\0';
} else if (lt) {
end = *lt;
} else {
assert(false);
}

BaseIterator it(database, reverse, lt, lte, gt, gte, limit, false);
if (begin.compare(end) > 0) {
return ToError(env, rocksdb::Status::OK());
}

it.SeekToRange();
rocksdb::WriteOptions options;
const auto status = database->db_->DeleteRange(options, database->db_->DefaultColumnFamily(), begin, end);
return ToError(env, status);
} else {
// TODO (perf): Use DeleteRange.

// TODO: add option
const uint32_t hwm = 16 * 1024;
BaseIterator it(database, reverse, lt, lte, gt, gte, limit, false);

rocksdb::WriteBatch batch;
rocksdb::WriteOptions options;
rocksdb::Status status;
it.SeekToRange();

while (true) {
size_t bytesRead = 0;
// TODO: add option
const uint32_t hwm = 16 * 1024;

while (bytesRead <= hwm && it.Valid() && it.Increment()) {
const auto key = it.CurrentKey();
batch.Delete(key);
bytesRead += key.size();
it.Next();
}
rocksdb::WriteBatch batch;
rocksdb::WriteOptions options;
rocksdb::Status status;

status = it.Status();
if (!status.ok() || bytesRead == 0) {
break;
}
while (true) {
size_t bytesRead = 0;

status = database->db_->Write(options, &batch);
if (!status.ok()) {
break;
}
while (bytesRead <= hwm && it.Valid() && it.Increment()) {
const auto key = it.CurrentKey();
batch.Delete(key);
bytesRead += key.size();
it.Next();
}

batch.Clear();
}
status = it.Status();
if (!status.ok() || bytesRead == 0) {
break;
}

it.Close();
status = database->db_->Write(options, &batch);
if (!status.ok()) {
break;
}

batch.Clear();
}

it.Close();

return ToError(env, status);
return ToError(env, status);
}
}

NAPI_METHOD(db_get_property) {
Expand Down

0 comments on commit ce739f0

Please sign in to comment.