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

Commit 10f863e

Browse files
committed
Skip writing empty (chained) batch and make callbacks asynchronous
1 parent 31b66cf commit 10f863e

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

binding.cc

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,9 +1583,10 @@ struct BatchWorker final : public PriorityWorker {
15831583
Database* database,
15841584
napi_value callback,
15851585
leveldb::WriteBatch* batch,
1586-
bool sync)
1586+
bool sync,
1587+
bool hasData)
15871588
: PriorityWorker(env, database, callback, "leveldown.batch.do"),
1588-
batch_(batch) {
1589+
batch_(batch), hasData_(hasData) {
15891590
options_.sync = sync;
15901591
}
15911592

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

15961597
void DoExecute () override {
1597-
SetStatus(database_->WriteBatch(options_, batch_));
1598+
if (hasData_) {
1599+
SetStatus(database_->WriteBatch(options_, batch_));
1600+
}
15981601
}
15991602

16001603
leveldb::WriteOptions options_;
16011604
leveldb::WriteBatch* batch_;
1605+
bool hasData_;
16021606
};
16031607

16041608
/**
@@ -1649,15 +1653,8 @@ NAPI_METHOD(batch_do) {
16491653
}
16501654
}
16511655

1652-
if (hasData) {
1653-
BatchWorker* worker = new BatchWorker(env, database, callback, batch, sync);
1654-
worker->Queue();
1655-
} else {
1656-
delete batch;
1657-
napi_value argv;
1658-
napi_get_null(env, &argv);
1659-
CallFunction(env, callback, 1, &argv);
1660-
}
1656+
BatchWorker* worker = new BatchWorker(env, database, callback, batch, sync, hasData);
1657+
worker->Queue();
16611658

16621659
NAPI_RETURN_UNDEFINED();
16631660
}
@@ -1783,7 +1780,9 @@ struct BatchWriteWorker final : public PriorityWorker {
17831780
~BatchWriteWorker () {}
17841781

17851782
void DoExecute () override {
1786-
SetStatus(batch_->Write(sync_));
1783+
if (batch_->hasData_) {
1784+
SetStatus(batch_->Write(sync_));
1785+
}
17871786
}
17881787

17891788
Batch* batch_;

0 commit comments

Comments
 (0)