Skip to content

Commit

Permalink
Skip writing empty (chained) batch and make callbacks asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Apr 26, 2019
1 parent 31b66cf commit 10f863e
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1583,9 +1583,10 @@ struct BatchWorker final : public PriorityWorker {
Database* database,
napi_value callback,
leveldb::WriteBatch* batch,
bool sync)
bool sync,
bool hasData)
: PriorityWorker(env, database, callback, "leveldown.batch.do"),
batch_(batch) {
batch_(batch), hasData_(hasData) {
options_.sync = sync;
}

Expand All @@ -1594,11 +1595,14 @@ struct BatchWorker final : public PriorityWorker {
}

void DoExecute () override {
SetStatus(database_->WriteBatch(options_, batch_));
if (hasData_) {
SetStatus(database_->WriteBatch(options_, batch_));
}
}

leveldb::WriteOptions options_;
leveldb::WriteBatch* batch_;
bool hasData_;
};

/**
Expand Down Expand Up @@ -1649,15 +1653,8 @@ NAPI_METHOD(batch_do) {
}
}

if (hasData) {
BatchWorker* worker = new BatchWorker(env, database, callback, batch, sync);
worker->Queue();
} else {
delete batch;
napi_value argv;
napi_get_null(env, &argv);
CallFunction(env, callback, 1, &argv);
}
BatchWorker* worker = new BatchWorker(env, database, callback, batch, sync, hasData);
worker->Queue();

NAPI_RETURN_UNDEFINED();
}
Expand Down Expand Up @@ -1783,7 +1780,9 @@ struct BatchWriteWorker final : public PriorityWorker {
~BatchWriteWorker () {}

void DoExecute () override {
SetStatus(batch_->Write(sync_));
if (batch_->hasData_) {
SetStatus(batch_->Write(sync_));
}
}

Batch* batch_;
Expand Down

0 comments on commit 10f863e

Please sign in to comment.