Skip to content

Commit cca1f90

Browse files
committed
Comments, cleanup
1 parent 071264d commit cca1f90

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

src/binding/database.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@ napi_value Database::Open(napi_env env, napi_callback_info info) {
936936

937937
try {
938938
(*dbHandle)->open(path, dbHandleOptions);
939+
940+
// now that the database is open and the dbHandle has a reference to
941+
// the descriptor, we can attach the database instance's smart_ptr to
942+
// the descriptor so it gets cleaned up when the descriptor is closed
939943
(*dbHandle)->descriptor->attach(*dbHandle);
940944
} catch (const std::exception& e) {
941945
DEBUG_LOG("%p Database::Open Error: %s\n", dbHandle->get(), e.what());

src/binding/db_descriptor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ void DBDescriptor::close() {
194194

195195
/**
196196
* Registers a database resource to be closed when the descriptor is closed.
197+
*
198+
* Important: The closable must be same smart_ptr that is napi-wrapped and
199+
* bound to the JavaScript class counterpart.
197200
*/
198201
void DBDescriptor::attach(std::shared_ptr<Closable> closable) {
199202
std::lock_guard<std::mutex> lock(this->txnsMutex);

src/binding/db_handle.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ void DBHandle::open(const std::string& path, const DBOptions& options) {
103103
this->disableWAL = options.disableWAL;
104104
this->path = path;
105105

106+
// Note: We cannot attach this handle to the descriptor because we don't
107+
// have the smart pointer to the dbHandle instance, so the caller needs to
108+
// do it.
109+
106110
// at this point, the DBDescriptor has at least 2 refs: the registry and this handle
107111
}
108112

src/binding/db_registry.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ void DBRegistry::CloseDB(const std::shared_ptr<DBHandle> handle) {
2222
return;
2323
}
2424

25+
#ifdef DEBUG
2526
DBRegistry::DebugLogDescriptorRefs();
27+
#endif
2628

2729
if (!handle->descriptor) {
2830
DEBUG_LOG("%p DBRegistry::CloseDB Database not opened\n", instance.get());
@@ -73,15 +75,15 @@ void DBRegistry::CloseDB(const std::shared_ptr<DBHandle> handle) {
7375
/**
7476
* Debug log the reference count of all descriptors in the registry.
7577
*/
76-
void DBRegistry::DebugLogDescriptorRefs() {
7778
#ifdef DEBUG
79+
void DBRegistry::DebugLogDescriptorRefs() {
7880
std::lock_guard<std::mutex> lock(instance->databasesMutex);
7981
DEBUG_LOG("DBRegistry::DebugLogDescriptorRefs %d descriptor%s in registry:\n", instance->databases.size(), instance->databases.size() == 1 ? "" : "s");
8082
for (auto& [path, entry] : instance->databases) {
8183
DEBUG_LOG(" %p for \"%s\" (ref count = %ld)\n", entry.descriptor.get(), path.c_str(), entry.descriptor.use_count());
8284
}
83-
#endif
8485
}
86+
#endif
8587

8688
/**
8789
* Destroy a RocksDB database.

src/binding/db_registry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ class DBRegistry final {
6464

6565
public:
6666
static void CloseDB(const std::shared_ptr<DBHandle> handle);
67+
#ifdef DEBUG
6768
static void DebugLogDescriptorRefs();
69+
#endif
6870
static void DestroyDB(const std::string& path);
6971
static void Init(napi_env env, napi_value exports);
7072
static std::unique_ptr<DBHandleParams> OpenDB(const std::string& path, const DBOptions& options);

0 commit comments

Comments
 (0)