mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-31 17:06:29 +00:00
Use version preprocessor guards for RocksDB unique_ptr API, retain unique_ptr internally
This commit is contained in:
+24
-35
@@ -4,6 +4,7 @@
|
|||||||
#ifdef IFOPSH_WITH_ROCKSDB
|
#ifdef IFOPSH_WITH_ROCKSDB
|
||||||
#include <rocksdb/table.h>
|
#include <rocksdb/table.h>
|
||||||
#include <rocksdb/convenience.h>
|
#include <rocksdb/convenience.h>
|
||||||
|
#include <rocksdb/version.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -412,30 +413,8 @@ IfcUtil::IfcBaseClass* IfcParse::impl::rocks_db_file_storage::assert_existance(s
|
|||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
std::unique_ptr<rocksdb::DB> init_db(const std::string& filepath, bool readonly) {
|
||||||
#ifdef IFOPSH_WITH_ROCKSDB
|
#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;
|
rocksdb::Options options;
|
||||||
// options.disable_auto_compactions = true;
|
// options.disable_auto_compactions = true;
|
||||||
options.create_if_missing = true;
|
options.create_if_missing = true;
|
||||||
@@ -467,20 +446,31 @@ namespace {
|
|||||||
options.table_factory.reset(rocksdb::NewBlockBasedTableFactory(tbo));
|
options.table_factory.reset(rocksdb::NewBlockBasedTableFactory(tbo));
|
||||||
|
|
||||||
rocksdb::Status status;
|
rocksdb::Status status;
|
||||||
|
std::unique_ptr<rocksdb::DB> db;
|
||||||
if (readonly) {
|
if (readonly) {
|
||||||
status = rocksdb_open([&](auto* dbptr) -> decltype(rocksdb::DB::OpenForReadOnly(options, filepath, dbptr)) {
|
#if ROCKSDB_MAJOR > 9 || (ROCKSDB_MAJOR == 9 && ROCKSDB_MINOR >= 11)
|
||||||
return rocksdb::DB::OpenForReadOnly(options, filepath, dbptr);
|
status = rocksdb::DB::OpenForReadOnly(options, filepath, &db);
|
||||||
}, db, 0);
|
#else
|
||||||
|
rocksdb::DB* raw = nullptr;
|
||||||
|
status = rocksdb::DB::OpenForReadOnly(options, filepath, &raw);
|
||||||
|
db.reset(raw);
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
status = rocksdb_open([&](auto* dbptr) -> decltype(rocksdb::DB::Open(options, filepath, dbptr)) {
|
#if ROCKSDB_MAJOR > 9 || (ROCKSDB_MAJOR == 9 && ROCKSDB_MINOR >= 11)
|
||||||
return rocksdb::DB::Open(options, filepath, dbptr);
|
status = rocksdb::DB::Open(options, filepath, &db);
|
||||||
}, db, 0);
|
#else
|
||||||
|
rocksdb::DB* raw = nullptr;
|
||||||
|
status = rocksdb::DB::Open(options, filepath, &raw);
|
||||||
|
db.reset(raw);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if (!status.ok()) {
|
if (!status.ok()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
#endif // IFOPSH_WITH_ROCKSDB#
|
|
||||||
return db;
|
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)
|
: file(ffile)
|
||||||
, db(init_db(filepath, readonly))
|
, db(init_db(filepath, readonly))
|
||||||
// @todo streaming serializer does not populate the byguid map
|
// @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(); })
|
, 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); })
|
, instance_by_name_(&instance_ids_, [this](size_t v) { return assert_existance(v, entityinstance_ref); })
|
||||||
, bytype_(db, "t|")
|
, bytype_(db.get(), "t|")
|
||||||
, byref_excl_(db, "v|")
|
, 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?
|
// @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(); })
|
// , 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());
|
assert(s.ok());
|
||||||
|
|
||||||
db->Close();
|
db->Close();
|
||||||
delete db;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ namespace rocksdb {
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#ifndef SWIG
|
#ifndef SWIG
|
||||||
|
|
||||||
@@ -306,7 +307,7 @@ namespace IfcParse {
|
|||||||
|
|
||||||
class IFC_PARSE_API rocks_db_file_storage {
|
class IFC_PARSE_API rocks_db_file_storage {
|
||||||
public:
|
public:
|
||||||
rocksdb::DB* db;
|
std::unique_ptr<rocksdb::DB> db;
|
||||||
rocksdb::WriteOptions wopts;
|
rocksdb::WriteOptions wopts;
|
||||||
rocksdb::ReadOptions ropts;
|
rocksdb::ReadOptions ropts;
|
||||||
IfcParse::IfcFile* file;
|
IfcParse::IfcFile* file;
|
||||||
|
|||||||
Reference in New Issue
Block a user