mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 17:58:20 +00:00
Use version preprocessor guards for RocksDB unique_ptr API, retain unique_ptr internally
This commit is contained in:
committed by
Thomas Krijnen
parent
818ca6b2bc
commit
3f9b25d4e0
+24
-34
@@ -4,6 +4,7 @@
|
||||
#ifdef IFOPSH_WITH_ROCKSDB
|
||||
#include <rocksdb/table.h>
|
||||
#include <rocksdb/convenience.h>
|
||||
#include <rocksdb/version.h>
|
||||
#endif
|
||||
|
||||
#include <fstream>
|
||||
@@ -79,29 +80,8 @@ express::Base ifcopenshell::impl::rocks_db_file_storage::assert_existance(size_t
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::unique_ptr<rocksdb::DB> 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<DB>*. Select whichever signature the installed headers expose.
|
||||
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;
|
||||
@@ -133,20 +113,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -514,7 +514,7 @@ namespace ifcopenshell {
|
||||
|
||||
class IFC_PARSE_API rocks_db_file_storage {
|
||||
public:
|
||||
rocksdb::DB* db;
|
||||
std::unique_ptr<rocksdb::DB> db;
|
||||
rocksdb::WriteOptions wopts;
|
||||
rocksdb::ReadOptions ropts;
|
||||
ifcopenshell::file* file;
|
||||
|
||||
Reference in New Issue
Block a user