From 3f9b25d4e0b65656eb95ff7c34465688f5d4a664 Mon Sep 17 00:00:00 2001 From: Bruno Postle Date: Fri, 5 Jun 2026 08:12:55 +0100 Subject: [PATCH] Use version preprocessor guards for RocksDB unique_ptr API, retain unique_ptr internally --- src/ifcparse/file.cpp | 58 +++++++++++++++++------------------------- src/ifcparse/storage.h | 2 +- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/ifcparse/file.cpp b/src/ifcparse/file.cpp index 567c726e0b..652be45adc 100644 --- a/src/ifcparse/file.cpp +++ b/src/ifcparse/file.cpp @@ -4,6 +4,7 @@ #ifdef IFOPSH_WITH_ROCKSDB #include #include +#include #endif #include @@ -79,29 +80,8 @@ express::Base ifcopenshell::impl::rocks_db_file_storage::assert_existance(size_t } namespace { + std::unique_ptr init_db(const std::string& filepath, bool readonly) { #ifdef IFOPSH_WITH_ROCKSDB - // Newer RocksDB releases changed DB::Open / DB::OpenForReadOnly to take - // std::unique_ptr*. Select whichever signature the installed headers expose. - template - auto rocksdb_open(Fn&& open, rocksdb::DB*& db, int) - -> decltype(open(std::declval*>())) { - std::unique_ptr owned; - auto status = open(&owned); - db = owned.release(); - return status; - } - - template - auto rocksdb_open(Fn&& open, rocksdb::DB*& db, long) - -> decltype(open(std::declval())) { - return open(&db); - } -#endif - - rocksdb::DB* init_db(const std::string& filepath, bool readonly) { - rocksdb::DB* db = nullptr; -#ifdef IFOPSH_WITH_ROCKSDB - rocksdb::Options options; // options.disable_auto_compactions = true; options.create_if_missing = true; @@ -133,20 +113,31 @@ namespace { options.table_factory.reset(rocksdb::NewBlockBasedTableFactory(tbo)); rocksdb::Status status; + std::unique_ptr db; if (readonly) { - status = rocksdb_open([&](auto* dbptr) -> decltype(rocksdb::DB::OpenForReadOnly(options, filepath, dbptr)) { - return rocksdb::DB::OpenForReadOnly(options, filepath, dbptr); - }, db, 0); +#if ROCKSDB_MAJOR > 9 || (ROCKSDB_MAJOR == 9 && ROCKSDB_MINOR >= 11) + status = rocksdb::DB::OpenForReadOnly(options, filepath, &db); +#else + rocksdb::DB* raw = nullptr; + status = rocksdb::DB::OpenForReadOnly(options, filepath, &raw); + db.reset(raw); +#endif } else { - status = rocksdb_open([&](auto* dbptr) -> decltype(rocksdb::DB::Open(options, filepath, dbptr)) { - return rocksdb::DB::Open(options, filepath, dbptr); - }, db, 0); +#if ROCKSDB_MAJOR > 9 || (ROCKSDB_MAJOR == 9 && ROCKSDB_MINOR >= 11) + status = rocksdb::DB::Open(options, filepath, &db); +#else + rocksdb::DB* raw = nullptr; + status = rocksdb::DB::Open(options, filepath, &raw); + db.reset(raw); +#endif } if (!status.ok()) { return nullptr; } -#endif // IFOPSH_WITH_ROCKSDB# return db; +#else + return nullptr; +#endif } } @@ -155,12 +146,12 @@ ifcopenshell::impl::rocks_db_file_storage::rocks_db_file_storage(const std::stri : file(ffile) , db(init_db(filepath, readonly)) // @todo streaming serializer does not populate the byguid map - , byguid_internal_(db, "g|"), + , byguid_internal_(db.get(), "g|"), byguid_(&byguid_internal_, [this](size_t v) { return assert_existance(v, entityinstance_ref); }, [](const express::Base& v) { return v.identity(); }) - , instance_ids_(db, "i|") + , instance_ids_(db.get(), "i|") , instance_by_name_(&instance_ids_, [this](size_t v) { return assert_existance(v, entityinstance_ref); }) - , bytype_(db, "t|") - , byref_excl_(db, "v|") + , bytype_(db.get(), "t|") + , byref_excl_(db.get(), "v|") // @todo by_identity is probably not correct here, this mapping is Name -> Identity, so Fn should have access to full pair? // , byidentity_(&byid_, [this](size_t v) { return assert_existance(v, by_identity); }, [](ifcopenshell::IfcBaseClass* v) { return v->identity(); }) { @@ -187,7 +178,6 @@ ifcopenshell::impl::rocks_db_file_storage::~rocks_db_file_storage() } db->Close(); - delete db; } #endif } diff --git a/src/ifcparse/storage.h b/src/ifcparse/storage.h index ed9d93f827..68d01406c2 100644 --- a/src/ifcparse/storage.h +++ b/src/ifcparse/storage.h @@ -514,7 +514,7 @@ namespace ifcopenshell { class IFC_PARSE_API rocks_db_file_storage { public: - rocksdb::DB* db; + std::unique_ptr db; rocksdb::WriteOptions wopts; rocksdb::ReadOptions ropts; ifcopenshell::file* file;