2025-08-25 12:45:10 +02:00
|
|
|
#ifndef STORAGE_H
|
|
|
|
|
#define STORAGE_H
|
|
|
|
|
|
2026-01-07 13:51:48 +01:00
|
|
|
// Avoid conflicts with OpenCascade HANDLE type and RocksDB Handle
|
|
|
|
|
#pragma push_macro("Handle")
|
|
|
|
|
#undef Handle
|
|
|
|
|
|
2025-09-03 11:11:55 +02:00
|
|
|
#ifndef IFOPSH_WITH_ROCKSDB
|
2025-08-26 13:56:03 +02:00
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
class DB {};
|
|
|
|
|
class Options {};
|
|
|
|
|
class WriteOptions {};
|
|
|
|
|
class ReadOptions {};
|
|
|
|
|
class Iterator {};
|
2026-03-31 15:32:36 +02:00
|
|
|
class status {};
|
2025-08-26 13:56:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-08-25 12:45:10 +02:00
|
|
|
#include "rocksdb_map_adapter.h"
|
|
|
|
|
#include "rocksdb_set_view.h"
|
|
|
|
|
#include "map_variant.h"
|
|
|
|
|
#include "map_transformer.h"
|
|
|
|
|
#include "set_to_map_transformer.h"
|
|
|
|
|
#include "file_open_status.h"
|
2026-07-09 13:30:48 +02:00
|
|
|
#include "logger.h"
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-06-11 21:04:44 +02:00
|
|
|
#include <functional>
|
2025-08-25 12:45:10 +02:00
|
|
|
#include <variant>
|
2026-06-11 15:51:40 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstdint>
|
2025-08-25 12:45:10 +02:00
|
|
|
#include <iterator>
|
2026-09-10 07:31:45 +10:00
|
|
|
#include <limits>
|
2026-06-11 15:51:40 +02:00
|
|
|
#include <map>
|
|
|
|
|
#include <memory>
|
2026-06-04 22:23:19 +01:00
|
|
|
#include <cstring>
|
2025-08-25 12:45:10 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
#include <iostream>
|
2026-04-11 16:27:06 +10:00
|
|
|
#include <deque>
|
2025-08-25 12:45:10 +02:00
|
|
|
#include <vector>
|
|
|
|
|
#include <list>
|
2026-05-08 10:07:34 +02:00
|
|
|
#include <mutex>
|
2026-01-04 10:40:02 +01:00
|
|
|
#include <set>
|
2026-06-11 15:51:40 +02:00
|
|
|
#include <unordered_map>
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
#ifndef SWIG
|
|
|
|
|
|
|
|
|
|
template <typename... Iterators>
|
|
|
|
|
class variant_iterator {
|
|
|
|
|
public:
|
|
|
|
|
// The variant type holding one of the underlying iterators.
|
|
|
|
|
using variant_type = std::variant<Iterators...>;
|
|
|
|
|
|
|
|
|
|
// Assuming that all iterator types have the same value_type, difference_type, etc.
|
|
|
|
|
using value_type = std::common_type_t<typename std::iterator_traits<Iterators>::value_type...>;
|
|
|
|
|
using difference_type = std::common_type_t<typename std::iterator_traits<Iterators>::difference_type...>;
|
|
|
|
|
using pointer = value_type*;
|
|
|
|
|
using reference = value_type&;
|
|
|
|
|
// For simplicity, we use input_iterator_tag; if all underlying iterators support more,
|
|
|
|
|
// you could compute the common iterator_category.
|
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
|
|
|
|
|
|
// Default constructor.
|
|
|
|
|
variant_iterator() = default;
|
|
|
|
|
|
|
|
|
|
// Construct from any one of the underlying iterator types.
|
|
|
|
|
template <typename Iterator>
|
2026-03-31 18:23:13 +02:00
|
|
|
variant_iterator(Iterator iterator) : it_(iterator) {}
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
// Dereference operator.
|
|
|
|
|
decltype(auto) operator*() const {
|
|
|
|
|
return std::visit([](const auto& iter) -> decltype(auto) {
|
|
|
|
|
return *iter;
|
|
|
|
|
}, it_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Arrow operator.
|
|
|
|
|
decltype(auto) operator->() const {
|
|
|
|
|
return std::visit([](const auto& iter) -> decltype(auto) {
|
|
|
|
|
return iter.operator->();
|
|
|
|
|
}, it_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pre-increment operator.
|
|
|
|
|
variant_iterator& operator++() {
|
|
|
|
|
std::visit([](auto& iter) { ++iter; }, it_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Post-increment operator.
|
|
|
|
|
variant_iterator operator++(int) {
|
|
|
|
|
variant_iterator temp(*this);
|
|
|
|
|
++(*this);
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pre-decrement operator.
|
|
|
|
|
variant_iterator& operator--() {
|
|
|
|
|
std::visit([](auto& iter) { --iter; }, it_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Post-decrement operator.
|
|
|
|
|
variant_iterator operator--(int) {
|
|
|
|
|
variant_iterator temp(*this);
|
|
|
|
|
--(*this);
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Equality comparison.
|
|
|
|
|
friend bool operator==(const variant_iterator& lhs, const variant_iterator& rhs) {
|
|
|
|
|
return lhs.it_ == rhs.it_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inequality comparison.
|
|
|
|
|
friend bool operator!=(const variant_iterator& lhs, const variant_iterator& rhs) {
|
|
|
|
|
return !(lhs == rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
variant_type it_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
namespace ifcopenshell {
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 13:58:39 +02:00
|
|
|
class mutable_attribute_value;
|
|
|
|
|
|
2026-04-21 21:48:54 +02:00
|
|
|
struct IFC_PARSE_API instance_reference {
|
2025-08-25 12:45:10 +02:00
|
|
|
int v;
|
|
|
|
|
size_t file_offset;
|
|
|
|
|
operator int() const {
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
typedef std::variant<instance_reference, express::base> reference_or_simple_type;
|
2026-06-11 15:51:40 +02:00
|
|
|
typedef std::vector<std::pair<mutable_attribute_value, std::variant<reference_or_simple_type, std::vector<reference_or_simple_type>, std::vector<std::vector<reference_or_simple_type>>>>> unresolved_references;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
class file;
|
2026-03-27 20:45:13 +01:00
|
|
|
template <typename Reader>
|
2026-03-31 15:32:36 +02:00
|
|
|
class spf_lexer;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-04-21 21:48:54 +02:00
|
|
|
struct IFC_PARSE_API token {
|
2026-03-31 15:32:36 +02:00
|
|
|
enum token_type {
|
2026-03-26 15:49:28 +01:00
|
|
|
Token_NONE,
|
|
|
|
|
Token_STRING,
|
|
|
|
|
Token_IDENTIFIER,
|
|
|
|
|
Token_OPERATOR,
|
|
|
|
|
Token_ENUMERATION,
|
|
|
|
|
Token_KEYWORD,
|
|
|
|
|
Token_INT,
|
|
|
|
|
Token_BOOL,
|
|
|
|
|
Token_FLOAT,
|
|
|
|
|
Token_BINARY
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
size_t start_pos;
|
2026-03-31 15:32:36 +02:00
|
|
|
token_type type;
|
2026-03-26 15:49:28 +01:00
|
|
|
|
2025-08-25 12:45:10 +02:00
|
|
|
union {
|
|
|
|
|
char value_char; //types: OPERATOR
|
2026-07-19 11:44:31 +03:00
|
|
|
int64_t value_int; //types: INT, IDENTIFIER
|
2025-08-25 12:45:10 +02:00
|
|
|
double value_double; //types: FLOAT
|
2026-03-31 15:32:36 +02:00
|
|
|
const std::string* value_string; //types: STR, ENUM, KEYWORD; lifetime managed by spf_lexer::string_pool_
|
2025-08-25 12:45:10 +02:00
|
|
|
};
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
token() : start_pos(0),
|
2026-03-26 15:49:28 +01:00
|
|
|
type(Token_NONE) {}
|
2026-07-19 11:44:31 +03:00
|
|
|
|
2026-03-31 18:23:13 +02:00
|
|
|
token(size_t start_position, token_type token_kind, const std::string& string_value)
|
|
|
|
|
: start_pos(start_position), type(token_kind), value_string(&string_value) {}
|
2026-03-26 15:49:28 +01:00
|
|
|
|
2026-07-19 11:44:31 +03:00
|
|
|
token(size_t start_position, token_type token_kind, int64_t integer_value)
|
2026-03-31 18:23:13 +02:00
|
|
|
: start_pos(start_position), type(token_kind), value_int(integer_value) {}
|
2026-03-26 15:49:28 +01:00
|
|
|
|
2026-03-31 18:23:13 +02:00
|
|
|
token(size_t start_position, double floating_value)
|
|
|
|
|
: start_pos(start_position), type(Token_FLOAT), value_double(floating_value) {}
|
2026-03-26 15:49:28 +01:00
|
|
|
|
2026-03-31 18:23:13 +02:00
|
|
|
token(size_t start_position, char operator_character)
|
|
|
|
|
: start_pos(start_position), type(Token_OPERATOR), value_char(operator_character) {}
|
2026-08-19 18:16:59 +05:00
|
|
|
|
2026-03-31 18:23:13 +02:00
|
|
|
token(size_t start_position, token_type token_kind, char character_value)
|
|
|
|
|
: start_pos(start_position), type(token_kind), value_char(character_value) {}
|
2026-03-26 15:49:28 +01:00
|
|
|
|
|
|
|
|
bool is_string();
|
|
|
|
|
bool is_identifier();
|
|
|
|
|
bool is_operator();
|
|
|
|
|
bool is_operator(char character);
|
|
|
|
|
bool is_enumeration();
|
|
|
|
|
bool is_keyword();
|
|
|
|
|
bool is_int();
|
|
|
|
|
bool is_bool();
|
|
|
|
|
bool is_logical();
|
|
|
|
|
bool is_float();
|
|
|
|
|
bool is_binary();
|
|
|
|
|
|
2026-07-19 11:44:31 +03:00
|
|
|
int64_t as_int();
|
2026-03-26 15:49:28 +01:00
|
|
|
unsigned as_identifier();
|
|
|
|
|
bool as_bool();
|
|
|
|
|
boost::logic::tribool as_logical();
|
|
|
|
|
double as_float();
|
|
|
|
|
const std::string& as_string();
|
|
|
|
|
boost::dynamic_bitset<> as_binary();
|
|
|
|
|
std::string to_string();
|
|
|
|
|
|
|
|
|
|
operator bool() const {
|
|
|
|
|
return type != Token_NONE;
|
2025-08-25 12:45:10 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-11 15:51:40 +02:00
|
|
|
namespace impl {
|
|
|
|
|
struct inverse_record {
|
|
|
|
|
uint32_t referenced_id;
|
|
|
|
|
uint32_t source_id;
|
|
|
|
|
uint16_t source_entity;
|
|
|
|
|
int16_t attribute_index;
|
|
|
|
|
};
|
|
|
|
|
|
2026-09-10 07:31:45 +10:00
|
|
|
// Which instances reference a given instance, and through which
|
|
|
|
|
// attribute. One index serves the whole file.
|
|
|
|
|
//
|
|
|
|
|
// Two tiers keep every operation cheap without giving up the compact
|
|
|
|
|
// flat layout that parsing relies on:
|
|
|
|
|
//
|
|
|
|
|
// - base_: one flat vector. Bulk loading appends to it unsorted and
|
|
|
|
|
// sort() finalizes it once; lookups then binary-search it. Removing
|
|
|
|
|
// a record tombstones it in place (attribute_index set to
|
|
|
|
|
// dead_attribute) rather than erasing, so removal doesn't shift the
|
|
|
|
|
// vector.
|
|
|
|
|
// - delta_: records added after sort(), bucketed by referenced_id.
|
|
|
|
|
// A lookup reads the base range and then the bucket.
|
|
|
|
|
//
|
|
|
|
|
// compact() folds the delta into the base and drops tombstones. add()
|
|
|
|
|
// and the removal methods run it once the delta or the tombstones
|
|
|
|
|
// outgrow the live base (capped by delta_fold_limit), so folding is
|
|
|
|
|
// amortised O(1) per mutation and the delta's memory stays bounded.
|
|
|
|
|
//
|
|
|
|
|
// Before the split every lookup re-sorted the entire vector if
|
|
|
|
|
// anything had been added since the previous lookup, so a loop that
|
|
|
|
|
// creates an instance and then reads an inverse cost O(R log R) per
|
|
|
|
|
// iteration on a file with R references.
|
2026-06-11 15:51:40 +02:00
|
|
|
class inverse_index {
|
|
|
|
|
public:
|
2026-08-08 14:58:15 +02:00
|
|
|
typedef std::map<std::tuple<short, short>, std::vector<uint32_t>> legacy_bucket;
|
|
|
|
|
typedef std::unordered_map<int, legacy_bucket> legacy_map;
|
|
|
|
|
typedef legacy_map::key_type key_type;
|
|
|
|
|
typedef legacy_map::mapped_type mapped_type;
|
|
|
|
|
typedef legacy_map::value_type value_type;
|
|
|
|
|
typedef legacy_map::iterator iterator;
|
|
|
|
|
typedef legacy_map::const_iterator const_iterator;
|
2026-06-11 15:51:40 +02:00
|
|
|
|
|
|
|
|
private:
|
2026-09-10 07:31:45 +10:00
|
|
|
typedef std::vector<inverse_record>::const_iterator base_iterator;
|
|
|
|
|
|
|
|
|
|
// Attribute indices are small and non-negative, so the minimum
|
|
|
|
|
// value can't collide with a live record.
|
|
|
|
|
static constexpr int16_t dead_attribute = std::numeric_limits<int16_t>::min();
|
|
|
|
|
static constexpr size_t delta_fold_limit = size_t(1) << 20;
|
|
|
|
|
|
|
|
|
|
// Lookups on a const index still need to finalize bulk loading.
|
|
|
|
|
mutable std::vector<inverse_record> base_;
|
|
|
|
|
mutable bool sorted_ = false;
|
|
|
|
|
size_t dead_ = 0;
|
|
|
|
|
std::unordered_map<uint32_t, std::vector<inverse_record>> delta_;
|
|
|
|
|
size_t delta_size_ = 0;
|
2026-08-08 14:58:15 +02:00
|
|
|
mutable std::unique_ptr<legacy_map> materialized_;
|
2026-06-11 15:51:40 +02:00
|
|
|
|
|
|
|
|
static bool record_less(const inverse_record& a, const inverse_record& b) {
|
|
|
|
|
if (a.referenced_id != b.referenced_id) {
|
|
|
|
|
return a.referenced_id < b.referenced_id;
|
|
|
|
|
}
|
|
|
|
|
if (a.source_entity != b.source_entity) {
|
|
|
|
|
return a.source_entity < b.source_entity;
|
|
|
|
|
}
|
|
|
|
|
if (a.attribute_index != b.attribute_index) {
|
|
|
|
|
return a.attribute_index < b.attribute_index;
|
|
|
|
|
}
|
|
|
|
|
return a.source_id < b.source_id;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 07:31:45 +10:00
|
|
|
struct referenced_id_less {
|
|
|
|
|
bool operator()(const inverse_record& a, uint32_t referenced_id) const {
|
|
|
|
|
return a.referenced_id < referenced_id;
|
|
|
|
|
}
|
|
|
|
|
bool operator()(uint32_t referenced_id, const inverse_record& a) const {
|
|
|
|
|
return referenced_id < a.referenced_id;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool same_record(const inverse_record& a, const inverse_record& b) {
|
|
|
|
|
return a.referenced_id == b.referenced_id &&
|
|
|
|
|
a.source_id == b.source_id &&
|
|
|
|
|
a.source_entity == b.source_entity &&
|
|
|
|
|
a.attribute_index == b.attribute_index;
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-10 07:31:45 +10:00
|
|
|
static bool is_dead(const inverse_record& record) {
|
|
|
|
|
return record.attribute_index == dead_attribute;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void kill(inverse_record& record) {
|
|
|
|
|
record.attribute_index = dead_attribute;
|
|
|
|
|
++dead_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t live_base_size() const {
|
|
|
|
|
return base_.size() - dead_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<base_iterator, base_iterator> base_range(uint32_t referenced_id) const {
|
|
|
|
|
sort();
|
|
|
|
|
return std::equal_range(base_.cbegin(), base_.cend(), referenced_id, referenced_id_less{});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<std::vector<inverse_record>::iterator, std::vector<inverse_record>::iterator> mutable_base_range(uint32_t referenced_id) {
|
|
|
|
|
sort();
|
|
|
|
|
return std::equal_range(base_.begin(), base_.end(), referenced_id, referenced_id_less{});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void compact() {
|
|
|
|
|
sort();
|
|
|
|
|
if (dead_ != 0) {
|
|
|
|
|
base_.erase(std::remove_if(base_.begin(), base_.end(), is_dead), base_.end());
|
|
|
|
|
dead_ = 0;
|
|
|
|
|
}
|
|
|
|
|
const auto base_end = (std::ptrdiff_t)base_.size();
|
|
|
|
|
base_.reserve(base_.size() + delta_size_);
|
|
|
|
|
for (const auto& bucket : delta_) {
|
|
|
|
|
base_.insert(base_.end(), bucket.second.begin(), bucket.second.end());
|
|
|
|
|
}
|
|
|
|
|
delta_.clear();
|
|
|
|
|
delta_size_ = 0;
|
|
|
|
|
std::sort(base_.begin() + base_end, base_.end(), record_less);
|
|
|
|
|
std::inplace_merge(base_.begin(), base_.begin() + base_end, base_.end(), record_less);
|
|
|
|
|
invalidate_materialized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void compact_if_tombstones_dominate() {
|
|
|
|
|
if (dead_ > live_base_size()) {
|
|
|
|
|
compact();
|
|
|
|
|
}
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void invalidate_materialized() const {
|
|
|
|
|
materialized_.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-08 14:58:15 +02:00
|
|
|
legacy_map& materialize() const {
|
2026-06-11 15:51:40 +02:00
|
|
|
if (!materialized_) {
|
2026-08-08 14:58:15 +02:00
|
|
|
materialized_ = std::make_unique<legacy_map>();
|
2026-09-10 07:31:45 +10:00
|
|
|
materialized_->reserve(size());
|
|
|
|
|
const auto insert = [this](const inverse_record& record) {
|
2026-06-11 15:51:40 +02:00
|
|
|
(*materialized_)[(int)record.referenced_id][{(short)record.source_entity, (short)record.attribute_index}].push_back(record.source_id);
|
2026-09-10 07:31:45 +10:00
|
|
|
};
|
|
|
|
|
sort();
|
|
|
|
|
for (const auto& record : base_) {
|
|
|
|
|
if (!is_dead(record)) {
|
|
|
|
|
insert(record);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const auto& bucket : delta_) {
|
|
|
|
|
for (const auto& record : bucket.second) {
|
|
|
|
|
insert(record);
|
|
|
|
|
}
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return *materialized_;
|
|
|
|
|
}
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-06-11 15:51:40 +02:00
|
|
|
public:
|
|
|
|
|
inverse_index() = default;
|
|
|
|
|
|
|
|
|
|
inverse_index(const inverse_index& other)
|
2026-09-10 07:31:45 +10:00
|
|
|
: base_(other.base_)
|
2026-06-11 15:51:40 +02:00
|
|
|
, sorted_(other.sorted_)
|
2026-09-10 07:31:45 +10:00
|
|
|
, dead_(other.dead_)
|
|
|
|
|
, delta_(other.delta_)
|
|
|
|
|
, delta_size_(other.delta_size_)
|
2026-06-11 15:51:40 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
inverse_index& operator=(const inverse_index& other) {
|
|
|
|
|
if (this != &other) {
|
2026-09-10 07:31:45 +10:00
|
|
|
base_ = other.base_;
|
2026-06-11 15:51:40 +02:00
|
|
|
sorted_ = other.sorted_;
|
2026-09-10 07:31:45 +10:00
|
|
|
dead_ = other.dead_;
|
|
|
|
|
delta_ = other.delta_;
|
|
|
|
|
delta_size_ = other.delta_size_;
|
2026-06-11 15:51:40 +02:00
|
|
|
materialized_.reset();
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-06-11 15:51:40 +02:00
|
|
|
inverse_index(inverse_index&&) noexcept = default;
|
|
|
|
|
inverse_index& operator=(inverse_index&&) noexcept = default;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-06-11 15:51:40 +02:00
|
|
|
void reserve(size_t size) {
|
2026-09-10 07:31:45 +10:00
|
|
|
base_.reserve(size);
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-06-11 15:51:40 +02:00
|
|
|
void add(uint32_t referenced_id, uint32_t source_id, uint16_t source_entity, int attribute_index) {
|
2026-09-10 07:31:45 +10:00
|
|
|
const inverse_record record{referenced_id, source_id, source_entity, (int16_t)attribute_index};
|
|
|
|
|
if (sorted_) {
|
|
|
|
|
delta_[referenced_id].push_back(record);
|
|
|
|
|
++delta_size_;
|
|
|
|
|
if (delta_size_ > std::min(live_base_size(), delta_fold_limit)) {
|
|
|
|
|
compact();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
base_.push_back(record);
|
|
|
|
|
}
|
2026-06-11 15:51:40 +02:00
|
|
|
invalidate_materialized();
|
|
|
|
|
}
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-06-11 15:51:40 +02:00
|
|
|
bool remove(uint32_t referenced_id, uint32_t source_id, uint16_t source_entity, int attribute_index) {
|
|
|
|
|
const inverse_record needle{referenced_id, source_id, source_entity, (int16_t)attribute_index};
|
2026-09-10 07:31:45 +10:00
|
|
|
const auto matches = [&needle](const inverse_record& record) {
|
|
|
|
|
return same_record(record, needle);
|
|
|
|
|
};
|
|
|
|
|
auto bucket = delta_.find(referenced_id);
|
|
|
|
|
if (bucket != delta_.end()) {
|
|
|
|
|
auto& records = bucket->second;
|
|
|
|
|
auto it = std::find_if(records.begin(), records.end(), matches);
|
|
|
|
|
if (it != records.end()) {
|
|
|
|
|
records.erase(it);
|
|
|
|
|
--delta_size_;
|
|
|
|
|
if (records.empty()) {
|
|
|
|
|
delta_.erase(bucket);
|
|
|
|
|
}
|
|
|
|
|
invalidate_materialized();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
auto range = mutable_base_range(referenced_id);
|
|
|
|
|
auto it = std::find_if(range.first, range.second, matches);
|
|
|
|
|
if (it == range.second) {
|
2026-06-11 15:51:40 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-09-10 07:31:45 +10:00
|
|
|
kill(*it);
|
|
|
|
|
compact_if_tombstones_dominate();
|
2026-06-11 15:51:40 +02:00
|
|
|
invalidate_materialized();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-09-10 07:31:45 +10:00
|
|
|
// Finalizes bulk loading. Subsequent add() calls go to the delta.
|
2026-06-11 15:51:40 +02:00
|
|
|
void sort() const {
|
|
|
|
|
if (!sorted_) {
|
2026-09-10 07:31:45 +10:00
|
|
|
std::sort(base_.begin(), base_.end(), record_less);
|
2026-09-11 16:06:08 +10:00
|
|
|
base_.shrink_to_fit();
|
2026-06-11 15:51:40 +02:00
|
|
|
sorted_ = true;
|
|
|
|
|
invalidate_materialized();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 07:31:45 +10:00
|
|
|
// Visits every live record referencing referenced_id: the base
|
|
|
|
|
// records in record_less order, then the delta in insertion order.
|
|
|
|
|
template <typename Fn>
|
|
|
|
|
void for_each(uint32_t referenced_id, Fn&& fn) const {
|
|
|
|
|
auto range = base_range(referenced_id);
|
|
|
|
|
for (auto it = range.first; it != range.second; ++it) {
|
|
|
|
|
if (!is_dead(*it)) {
|
|
|
|
|
fn(*it);
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
2026-09-10 07:31:45 +10:00
|
|
|
}
|
|
|
|
|
auto bucket = delta_.find(referenced_id);
|
|
|
|
|
if (bucket != delta_.end()) {
|
|
|
|
|
for (const auto& record : bucket->second) {
|
|
|
|
|
fn(record);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-10 07:31:45 +10:00
|
|
|
size_t count(uint32_t referenced_id) const {
|
|
|
|
|
size_t n = 0;
|
|
|
|
|
for_each(referenced_id, [&n](const inverse_record&) { ++n; });
|
|
|
|
|
return n;
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool empty() const {
|
2026-09-10 07:31:45 +10:00
|
|
|
return size() == 0;
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t size() const {
|
2026-09-10 07:31:45 +10:00
|
|
|
return live_base_size() + delta_size_;
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear() {
|
2026-09-10 07:31:45 +10:00
|
|
|
base_.clear();
|
|
|
|
|
sorted_ = false;
|
|
|
|
|
dead_ = 0;
|
|
|
|
|
delta_.clear();
|
|
|
|
|
delta_size_ = 0;
|
2026-06-11 15:51:40 +02:00
|
|
|
materialized_.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator begin() {
|
|
|
|
|
return materialize().begin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator end() {
|
|
|
|
|
return materialize().end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const_iterator begin() const {
|
|
|
|
|
return materialize().begin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const_iterator end() const {
|
|
|
|
|
return materialize().end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator find(const key_type& key) {
|
|
|
|
|
return materialize().find(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const_iterator find(const key_type& key) const {
|
|
|
|
|
return materialize().find(key);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 07:31:45 +10:00
|
|
|
// Removes every record referencing key.
|
2026-06-11 15:51:40 +02:00
|
|
|
size_t erase(const key_type& key) {
|
2026-09-10 07:31:45 +10:00
|
|
|
const auto referenced_id = (uint32_t)key;
|
|
|
|
|
size_t removed = 0;
|
|
|
|
|
auto range = mutable_base_range(referenced_id);
|
|
|
|
|
for (auto it = range.first; it != range.second; ++it) {
|
|
|
|
|
if (!is_dead(*it)) {
|
|
|
|
|
kill(*it);
|
|
|
|
|
++removed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
auto bucket = delta_.find(referenced_id);
|
|
|
|
|
if (bucket != delta_.end()) {
|
|
|
|
|
removed += bucket->second.size();
|
|
|
|
|
delta_size_ -= bucket->second.size();
|
|
|
|
|
delta_.erase(bucket);
|
|
|
|
|
}
|
|
|
|
|
compact_if_tombstones_dominate();
|
2026-06-11 15:51:40 +02:00
|
|
|
invalidate_materialized();
|
2026-09-10 07:31:45 +10:00
|
|
|
return removed;
|
2026-06-11 15:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<iterator, bool> insert(const value_type& value) {
|
|
|
|
|
for (const auto& bucket : value.second) {
|
|
|
|
|
for (auto source_id : bucket.second) {
|
|
|
|
|
add((uint32_t)value.first, source_id, (uint16_t)std::get<0>(bucket.first), std::get<1>(bucket.first));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
auto it = find(value.first);
|
|
|
|
|
return {it, true};
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2025-09-26 14:24:49 +02:00
|
|
|
struct IFC_PARSE_API in_memory_file_storage {
|
2026-03-27 20:45:13 +01:00
|
|
|
|
2026-06-11 15:51:40 +02:00
|
|
|
std::vector<shared_pointer_type> read_simple_type_instances;
|
|
|
|
|
std::vector<shared_pointer_type> steal_instances() {
|
|
|
|
|
return std::move(read_simple_type_instances);
|
2025-10-08 14:17:47 +02:00
|
|
|
}
|
2025-10-03 14:18:07 +02:00
|
|
|
|
2026-08-08 13:58:39 +02:00
|
|
|
std::reference_wrapper<ifcopenshell::logger> logger_;
|
2025-10-13 20:47:43 +02:00
|
|
|
// IfcParse::FileReader* stream;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
// Either one of these needs to be set
|
2026-03-31 15:32:36 +02:00
|
|
|
ifcopenshell::file* file;
|
|
|
|
|
const ifcopenshell::schema_definition* schema;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
unresolved_references* references_to_resolve = nullptr;
|
|
|
|
|
|
2026-08-08 14:58:15 +02:00
|
|
|
typedef std::map<const ifcopenshell::declaration*, std::vector<express::base>> entities_by_type;
|
|
|
|
|
typedef std::unordered_map<uint32_t, shared_pointer_type> entity_instance_by_name_storage;
|
|
|
|
|
typedef map_transformer<entity_instance_by_name_storage, std::function<express::base(shared_pointer_type)>> entity_instance_by_name;
|
|
|
|
|
typedef std::unordered_map<uint32_t, shared_pointer_type> type_instance_by_name;
|
|
|
|
|
typedef std::map<std::string, express::base> entity_instance_by_guid;
|
|
|
|
|
typedef inverse_index entities_by_ref;
|
|
|
|
|
typedef entity_instance_by_name::iterator iterator;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 13:58:39 +02:00
|
|
|
in_memory_file_storage(ifcopenshell::file* owner_file = nullptr, ifcopenshell::logger& logger = ifcopenshell::logger::root()) : logger_(logger), file(owner_file), schema(nullptr), byid_read_(&byid_, [this](const shared_pointer_type& data) { return express::base(data); }) {};
|
2026-03-31 18:23:13 +02:00
|
|
|
in_memory_file_storage(const in_memory_file_storage& other) = delete;
|
|
|
|
|
in_memory_file_storage(const in_memory_file_storage&& other) = delete;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2025-10-08 14:17:47 +02:00
|
|
|
|
2026-08-08 14:58:15 +02:00
|
|
|
class type_iterator : public entities_by_type::const_iterator {
|
2025-08-25 12:45:10 +02:00
|
|
|
public:
|
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
2026-08-08 14:58:15 +02:00
|
|
|
using value_type = entities_by_type::key_type;
|
|
|
|
|
using difference_type = typename entities_by_type::const_iterator::difference_type;
|
2025-08-25 12:45:10 +02:00
|
|
|
using pointer = value_type const*;
|
|
|
|
|
using reference = value_type const&;
|
|
|
|
|
|
2026-08-08 14:58:15 +02:00
|
|
|
type_iterator() : entities_by_type::const_iterator() {};
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 14:58:15 +02:00
|
|
|
type_iterator(const entities_by_type::const_iterator& iterator)
|
|
|
|
|
: entities_by_type::const_iterator(iterator) {};
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 14:58:15 +02:00
|
|
|
entities_by_type::key_type const* operator->() const {
|
|
|
|
|
return &entities_by_type::const_iterator::operator->()->first;
|
2025-08-25 12:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-08 14:58:15 +02:00
|
|
|
entities_by_type::key_type const& operator*() const {
|
|
|
|
|
return entities_by_type::const_iterator::operator*().first;
|
2025-08-25 12:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type_iterator& operator++() {
|
2026-08-08 14:58:15 +02:00
|
|
|
entities_by_type::const_iterator::operator++();
|
2025-08-25 12:45:10 +02:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type_iterator operator++(int) {
|
|
|
|
|
type_iterator tmp(*this);
|
|
|
|
|
operator++();
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-08 14:58:15 +02:00
|
|
|
entity_instance_by_name_storage byid_;
|
|
|
|
|
type_instance_by_name tbyid_;
|
|
|
|
|
entities_by_type bytype_excl_;
|
|
|
|
|
entities_by_ref byref_excl_;
|
|
|
|
|
entity_instance_by_guid byguid_;
|
|
|
|
|
entity_instance_by_name byid_read_;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-03-27 20:45:13 +01:00
|
|
|
template <typename Reader>
|
2026-06-11 15:51:40 +02:00
|
|
|
shared_pointer_type load(ifcopenshell::spf_lexer<Reader>* tokens, std::optional<size_t> entity_instance_name, const ifcopenshell::declaration* declaration, const ifcopenshell::entity* entity, int attribute_index = -1, bool coerce_attribute_count = true);
|
2026-03-27 20:45:13 +01:00
|
|
|
template <typename Reader>
|
2026-03-31 15:32:36 +02:00
|
|
|
void try_read_semicolon(ifcopenshell::spf_lexer<Reader>* tokens) const;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-03-31 18:23:13 +02:00
|
|
|
void register_inverse(unsigned referenced_id, const ifcopenshell::entity* from_entity, int instance_id, int attribute_index);
|
2026-08-08 07:42:45 +02:00
|
|
|
void unregister_inverse(unsigned referenced_id, const ifcopenshell::entity* from_entity, const express::base& entity, int attribute_index);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-03-27 20:45:13 +01:00
|
|
|
template <typename Reader>
|
2026-03-31 18:23:13 +02:00
|
|
|
void read_from_stream(Reader* stream, const ifcopenshell::schema_definition*& schema, unsigned int& max_id, const std::set<std::string>& types_to_bypass);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
file_open_status good_ = file_open_status::SUCCESS;
|
|
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
express::base instance_by_id(int instance_id);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
void add_type_ref(const express::base& new_entity) {
|
2026-01-04 10:40:02 +01:00
|
|
|
if (auto* ty = new_entity.declaration().as_entity()) {
|
|
|
|
|
bytype_excl_[ty].push_back(new_entity);
|
2025-08-25 12:45:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-08 07:42:45 +02:00
|
|
|
void remove_type_ref(const express::base& new_entity) {
|
2026-01-04 10:40:02 +01:00
|
|
|
if (auto* ty = new_entity.declaration().as_entity()) {
|
2025-08-25 12:45:10 +02:00
|
|
|
auto it = bytype_excl_.find(ty);
|
|
|
|
|
if (it != bytype_excl_.end()) {
|
2026-01-04 10:40:02 +01:00
|
|
|
it->second.erase(std::remove(it->second.begin(), it->second.end(), new_entity), it->second.end());
|
|
|
|
|
if (it->second.empty()) {
|
2025-08-25 12:45:10 +02:00
|
|
|
bytype_excl_.erase(ty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
void process_deletion_inverse(const express::base& entity);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
template <typename T>
|
2026-03-31 18:23:13 +02:00
|
|
|
T create(int instance_id = -1);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
express::base create(const ifcopenshell::declaration* declaration, int instance_id = -1);
|
2025-08-25 12:45:10 +02:00
|
|
|
};
|
|
|
|
|
|
2025-09-26 14:24:49 +02:00
|
|
|
class IFC_PARSE_API rocks_db_file_storage {
|
2025-08-25 12:45:10 +02:00
|
|
|
public:
|
2026-06-05 08:12:55 +01:00
|
|
|
std::unique_ptr<rocksdb::DB> db;
|
2025-08-25 12:45:10 +02:00
|
|
|
rocksdb::WriteOptions wopts;
|
|
|
|
|
rocksdb::ReadOptions ropts;
|
2026-03-31 15:32:36 +02:00
|
|
|
ifcopenshell::file* file;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
enum instance_ref {
|
|
|
|
|
typedecl_ref,
|
|
|
|
|
entityinstance_ref
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// to make sure that instance pointer are constant during file lifetime
|
|
|
|
|
// cache instances because we want stable pointers
|
2026-03-31 15:32:36 +02:00
|
|
|
// @todo this is silly, but we cannot have the same type, this should be just a pointer then on the file side?
|
2026-08-08 14:58:15 +02:00
|
|
|
typedef std::map<uint32_t, shared_pointer_type> entity_by_iden_cache;
|
|
|
|
|
entity_by_iden_cache instance_cache_, type_instance_cache_;
|
2026-05-08 10:07:34 +02:00
|
|
|
std::mutex instance_cache_mutex_;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
// @todo all these size_ts should probably be uint32_t for consistency with in-mem storage
|
|
|
|
|
|
|
|
|
|
// lookup id->identity
|
2026-08-08 14:58:15 +02:00
|
|
|
// typedef rocksdb_map_adapter<size_t, size_t> identity_by_id;
|
|
|
|
|
// identity_by_id byid_;
|
|
|
|
|
typedef rocksdb_set_view<size_t> instance_name_view;
|
|
|
|
|
instance_name_view instance_ids_;
|
|
|
|
|
typedef set_to_map_transformer<instance_name_view, std::function<express::base(size_t)>> entity_instance_by_name;
|
|
|
|
|
entity_instance_by_name instance_by_name_;
|
|
|
|
|
|
|
|
|
|
// typedef map_transformer<rocksdb_map_adapter<size_t, size_t>, std::function<ifcopenshell::IfcBaseClass*(size_t)>, std::function<size_t(ifcopenshell::IfcBaseClass*)>> entity_by_id;
|
2025-08-25 12:45:10 +02:00
|
|
|
// storage is now Instance name -> Identity -> Pointer (cached)
|
2026-08-08 14:58:15 +02:00
|
|
|
// entity_by_id byidentity_;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
// index in schema to binary serialized ids
|
2026-08-08 14:58:15 +02:00
|
|
|
typedef rocksdb_map_adapter<size_t, std::string> instance_id_str_by_type;
|
|
|
|
|
instance_id_str_by_type bytype_;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
// guid -> id
|
2026-08-08 14:58:15 +02:00
|
|
|
typedef rocksdb_map_adapter<std::string, size_t> instance_id_by_guid_str;
|
|
|
|
|
instance_id_by_guid_str byguid_internal_;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
// guid -> id -> instance
|
2026-08-08 14:58:15 +02:00
|
|
|
typedef map_transformer<rocksdb_map_adapter<std::string, size_t>, std::function<express::base(size_t)>, std::function<size_t(const express::base&)>> entity_instance_by_guid;
|
|
|
|
|
entity_instance_by_guid byguid_;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
typedef std::tuple<int, int, int> inverse_attr_record;
|
|
|
|
|
enum INVERSE_ATTR {
|
|
|
|
|
INSTANCE_ID,
|
|
|
|
|
INSTANCE_TYPE,
|
|
|
|
|
ATTRIBUTE_INDEX
|
|
|
|
|
};
|
2026-08-08 14:58:15 +02:00
|
|
|
typedef rocksdb_map_adapter<inverse_attr_record, std::vector<uint32_t>> entities_by_ref;
|
|
|
|
|
entities_by_ref byref_excl_;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-05-07 10:16:15 +10:00
|
|
|
bool read_only_ = false;
|
|
|
|
|
|
2025-08-25 12:45:10 +02:00
|
|
|
// @todo naming
|
2026-03-31 18:23:13 +02:00
|
|
|
rocks_db_file_storage(const std::string& path, ifcopenshell::file* owner_file, bool read_only = false);
|
2025-08-25 12:45:10 +02:00
|
|
|
~rocks_db_file_storage();
|
|
|
|
|
|
2026-03-31 15:32:36 +02:00
|
|
|
bool read_schema(const ifcopenshell::schema_definition*& schema);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
express::base assert_existance(size_t instance_id, instance_ref reference_type);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
// @todo merge iterators (template?)
|
2025-09-26 14:24:49 +02:00
|
|
|
class IFC_PARSE_API rocksdb_types_iterator {
|
2025-08-25 12:45:10 +02:00
|
|
|
private:
|
|
|
|
|
rocksdb::Iterator* state_;
|
|
|
|
|
const rocks_db_file_storage* storage_;
|
|
|
|
|
|
|
|
|
|
static constexpr char prefix_[] = "t|";
|
|
|
|
|
|
2026-01-04 10:40:02 +01:00
|
|
|
std::optional<size_t> read_id_() const {
|
2025-09-03 11:11:55 +02:00
|
|
|
#ifdef IFOPSH_WITH_ROCKSDB
|
2025-08-25 12:45:10 +02:00
|
|
|
auto sv = state_->key().ToStringView();
|
|
|
|
|
auto ii = sv.find("|", 2);
|
|
|
|
|
if (ii != decltype(sv)::npos) {
|
|
|
|
|
char* pEnd;
|
|
|
|
|
long result = strtol(sv.data() + 2, &pEnd, 10);
|
|
|
|
|
if (*pEnd == '|') {
|
|
|
|
|
return (size_t)result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-26 13:56:03 +02:00
|
|
|
#endif
|
2026-01-04 10:40:02 +01:00
|
|
|
return std::nullopt;
|
2025-08-25 12:45:10 +02:00
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
2026-03-31 15:32:36 +02:00
|
|
|
using value_type = const ifcopenshell::declaration*;
|
2025-08-25 12:45:10 +02:00
|
|
|
// @todo ?
|
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
|
using pointer = value_type const*;
|
|
|
|
|
using reference = value_type const&;
|
|
|
|
|
|
|
|
|
|
rocksdb_types_iterator()
|
|
|
|
|
: state_(nullptr)
|
|
|
|
|
, storage_(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 18:23:13 +02:00
|
|
|
rocksdb_types_iterator(const rocks_db_file_storage* storage)
|
|
|
|
|
: storage_(storage)
|
2025-08-25 12:45:10 +02:00
|
|
|
{
|
2025-09-03 11:11:55 +02:00
|
|
|
#ifdef IFOPSH_WITH_ROCKSDB
|
2026-03-31 18:23:13 +02:00
|
|
|
state_ = storage->db->NewIterator(rocksdb::ReadOptions());
|
2025-08-25 12:45:10 +02:00
|
|
|
state_->Seek(prefix_);
|
|
|
|
|
if (!state_->Valid() || !state_->key().starts_with(prefix_)) {
|
|
|
|
|
delete state_;
|
|
|
|
|
state_ = nullptr;
|
|
|
|
|
}
|
2025-08-26 13:56:03 +02:00
|
|
|
#endif
|
2025-08-25 12:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rocksdb_types_iterator& operator++() {
|
2025-09-03 11:11:55 +02:00
|
|
|
#ifdef IFOPSH_WITH_ROCKSDB
|
2025-08-25 12:45:10 +02:00
|
|
|
if (!state_) {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
auto last_id = read_id_();
|
|
|
|
|
while (state_->Valid()) {
|
|
|
|
|
state_->Next();
|
|
|
|
|
// Stop if we've left the prefix range.
|
|
|
|
|
if (!state_->Valid() || !state_->key().starts_with(prefix_)) {
|
|
|
|
|
delete state_;
|
|
|
|
|
state_ = nullptr;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (read_id_() != last_id) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-26 13:56:03 +02:00
|
|
|
#endif
|
2025-08-26 15:59:05 +02:00
|
|
|
return *this;
|
2025-08-25 12:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rocksdb_types_iterator operator++(int) {
|
|
|
|
|
rocksdb_types_iterator temp = *this;
|
|
|
|
|
++(*this);
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator==(const rocksdb_types_iterator& other) const {
|
|
|
|
|
if (state_ == nullptr && other.state_ == nullptr) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return read_id_() == other.read_id_();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator!=(const rocksdb_types_iterator& other) const {
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value_type const& operator*() const;
|
|
|
|
|
|
|
|
|
|
value_type const* operator->() const {
|
|
|
|
|
return &operator*();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// @todo rocksdb_instance_iterator?
|
2026-08-08 14:58:15 +02:00
|
|
|
using const_iterator = entity_instance_by_name::iterator;
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-03-31 18:23:13 +02:00
|
|
|
void register_inverse(unsigned referenced_id, const ifcopenshell::entity* from_entity, int instance_id, int attribute_index);
|
2026-08-08 07:42:45 +02:00
|
|
|
void unregister_inverse(unsigned referenced_id, const ifcopenshell::entity* from_entity, const express::base& entity, int attribute_index);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
// @todo a bit hard as a map because of value_type being an aggregate
|
2026-08-08 07:42:45 +02:00
|
|
|
void add_type_ref(const express::base& new_entity);
|
|
|
|
|
void remove_type_ref(const express::base& new_entity);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
express::base instance_by_id(int instance_id);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
void process_deletion_inverse(const express::base& entity);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
|
|
|
|
template <typename T>
|
2026-03-31 18:23:13 +02:00
|
|
|
T create(int instance_id = -1);
|
2025-08-25 12:45:10 +02:00
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
express::base create(const ifcopenshell::declaration* declaration, int instance_id = -1);
|
2025-08-25 12:45:10 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 13:51:48 +01:00
|
|
|
// redefine Handle macro.
|
|
|
|
|
#pragma pop_macro("Handle")
|
|
|
|
|
|
2025-08-25 12:45:10 +02:00
|
|
|
#endif // STORAGE_H
|