Compare commits

...

2 Commits

Author SHA1 Message Date
Bruno Postle 99cfa2bd56 Use version preprocessor guards for RocksDB unique_ptr API, retain unique_ptr internally 2026-06-05 09:26:11 +01:00
Bruno Postle 8402dfd304 Support RocksDB shared library and new unique_ptr DB::Open API
Some distributions (e.g. Fedora) ship only a shared RocksDB that exports
RocksDB::rocksdb-shared rather than RocksDB::rocksdb. The CMake target
selection now falls back to the shared target when the static one is absent.

Newer RocksDB also changed DB::Open and DB::OpenForReadOnly to take
std::unique_ptr<DB>* instead of DB**. IfcFile.cpp uses SFINAE tag dispatch
to build against both old and new APIs without version detection.
2026-06-04 22:52:41 +01:00
3 changed files with 33 additions and 13 deletions
+7 -3
View File
@@ -258,10 +258,14 @@ if(WITH_ROCKSDB)
set(ROCKSDB_LIBRARIES "IFCOPENSHELL_RocksDB")
target_compile_definitions(IFCOPENSHELL_RocksDB INTERFACE IFOPSH_WITH_ROCKSDB)
set(SWIG_DEFINES ${SWIG_DEFINES} -DIFOPSH_WITH_ROCKSDB)
# Shared binaries for `rocksdb` only support limited API (only `c.h`), but we use `db.h` API.
# So rocksdb supported only as a static library.
# See https://github.com/facebook/rocksdb/issues/981.
target_link_libraries(IFCOPENSHELL_RocksDB INTERFACE RocksDB::rocksdb)
if(TARGET RocksDB::rocksdb)
target_link_libraries(IFCOPENSHELL_RocksDB INTERFACE RocksDB::rocksdb)
elseif(TARGET RocksDB::rocksdb-shared)
target_link_libraries(IFCOPENSHELL_RocksDB INTERFACE RocksDB::rocksdb-shared)
else()
message(FATAL_ERROR "RocksDB found but neither RocksDB::rocksdb nor RocksDB::rocksdb-shared target exists")
endif()
if(WITH_ZSTD)
# @todo do we actually need the zstd include dir or rather just pass
+24 -9
View File
@@ -4,9 +4,12 @@
#ifdef IFOPSH_WITH_ROCKSDB
#include <rocksdb/table.h>
#include <rocksdb/convenience.h>
#include <rocksdb/version.h>
#endif
#include <fstream>
#include <memory>
#include <utility>
#include <sys/types.h>
#include <sys/stat.h>
@@ -410,10 +413,8 @@ IfcUtil::IfcBaseClass* IfcParse::impl::rocks_db_file_storage::assert_existance(s
}
namespace {
rocksdb::DB* init_db(const std::string& filepath, bool readonly) {
rocksdb::DB* db = nullptr;
std::unique_ptr<rocksdb::DB> init_db(const std::string& filepath, bool readonly) {
#ifdef IFOPSH_WITH_ROCKSDB
rocksdb::Options options;
// options.disable_auto_compactions = true;
options.create_if_missing = true;
@@ -445,16 +446,31 @@ namespace {
options.table_factory.reset(rocksdb::NewBlockBasedTableFactory(tbo));
rocksdb::Status status;
std::unique_ptr<rocksdb::DB> db;
if (readonly) {
#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 {
#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
}
}
@@ -463,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(); })
{
@@ -491,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
@@ -29,6 +29,7 @@ namespace rocksdb {
#include <iostream>
#include <vector>
#include <list>
#include <memory>
#ifndef SWIG
@@ -306,7 +307,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;