Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit e53ef55

Browse files
authored
Skip writing empty (chained) batch and make callbacks asynchronous (#619)
1 parent efcf3d3 commit e53ef55

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

binding.cc

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,9 +1589,10 @@ struct BatchWorker final : public PriorityWorker {
15891589
Database* database,
15901590
napi_value callback,
15911591
leveldb::WriteBatch* batch,
1592-
bool sync)
1592+
bool sync,
1593+
bool hasData)
15931594
: PriorityWorker(env, database, callback, "leveldown.batch.do"),
1594-
batch_(batch) {
1595+
batch_(batch), hasData_(hasData) {
15951596
options_.sync = sync;
15961597
}
15971598

@@ -1600,11 +1601,14 @@ struct BatchWorker final : public PriorityWorker {
16001601
}
16011602

16021603
void DoExecute () override {
1603-
SetStatus(database_->WriteBatch(options_, batch_));
1604+
if (hasData_) {
1605+
SetStatus(database_->WriteBatch(options_, batch_));
1606+
}
16041607
}
16051608

16061609
leveldb::WriteOptions options_;
16071610
leveldb::WriteBatch* batch_;
1611+
bool hasData_;
16081612
};
16091613

16101614
/**
@@ -1655,15 +1659,8 @@ NAPI_METHOD(batch_do) {
16551659
}
16561660
}
16571661

1658-
if (hasData) {
1659-
BatchWorker* worker = new BatchWorker(env, database, callback, batch, sync);
1660-
worker->Queue();
1661-
} else {
1662-
delete batch;
1663-
napi_value argv;
1664-
napi_get_null(env, &argv);
1665-
CallFunction(env, callback, 1, &argv);
1666-
}
1662+
BatchWorker* worker = new BatchWorker(env, database, callback, batch, sync, hasData);
1663+
worker->Queue();
16671664

16681665
NAPI_RETURN_UNDEFINED();
16691666
}
@@ -1789,7 +1786,9 @@ struct BatchWriteWorker final : public PriorityWorker {
17891786
~BatchWriteWorker () {}
17901787

17911788
void DoExecute () override {
1792-
SetStatus(batch_->Write(sync_));
1789+
if (batch_->hasData_) {
1790+
SetStatus(batch_->Write(sync_));
1791+
}
17931792
}
17941793

17951794
Batch* batch_;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"prebuild-android-arm64": "IMAGE=android-arm64 ./scripts/cross-compile"
2424
},
2525
"dependencies": {
26-
"abstract-leveldown": "~6.0.0",
26+
"abstract-leveldown": "~6.0.3",
2727
"fast-future": "~1.0.2",
2828
"napi-macros": "~1.8.1",
2929
"node-gyp-build": "~3.8.0"

0 commit comments

Comments
 (0)