Files
IfcOpenShell/src/ifcparse/set_to_map_transformer.h
T
Dion Moult 4433d8b3d6 ifcparse: make deleting and creating instances work on RocksDB-backed files
file.remove() on a RocksDB-backed file segfaulted. Reducing it turned up
five gaps that each made editing such a file crash or silently do
nothing:

- process_deletion_inverse() decoded the v| inverse-record values as
  size_t while the serializer, register_inverse(), unregister_inverse()
  and instances_by_reference() use uint32_t, so std::find failed and
  vals.erase(end()) was undefined behaviour. It also took the DeleteRange
  end from an iterator that is invalid when the instance has no inverse
  records. Decode as uint32_t, remove every occurrence guarded on
  "found", and derive the range end from the prefix itself.
- attribute_value::size() ignored storage_model_, so every aggregate
  assignment on a RocksDB instance threw "Invalid variant index" from
  set_attribute_value(). Branch on the storage model like the sibling
  accessors and count the deserialized aggregate.
- rocks_db_file_storage::create() was a stub returning an empty handle,
  which anything creating an instance then dereferenced. Implement it
  after in_memory_file_storage::create().
- max_id_ is only initialised by the in-memory parse, so a RocksDB file
  would have handed out ids that overwrite existing instances.
  Implement the recalculate_id_counter() stub per backend and run it
  once before the first fresh_id() on RocksDB.
- byid_.erase() was a no-op: set_to_map_transformer::erase() and
  rocksdb_set_view::erase() were stubs. The deleted instance's attribute
  keys and cached handle survived, entity_names() still listed it and
  reopening the database threw. Erase deletes every key under the
  instance's prefix; the transformer forwards to it and takes an
  on-erase hook the storage uses to drop the cached handle.

root.remove_product on the first 200 products of a 61 MB model now
leaves the same surviving ids and inverse counts whether the file was
opened from SPF or converted to RocksDB.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HNrXDmR88wKPCYwGE21SyH
2026-09-16 09:03:06 +10:00

123 lines
4.8 KiB
C++

/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#include <iterator>
#include <type_traits>
#include <utility>
#include <functional>
// map_transformer: wraps a set-like construct so that its iterator returns
// a value_type where of the underlying set element as the key, with the value
// that same key transformed via a function.
template <typename BaseSet, typename Transform>
class set_to_map_transformer {
public:
using key_type = typename BaseSet::value_type;
using transformed_mapped_type = std::invoke_result_t<Transform, key_type>;
using value_type = std::pair<key_type, transformed_mapped_type>;
using mapped_type = transformed_mapped_type;
private:
BaseSet* base_map_;
Transform transform_;
std::function<void(const key_type&)> on_erase_;
public:
set_to_map_transformer(BaseSet* base_set, Transform transform)
: base_map_(base_set), transform_(transform) {}
// on_erase runs after erase(key), whether or not the base set still
// held the key, so state derived from the set can be dropped.
set_to_map_transformer(BaseSet* base_set, Transform transform, std::function<void(const key_type&)> on_erase)
: base_map_(base_set), transform_(transform), on_erase_(std::move(on_erase)) {}
class iterator {
public:
using base_iterator = typename BaseSet::iterator;
using iterator_category = std::forward_iterator_tag;
using difference_type = typename std::iterator_traits<base_iterator>::difference_type;
using key_type = typename BaseSet::key_type;
using transformed_mapped_type = std::invoke_result_t<Transform, key_type>;
using value_type = std::pair<key_type, transformed_mapped_type>;
private:
base_iterator base_it_;
Transform* transform_ptr_;
mutable value_type cached_value_;
public:
iterator() : base_it_(), transform_ptr_(nullptr) {}
iterator(base_iterator base_iterator, Transform* transform)
: base_it_(base_iterator), transform_ptr_(transform) {}
// On dereference, return a pair where the key is the set value and the mapped value
// is the result of applying the transform to the underlying value.
value_type operator*() const {
auto base_val = *base_it_;
return { base_val, (*transform_ptr_)(base_val) };
}
// operator-> uses a mutable cache to return a pointer to the current value.
value_type* operator->() const {
cached_value_ = **this;
return &cached_value_;
}
iterator& operator++() {
++base_it_;
return *this;
}
iterator operator++(int) {
iterator tmp(*this);
++(*this);
return tmp;
}
bool operator==(const iterator& other) const {
return base_it_ == other.base_it_;
}
bool operator!=(const iterator& other) const {
return !(*this == other);
}
};
iterator begin() {
return iterator(base_map_->begin(), &transform_);
}
iterator end() {
return iterator(base_map_->end(), &transform_);
}
iterator find(const key_type& key) {
return iterator(base_map_->find(key), &transform_);
}
size_t erase(const key_type& key) {
const size_t erased = base_map_->erase(key);
if (on_erase_) {
on_erase_(key);
}
return erased;
}
};