From 24a241addc559ddd4669c6e8b6b755a3d94d2152 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/IfcFile.cpp | 59 ++++++++++++++++------------------------ src/ifcparse/storage.h | 3 +- 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/ifcparse/IfcFile.cpp b/src/ifcparse/IfcFile.cpp index 06521d5565..a1cd77e437 100644 --- a/src/ifcparse/IfcFile.cpp +++ b/src/ifcparse/IfcFile.cpp @@ -4,6 +4,7 @@ #ifdef IFOPSH_WITH_ROCKSDB #include #include +#include #endif #include @@ -412,30 +413,8 @@ IfcUtil::IfcBaseClass* IfcParse::impl::rocks_db_file_storage::assert_existance(s } namespace { + std::unique_ptr init_db(const std::string& filepath, bool readonly) { #ifdef IFOPSH_WITH_ROCKSDB - // Newer RocksDB releases (e.g. the one shipped by Fedora rawhide) changed - // DB::Open / DB::OpenForReadOnly to take a std::unique_ptr* and removed - // the raw DB** overloads. These helpers select whichever signature the - // installed RocksDB exposes, so the code builds against both old and new - // headers. The int/long tag prefers the unique_ptr form when both exist. - 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; @@ -467,20 +446,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 } } @@ -489,12 +479,12 @@ IfcParse::impl::rocks_db_file_storage::rocks_db_file_storage(const std::string& : 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); }, [](IfcUtil::IfcBaseClass* 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); }, [](IfcUtil::IfcBaseClass* v) { return v->identity(); }) { @@ -517,7 +507,6 @@ IfcParse::impl::rocks_db_file_storage::~rocks_db_file_storage() assert(s.ok()); db->Close(); - delete db; #endif } diff --git a/src/ifcparse/storage.h b/src/ifcparse/storage.h index d0ae6f3cf2..39e5139040 100644 --- a/src/ifcparse/storage.h +++ b/src/ifcparse/storage.h @@ -31,6 +31,7 @@ namespace rocksdb { #include #include #include +#include #ifndef SWIG @@ -308,7 +309,7 @@ namespace IfcParse { class IFC_PARSE_API rocks_db_file_storage { public: - rocksdb::DB* db; + std::unique_ptr db; rocksdb::WriteOptions wopts; rocksdb::ReadOptions ropts; IfcParse::IfcFile* file;