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
This commit is contained in:
Dion Moult
2026-09-16 09:03:06 +10:00
parent 336cb80394
commit 4b642c5e00
6 changed files with 161 additions and 54 deletions
+22 -5
View File
@@ -23,6 +23,7 @@
#ifdef IFOPSH_WITH_ROCKSDB
#include <rocksdb/db.h>
#include <rocksdb/options.h>
#include <rocksdb/write_batch.h>
#endif
#include <memory>
@@ -196,6 +197,27 @@ public:
return iterator();
}
// Removes the element: every key under prefix + key + "|". The prefix
// with its last byte incremented is the exclusive upper bound ('}'
// follows '|'). Returns 1 if the element existed, 0 otherwise.
size_t erase(const key_type& key) {
#ifdef IFOPSH_WITH_ROCKSDB
if (find(key) == end()) {
return 0;
}
const std::string lower_bound = prefix_ + key_to_string(key) + "|";
std::string upper_bound = lower_bound;
upper_bound.back() = '}';
rocksdb::WriteBatch batch;
batch.DeleteRange(lower_bound, upper_bound);
db_->Write(rocksdb::WriteOptions{}, &batch);
return 1;
#else
static_cast<void>(key);
return 0;
#endif
}
// Read-only find: returns an iterator to the element with the given key if it exists.
iterator find(const key_type& key) const {
#ifdef IFOPSH_WITH_ROCKSDB
@@ -216,11 +238,6 @@ public:
return end();
}
size_t erase(const key_type& key) {
static_cast<void>(key);
// @todo
return 0;
}
};
#endif