Use version preprocessor guards for RocksDB unique_ptr API, retain unique_ptr internally

This commit is contained in:
Bruno Postle
2026-06-05 08:12:55 +01:00
committed by Thomas Krijnen
parent 365be8fb52
commit 24a241addc
2 changed files with 26 additions and 36 deletions
+24 -35
View File
@@ -4,6 +4,7 @@
#ifdef IFOPSH_WITH_ROCKSDB
#include <rocksdb/table.h>
#include <rocksdb/convenience.h>
#include <rocksdb/version.h>
#endif
#include <fstream>
@@ -412,30 +413,8 @@ IfcUtil::IfcBaseClass* IfcParse::impl::rocks_db_file_storage::assert_existance(s
}
namespace {
std::unique_ptr<rocksdb::DB> 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<DB>* 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 <typename Fn>
auto rocksdb_open(Fn&& open, rocksdb::DB*& db, int)
-> decltype(open(std::declval<std::unique_ptr<rocksdb::DB>*>())) {
std::unique_ptr<rocksdb::DB> owned;
auto status = open(&owned);
db = owned.release();
return status;
}
template <typename Fn>
auto rocksdb_open(Fn&& open, rocksdb::DB*& db, long)
-> decltype(open(std::declval<rocksdb::DB**>())) {
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<rocksdb::DB> 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
}
+2 -1
View File
@@ -31,6 +31,7 @@ namespace rocksdb {
#include <iostream>
#include <vector>
#include <list>
#include <memory>
#ifndef SWIG
@@ -308,7 +309,7 @@ namespace IfcParse {
class IFC_PARSE_API rocks_db_file_storage {
public:
rocksdb::DB* db;
std::unique_ptr<rocksdb::DB> db;
rocksdb::WriteOptions wopts;
rocksdb::ReadOptions ropts;
IfcParse::IfcFile* file;