Skip to content

Commit

Permalink
src: Update to leveldown v6.1.1
Browse files Browse the repository at this point in the history
Update thread-safety-patch.
  • Loading branch information
nodech committed May 1, 2023
1 parent 93172f0 commit b66d629
Show file tree
Hide file tree
Showing 2 changed files with 760 additions and 577 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
diff --git a/src/binding.cc b/src/binding.cc
index b42e570..26c8fed 100644
index c30ac76..e933861 100644
--- a/src/binding.cc
+++ b/src/binding.cc
@@ -1677,7 +1677,8 @@ struct Batch {
@@ -1874,7 +1874,8 @@ struct Batch {
Batch (Database* database)
: database_(database),
batch_(new leveldb::WriteBatch()),
Expand All @@ -12,7 +12,7 @@ index b42e570..26c8fed 100644

~Batch () {
delete batch_;
@@ -1704,9 +1705,22 @@ struct Batch {
@@ -1901,9 +1902,22 @@ struct Batch {
return database_->WriteBatch(options, batch_);
}

Expand All @@ -35,7 +35,7 @@ index b42e570..26c8fed 100644
};

/**
@@ -1741,11 +1755,15 @@ NAPI_METHOD(batch_put) {
@@ -1938,11 +1952,15 @@ NAPI_METHOD(batch_put) {
NAPI_ARGV(3);
NAPI_BATCH_CONTEXT();

Expand All @@ -56,7 +56,7 @@ index b42e570..26c8fed 100644

NAPI_RETURN_UNDEFINED();
}
@@ -1757,9 +1775,13 @@ NAPI_METHOD(batch_del) {
@@ -1954,9 +1972,13 @@ NAPI_METHOD(batch_del) {
NAPI_ARGV(2);
NAPI_BATCH_CONTEXT();

Expand All @@ -73,7 +73,7 @@ index b42e570..26c8fed 100644

NAPI_RETURN_UNDEFINED();
}
@@ -1771,7 +1793,11 @@ NAPI_METHOD(batch_clear) {
@@ -1968,7 +1990,11 @@ NAPI_METHOD(batch_clear) {
NAPI_ARGV(1);
NAPI_BATCH_CONTEXT();

Expand All @@ -86,48 +86,34 @@ index b42e570..26c8fed 100644

NAPI_RETURN_UNDEFINED();
}
@@ -1788,6 +1814,9 @@ struct BatchWriteWorker final : public PriorityWorker {
@@ -1985,6 +2011,9 @@ struct BatchWriteWorker final : public PriorityWorker {
: PriorityWorker(env, batch->database_, callback, "leveldown.batch.write"),
batch_(batch),
sync_(sync) {
+ // For thread saftey, consider BatchWrite as shared.
+ // For thread safety, consider BatchWrite as shared.
+ batch->Share();
+
// Prevent GC of batch object before we execute
NAPI_STATUS_THROWS_VOID(napi_create_reference(env_, context, 1, &contextRef_));
NAPI_STATUS_THROWS_VOID(napi_create_reference(env, context, 1, &contextRef_));
}
@@ -1802,6 +1831,11 @@ struct BatchWriteWorker final : public PriorityWorker {
}
}
@@ -1999,6 +2028,7 @@ struct BatchWriteWorker final : public PriorityWorker {

+ void DoFinally () override {
+ database_->DecrementPriorityWork();
void DoFinally (napi_env env) override {
napi_delete_reference(env, contextRef_);
+ batch_->Unshare();
+ }
+
Batch* batch_;
bool sync_;

@@ -1816,12 +1850,18 @@ NAPI_METHOD(batch_write) {
NAPI_ARGV(3);
NAPI_BATCH_CONTEXT();
PriorityWorker::DoFinally(env);
}

- napi_value options = argv[1];
- bool sync = BooleanProperty(env, options, "sync", false);
@@ -2019,6 +2049,12 @@ NAPI_METHOD(batch_write) {
const bool sync = BooleanProperty(env, options, "sync", false);
napi_value callback = argv[2];

- BatchWriteWorker* worker = new BatchWriteWorker(env, argv[0], batch, callback, sync);
- worker->Queue();
+ if (!batch->IsShared()) {
+ napi_value options = argv[1];
+ bool sync = BooleanProperty(env, options, "sync", false);
+
+ BatchWriteWorker* worker = new BatchWriteWorker(env, argv[0], batch, callback, sync);
+ worker->Queue();
+ } else {
+ if (batch->IsShared()) {
+ napi_value argv = CreateError(env, "Unsafe batch write.");
+ CallFunction(env, callback, 1, &argv);
+ NAPI_RETURN_UNDEFINED();
+ }
+
BatchWriteWorker* worker = new BatchWriteWorker(env, argv[0], batch, callback, sync);
worker->Queue(env);

NAPI_RETURN_UNDEFINED();
}
Loading

0 comments on commit b66d629

Please sign in to comment.