Skip to content

Commit

Permalink
Skip writing empty (chained) batch and make callbacks asynchronous (#619
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vweevers committed Apr 26, 2019
1 parent efcf3d3 commit e53ef55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1589,9 +1589,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 @@ -1600,11 +1601,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 @@ -1655,15 +1659,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 @@ -1789,7 +1786,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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"prebuild-android-arm64": "IMAGE=android-arm64 ./scripts/cross-compile"
},
"dependencies": {
"abstract-leveldown": "~6.0.0",
"abstract-leveldown": "~6.0.3",
"fast-future": "~1.0.2",
"napi-macros": "~1.8.1",
"node-gyp-build": "~3.8.0"
Expand Down

0 comments on commit e53ef55

Please sign in to comment.