API Reference
Graph Models and Database
- class gestaltdb.graphdb.Edge(edge_id=None, source=None, target=None, properties=None)[source]
Bases:
objectDirected graph edge with source, target, and properties.
- Parameters:
edge_id – Optional stable edge identifier. A UUID is generated when omitted.
source – Source node ID or
Nodeinstance.target – Target node ID or
Nodeinstance.properties – Optional edge attributes. Typed traversal reads
properties["type"].
Examples
>>> Edge(edge_id="d1-p1", source="drug-1", target="protein-1").source 'drug-1'
- __init__(edge_id=None, source=None, target=None, properties=None)[source]
If no edge_id is provided, generate a UUID.
- property get_id
Unique identifier for this edge.
- property get_id_bytes
Return the edge ID encoded as UTF-8 bytes.
Examples
>>> Edge(edge_id="d1-p1").get_id_bytes b'd1-p1'
- property get_type
Return the typed traversal edge type.
Examples
>>> Edge(properties={"type": "drug-to-protein"}).get_type 'drug-to-protein'
- class gestaltdb.graphdb.GraphDB(store, serializer, indexed_node_properties=None, indexed_edge_properties=None)[source]
Bases:
objectHigh-level interface to manage Node/Edge storing, retrieval, and indexing.
- Parameters:
- __init__(store, serializer, indexed_node_properties=None, indexed_edge_properties=None)[source]
Initialize a graph database wrapper.
- Parameters:
store (KVStore) –
KVStoreinstance such asLMDBStore,LevelDBStore, orPyRexStore.serializer (Serializer) – Serializer for node, edge, and adjacency payloads.
indexed_node_properties (Optional[list[str]]) – Optional exact-match node property indexes to maintain for future writes.
indexed_edge_properties (Optional[list[str]]) – Optional exact-match edge property indexes to maintain for future writes.
Examples
>>> from gestaltdb.kvstores import LMDBStore >>> from gestaltdb.serializers import PickleSerializer >>> graph = GraphDB(LMDBStore(path="/tmp/example"), PickleSerializer(), indexed_node_properties=["name"])
- assert_claim(*, subject, predicate, object, polarity, agent, source=None, confidence=None, world='default', provenance=None, object_kind=ClaimObjectKind.ENTITY, valid=None, valid_from=None, valid_to=None, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append an immutable sourced assertion or denial.
- Return type:
- assert_world_accessibility(*, agent, from_world, to_world, kind, valid=None, valid_from=None, valid_to=None, source=None, provenance=None, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append a positive temporal accessibility fact for one agent frame.
- Return type:
- bfs(start_node_id, direction='any', edge_key_serializer=<function GraphDB.<lambda>>, node_key_serializer=<function GraphDB.<lambda>>)[source]
Returns a list of node_ids in BFS order starting from start_node_id. Demonstrates how adjacency is used for graph traversal.
- build_sampler_snapshot(output_path, *, temporal=False, system_time=None, time_bucket=None, source_db_reference=True, source_db_path=None, source_db_path_mode='relative', read_snapshot=None, **kwargs)[source]
Build a read-optimized array sampler snapshot from this graph.
- Parameters:
output_path – Directory where snapshot arrays and metadata are written.
temporal (bool) – Build temporal-history arrays from one pinned read view.
system_time – Optional system-time horizon for a temporal build.
time_bucket (str | None) – Temporal candidate-index bucket policy:
None,"none","hour", or"day".read_snapshot (bool | None) – Require or disable a verifiable mutable-state backend snapshot. The default uses one automatically when supported.
**kwargs – Options forwarded to
SamplerSnapshot.build.source_db_reference (bool)
source_db_path_mode (str)
- Returns:
A loaded
SamplerSnapshotinstance.
- claim_status(subject, predicate, object, *, valid_time, object_kind=ClaimObjectKind.ENTITY, agent=None, source=<object object>, world=None, system_time=None, through_commit=None)[source]
Return supported/refuted/both/unknown under open-world semantics.
- Return type:
- commit_versions(writes, *, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append one logical commit containing immutable node/edge versions.
- Return type:
- correct_claim(claim, *, supersedes_version_id, confidence=<object object>, provenance=<object object>, valid=None, valid_from=None, valid_to=None, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append corrected confidence/provenance for one claim identity.
- Return type:
- correct_edge_version(edge, *, supersedes_version_id, valid=None, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append a corrected edge payload while preserving the superseded version.
- Parameters:
- Return type:
- correct_node_version(node, *, supersedes_version_id, valid=None, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append a corrected node payload while preserving the superseded version.
- Parameters:
- Return type:
- count_edges_by_property(property_name, value)[source]
Return the number of edges currently indexed for an exact property value.
- count_edges_by_property_range(property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Return the number of edges indexed in a scalar property range.
- count_edges_by_type(edge_type)[source]
Return the number of edges currently indexed for a relationship type.
- count_edges_by_type_property(edge_type, property_name, value)[source]
Return the number of edges indexed for a type and exact property value.
- count_edges_by_type_property_range(edge_type, property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Return the number of edges indexed for a type/property range.
- count_nodes_by_label_property(label, property_name, value)[source]
Return the number of nodes indexed for a label and exact property value.
- count_nodes_by_label_property_range(label, property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Return the number of nodes indexed for a label/property range.
- count_nodes_by_property(property_name, value)[source]
Return the number of nodes currently indexed for an exact property value.
- count_nodes_by_property_range(property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Return the number of nodes indexed in a scalar property range.
- count_typed_adjacency(node_id, edge_type, direction='out')[source]
Count typed adjacency records without materializing traversal records.
- classmethod create(path, *, backend='pyrex', serializer='json', backend_options=None, indexed_node_properties=None, indexed_edge_properties=None, overwrite=False)[source]
Create a self-describing graph store and return an open handle.
- Return type:
- create_edge_property_index(property_name)[source]
Register and rebuild an exact-match edge property index.
- Parameters:
property_name (str) – Edge property to index for exact-match lookup.
- Returns:
Number of existing edges added to the index.
Examples
>>> graph_db.create_edge_property_index("score") 7
- create_node_property_index(property_name)[source]
Register and rebuild an exact-match node property index.
- Parameters:
property_name (str) – Node property to index for exact-match lookup.
- Returns:
Number of existing nodes added to the index.
Examples
>>> graph_db.create_node_property_index("kind") 10
- create_rule(rule_id, *, when, then, version_id=None)[source]
Validate and append one immutable version of a positive Horn rule.
- Return type:
- current_read_view(*, require_verifiable=False, capture_provenance=True)[source]
Yield current graph reads pinned to one backend snapshot.
- delete_edge(edge_id, edge_key_serializer=<function GraphDB.<lambda>>)[source]
Removes the edge from the edge store, and from adjacency of both source and target nodes. If either node doesn’t exist, we skip gracefully.
- Parameters:
edge_id (str)
- delete_node(node_id)[source]
Delete a node by byte key.
- Parameters:
node_id – Node ID bytes.
Examples
>>> graph_db.delete_node(b"drug-1")
- edge_key_to_bytes(edge_key)[source]
Normalize an edge key to bytes.
- Parameters:
edge_key – String or bytes edge key.
- Returns:
UTF-8 encoded bytes.
Examples
>>> GraphDB.edge_key_to_bytes(None, "d1-p1") b'd1-p1'
- edge_type(edge)[source]
Return the type used by typed traversal for an edge.
- Parameters:
edge (Edge) – Edge to inspect.
- Returns:
Edge type string, or
None.
Examples
>>> GraphDB.edge_type(None, Edge(properties={"type": "drug-to-protein"})) 'drug-to-protein'
- edges_by_edge_type(node_id, edge_type, direction='out')[source]
Return edge IDs connected by a specific edge type.
- Parameters:
- Returns:
List of edge ID bytes.
Examples
>>> graph_db.edges_by_edge_type("drug-1", "drug-to-protein")
- edges_by_property(property_name, value)[source]
Return edges using an exact-match property index.
- Parameters:
property_name (str) – Indexed edge property name.
value – Exact property value to match.
- Returns:
List of decoded
Edgeobjects.
Examples
>>> graph_db.edges_by_property("score", 1)
- edges_by_property_range(property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Return edges using a scalar property range index.
- edges_by_type(edge_type)[source]
Return edges using the relationship type catalog.
- Parameters:
edge_type (str) – Relationship type stored in
edge.properties["type"].- Returns:
List of decoded
Edgeobjects.
Examples
>>> graph_db.edges_by_type("drug-to-protein")
- edges_by_type_property(edge_type, property_name, value)[source]
Return edges using the composite type/property exact-match index.
- edges_by_type_property_range(edge_type, property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Return edges using a composite type/property range index.
- entails(agent, proposition, mode, *, world, valid_time, system_time=None, through_commit=None, max_depth=4, max_states=1000)[source]
Evaluate a bounded modal proposition under one pinned temporal view.
- explain_claim(claim_id, *, valid_time, system_time=None, max_depth=100, max_nodes=10000)[source]
Return a finite derivation graph for a visible historical claim.
- Return type:
ClaimExplanation | None
- get_adjacency_list(node_id, direction='forward', return_raw=False)[source]
Returns the list of edge IDs connected to node_id. If none found, returns an empty list.
- Parameters:
node_id (bytes) – a string representing the node_id
direction – ‘forward’, ‘backward’ or ‘any’ -> controls whether the source, target, or un-directed adjacency of the node will be returned.
return_raw – if this flag is true it will return the data as they are stored (e.g., a dictionary of ‘source’ and ‘target’ lists. )
- Return type:
- get_claim_as_of(claim_id, *, valid_time, system_time=None, through_commit=None)[source]
Resolve one claim identity at valid time and an optional system horizon.
- Return type:
ClaimVersion | None
- get_claim_version(version_id, *, system_time=None, through_commit=None)[source]
Return a visible epistemic claim version by UUID.
- Parameters:
version_id (str)
- Return type:
ClaimVersion | None
- get_edge(edge_id)[source]
Return an edge by byte key.
- Parameters:
edge_id – Edge ID bytes as stored in the backend.
- Returns:
The decoded edge, or
Nonewhen absent.- Return type:
Examples
>>> graph_db.get_edge(b"d1-p1")
- get_edge_as_of(logical_id, *, valid_time, system_time=None, through_commit=None)[source]
Resolve a logical edge at valid time and an optional system horizon.
- Return type:
EdgeVersion | None
- get_edge_version(version_id, *, system_time=None, through_commit=None)[source]
Return a visible edge version by UUID using the temporal index.
- Parameters:
version_id (str)
- Return type:
EdgeVersion | None
- get_node(node_id)[source]
Return a node by byte key.
- Parameters:
node_id – Node ID bytes as stored in the backend.
- Returns:
The decoded node, or
Nonewhen absent.- Return type:
Examples
>>> graph_db.get_node(b"drug-1")
- get_node_as_of(logical_id, *, valid_time, system_time=None, through_commit=None)[source]
Resolve a logical node at valid time and an optional system horizon.
- Return type:
NodeVersion | None
- get_node_keys_generator(num_nodes=None, key_offset=None)[source]
Yield node keys from the backing store.
- Parameters:
num_nodes – Optional maximum number of keys to yield.
key_offset – Optional starting key.
- Returns:
Generator of node key bytes.
Examples
>>> list(graph_db.get_node_keys_generator(num_nodes=10))
- get_node_version(version_id, *, system_time=None, through_commit=None)[source]
Return a visible node version by UUID using the temporal index.
- Parameters:
version_id (str)
- Return type:
NodeVersion | None
- get_nodes(node_ids)[source]
Use store.get_nodes_bulk(…) and deserialize each one. Return a list of Node (in the same order as node_ids, or possibly just all found).
- get_rule(rule_id, *, system_time=None)[source]
Return the latest version of a named rule at a system-time horizon.
- Return type:
RuleVersion | None
- get_temporal_commit(commit_id, *, _validate_supersession=True)[source]
Return a fully validated visible temporal commit.
- Parameters:
- Return type:
TemporalCommit | None
- get_typed_adjacency(node_id, edge_type, direction='out')[source]
Return typed adjacency records with clean direction semantics.
out means source -> target, in means target -> source, and any returns the union of both directions.
- ingest_arrow(node_ids, edge_ids, sources, targets, edge_types, *, ingestion_mode=ColumnarIngestionMode.ENTITY_COLUMNS, index_mode=IndexMaintenanceMode.DEFER_REBUILD, node_values=None, edge_values=None, labels=None, node_properties=None, edge_properties=None, native=True, chunk_size=100000, progress=False)[source]
Ingest a graph from Arrow-like columns with bulk-load defaults.
ENTITY_COLUMNSbuilds payloads from structured node/edge columns.SERIALIZED_PAYLOADSexpectsnode_valuesandedge_valuesto contain serializer-compatible payload bytes.Example
>>> graph.ingest_arrow(node_ids, edge_ids, sources, targets, edge_types, node_properties={"kind": kinds})
- ingest_edges_arrow(edge_ids, sources, targets, edge_types, edge_values, *, append_only=True, native=True, chunk_size=100000, index_mode='maintain', progress=False)[source]
Ingest typed edges from Arrow-like columns.
edge_valuesis required and must contain serialized edge payloads compatible with the currentGraphDBserializer. This ingestion path writes edge records and typed adjacency records only; it intentionally skips legacy adjacency blobs for append-friendly bulk loading.- Parameters:
edge_ids – Arrow-like or Python column of edge IDs.
sources – Arrow-like or Python column of source node IDs.
targets – Arrow-like or Python column of target node IDs.
edge_types – Arrow-like or Python column of typed traversal labels.
edge_values – Arrow-like or Python column of serialized edge bytes.
append_only (bool) – Columnar ingestion currently requires
True.native (bool) – Use native backend columnar ingestion when available.
chunk_size (int) – Maximum rows per backend write.
index_mode (str) –
"maintain"updates secondary indexes immediately;"defer"writes edge records and typed adjacency and marks secondary indexes stale.progress (bool)
- Returns:
Number of ingested edges.
- ingest_edges_arrow_entities(edge_ids, sources, targets, edge_types, *, properties=None, append_only=True, native=True, chunk_size=100000, index_mode='maintain', progress=False)[source]
Ingest edge entity columns from Arrow-like columns.
- ingest_edges_polars(df, *, edge_id='edge_id', source='source', target='target', edge_type='edge_type', edge_value='edge_value', append_only=True, native=True, chunk_size=100000, index_mode='maintain', progress=False)[source]
Ingest typed edges from a Polars DataFrame.
The
edge_valuecolumn is required and must contain serialized edge payload bytes compatible with the currentGraphDBserializer.
- ingest_edges_polars_entities(df, *, edge_id='edge_id', source='source', target='target', edge_type='edge_type', property_columns=None, append_only=True, native=True, chunk_size=100000, index_mode='maintain', progress=False)[source]
Ingest edge entity columns from a Polars DataFrame.
With
JSONSerializer, edge payloads are built with Polars struct JSON encoding. Other serializers fall back to PythonEdgeserialization.
- ingest_nodes_arrow(node_ids, node_values, *, native=True, chunk_size=100000, append_only=False, index_mode='maintain', progress=False)[source]
Ingest attributed nodes from Arrow-like columns.
node_valuesis required and must contain serialized node payloads compatible with the currentGraphDBserializer.- Parameters:
node_ids – Arrow-like or Python column of node IDs.
node_values – Arrow-like or Python column of serialized node bytes.
native (bool) – Use native backend columnar ingestion when available.
chunk_size (int) – Maximum rows per backend write.
append_only (bool) – Skip existing-node index deletion for known-new nodes.
index_mode (str) –
"maintain"updates secondary indexes immediately;"defer"writes node records and marks indexes stale.progress (bool)
- Returns:
Number of ingested nodes.
- ingest_nodes_arrow_entities(node_ids, *, labels=None, properties=None, native=True, chunk_size=100000, append_only=False, index_mode='maintain', progress=False)[source]
Ingest node entity columns from Arrow-like columns.
- ingest_nodes_polars(df, *, node_id='node_id', node_value='node_value', native=True, chunk_size=100000, append_only=False, index_mode='maintain', progress=False)[source]
Ingest attributed nodes from a Polars DataFrame.
The
node_valuecolumn is required and must contain serialized node payload bytes compatible with the currentGraphDBserializer.
- ingest_nodes_polars_entities(df, *, node_id='node_id', labels='labels', property_columns=None, native=True, chunk_size=100000, append_only=False, index_mode='maintain', progress=False)[source]
Ingest node entity columns from a Polars DataFrame.
With
JSONSerializer, node payloads are built with Polars struct JSON encoding. Other serializers fall back to PythonNodeserialization.
- ingest_polars(node_df, edge_df, *, ingestion_mode=ColumnarIngestionMode.ENTITY_COLUMNS, index_mode=IndexMaintenanceMode.DEFER_REBUILD, node_id='node_id', node_value='node_value', labels='labels', node_property_columns=None, edge_id='edge_id', source='source', target='target', edge_type='edge_type', edge_value='edge_value', edge_property_columns=None, native=True, chunk_size=100000, progress=False)[source]
Ingest a graph from Polars DataFrames with bulk-load defaults.
By default this uses structured entity columns, defers secondary index maintenance during ingestion, and immediately performs a one-pass rebuild before returning. Typed adjacency is maintained during ingestion, so traversal remains correct throughout.
Example
>>> graph.ingest_polars(nodes, edges, node_property_columns=["kind"], edge_property_columns=["score"])
- iter_claim_versions(claim_id=None, *, system_time=None, through_commit=None)[source]
Iterate claim versions, optionally restricted to one deterministic claim ID.
- iter_claims_as_of(*, valid_time, statement_id=None, subject=None, predicate=None, object=<object object>, object_kind=None, agent=None, source=<object object>, world=None, polarity=None, system_time=None, through_commit=None)[source]
Iterate visible claims matching indexed epistemic dimensions.
- iter_edge_ids(num_edges=None, key_offset=None)[source]
Yield all canonical edge IDs from the backing store.
- Parameters:
num_edges – Optional maximum number of edge IDs to yield.
key_offset – Optional starting key.
- Yields:
Edge ID bytes in backend key order.
Examples
>>> list(graph_db.iter_edge_ids()) [b'd1-p1']
- iter_edge_ids_by_property(property_name, value)[source]
Yield edge IDs from an exact-match edge property index.
- Parameters:
property_name (str) – Indexed edge property name.
value – Exact property value to match.
- Yields:
Edge ID bytes matching the property value.
Examples
>>> list(graph_db.iter_edge_ids_by_property("score", 1)) [b'e1']
- iter_edge_ids_by_property_range(property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Yield edge IDs from a scalar property range index.
- iter_edge_ids_by_type(edge_type)[source]
Yield edge IDs from the relationship type catalog.
- Parameters:
edge_type (str) – Relationship type stored in
edge.properties["type"].- Yields:
Edge ID bytes with the requested relationship type.
Examples
>>> list(graph_db.iter_edge_ids_by_type("drug-to-protein")) [b'd1-p1']
- iter_edge_ids_by_type_property(edge_type, property_name, value)[source]
Yield edge IDs from the composite type/property exact-match index.
- iter_edge_ids_by_type_property_range(edge_type, property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Yield edge IDs from a composite type/property range index.
- iter_edge_versions(logical_id=None, *, system_time=None, through_commit=None)[source]
Iterate edge versions, using the logical-history index when filtered.
- iter_edges_as_of(*, edge_type=None, direction='out', source=None, target=None, valid_time, system_time=None, through_commit=None)[source]
Iterate endpoint edges visible at valid time and a system horizon.
- iter_node_ids_by_label(label)[source]
Yield node IDs from the label index.
- Parameters:
label (str) – Node label to scan.
- Yields:
Node ID bytes with the requested label.
Examples
>>> list(graph_db.iter_node_ids_by_label("Drug")) [b'drug-1']
- iter_node_ids_by_label_property(label, property_name, value)[source]
Yield node IDs from the composite label/property exact-match index.
- iter_node_ids_by_label_property_range(label, property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Yield node IDs from a composite label/property range index.
- iter_node_ids_by_property(property_name, value)[source]
Yield node IDs from an exact-match property index.
- Parameters:
property_name (str) – Indexed node property name.
value – Exact property value to match.
- Yields:
Node ID bytes matching the property value.
Examples
>>> list(graph_db.iter_node_ids_by_property("kind", "drug")) [b'drug-1']
- iter_node_ids_by_property_range(property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Yield node IDs from a scalar property range index.
- iter_node_versions(logical_id=None, *, system_time=None, through_commit=None)[source]
Iterate node versions, using the logical-history index when filtered.
- iter_nodes_as_of(*, valid_time, system_time=None, through_commit=None)[source]
Iterate node versions visible at one valid and system-time point.
- iter_rule_versions(rule_id=None, *, system_time=None)[source]
Iterate immutable rule versions in catalog order at a system-time horizon.
- iter_temporal_commits(*, through_commit=None)[source]
Iterate visible commits in commit order, skipping incomplete commits.
- iter_typed_adjacency(node_id, edge_type, direction='out')[source]
Yield typed adjacency records with clean direction semantics.
- Parameters:
- Yields:
Typed adjacency records containing edge, neighbor, source, target, edge type, and concrete direction fields.
Examples
>>> graph_db.iter_typed_adjacency("drug-1", "drug-to-protein")
- key_to_string(key)[source]
Normalize a key to a string.
- Parameters:
key – String or UTF-8 bytes key.
- Returns:
String key.
Examples
>>> GraphDB.key_to_string(None, b"drug-1") 'drug-1'
- list_temporal_orphans(*, through_commit=None)[source]
List durable allocation gaps and incomplete unpublished commits.
- Return type:
TemporalOrphanReport
- maintain_truth(*, as_of=None, system_time=None, world=None, max_iterations=100, max_derivations=10000, max_justifications=100000, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Incrementally reconcile derived claims with the current rule fixpoint.
The authoritative base excludes claims emitted by the rule engine, so a cycle cannot preserve itself after its final independent support is removed. If
as_ofis omitted, the valid time from the most recent rule run or maintenance pass is resumed.- Return type:
- migrate_time_indexed_edges(*, delete_legacy=False, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append legacy
TimeIndexedEdgerecords as temporal edge history.Records are grouped by logical edge ID and ordered by timestamp. Each record is valid until the next timestamp for that ID; the final record has an open end. Existing equivalent assertions from an earlier run are skipped, making cleanup retryable after a completed or interrupted migration. Changed legacy input after a successful migration is rejected instead of creating overlapping history. When requested, legacy current-state records are deleted only after the temporal commit is visible.
- Returns:
Newly appended edge versions in migration order.
- Raises:
TemporalVersionError – If timestamps are duplicated or legacy input changed after an earlier migration.
TemporalCorruptionError – If a legacy record cannot be decoded or its timestamp-prefixed key does not match its payload.
- Parameters:
delete_legacy (bool)
- Return type:
tuple[EdgeVersion, …]
- neighbors_by_edge_type(node_id, edge_type, direction='out')[source]
Return neighbor IDs connected by a specific edge type.
- Parameters:
- Returns:
List of neighbor ID bytes.
Examples
>>> graph_db.neighbors_by_edge_type("drug-1", "drug-to-protein")
- node_key_to_bytes(node_key)[source]
Normalize a node key to bytes.
- Parameters:
node_key – String or bytes node key.
- Returns:
UTF-8 encoded bytes.
Examples
>>> GraphDB.node_key_to_bytes(None, "drug-1") b'drug-1'
- nodes_by_label(label)[source]
Return nodes with a label using the label index.
- Parameters:
label (str) – Node label to scan.
- Returns:
List of decoded
Nodeobjects.
Examples
>>> graph_db.nodes_by_label("Drug")
- nodes_by_label_property(label, property_name, value)[source]
Return nodes using the composite label/property exact-match index.
- nodes_by_label_property_range(label, property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Return nodes using a composite label/property range index.
- nodes_by_property(property_name, value)[source]
Return nodes using an exact-match property index.
- Parameters:
property_name (str) – Indexed node property name.
value – Exact property value to match.
- Returns:
List of decoded
Nodeobjects.
Examples
>>> graph_db.nodes_by_property("kind", "drug")
- nodes_by_property_range(property_name, start_value=None, end_value=None, include_start=True, include_end=True)[source]
Return nodes using a scalar property range index.
- classmethod open(path, *, backend_options=None, validate_manifest=True)[source]
Open a self-describing graph store from a directory.
- Return type:
- put_edge(edge, update_adjacency=True)[source]
Store an edge and update adjacency indexes.
- Parameters:
edge (Edge) – Edge to serialize and write.
update_adjacency – Whether to update the legacy untyped adjacency list.
Examples
>>> graph_db.put_edge(Edge(source="drug-1", target="protein-1"))
- put_edge_version(edge, *, valid, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append an immutable edge assertion without changing the current graph.
- Parameters:
edge (Edge)
- Return type:
- put_edges_bulk(edges, check_existing=True)[source]
Store multiple edges and update adjacency indexes in bulk.
- Parameters:
Examples
>>> graph_db.put_edges_bulk([Edge(source="drug-1", target="protein-1")], check_existing=False)
- put_node(node)[source]
Store a node.
- Parameters:
node (Node) – Node to serialize and write.
Examples
>>> graph_db.put_node(Node(node_id="drug-1"))
- put_node_version(node, *, valid, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append an immutable node assertion without changing the current graph.
- Parameters:
node (Node)
- Return type:
- put_nodes(nodes)[source]
Store multiple nodes and maintain label/property indexes.
Examples
>>> graph_db.put_nodes([Node(node_id="drug-1", labels=["Drug"])])
- query(cypher, parameters=None, *, read_snapshot=None)[source]
Execute a supported Cypher query.
Read queries run directly; queries with
CREATE/SET/REMOVErun insidetransaction()when the backend supports it and fall back to best-effort direct writes otherwise.- Parameters:
cypher (str) – Query text in the supported GestaltDB Cypher subset.
parameters (dict[str, object] | None) – Optional Cypher parameter values keyed without the leading
$.read_snapshot (bool | None) – Require or disable a unified mutable-state read snapshot. The default uses one automatically when supported.
- Returns:
gestaltdb.QueryResultcontaining projected records.
Examples
>>> graph_db.query('MATCH (n:Drug) RETURN n') >>> graph_db.query('MATCH (a {id: "drug-1"})-[:drug-to-protein]->(b) RETURN a, b')
- range_query_nodes(property_name, start_val, end_val)[source]
Example stub: You might rely on the underlying store to handle indexing for nodes.
- Parameters:
property_name (str)
- read_view(*, valid_time, system_time=None, through_commit=None)[source]
Yield a temporal read view pinned to one visible commit horizon.
- rebuild_deferred_indexes()[source]
Rebuild secondary indexes marked stale by deferred bulk ingestion.
- rebuild_edge_indexes(*, edge_types=True, properties=None, batch_size=100000)[source]
Rebuild requested edge secondary indexes in one edge scan.
- rebuild_edge_property_index(property_name)[source]
Rebuild an exact-match edge property index from stored edges.
- Parameters:
property_name (str) – Edge property to index.
- Returns:
Number of indexed edge records.
Examples
>>> graph_db.rebuild_edge_property_index("score") 7
- rebuild_label_index()[source]
Rebuild the node label index from stored nodes.
- Returns:
Number of label index entries written.
Examples
>>> graph_db.rebuild_label_index() 12
- rebuild_node_indexes(*, labels=True, properties=None, batch_size=100000)[source]
Rebuild requested node secondary indexes in one node scan.
- rebuild_node_property_index(property_name)[source]
Rebuild an exact-match node property index from stored nodes.
- Parameters:
property_name (str) – Node property to index.
- Returns:
Number of indexed node records.
Examples
>>> graph_db.rebuild_node_property_index("name") 3
- rebuild_relationship_type_index()[source]
Rebuild the relationship type catalog from stored edges.
- Returns:
Number of typed edge records indexed.
Examples
>>> graph_db.rebuild_relationship_type_index() 20
- rebuild_temporal_indexes(*, through_commit=None)[source]
Rebuild all derived temporal indexes from visible canonical history.
- rebuild_typed_adjacency()[source]
Rebuild typed adjacency indexes from stored edge records.
- Returns:
Number of typed edges indexed.
Examples
>>> graph_db.rebuild_typed_adjacency()
- reclaim_temporal_orphans(*, through_commit)[source]
Delete validated unpublished artifacts through an explicit commit horizon.
- Parameters:
through_commit (int)
- Return type:
TemporalOrphanReclaimResult
- retract_claim(claim_id, *, valid=None, valid_from=None, valid_to=None, supersedes_version_id=None, reason=None, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append a bitemporal retraction without deleting claim history.
- Return type:
- retract_edge_version(logical_id, *, valid=None, valid_from=None, supersedes_version_id=None, reason=None, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append an edge retraction over an explicit or inherited interval.
- Parameters:
logical_id (str)
- Return type:
- retract_node_version(logical_id, *, valid=None, valid_from=None, supersedes_version_id=None, reason=None, version_id=None, metadata=None, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Append a node retraction over an explicit or inherited interval.
- Parameters:
logical_id (str)
- Return type:
- run_rules(*, as_of, system_time=None, world=None, max_iterations=100, max_derivations=10000, max_justifications=100000, index_mode=IndexMaintenanceMode.MAINTAIN)[source]
Evaluate active rules semi-naively and persist a bounded fixpoint.
Only positive entity-object claims participate. Premises must share a world, and each conclusion receives their half-open validity intersection. No claims are written if any resource bound is exceeded.
- Return type:
- sample_neighbors(node_id, edge_type, direction='out', sample_size=10, rng=None)[source]
Sample typed neighbors using reservoir sampling.
- Parameters:
- Returns:
List of typed adjacency records.
Examples
>>> graph_db.sample_neighbors("drug-1", "drug-to-protein", sample_size=2)
- sample_typed_paths(seed_ids, pattern, rng=None)[source]
Sample paths that follow an ordered typed edge pattern.
- Parameters:
seed_ids – Starting node IDs as strings or bytes.
pattern (SamplingPattern | list[dict]) –
SamplingPatternor list of dictionaries such as{"edge_type": "drug-to-protein", "direction": "out", "sample_size": 2}.rng – Optional random number generator with
randrange.
- Returns:
List of dictionaries with
seedand sampledpathrecords.
Examples
>>> from gestaltdb.sampling import SamplingHop, SamplingPattern >>> pattern = SamplingPattern([SamplingHop("drug-to-protein", sample_size=2)]) >>> graph_db.sample_typed_paths(["drug-1"], pattern)
- sample_typed_subgraph(seed_ids, pattern, rng=None)[source]
Sample and materialize a typed subgraph around seed nodes.
- Parameters:
seed_ids – Starting node IDs as strings or bytes.
pattern (SamplingPattern | list[dict]) –
SamplingPatternor list of dictionary hop configs.rng – Optional random number generator with
randrange.
- Returns:
Dictionary with
nodes,edges, andpathsentries.
Examples
>>> pattern = [{"edge_type": "drug-to-protein", "direction": "out", "sample_size": 2}] >>> graph_db.sample_typed_subgraph(["drug-1"], pattern)
- transaction(**options)[source]
Run graph operations in a backend transaction when supported.
The transaction commits on clean context exit and rolls back if an exception leaves the context.
- update_edge(edge_id, new_data, merge_func)[source]
Similar approach for edges. The new_data might include new properties, or you might also allow changing source/target if that makes sense.
- update_node(node_id, new_data, merge_func)[source]
Fetch existing node (if any). If none found, treat as new or handle gracefully. merge_func(old_node_dict, new_data_dict) -> merged_properties (dict)
- visualize(cypher=None, *, seeds=None, pattern=None, parameters=None, options=None, rng=None)[source]
Build an offline interactive visualization of this graph.
The front-end bundle is prebuilt and packaged with the library, so this works with no JavaScript toolchain and no network access. The returned figure saves self-contained
.htmlartifacts and renders inline in Jupyter.- Parameters:
cypher – Optional Cypher query whose matched entities are visualized (with query matches highlighted).
seeds – Optional seed node IDs for typed-subgraph sampling; requires
pattern.pattern –
SamplingPatternor hop dicts used withseeds.parameters – Optional Cypher parameters for
cypher.options – Optional
gestaltdb.viz.api.VizOptionsor mapping.rng – Optional random generator for sampling mode.
- Returns:
gestaltdb.viz.api.VizFigure.
Examples
>>> figure = graph_db.visualize('MATCH (a:Person) RETURN a LIMIT 25') >>> figure.save("/tmp/people.html")
- class gestaltdb.graphdb.GraphEntityDictSerializer(serializer)[source]
Bases:
objectSerialize graph entities through a dictionary-compatible serializer.
- Parameters:
serializer (Serializer) – Serializer used for the final bytes conversion.
Examples
>>> from gestaltdb.serializers import JSONSerializer >>> s = GraphEntityDictSerializer(JSONSerializer()) >>> s.deserialize(s.serialize(Node(node_id="n1"), "Node"), "Node").get_id 'n1'
- __init__(serializer)[source]
Initialize the entity serializer wrapper.
- Parameters:
serializer (Serializer) – Serializer used to encode dictionaries as bytes.
- deserialize(val, entity_type)[source]
Deserializer (conditional on entity type)
- Parameters:
val – bytes containing the data
entity_type (str) – (str) is Edge, Node, AdjacencyList
- serialize(entity, entity_type)[source]
Serialize a graph entity by entity type.
- Parameters:
entity –
Node,Edge, or adjacency-list object.entity_type (str) – One of
"Node","Edge", or"AdjacencyList".
- Returns:
Serialized bytes.
Examples
>>> from gestaltdb.serializers import PickleSerializer >>> GraphEntityDictSerializer(PickleSerializer()).serialize(Node("n1"), "Node")[:1] b'\x80'
- class gestaltdb.graphdb.Node(node_id=None, properties=None, labels=None)[source]
Bases:
objectGraph node with an ID, native labels, and arbitrary properties.
- Parameters:
node_id – Optional stable node identifier. A UUID is generated when omitted.
properties – Optional dictionary of node attributes.
labels – Optional iterable of node labels. Labels are stored natively and maintained in the label index by
GraphDB.
Examples
>>> Node(node_id="drug-1", labels=["Drug"], properties={"kind": "drug"}).get_id 'drug-1' >>> Node(node_id="drug-1", labels=["Drug", "Drug"]).labels ('Drug',)
- __init__(node_id=None, properties=None, labels=None)[source]
Initialize a node, generating a UUID when
node_idis omitted.
- classmethod from_dict(data)[source]
Create a node from serialized dictionary data.
- Parameters:
data (dict) – Dictionary produced by
to_dict. Older dictionaries withoutlabelsdeserialize with an empty label tuple.- Returns:
Nodeinstance.
Examples
>>> Node.from_dict({"id": "n1", "properties": {}}).labels ()
- property get_id
Unique identifier for this node.
- property get_id_bytes
Return the node ID encoded as UTF-8 bytes.
Examples
>>> Node(node_id="drug-1").get_id_bytes b'drug-1'
- class gestaltdb.graphdb.TimeIndexedEdge(timestamp_dat, *args, **kwargs)[source]
Bases:
EdgeDeprecated edge whose byte key is prefixed by a timestamp.
This legacy current-state layout is not the immutable temporal storage model. Use
GraphDB.migrate_time_indexed_edges()to append equivalent edge history before adopting temporal reads or snapshots.- Parameters:
timestamp_dat – Datetime used as the sortable key prefix.
*args – Positional arguments passed to
Edge.**kwargs – Keyword arguments passed to
Edge.
Examples
>>> edge = TimeIndexedEdge(datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc), edge_id="e1") >>> edge.get_id_bytes.endswith(b':e1') True
- __init__(timestamp_dat, *args, **kwargs)[source]
Initialize a timestamp-prefixed edge.
- Parameters:
timestamp_dat – Datetime used as the sortable key prefix.
*args – Positional arguments passed to
Edge.**kwargs – Keyword arguments passed to
Edge.
- property get_id_bytes
Return timestamp-prefixed edge ID bytes.
Examples
>>> edge = TimeIndexedEdge(datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc), edge_id="e1") >>> edge.get_id_bytes.endswith(b':e1') True
- gestaltdb.graphdb.bytes_to_datetime(b, tzinfo=datetime.timezone.utc)[source]
Convert bytes produced by
datetime_to_bytesback to a datetime.- Parameters:
b (bytes) – Eight-byte timestamp generated by
datetime_to_bytes.tzinfo – Time zone used for the epoch reference.
- Returns:
Decoded datetime.
- Return type:
Examples
>>> bytes_to_datetime(b'\x00' * 8) datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
- gestaltdb.graphdb.datetime_to_bytes(dt, tzinfo=datetime.timezone.utc)[source]
Convert a datetime to big-endian microseconds since the Unix epoch.
- Parameters:
dt (datetime) – Datetime at or after 1970-01-01.
tzinfo – Time zone used for the epoch reference.
- Returns:
Eight bytes containing the timestamp as an unsigned integer.
- Return type:
Examples
>>> datetime_to_bytes(datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)) b'\x00\x00\x00\x00\x00\x00\x00\x00'
Temporal Semantics
Canonical temporal values shared by future graph and sampling APIs.
This module defines storage-independent temporal semantics. It does not make
GraphDB records or sampler snapshots temporal by itself.
- class gestaltdb.temporal.TemporalContext(instant=None, interval=None)[source]
Bases:
objectA point-in-time or finite-window temporal selection.
- Parameters:
instant (TemporalInstant | None)
interval (TemporalInterval | None)
- matches(value)[source]
Test a point event or validity interval against this selection.
- Parameters:
value (TemporalInterval | TemporalInstant | datetime | int)
- Return type:
- class gestaltdb.temporal.TemporalDate(value)[source]
Bases:
objectA calendar date without time or timezone.
- Parameters:
value (date)
- class gestaltdb.temporal.TemporalDuration(total_microseconds)[source]
Bases:
objectAn exact signed duration represented as total microseconds.
- Parameters:
total_microseconds (int)
- class gestaltdb.temporal.TemporalInstant(epoch_microseconds)[source]
Bases:
objectAn exact UTC instant represented as signed epoch microseconds.
- Parameters:
epoch_microseconds (int)
- classmethod decode_sortable(value)[source]
Decode the eight-byte chronological encoding from
encode_sortable.- Parameters:
value (bytes)
- Return type:
- encode_sortable()[source]
Encode the instant so lexicographic byte order is chronological.
- Return type:
- class gestaltdb.temporal.TemporalInterval(start, end=None)[source]
Bases:
objectA non-empty half-open interval
[start, end).end=Nonedenotes positive infinity.- Parameters:
start (TemporalInstant)
end (TemporalInstant | None)
- intersection(other)[source]
Return the non-empty intersection, or
Nonefor disjoint intervals.- Parameters:
other (TemporalInterval)
- Return type:
TemporalInterval | None
- class gestaltdb.temporal.TemporalLocalDateTime(value)[source]
Bases:
objectA timezone-free local date and time.
- Parameters:
value (datetime)
- class gestaltdb.temporal.TemporalLocalTime(microseconds)[source]
Bases:
objectA timezone-free time represented as microseconds since midnight.
- Parameters:
microseconds (int)
- class gestaltdb.temporal.TemporalTime(local_microseconds, offset_seconds)[source]
Bases:
objectA fixed-offset time of day.
- gestaltdb.temporal.as_temporal_context(value)[source]
Normalize a context or adapt an instant-like value to an as-of context.
- Parameters:
value (TemporalContext | TemporalInstant | datetime | int)
- Return type:
- gestaltdb.temporal.as_temporal_instant(value)[source]
Normalize an instant, aware datetime, or epoch-microsecond integer.
- Parameters:
value (TemporalInstant | datetime | int)
- Return type:
- gestaltdb.temporal.as_temporal_interval(value)[source]
Normalize an interval or a
(start, end)pair.- Parameters:
value (TemporalInterval | Tuple[TemporalInstant | datetime | int, TemporalInstant | datetime | int | None])
- Return type:
Temporal Version History
Immutable temporal version records for GestaltDB.
The models in this module describe append-only history. Current-state graph records, temporal indexes, and as-of traversal are separate concerns.
- class gestaltdb.versioning.ClaimVersion(version_id, logical_id, valid, commit_id, commit_ordinal, system_time, operation, payload_hash, supersedes_version_id=None, reason=None, claim=None)[source]
Bases:
TemporalVersionMetadataAn immutable metadata envelope with an optional epistemic claim.
- Parameters:
version_id (str)
logical_id (str)
valid (TemporalInterval)
commit_id (int)
commit_ordinal (int)
system_time (TemporalInstant)
operation (VersionOperation)
payload_hash (str)
supersedes_version_id (Optional[str])
reason (Optional[str])
claim (Optional['Claim'])
- class gestaltdb.versioning.ClaimVersionWrite(operation, logical_id, valid, claim=None, version_id=None, supersedes_version_id=None, reason=None)[source]
Bases:
objectValidated input describing an epistemic claim-version operation.
- Parameters:
operation (VersionOperation)
logical_id (str)
valid (Optional[TemporalInterval])
claim (Optional['Claim'])
version_id (Optional[str])
supersedes_version_id (Optional[str])
reason (Optional[str])
- class gestaltdb.versioning.EdgeVersion(version_id, logical_id, valid, commit_id, commit_ordinal, system_time, operation, payload_hash, supersedes_version_id=None, reason=None, edge=None)[source]
Bases:
TemporalVersionMetadataAn immutable metadata envelope with an optional edge payload.
- Parameters:
version_id (str)
logical_id (str)
valid (TemporalInterval)
commit_id (int)
commit_ordinal (int)
system_time (TemporalInstant)
operation (VersionOperation)
payload_hash (str)
supersedes_version_id (Optional[str])
reason (Optional[str])
edge (Optional['Edge'])
- class gestaltdb.versioning.EdgeVersionWrite(operation, logical_id, valid, edge=None, version_id=None, supersedes_version_id=None, reason=None)[source]
Bases:
objectValidated input describing an edge-version operation.
- Parameters:
operation (VersionOperation)
logical_id (str)
valid (Optional[TemporalInterval])
edge (Optional['Edge'])
version_id (Optional[str])
supersedes_version_id (Optional[str])
reason (Optional[str])
- class gestaltdb.versioning.NodeVersion(version_id, logical_id, valid, commit_id, commit_ordinal, system_time, operation, payload_hash, supersedes_version_id=None, reason=None, node=None)[source]
Bases:
TemporalVersionMetadataAn immutable metadata envelope with an optional node payload.
- Parameters:
version_id (str)
logical_id (str)
valid (TemporalInterval)
commit_id (int)
commit_ordinal (int)
system_time (TemporalInstant)
operation (VersionOperation)
payload_hash (str)
supersedes_version_id (Optional[str])
reason (Optional[str])
node (Optional['Node'])
- class gestaltdb.versioning.NodeVersionWrite(operation, logical_id, valid, node=None, version_id=None, supersedes_version_id=None, reason=None)[source]
Bases:
objectValidated input describing a node-version operation.
- Parameters:
operation (VersionOperation)
logical_id (str)
valid (Optional[TemporalInterval])
node (Optional['Node'])
version_id (Optional[str])
supersedes_version_id (Optional[str])
reason (Optional[str])
- class gestaltdb.versioning.TemporalCommit(commit_id, system_time, metadata, versions)[source]
Bases:
objectOne visible temporal commit and its ordered entity versions.
- Parameters:
commit_id (int)
system_time (TemporalInstant)
versions (Tuple[NodeVersion | EdgeVersion | ClaimVersion, ...])
- exception gestaltdb.versioning.TemporalCorruptionError[source]
Bases:
TemporalVersionErrorRaised when marker-backed temporal history fails integrity checks.
- exception gestaltdb.versioning.TemporalVersionError[source]
Bases:
ValueErrorBase error for invalid temporal version operations.
- class gestaltdb.versioning.TemporalVersionMetadata(version_id, logical_id, valid, commit_id, commit_ordinal, system_time, operation, payload_hash, supersedes_version_id=None, reason=None)[source]
Bases:
objectMetadata common to node and edge versions.
- Parameters:
version_id (str)
logical_id (str)
valid (TemporalInterval)
commit_id (int)
commit_ordinal (int)
system_time (TemporalInstant)
operation (VersionOperation)
payload_hash (str)
supersedes_version_id (str | None)
reason (str | None)
Epistemic Claims
Immutable epistemic claim values and open-world status semantics.
- class gestaltdb.epistemic.Claim(subject, predicate, object, polarity, agent, source=None, confidence=None, world='default', provenance=<factory>, object_kind=ClaimObjectKind.ENTITY)[source]
Bases:
objectA sourced assertion or denial, independent of temporal version metadata.
statement_ididentifies only the subject-predicate-object proposition.claim_idadditionally identifies the asserting context (agent, source, world, and polarity), allowing contradictory claims to coexist.- Parameters:
- class gestaltdb.epistemic.ClaimPolarity(*values)[source]
-
Whether an agent asserts or denies a proposition.
- class gestaltdb.epistemic.ClaimStatus(*values)[source]
-
Open-world four-valued status of a proposition at a read view.
Positive Horn Rules
Immutable positive Horn rules and rule-evaluation result values.
- class gestaltdb.rules.ClaimExplanation(claim_id, root_version_id, valid_time, system_time, nodes, edges, truncated=False)[source]
Bases:
objectA finite historical explanation rooted at one visible claim version.
- Parameters:
claim_id (str)
root_version_id (str)
valid_time (TemporalInstant)
system_time (TemporalInstant)
nodes (tuple[ExplanationNode, ...])
edges (tuple[ExplanationEdge, ...])
truncated (bool)
- class gestaltdb.rules.ExplanationEdge(source_id, target_id, relationship, premise_ordinal=None)[source]
Bases:
objectA directed derivation or premise edge in an explanation graph.
- class gestaltdb.rules.ExplanationNode(node_id, kind, value)[source]
Bases:
objectOne immutable claim or rule version in an explanation graph.
- class gestaltdb.rules.RuleAtom(subject, predicate, object)[source]
Bases:
objectOne
(subject, predicate, object)atom in a positive Horn rule.
- exception gestaltdb.rules.RuleError[source]
Bases:
TemporalVersionErrorBase error for invalid rule catalogs and evaluations.
- exception gestaltdb.rules.RuleEvaluationLimitError[source]
Bases:
RuleErrorRaised before persistence when a rule run exceeds a resource bound.
- class gestaltdb.rules.RuleJustification(rule_version_id, premise_version_ids)[source]
Bases:
objectA rule version and the ordered claim versions that satisfied its body.
- class gestaltdb.rules.RuleRunResult(iterations, derived_count, justification_count, commit_id, versions)[source]
Bases:
objectSummary of one bounded rule run and its persisted claim versions.
- class gestaltdb.rules.RuleVersion(rule_id, version_id, when, then, system_time, catalog_ordinal)[source]
Bases:
objectOne immutable system-time version of a named safe positive Horn rule.
Modal Evaluation
Bounded four-valued modal evaluation over temporal epistemic claims.
- class gestaltdb.modal.AccessibilityKind(*values)[source]
-
Independent accessibility frames used by modal operators.
- class gestaltdb.modal.ModalEntailmentResult(status, confidence, explanation)[source]
Bases:
objectFour-valued result and deterministic finite evaluation evidence.
- Parameters:
status (ClaimStatus)
confidence (float | None)
- exception gestaltdb.modal.ModalError[source]
Bases:
TemporalVersionErrorBase error for invalid modal formulas and evaluations.
- exception gestaltdb.modal.ModalEvaluationLimitError[source]
Bases:
ModalErrorRaised when a bounded modal evaluation exhausts a configured limit.
- class gestaltdb.modal.ModalExpression(operator, subject=None, predicate=None, object=None, object_kind=ClaimObjectKind.ENTITY, agent=None, operands=())[source]
Bases:
objectA validated finite atom, connective, or nested modal expression.
Mapping forms use
subject/predicate/objectfor atoms,formulafor unary operators, andoperandsforAND/OR. Every modal operator requires an explicitagent.- Parameters:
operator (ModalOperator)
subject (str | None)
predicate (str | None)
object (object)
object_kind (ClaimObjectKind)
agent (str | None)
operands (tuple[ModalExpression, ...])
- class gestaltdb.modal.ModalOperator(*values)[source]
-
Operators accepted by
ModalExpression.
Consistent Read Views
Consistent temporal read views and reproducible source provenance.
- class gestaltdb.readview.CurrentGraphReadView(graph, provenance)[source]
Bases:
objectGraph facade whose current-state reads share one backend snapshot.
- Parameters:
provenance (CurrentReadProvenance | None)
- class gestaltdb.readview.CurrentReadProvenance(database_id, snapshot_sequence, backend_name, backend_layout_version, serializer_name, serializer_format_version, backend_snapshot_kind, state_sha256, snapshot_kind='mutable_backend_snapshot', format_version=1)[source]
Bases:
objectIdentity of one verifiable mutable-state backend snapshot.
- Parameters:
- verify_snapshot_store(view)[source]
Verify a provenance-free view is pinned to the captured sequence.
- Return type:
None
- class gestaltdb.readview.GraphReadView(graph, provenance)[source]
Bases:
objectA fixed temporal commit horizon and default valid-time instant.
- Parameters:
provenance (ReadViewProvenance)
- exception gestaltdb.readview.ProvenanceMismatchError[source]
Bases:
ValueErrorRaised when a source graph does not match captured read provenance.
- class gestaltdb.readview.ReadViewProvenance(database_id, commit_horizon, commit_system_time_us, commit_marker_sha256, visibility_sha256, valid_time_us, backend_name, backend_layout_version, serializer_name, serializer_format_version, snapshot_kind='commit_visibility', format_version=1)[source]
Bases:
objectJSON-serializable identity of one immutable temporal graph view.
- Parameters:
- classmethod from_dict(value)[source]
Parse and authenticate a provenance mapping.
- Parameters:
value (dict)
- Return type:
Sampling Configuration
Sampling APIs for GestaltDB.
This package preserves the original typed traversal configuration objects while adding array-native sampler snapshot and engine primitives.
- class gestaltdb.sampling.SamplingHop(edge_type, direction='out', sample_size=10)[source]
Bases:
objectConfiguration for one typed sampling hop.
- class gestaltdb.sampling.SamplingPattern(hops)[source]
Bases:
objectOrdered typed sampling pattern.
- Parameters:
hops (Sequence[SamplingHop | Mapping[str, object]])
- gestaltdb.sampling.as_sampling_hop(hop)[source]
Normalize a hop configuration to
SamplingHop.- Parameters:
hop (SamplingHop | Mapping[str, object])
- Return type:
- gestaltdb.sampling.as_sampling_pattern(pattern)[source]
Normalize a sampling pattern to
SamplingPattern.- Parameters:
pattern (SamplingPattern | Iterable[SamplingHop | Mapping[str, object]])
- Return type:
Sampling Engine
Array-native sampler engine.
- class gestaltdb.sampling.engine.SampledNeighbors(input_nodes, edge_indices, neighbor_nodes, offsets, edge_version_ids=None, valid_from_us=None, valid_to_us=None, valid_to_open=None)[source]
Bases:
objectBatched neighbor-sampling result.
- Parameters:
input_nodes (ndarray)
edge_indices (ndarray)
neighbor_nodes (ndarray)
offsets (ndarray)
edge_version_ids (ndarray | None)
valid_from_us (ndarray | None)
valid_to_us (ndarray | None)
valid_to_open (ndarray | None)
- input_nodes
Input compact node IDs, one per requested seed node.
- Type:
numpy.ndarray
- edge_indices
Sampled global compact edge IDs concatenated across all input nodes.
- Type:
numpy.ndarray
- neighbor_nodes
Neighbor compact node IDs aligned with
edge_indices.- Type:
numpy.ndarray
- offsets
Prefix-sum offsets into
edge_indicesandneighbor_nodes. Samples forinput_nodes[i]are inoffsets[i]:offsets[i + 1].- Type:
numpy.ndarray
- edge_version_ids
Sampled edge-version IDs for temporal snapshots.
- Type:
numpy.ndarray | None
- valid_from_us
Inclusive valid starts aligned with sampled edges.
- Type:
numpy.ndarray | None
- valid_to_us
Exclusive valid ends, with zero for open intervals.
- Type:
numpy.ndarray | None
- valid_to_open
Open-end masks aligned with sampled edges.
- Type:
numpy.ndarray | None
Examples
Iterate over sampled neighbors for each input node:
result = engine.sample_neighbors([0, 1], fanout=5) for row, node in enumerate(result.input_nodes): start, end = result.offsets[row], result.offsets[row + 1] neighbors = result.neighbor_nodes[start:end]
- class gestaltdb.sampling.engine.SamplerEngine(snapshot, *, seed=None)[source]
Bases:
objectHigh-throughput sampler over a static
SamplerSnapshot.The engine samples compact integer arrays instead of materializing GraphDB
Node/Edgeobjects. It supports direction-aware traversal, relation-aware traversal, multihop subgraph sampling, exact positive triple checks, and configurable hard-negative generation.- Parameters:
snapshot (SamplerSnapshot) – Loaded sampler snapshot.
seed (int | None) – Optional NumPy RNG seed for reproducible sampling.
Examples
Load a snapshot and sample two-hop neighborhoods around seed edges:
engine = SamplerEngine.load("data/sampler", mode="memmap", seed=7) batch = engine.sample_subgraph([10, 11], fanouts=[15, 10]) arrays = batch.to_numpy()
- is_positive(src, rel, dst, *, temporal=None, policy='any_time', window_policy='overlap')[source]
Return whether a triple is a known positive in the snapshot.
- classmethod load(path, *, mode='ram', seed=None, **_kwargs)[source]
Load a sampler engine from a snapshot path.
- Parameters:
- Returns:
Initialized
SamplerEngine.- Raises:
ValueError – If
modeis not"ram"or"memmap".- Return type:
Examples
Load a RAM-backed engine:
engine = SamplerEngine.load("snapshot", mode="ram", seed=13)
- sample_hard_negatives(positive_triples, context_nodes=None, config=None, *, positive_times_us=None, temporal=None, return_diagnostics=False)[source]
Generate grouped hard negatives for positive triples.
- Parameters:
positive_triples (Sequence[Sequence[int]] | ndarray) – Array-like positive triples shaped
(n, 3)using compact global IDs.context_nodes (Iterable[int] | None) – Optional compact node IDs from the sampled context. Context-aware negative sources draw candidates from this set.
config (HardNegativeConfig | None) – Negative sampling configuration. Defaults to
HardNegativeConfig().positive_times_us (Sequence[int] | ndarray | None) – Optional example instants aligned with positives. Required by example-time policies and by leakage-safe candidate filtering unless
temporalprovides the needed context.temporal (TemporalContext | None) – Optional point/window used by temporal positive rejection.
return_diagnostics (bool) – Return
(negatives, counters)when true.
- Returns:
Array shaped
(num_positives, negatives_per_positive, 3)using compact global IDs, optionally paired with rejection counters.- Raises:
ValueError – If
positive_triplesis not shaped(n, 3).RuntimeError – If a valid non-positive negative cannot be found within the configured retry budget.
- Return type:
- sample_multihop(seeds, fanouts, *, direction='any', relations=None, temporal=None, window_policy='overlap', causal_policy='none')[source]
Sample a merged multihop subgraph around seed nodes.
- Parameters:
seeds (Sequence[int] | ndarray) – Compact node IDs used as the initial frontier.
fanouts (Sequence[int]) – Fanout per hop.
[15, 10]performs two hops.direction (str) – Traversal direction for all hops.
relations (Sequence[int] | ndarray | None) – Optional relation filter applied to all hops.
temporal (TemporalContext | None) – Optional point or finite-window validity filter.
window_policy (str) – Window matching policy,
"overlap"or"contained".causal_policy (str) – Valid-start policy across each sampled path:
"none","nondecreasing", or"nonincreasing".
- Returns:
SampledSubgraphBatchcontaining one merged sampled graph.- Return type:
Examples
Sample a two-hop undirected context:
batch = engine.sample_multihop([u, v], [15, 10], direction="any")
- sample_neighbors(nodes, fanout, *, direction='out', relations=None, replace=False, temporal=None, window_policy='overlap', causal_policy='none')[source]
Sample neighbors for each input node.
- Parameters:
nodes (Sequence[int] | ndarray) – Compact node IDs to sample from.
fanout (int) – Maximum number of edges sampled per node.
direction (str) –
"out"for source-to-target,"in"for target-to-source, or"any"for incident traversal.relations (Sequence[int] | ndarray | None) – Optional compact relation IDs to restrict traversal.
replace (bool) – Whether to sample with replacement.
temporal (TemporalContext | None) – Optional point or finite-window validity filter. Requires a temporal format-v2 snapshot.
window_policy (str) –
"overlap"selects intervals intersecting a window;"contained"selects intervals fully inside it.causal_policy (str) – Accepted for API consistency. It constrains valid starts across hops in multihop and subgraph sampling.
- Returns:
SampledNeighborswith concatenated edge and neighbor arrays.- Raises:
ValueError – If
fanoutis negative ordirectionis invalid.- Return type:
Examples
Relation-aware neighbor sampling:
sampled = engine.sample_neighbors( [drug_id], fanout=10, direction="out", relations=[binds_id] )
- sample_subgraph(seed_edges, fanouts, *, direction='any', relations=None, negative_config=None, temporal=None, window_policy='overlap', causal_policy='none')[source]
Sample a merged training subgraph around positive seed edges.
- Parameters:
seed_edges (Sequence[int] | ndarray) – Compact edge IDs treated as positive triples.
fanouts (Sequence[int]) – Fanout per hop around the positive edge endpoints.
direction (str) – Traversal direction for context expansion.
relations (Sequence[int] | ndarray | None) – Optional relation filter for context expansion.
negative_config (HardNegativeConfig | None) – Optional hard-negative configuration. When provided, negatives are returned as
(positives, negatives_per_positive, 3)local triples.temporal (TemporalContext | None) – Optional point or finite-window validity filter applied to seed and context edges.
window_policy (str) – Window matching policy,
"overlap"or"contained".causal_policy (str) – Valid-start policy propagated from each seed edge through context hops.
- Returns:
SampledSubgraphBatchwith local node IDs in positive and negative triples.- Return type:
Examples
Sample positives with hard negatives:
batch = engine.sample_subgraph( seed_edges, fanouts=[12, 8], negative_config=HardNegativeConfig(negatives_per_positive=8), )
Sampling Snapshots
Sampler snapshot build/load support.
- class gestaltdb.sampling.snapshot.SamplerSnapshot(path, metadata, external_node_ids, node_type_ids, external_edge_ids, external_relation_ids, relation_src_type_ids, relation_dst_type_ids, src_int, dst_int, rel_int, out, in_, incident, relation_out, relation_in, positive_triples, edge_version_ids=None, valid_from_us=None, valid_to_us=None, valid_to_open=None, edge_system_time_us=None, edge_commit_ids=None, temporal_out=None, temporal_in=None, positive_history_triples=None, positive_history_indptr=None, positive_history_edge_indices=None, node_history_indptr=None, node_valid_from_us=None, node_valid_to_us=None, node_valid_to_open=None)[source]
Bases:
objectRead-optimized array snapshot used by
SamplerEngine.A sampler snapshot is a derived, immutable index over a graph. It stores compact integer node IDs, compact integer relation IDs, edge endpoint arrays, CSR adjacency indexes, and exact positive triples. The durable graph database remains the source of truth; this object is optimized for training-time randomized sampling.
- Parameters:
path (Path)
metadata (dict)
external_node_ids (ndarray)
node_type_ids (ndarray)
external_edge_ids (ndarray)
external_relation_ids (ndarray)
relation_src_type_ids (ndarray)
relation_dst_type_ids (ndarray)
src_int (ndarray)
dst_int (ndarray)
rel_int (ndarray)
out (CSRAdjacency)
in_ (CSRAdjacency)
incident (CSRAdjacency)
relation_out (CSRAdjacency)
relation_in (CSRAdjacency)
positive_triples (ndarray)
edge_version_ids (ndarray | None)
valid_from_us (ndarray | None)
valid_to_us (ndarray | None)
valid_to_open (ndarray | None)
edge_system_time_us (ndarray | None)
edge_commit_ids (ndarray | None)
temporal_out (CSRAdjacency | None)
temporal_in (CSRAdjacency | None)
positive_history_triples (ndarray | None)
positive_history_indptr (ndarray | None)
positive_history_edge_indices (ndarray | None)
node_history_indptr (ndarray | None)
node_valid_from_us (ndarray | None)
node_valid_to_us (ndarray | None)
node_valid_to_open (ndarray | None)
- path
Directory containing the persisted snapshot arrays and metadata.
- Type:
- external_node_ids
External node identifiers ordered by compact node ID.
- Type:
numpy.ndarray
- node_type_ids
Integer node type ID per compact node, or
-1when unknown.- Type:
numpy.ndarray
- external_edge_ids
External edge identifiers ordered by compact edge ID.
- Type:
numpy.ndarray
- external_relation_ids
External relation identifiers ordered by compact relation ID.
- Type:
numpy.ndarray
- relation_src_type_ids
Expected source node type ID per relation, or
-1when the source type is heterogeneous or unknown.- Type:
numpy.ndarray
- relation_dst_type_ids
Expected target node type ID per relation, or
-1when the target type is heterogeneous or unknown.- Type:
numpy.ndarray
- src_int
Source compact node ID for each edge.
- Type:
numpy.ndarray
- dst_int
Target compact node ID for each edge.
- Type:
numpy.ndarray
- rel_int
Relation compact ID for each edge.
- Type:
numpy.ndarray
- out
Source-to-edge CSR adjacency.
- Type:
gestaltdb.sampling.adjacency.CSRAdjacency
- in_
Target-to-edge CSR adjacency.
- Type:
gestaltdb.sampling.adjacency.CSRAdjacency
- incident
Undirected incident edge CSR adjacency.
- Type:
gestaltdb.sampling.adjacency.CSRAdjacency
- relation_out
Relation-grouped source-to-edge CSR adjacency using keys
node_id * num_relations + relation_id.- Type:
gestaltdb.sampling.adjacency.CSRAdjacency
- relation_in
Relation-grouped target-to-edge CSR adjacency using keys
node_id * num_relations + relation_id.- Type:
gestaltdb.sampling.adjacency.CSRAdjacency
- positive_triples
Array of
(src, rel, dst)positives.- Type:
numpy.ndarray
- edge_version_ids
Temporal edge-version UUIDs aligned with edge arrays, or
Nonefor non-temporal and legacy v1 snapshots.- Type:
numpy.ndarray | None
- valid_from_us
Inclusive valid-time starts in UTC epoch microseconds.
- Type:
numpy.ndarray | None
- valid_to_us
Exclusive valid-time ends; open ends use zero together with
valid_to_open.- Type:
numpy.ndarray | None
- temporal_out
Source/relation CSR ordered by valid start.
- Type:
gestaltdb.sampling.adjacency.CSRAdjacency | None
- temporal_in
Target/relation CSR ordered by valid start.
- Type:
gestaltdb.sampling.adjacency.CSRAdjacency | None
- positive_history_triples
Unique temporal positive triples.
- Type:
numpy.ndarray | None
- positive_history_indptr
Group offsets into positive history edge rows.
- Type:
numpy.ndarray | None
- positive_history_edge_indices
Edge rows ordered by triple and valid start.
- Type:
numpy.ndarray | None
- node_history_indptr
Node offsets into availability intervals.
- Type:
numpy.ndarray | None
- node_valid_from_us
Node availability interval starts.
- Type:
numpy.ndarray | None
- node_valid_to_us
Node availability interval ends.
- Type:
numpy.ndarray | None
- node_valid_to_open
Node availability open-end masks.
- Type:
numpy.ndarray | None
Examples
Build from already compact arrays and sample with
SamplerEngine:snapshot = SamplerSnapshot.from_edge_arrays( "snapshot", external_node_ids=["drug-1", "protein-1"], external_edge_ids=["edge-1"], external_relation_ids=["binds"], src_int=[0], dst_int=[1], rel_int=[0], node_type_values=["drug", "protein"], )
- classmethod build(graph, output_path, *, node_filter=None, edge_filter=None, edge_type_property='type', node_type_property='kind', directed=True, include_reverse=True, reverse_relation_policy='adjacency_only', storage='npy', layout='csr', source_db=None, source_artifacts=None, source_provenance=None)[source]
Build and persist a static sampler snapshot from a
GraphDB.- Parameters:
graph – GraphDB-like object exposing
store.get_node_keys_generator,store.get_edge_keys_generator,get_node,get_edge, andkey_to_string.output_path (str | Path) – Directory where snapshot metadata and
.npyarrays are written.node_filter (Callable[[object], bool] | None) – Optional predicate receiving each node object. Nodes for which the predicate returns
Falseare omitted.edge_filter (Callable[[object], bool] | None) – Optional predicate receiving each edge object. Edges for which the predicate returns
Falseare omitted.edge_type_property (str) – Edge property containing the relation identifier.
node_type_property (str) – Node property containing the semantic node type.
directed (bool) – Stored in metadata for consumers; current adjacency arrays preserve directed source/target endpoints and also build an incident view.
include_reverse (bool) – Stored in metadata for consumers. Reverse synthetic relations are not currently materialized.
reverse_relation_policy (str) – Metadata policy for reverse traversal. Must be
"adjacency_only","synthetic_relations", or"none".storage (str) – Metadata storage hint. Arrays are currently written as
.npyfiles.layout (str) – Adjacency layout. Only
"csr"is currently supported.source_db (dict | None)
source_artifacts (dict | None)
source_provenance (dict | None)
- Returns:
Loaded
SamplerSnapshotpointing atoutput_path.- Raises:
ValueError – If
reverse_relation_policyorlayoutis invalid.- Return type:
Examples
Build a snapshot from a graph database:
snapshot = graph.build_sampler_snapshot( "data/sampler", edge_type_property="type", node_type_property="kind", )
- classmethod build_temporal(read_view, output_path, *, node_filter=None, edge_filter=None, edge_type_property='type', node_type_property='kind', time_bucket='day', source_db=None, **metadata_options)[source]
Build a deterministic temporal-history snapshot from one read view.
- classmethod from_arrays(output_path, *, external_node_ids, node_type_ids, external_edge_ids, external_relation_ids, src_int, dst_int, rel_int, relation_src_type_ids=None, relation_dst_type_ids=None, edge_version_ids=None, valid_from_us=None, valid_to_us=None, valid_to_open=None, edge_system_time_us=None, edge_commit_ids=None, node_intervals=None, source_db=None, source_artifacts=None, metadata=None)[source]
Build and persist a snapshot from fully encoded compact arrays.
- Parameters:
output_path (str | Path) – Directory where the snapshot is written.
external_node_ids – External node IDs ordered by compact node ID.
node_type_ids – Integer node type ID per compact node.
external_edge_ids – External edge IDs ordered by compact edge ID.
external_relation_ids – External relation IDs ordered by compact relation ID.
src_int – Source compact node ID per edge.
dst_int – Target compact node ID per edge.
rel_int – Relation compact ID per edge.
relation_src_type_ids – Optional source endpoint type ID per relation.
relation_dst_type_ids – Optional target endpoint type ID per relation.
metadata (dict | None) – Optional metadata merged into generated metadata.
source_db (dict | None)
source_artifacts (dict | None)
- Returns:
Loaded
SamplerSnapshot.- Return type:
Examples
Use this lower-level API when all type IDs are already encoded:
snapshot = SamplerSnapshot.from_arrays( "snapshot", external_node_ids=["n0", "n1"], node_type_ids=[0, 1], external_edge_ids=["e0"], external_relation_ids=["r0"], src_int=[0], dst_int=[1], rel_int=[0], )
- classmethod from_edge_arrays(output_path, *, external_node_ids, external_edge_ids, external_relation_ids, src_int, dst_int, rel_int, node_type_values=None, node_type_ids=None, edge_src_type_values=None, edge_dst_type_values=None, relation_src_type_ids=None, relation_dst_type_ids=None, source_db=None, source_artifacts=None, metadata=None)[source]
Build and persist a snapshot from generic edge arrays.
This is the preferred API for pipelines that already have compact graph arrays, such as parquet/Arrow/NumPy preprocessing jobs. The method is deliberately free of pandas, Polars, Arrow, or database dependencies: the caller loads data with whichever tool is appropriate, then passes plain array-like values.
Exactly one of
node_type_valuesornode_type_idsmay be provided. Ifnode_type_valuesis provided, values are deterministically encoded to integer IDs sorted by their string representation, with missing values encoded as-1. If edge endpoint type values are provided, they are used to derive relation-compatible source and target type constraints. Otherwise, relation endpoint type constraints are inferred from the node type IDs of the actual edge endpoints.- Parameters:
output_path (str | Path) – Directory where the snapshot is written.
external_node_ids (Sequence[object]) – External node IDs ordered by compact node ID.
external_edge_ids (Sequence[object]) – External edge IDs ordered by compact edge ID.
external_relation_ids (Sequence[object]) – External relation IDs ordered by compact relation ID.
src_int – Source compact node ID per edge.
dst_int – Target compact node ID per edge.
rel_int – Relation compact ID per edge.
node_type_values (Sequence[object] | None) – Optional semantic node type values ordered by compact node ID. Values are encoded to integer IDs.
node_type_ids – Optional already encoded node type IDs ordered by compact node ID.
edge_src_type_values (Sequence[object] | None) – Optional semantic source endpoint type value per edge, used to derive relation source endpoint constraints.
edge_dst_type_values (Sequence[object] | None) – Optional semantic target endpoint type value per edge, used to derive relation target endpoint constraints.
relation_src_type_ids – Optional precomputed relation source type IDs.
relation_dst_type_ids – Optional precomputed relation target type IDs.
metadata (dict | None) – Optional JSON-compatible metadata merged into the generated snapshot metadata.
source_db (dict | None)
source_artifacts (dict | None)
- Returns:
Loaded
SamplerSnapshot.- Raises:
ValueError – If array lengths are inconsistent, compact IDs are out of range, or both
node_type_valuesandnode_type_idsare provided.- Return type:
Examples
Build from NumPy arrays without any dataframe dependency:
snapshot = SamplerSnapshot.from_edge_arrays( "snapshot", external_node_ids=["n0", "n1", "n2"], external_edge_ids=["e0", "e1"], external_relation_ids=["r0"], src_int=np.array([0, 1]), dst_int=np.array([1, 2]), rel_int=np.array([0, 0]), node_type_values=["drug", "protein", "protein"], )
- classmethod load(path, *, mmap=False)[source]
Load a sampler snapshot from disk.
- Parameters:
- Returns:
Loaded
SamplerSnapshot.- Raises:
ValueError – If the snapshot format version is unsupported.
- Return type:
Examples
Load eagerly into RAM:
snapshot = SamplerSnapshot.load("snapshot")
Load arrays through memory maps:
snapshot = SamplerSnapshot.load("snapshot", mmap=True)
- open_source_graph(*, base_path=None, backend_options=None)[source]
Open the
GraphDBreferenced by this snapshot’s source metadata.
- source_graph_exists(*, base_path=None)[source]
Return whether the referenced source graph path exists without opening it.
- Return type:
- temporal_candidates(node, relation, valid_time, *, direction='out')[source]
Return a start-time-pruned superset for a temporal point query.
No edge valid at
valid_timeis omitted, while edges starting after the selected bucket are pruned.SamplerEngineapplies the exact half-open point or window predicate before random selection.
Sampled Batches
Framework-neutral sampled batch containers.
- class gestaltdb.sampling.batch.SampledSubgraphBatch(node_ids_global, node_type_ids, senders, receivers, edge_ids_global, edge_relation_ids, positives, negatives, graph_node_offsets=None, graph_edge_offsets=None, edge_version_ids=None, valid_from_us=None, valid_to_us=None, valid_to_open=None, positive_time_us=None, negative_time_us=None, negative_diagnostics=None)[source]
Bases:
objectFramework-neutral sampled subgraph batch.
Node IDs inside
senders,receivers,positives, andnegativesare local to this batch.node_ids_globalmaps each local node row back to the compact global node ID in theSamplerSnapshot.- Parameters:
node_ids_global (ndarray)
node_type_ids (ndarray)
senders (ndarray)
receivers (ndarray)
edge_ids_global (ndarray)
edge_relation_ids (ndarray)
positives (ndarray)
negatives (ndarray)
graph_node_offsets (ndarray | None)
graph_edge_offsets (ndarray | None)
edge_version_ids (ndarray | None)
valid_from_us (ndarray | None)
valid_to_us (ndarray | None)
valid_to_open (ndarray | None)
positive_time_us (ndarray | None)
negative_time_us (ndarray | None)
- node_ids_global
Compact global node IDs included in this batch.
- Type:
numpy.ndarray
- node_type_ids
Node type IDs aligned with
node_ids_global.- Type:
numpy.ndarray
- senders
Local source node IDs for sampled edges.
- Type:
numpy.ndarray
- receivers
Local target node IDs for sampled edges.
- Type:
numpy.ndarray
- edge_ids_global
Compact global edge IDs included in this batch.
- Type:
numpy.ndarray
- edge_relation_ids
Relation IDs aligned with
edge_ids_global.- Type:
numpy.ndarray
- positives
Local positive triples shaped
(num_positives, 3).- Type:
numpy.ndarray
- negatives
Local negative triples, usually shaped
(num_positives, negatives_per_positive, 3).- Type:
numpy.ndarray
- graph_node_offsets
Optional graph component offsets for packed batches.
- Type:
numpy.ndarray | None
- graph_edge_offsets
Optional edge component offsets for packed batches.
- Type:
numpy.ndarray | None
- edge_version_ids
Edge-version IDs aligned with
edge_ids_globalfor temporal snapshots, otherwiseNone.- Type:
numpy.ndarray | None
- valid_from_us
Inclusive valid starts aligned with sampled edges.
- Type:
numpy.ndarray | None
- valid_to_us
Exclusive valid ends, with zero for open intervals.
- Type:
numpy.ndarray | None
- valid_to_open
Open-end masks aligned with sampled edges.
- Type:
numpy.ndarray | None
- positive_time_us
Example times aligned with positive triples.
- Type:
numpy.ndarray | None
- negative_time_us
Example times aligned with grouped negatives.
- Type:
numpy.ndarray | None
Examples
Convert to plain NumPy arrays:
arrays = batch.to_numpy() edge_index = np.stack([arrays["senders"], arrays["receivers"]])
- to_arrow()[source]
Return a PyArrow table for nodes and edges plus triple arrays.
- Returns:
Dictionary with Arrow tables for
nodes,edges,positives, andnegatives. If negatives are grouped, anegative_group_offsetsArrow array is included.- Raises:
ImportError – If
pyarrowis not installed.
Examples
Export node and edge tables:
arrow_batch = batch.to_arrow() node_table = arrow_batch["nodes"]
- to_dgl()[source]
Return a DGL graph with node/edge features and label tensors.
- Returns:
Tuple
(graph, labels)wheregraphis a DGL graph and labels contains positive and negative PyTorch tensors. Temporal graphs keep string edge-version IDs ongraph.edge_version_ids.- Raises:
ImportError – If
dglortorchis not installed.
- to_pyg()[source]
Return a PyTorch Geometric-style dictionary of tensors.
- Returns:
Dictionary with
edge_index, node/edge type tensors, global ID tensors, positive/negative label tensors, and temporal edge-version IDs as string-array metadata when present.- Raises:
ImportError – If
torchis not installed.
- to_tf_gnns()[source]
Return a TensorFlow-friendly graph dictionary and labels.
- Returns:
Tuple
(graph, labels)where both entries contain TensorFlow tensors. The graph dictionary follows the lightweight tensor-dict format used by the TarKG example.- Raises:
ImportError – If
tensorflowis not installed.
Sampling Negatives
Negative sampling configuration.
- class gestaltdb.sampling.negatives.HardNegativeConfig(negatives_per_positive=1, source='random', reject_known_positives=True, same_endpoint_type=False, relation_endpoint_types=False, candidate_fanout=None, head_probability=0.5, max_retries=100, temporal_positive_policy='any_time', temporal_positive_window_policy='overlap', temporal_candidate_window_days=None, allow_future_candidates=False, exhaustion_policy='raise')[source]
Bases:
objectConfiguration for KG hard-negative generation.
- Parameters:
negatives_per_positive (int)
source (str)
reject_known_positives (bool)
same_endpoint_type (bool)
relation_endpoint_types (bool)
candidate_fanout (int | None)
head_probability (float)
max_retries (int)
temporal_positive_policy (str)
temporal_positive_window_policy (str)
temporal_candidate_window_days (float | None)
allow_future_candidates (bool)
exhaustion_policy (str)
Sampling Feeders
Async prefetch utilities for sampler batches.
- class gestaltdb.sampling.feeder.AsyncBatchFeeder(producer, *, max_prefetch=2)[source]
Bases:
Generic[T]Background producer that keeps a bounded queue of sampled batches.
- Parameters:
producer (Callable[[], T])
max_prefetch (int)
- get(timeout=None)[source]
Return the next prefetched batch, raising producer errors inline.
- Parameters:
timeout (float | None)
- Return type:
T
Columnar Ingestion
Columnar ingestion containers for GestaltDB.
- class gestaltdb.ingestion.ColumnarIngestionMode(*values)[source]
-
How high-level columnar ingestion should interpret input columns.
ENTITY_COLUMNSmeans node and edge payloads are built from structured entity columns such as IDs, labels, sources, targets, types, and properties. WithJSONSerializer, this enables the fast Polars/Arrow JSON payload path.SERIALIZED_PAYLOADSmeans inputs already contain serializer-compatiblenode_valueandedge_valuebyte payload columns.- ENTITY_COLUMNS = 'entity_columns'
- SERIALIZED_PAYLOADS = 'serialized_payloads'
- class gestaltdb.ingestion.EdgeList(edge_ids, sources, targets, edge_types, edge_values, edge_ids_column=None, sources_column=None, targets_column=None, edge_types_column=None, edge_values_column=None)[source]
Bases:
objectColumnar typed edges with caller-provided serialized edge values.
- Parameters:
- classmethod from_arrow(edge_ids, sources, targets, edge_types, edge_values)[source]
Create an edge list from Arrow-like or Python columns.
- class gestaltdb.ingestion.IndexMaintenanceMode(*values)[source]
-
Secondary-index maintenance policy for columnar ingestion.
MAINTAINupdates secondary indexes during ingestion. This is safest for incremental writes, but expensive for large append-only bulk loads.DEFERwrites canonical graph records and traversal-critical typed adjacency, marks secondary indexes stale, and requires an explicitGraphDB.rebuild_deferred_indexes()before index-backed queries.DEFER_REBUILDis a high-level convenience mode: defer index maintenance during ingestion, then immediately run the one-pass deferred index rebuild.- DEFER = 'defer'
- DEFER_REBUILD = 'defer_rebuild'
- MAINTAIN = 'maintain'
- class gestaltdb.ingestion.NodeList(node_ids, node_values, node_ids_column=None, node_values_column=None)[source]
Bases:
objectColumnar nodes with caller-provided serialized node values.
- Parameters:
- classmethod from_arrow(node_ids, node_values)[source]
Create a node list from Arrow-like or Python columns.
Cypher Queries
Canonical Cypher query engine API.
- class gestaltdb.query_engine.cypher.QueryResult(columns, records)[source]
Bases:
objectTabular query result returned by
GraphDB.query.columnscontains projected column names in return order.recordsis a list of dictionaries keyed by column name.Examples
>>> result = QueryResult(columns=("n",), records=[{"n": "node"}]) >>> len(result) 1 >>> list(result)[0]["n"] 'node'
- gestaltdb.query_engine.cypher.execute(graph, query, parameters=None, *, read_snapshot=None)[source]
Execute a supported Cypher query against a
GraphDBinstance.- Parameters:
- Returns:
QueryResultwith projected records.- Return type:
Examples
>>> execute(graph_db, 'MATCH (n:Drug) RETURN n')
- gestaltdb.query_engine.cypher.parse(query)[source]
Parse the supported Cypher subset.
- Parameters:
query (str) – Cypher query text.
- Returns:
Parsed query object.
- Raises:
ValueError – If the query is outside the supported subset.
- Return type:
MatchQuery | SampleTypedPathsCall | EntailsCall | NodeScanQuery | RelationshipScanQuery | MultiMatchQuery
Examples
>>> parse('MATCH (n:Drug) RETURN n').label 'Drug'
- gestaltdb.query_engine.cypher.parse_ast(query)[source]
Parse the supported Cypher subset into its canonical clause AST.
- Parameters:
query (str)
- Return type:
Query | SampleTypedPathsCall | EntailsCall | UnionQuery | CreateConstraint | DropConstraint | ShowConstraints | ShowIndexes
- gestaltdb.query_engine.cypher.plan(query)[source]
Return the logical plan for a supported Cypher query.
Clause queries plan to the staged operator pipeline,
UNIONqueries plan each branch independently, and only the sampling procedure call keeps its dedicated source-backed plan. Constraint commands are executed directly and cannot be planned.- Parameters:
query (str)
- Return type:
LogicalPlan
Key-Value Stores
- class gestaltdb.kvstores.KVStore[source]
Bases:
objectAbstract interface for a simple key-value store.
- count_typed_adjacency(node_id, edge_type, direction='out')[source]
Count typed adjacency records for a node and edge type.
- delete_adjacency(node_id)[source]
Delete a serialized adjacency list for a node.
- Parameters:
node_id (bytes)
- delete_index_entry(index_name, key_parts, value)[source]
Delete one sorted index entry.
- Parameters:
Examples
>>> store.delete_index_entry("node_label", [b"Drug"], b"drug-1")
- delete_range_index_entry(index_name, key_parts, range_value, value)[source]
Delete one sorted range index entry.
- delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Delete typed adjacency records for an edge.
- ingest_edges_columnar(edge_list, *, append_only=True, native=True, maintain_indexes=True)[source]
Store columnar typed edges with caller-provided serialized values.
- ingest_nodes_columnar(node_list, *, native=True)[source]
Store columnar nodes with caller-provided serialized values.
- Parameters:
native (bool)
- iter_index_prefix(index_name, key_parts)[source]
Yield values whose index key starts with
key_parts.- Parameters:
- Yields:
Values associated with matching index entries.
Examples
>>> list(store.iter_index_prefix("node_label", [b"Drug"])) [b'drug-1']
- iter_range_index(index_name, key_parts, start_value=None, end_value=None, include_start=True, include_end=True, *, reverse=False, limit=None)[source]
Yield bounded range-index values, optionally greatest-first.
- iter_typed_adjacency(node_id, edge_type, direction='out')[source]
Yield typed adjacency records for a node and edge type.
- put_index_entries_bulk(entries)[source]
Store many sorted index entries.
- Parameters:
entries (list[tuple[str, list[bytes], bytes]]) – Tuples of
(index_name, key_parts, value).
Examples
>>> store.put_index_entries_bulk([("node_label", [b"Drug"], b"drug-1")])
- put_index_entry(index_name, key_parts, value)[source]
Store one sorted index entry.
- Parameters:
Examples
>>> store.put_index_entry("node_label", [b"Drug"], b"drug-1")
- put_nodes_bulk(keys_and_values)[source]
Store multiple node (serialized) values in a single batch/transaction if possible.
- put_range_index_entry(index_name, key_parts, range_value, value)[source]
Store one sorted range index entry.
- put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Store typed adjacency records for an edge.
- supports_read_snapshots = False
- supports_transactions = False
- supports_verifiable_read_snapshots = False
- class gestaltdb.kvstores.LMDBReadSnapshotStore(parent)[source]
Bases:
LMDBTransactionStoreRead-only LMDB transaction with a verifiable MVCC identity.
- Parameters:
parent (LMDBStore)
- delete(*args, **kwargs)
Delete a raw key/value pair.
- delete_adjacency(*args, **kwargs)
Delete a serialized adjacency list for a node.
- delete_edge(*args, **kwargs)
Delete an edge.
- delete_index_entry(*args, **kwargs)
Delete one sorted index entry.
- Parameters:
index_name – Logical index name.
key_parts – Ordered components used when the entry was written.
value – Entity ID or payload used when the entry was written.
Examples
>>> store.delete_index_entry("node_label", [b"Drug"], b"drug-1")
- delete_metadata(*args, **kwargs)
Delete a metadata key/value pair.
- delete_node(*args, **kwargs)
Delete a node.
- delete_range_index_entry(*args, **kwargs)
Delete one sorted range index entry.
- delete_typed_adjacency(*args, **kwargs)
Delete typed adjacency records for an edge.
- put(*args, **kwargs)
Store a raw key/value pair.
- put_adjacency(*args, **kwargs)
- put_adjacency_bulk(*args, **kwargs)
- put_edge(*args, **kwargs)
Store an edge (serialized).
- put_edges_bulk(*args, **kwargs)
Store multiple serialized edges.
- put_index_entries_bulk(*args, **kwargs)
Store many sorted index entries.
- Parameters:
entries – Tuples of
(index_name, key_parts, value).
Examples
>>> store.put_index_entries_bulk([("node_label", [b"Drug"], b"drug-1")])
- put_index_entry(*args, **kwargs)
Store one sorted index entry.
- Parameters:
index_name – Logical index name, such as
"node_label".key_parts – Ordered components used as the scan prefix.
value – Entity ID or payload returned by prefix scans.
Examples
>>> store.put_index_entry("node_label", [b"Drug"], b"drug-1")
- put_metadata(*args, **kwargs)
Store a metadata key/value pair.
- put_node(*args, **kwargs)
Store a node (serialized).
- put_nodes_bulk(*args, **kwargs)
Store multiple node (serialized) values in a single batch/transaction if possible.
- put_range_index_entries_bulk(*args, **kwargs)
Store many sorted range index entries.
- put_range_index_entry(*args, **kwargs)
Store one sorted range index entry.
- put_typed_adjacency(*args, **kwargs)
Store typed adjacency records for an edge.
- put_typed_adjacency_bulk(*args, **kwargs)
Store typed adjacency records for multiple edges.
- supports_read_snapshots = False
- supports_transactions = False
- supports_verifiable_read_snapshots = True
- class gestaltdb.kvstores.LMDBStore(path='graph_lmdb', map_size=10485760, map_id=True, map_keys=False)[source]
Bases:
KVStoreLMDB implementation of the GestaltDB key-value store.
Examples
>>> store = LMDBStore(path="/tmp/example_graph_lmdb")
- __init__(path='graph_lmdb', map_size=10485760, map_id=True, map_keys=False)[source]
- Creates/opens an LMDB environment with three named sub-databases:
b’nodes’ for node data
b’edges’ for edge data
b’adj’ for adjacency lists
- delete(key)[source]
Placeholder generic delete; graph code uses specialized methods.
- Parameters:
key (bytes)
- delete_adjacency(node_id)[source]
Delete a serialized adjacency list for a node.
- Parameters:
node_id (bytes)
- delete_range_index_entry(index_name, key_parts, range_value, value)[source]
Delete one sorted range index entry.
- delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Delete forward and reverse typed adjacency records.
- get_adjacency_bulk(node_ids)[source]
Retrieve multiple adjacency lists in a single read transaction. Returns a dict { node_id: serialized adjacency } for all found items.
- get_edge_keys_generator(num_edges=None, key_offset=None)[source]
Yield edge keys from the edge database.
- get_node_keys_generator(num_nodes=None, key_offset=None)[source]
Yield node keys from the node database.
- iter_index_prefix(index_name, key_parts)[source]
Yield values whose index key starts with
key_parts.
- iter_range_index(index_name, key_parts, start_value=None, end_value=None, include_start=True, include_end=True, *, reverse=False, limit=None)[source]
Yield values whose range index key falls between start and end values.
- iter_typed_adjacency(node_id, edge_type, direction='out')[source]
Yield typed adjacency
(edge_id, neighbor_id)pairs.
- put_adjacency_bulk(adj_dict)[source]
Insert/update multiple adjacency lists in one transaction. :param adj_dict: a dict mapping node_id -> serialized adjacency (list of edges)
- put_range_index_entries_bulk(entries)[source]
Store many sorted range index entries in one transaction.
- put_range_index_entry(index_name, key_parts, range_value, value)[source]
Store one sorted range index entry.
- put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Store forward and reverse typed adjacency records.
- range_iter(start_key, end_key)[source]
Yield node records whose keys fall within an inclusive range.
- supports_read_snapshots = True
- supports_transactions = True
- supports_verifiable_read_snapshots = True
- class gestaltdb.kvstores.LMDBTransactionStore(parent, write=True, **options)[source]
Bases:
KVStoreTransaction-bound LMDB store using one environment transaction.
- delete_adjacency(node_id)[source]
Delete a serialized adjacency list for a node.
- Parameters:
node_id (bytes)
- delete_index_entry(index_name, key_parts, value)[source]
Delete one sorted index entry.
- Parameters:
Examples
>>> store.delete_index_entry("node_label", [b"Drug"], b"drug-1")
- delete_range_index_entry(index_name, key_parts, range_value, value)[source]
Delete one sorted range index entry.
- delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Delete typed adjacency records for an edge.
- iter_index_prefix(index_name, key_parts)[source]
Yield values whose index key starts with
key_parts.- Parameters:
- Yields:
Values associated with matching index entries.
Examples
>>> list(store.iter_index_prefix("node_label", [b"Drug"])) [b'drug-1']
- iter_range_index(index_name, key_parts, start_value=None, end_value=None, include_start=True, include_end=True, *, reverse=False, limit=None)[source]
Yield bounded range-index values, optionally greatest-first.
- iter_typed_adjacency(node_id, edge_type, direction='out')[source]
Yield typed adjacency records for a node and edge type.
- put_index_entries_bulk(entries)[source]
Store many sorted index entries.
- Parameters:
entries (list[tuple[str, list[bytes], bytes]]) – Tuples of
(index_name, key_parts, value).
Examples
>>> store.put_index_entries_bulk([("node_label", [b"Drug"], b"drug-1")])
- put_index_entry(index_name, key_parts, value)[source]
Store one sorted index entry.
- Parameters:
Examples
>>> store.put_index_entry("node_label", [b"Drug"], b"drug-1")
- put_nodes_bulk(keys_and_values)[source]
Store multiple node (serialized) values in a single batch/transaction if possible.
- put_range_index_entry(index_name, key_parts, range_value, value)[source]
Store one sorted range index entry.
- put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Store typed adjacency records for an edge.
- supports_transactions = True
- class gestaltdb.kvstores.LevelDBStore(path='graph_leveldb')[source]
Bases:
KVStoreLevelDB implementation backed by
plyvel.- Parameters:
path – Directory that will contain the LevelDB sub-databases.
Examples
>>> store = LevelDBStore(path="/tmp/example_graph_leveldb")
- __init__(path='graph_leveldb')[source]
Create or open a LevelDB store. We’ll store nodes/edges by prefix.
- delete_adjacency(node_id)[source]
Delete a serialized adjacency list for a node.
- Parameters:
node_id (bytes)
- delete_range_index_entry(index_name, key_parts, range_value, value)[source]
Delete one sorted range index entry.
- delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Delete forward and reverse typed adjacency records.
- get_db_path(db_string='nodes')[source]
Return the relative path for a named LevelDB database.
Examples
>>> LevelDBStore.get_db_path.__name__ 'get_db_path'
- get_edge_keys_generator(num_edges=None, key_offset=None)[source]
Yield edge keys from the edge database.
- get_node_keys_generator(num_nodes=None, key_offset=None)[source]
Yield node keys from the node database.
- iter_index_prefix(index_name, key_parts)[source]
Yield values whose index key starts with
key_parts.
- iter_range_index(index_name, key_parts, start_value=None, end_value=None, include_start=True, include_end=True, *, reverse=False, limit=None)[source]
Yield values whose range index key falls between start and end values.
- iter_typed_adjacency(node_id, edge_type, direction='out')[source]
Yield typed adjacency
(edge_id, neighbor_id)pairs.
- put_adjacency_bulk(adj_dict)[source]
Insert/update multiple adjacency lists in one write batch. :param adj_dict: a dict mapping node_id -> serialized adjacency
- put_range_index_entries_bulk(entries)[source]
Store many sorted range index entries in one write batch.
- put_range_index_entry(index_name, key_parts, range_value, value)[source]
Store one sorted range index entry.
- put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Store forward and reverse typed adjacency records.
- class gestaltdb.kvstores.PyRexReadSnapshotStore(parent, txn, *, sequence)[source]
Bases:
PyRexTransactionStoreRead-only transaction-bound PyRex store.
- Parameters:
parent (PyRexStore)
sequence (int | None)
- delete(*args, **kwargs)
Delete a raw key/value pair.
- delete_adjacency(*args, **kwargs)
Delete a serialized adjacency list for a node.
- delete_edge(*args, **kwargs)
Delete an edge by byte key.
- delete_index_entry(*args, **kwargs)
Delete one sorted index entry.
- delete_metadata(*args, **kwargs)
Delete a metadata key/value pair.
- delete_node(*args, **kwargs)
Delete a node by byte key.
- delete_range_index_entry(*args, **kwargs)
Delete one sorted range index entry.
- delete_typed_adjacency(*args, **kwargs)
Delete forward and reverse typed adjacency records.
- put(*args, **kwargs)
Store a raw key/value pair in the shared RocksDB keyspace.
- put_adjacency(*args, **kwargs)
Store a serialized adjacency list for a node.
- put_adjacency_bulk(*args, **kwargs)
Store many serialized adjacency lists in one RocksDB write batch.
- put_edge(*args, **kwargs)
Store a serialized edge by byte key.
- put_edges_bulk(*args, **kwargs)
Store many serialized edges in one RocksDB write batch.
- put_index_entries_bulk(*args, **kwargs)
Store many sorted index entries in one write batch.
- put_index_entry(*args, **kwargs)
Store one sorted index entry.
- put_metadata(*args, **kwargs)
Store a metadata key/value pair.
- put_node(*args, **kwargs)
Store a serialized node by byte key.
- put_nodes_bulk(*args, **kwargs)
Store many serialized nodes in one RocksDB write batch.
- put_range_index_entries_bulk(*args, **kwargs)
Store many sorted range index entries in one write batch.
- put_range_index_entry(*args, **kwargs)
Store one sorted range index entry.
- put_typed_adjacency(*args, **kwargs)
Store forward and reverse typed adjacency records.
- put_typed_adjacency_bulk(*args, **kwargs)
Store many typed adjacency records in one RocksDB write batch.
- supports_read_snapshots = False
- supports_transactions = False
- class gestaltdb.kvstores.PyRexStore(path='graph_rocksdb', parallelism=None, max_background_jobs=None, write_buffer_size=None, bloom_bits_per_key=None, disable_wal=False, transactional=False, transaction_db_options=None)[source]
Bases:
KVStoreRocksDB implementation backed by
pyrex-rocksdb.PyRexStoreuses one physical RocksDB database with prefixed keys instead of separate databases. This lets node, edge, adjacency, and typed adjacency records share RocksDB’s write path and makes it possible to benchmark RocksDB tuning options against the existing LevelDB backend.- Parameters:
path – Directory for the RocksDB database.
parallelism – Optional number of RocksDB background threads.
max_background_jobs – Optional RocksDB background job limit.
write_buffer_size – Optional write buffer size in bytes.
bloom_bits_per_key – Optional block-based Bloom filter bits per key.
disable_wal – Disable RocksDB’s write-ahead log for faster but less durable ingestion benchmarks.
Examples
>>> store = PyRexStore(path="/tmp/example_graph_rocksdb")
- __init__(path='graph_rocksdb', parallelism=None, max_background_jobs=None, write_buffer_size=None, bloom_bits_per_key=None, disable_wal=False, transactional=False, transaction_db_options=None)[source]
Open a PyRex/RocksDB store with optional tuning settings.
- count_typed_adjacency(node_id, edge_type, direction='out')[source]
Count typed adjacency records without constructing record dictionaries.
- delete_adjacency(node_id)[source]
Delete a serialized adjacency list for a node.
- Parameters:
node_id (bytes)
- delete_range_index_entry(index_name, key_parts, range_value, value)[source]
Delete one sorted range index entry.
- delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Delete forward and reverse typed adjacency records.
- get_edge_keys_generator(num_edges=None, key_offset=None)[source]
Yield edge keys from the shared RocksDB keyspace.
- get_node_keys_generator(num_nodes=None, key_offset=None)[source]
Yield node keys from the shared RocksDB keyspace.
- has_native_columnar_ingestion()[source]
Return whether this PyRex runtime exposes native columnar writes.
- Return type:
- ingest_edges_columnar(edge_list, *, append_only=True, native=True, maintain_indexes=True)[source]
Store columnar typed edges, using native PyRex ingestion when available.
- ingest_nodes_columnar(node_list, *, native=True)[source]
Store columnar nodes, using native PyRex ingestion when available.
- Parameters:
native (bool)
- iter_index_prefix(index_name, key_parts)[source]
Yield values whose index key starts with
key_parts.
- iter_range_index(index_name, key_parts, start_value=None, end_value=None, include_start=True, include_end=True, *, reverse=False, limit=None)[source]
Yield values whose range index key falls between start and end values.
- iter_typed_adjacency(node_id, edge_type, direction='out')[source]
Yield typed adjacency
(edge_id, neighbor_id)pairs.
- put_adjacency_bulk(adj_dict)[source]
Store many serialized adjacency lists in one RocksDB write batch.
- put_range_index_entries_bulk(entries)[source]
Store many sorted range index entries in one write batch.
- put_range_index_entry(index_name, key_parts, range_value, value)[source]
Store one sorted range index entry.
- put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]
Store forward and reverse typed adjacency records.
- put_typed_adjacency_bulk(records)[source]
Store many typed adjacency records in one RocksDB write batch.
- class gestaltdb.kvstores.PyRexTransactionStore(parent, txn)[source]
Bases:
PyRexStoreTransaction-bound PyRex/RocksDB store.
- Parameters:
parent (PyRexStore)
- has_native_columnar_ingestion()[source]
Return whether this PyRex runtime exposes native columnar writes.
- Return type:
- supports_transactions = True
- class gestaltdb.kvstores.ReadSnapshotIdentity(backend, kind, sequence, verifiable)[source]
Bases:
objectBackend identity for one pinned mutable-state read horizon.
- class gestaltdb.kvstores.SimpleIndexCounterKVStore(dbenv=None, db_path=b'nodes')[source]
Bases:
objectThis is to help with lowering storage requirements for edge and node keys, by casting them to long ints.
It makes use of the struct.pack and struct.unpack functions and a simple counter (also stored in the medatadata) to count the number of keys (and hence the index) already entered.
- __init__(dbenv=None, db_path=b'nodes')[source]
Initialize an index counter helper.
- Parameters:
dbenv – LMDB environment.
db_path – Named LMDB database for the counter mapping.
- encode_db_key(key)[source]
If the key exists, it will return the existing key. if the key does not exist, it will add it to the KV store with a new increment, and return that.
- class gestaltdb.kvstores.SimpleKV(db_path)[source]
Bases:
objectSmall LMDB-backed helper for metadata key/value access.
- Parameters:
db_path – LMDB database handle or name used by transactions.
Serializers
- class gestaltdb.serializers.JSONSerializer[source]
Bases:
SerializerUses JSON for serialization.
- class gestaltdb.serializers.MessagePackSerializer[source]
Bases:
SerializerUses MessagePack for serialization.
- deserialize(data)[source]
Deserialize MessagePack bytes.
- Raises:
ImportError – If the optional
msgpackpackage is missing.- Parameters:
data (bytes)
- Return type:
Examples
>>> MessagePackSerializer().deserialize(MessagePackSerializer().serialize({"a": 1})) {'a': 1}
- serialize(obj)[source]
Serialize an object with MessagePack.
- Raises:
ImportError – If the optional
msgpackpackage is missing.- Parameters:
obj (dict)
- Return type:
Examples
>>> MessagePackSerializer().deserialize(MessagePackSerializer().serialize({"a": 1})) {'a': 1}
- class gestaltdb.serializers.PickleSerializer[source]
Bases:
SerializerUses Python’s pickle for serialization.
- class gestaltdb.serializers.ProtobufSerializer[source]
Bases:
SerializerUses google.protobuf Struct for JSON-like dictionaries.
Struct does not have native integer or bytes types. This serializer tags those values before encoding so Python dictionaries round-trip without losing them.
- deserialize(data)[source]
Deserialize protobuf Struct bytes.
- Parameters:
data (bytes) – Protobuf binary payload.
- Returns:
Decoded dictionary.
- Raises:
ImportError – If the optional
protobufpackage is missing.- Return type:
- static ensure_available()[source]
Import and return protobuf helpers, or raise installation guidance.
- serialize(obj)[source]
Serialize a JSON-like dictionary with protobuf Struct.
- Parameters:
obj (dict) – Dictionary containing JSON-like values plus tagged ints/bytes.
- Returns:
Protobuf binary payload.
- Raises:
ImportError – If the optional
protobufpackage is missing.- Return type:
Visualization IR
Deterministic visualization intermediate representation (VIZ-01).
Every visualization surface (static HTML files, Jupyter cells, client-side
PNG/SVG/JSON export) renders the same JSON payload produced here. Builders
accept Node/Edge objects (or their to_dict() shapes), Cypher
QueryResult rows, sample_typed_subgraph mappings, and
SampledSubgraphBatch + SamplerSnapshot pairs.
Standard library only: this module must not gain mandatory third-party runtime dependencies.
- gestaltdb.viz.ir.DEFAULT_ABSOLUTE_CEILING_MULTIPLIER = 2
Multiplier applied to
max_nodes/max_edgesto derive the default absolute ceiling. Payloads beyond the ceiling raise instead of rendering.
- gestaltdb.viz.ir.DEFAULT_MAX_EDGES = 5000
maximum edges kept in a payload before deterministic truncation.
- Type:
Default cap
- gestaltdb.viz.ir.DEFAULT_MAX_NODES = 2000
maximum nodes kept in a payload before deterministic truncation.
- Type:
Default cap
- gestaltdb.viz.ir.RESERVED_EXTRA_KEYS = ('valid', 'score', 'highlight')
Passthrough property names preserved verbatim on nodes/edges when present.
- class gestaltdb.viz.ir.TruncationInfo(nodes_dropped=0, edges_dropped=0)[source]
Bases:
objectExact accounting of deterministic cap enforcement.
- exception gestaltdb.viz.ir.TruncationWarning[source]
Bases:
UserWarningEmitted when caps deterministically drop nodes or edges (VIZ-07).
- gestaltdb.viz.ir.VIZ_IR_VERSION = 1
Version of the payload schema consumed by the
web/front end.
- exception gestaltdb.viz.ir.VizCapExceededError[source]
Bases:
ValueErrorRaised when a payload exceeds its absolute ceiling (VIZ-07).
- class gestaltdb.viz.ir.VizEdge(id, source, target, type='', properties=<factory>, extra=<factory>)[source]
Bases:
objectA single directed edge in the visualization IR.
- Parameters:
- class gestaltdb.viz.ir.VizGraph(nodes=(), edges=(), highlight_nodes=(), highlight_edges=(), truncation=<factory>, meta=<factory>)[source]
Bases:
objectDeterministic, JSON-serializable graph payload for the front end.
- Parameters:
- classmethod from_cypher_result(result, *, max_nodes=2000, max_edges=5000, absolute_max_nodes=None, absolute_max_edges=None, meta=None)[source]
Build a payload from a Cypher
QueryResult.Node/Edge/PathValuecell values become graph elements and populate the highlight set; node/edge-shaped dicts are accepted too. Scalar-only projections yield an empty topology and record their columns inmetaso the outcome stays explainable.
- classmethod from_nodes_edges(nodes, edges, *, max_nodes=2000, max_edges=5000, absolute_max_nodes=None, absolute_max_edges=None, highlight_nodes=(), highlight_edges=(), meta=None)[source]
Build a payload from node/edge objects or dict shapes.
Nodes sort by stable ID and keep the first
max_nodes; edges sort by(source, target, id)and keep the firstmax_edges. Edges touching a dropped node are dropped as well and counted inedges_dropped. Duplicate IDs keep their first occurrence.Payloads with more distinct nodes than
absolute_max_nodes(or edges thanabsolute_max_edges) raiseVizCapExceededErrorinstead of freezing the browser; both default to twice their respective cap. Sample a subgraph or narrow the query instead.
- classmethod from_sampled_subgraph(subgraph, *, max_nodes=2000, max_edges=5000, absolute_max_nodes=None, absolute_max_edges=None, meta=None)[source]
Build a payload from a
sample_typed_subgraphmapping.Missing (
None) node/edge entries become ID-only placeholders so sampled paths never reference absent endpoints. Nodes and edges that appear on sampled paths populate the highlight set.
- classmethod from_sampler_batch(batch, snapshot, *, max_nodes=2000, max_edges=5000, absolute_max_nodes=None, absolute_max_edges=None, meta=None)[source]
Build a payload from a
SampledSubgraphBatchand snapshot.Local batch rows map back through
node_ids_globalto compact global IDs and then to external IDs viasnapshot.external_node_id(...); relation IDs resolve viasnapshot.external_relation_id(...).numpyis used only through duck typing (tolist/indexing), so this module keeps no mandatory third-party imports.
- truncation: TruncationInfo
- class gestaltdb.viz.ir.VizNode(id, labels=(), properties=<factory>, extra=<factory>)[source]
Bases:
objectA single node in the visualization IR.
- gestaltdb.viz.ir.warn_if_truncated(viz_graph, *, source)[source]
Emit a
TruncationWarningwhen caps dropped anything.
Visualization Artifacts and API
Public Python API for packaged graph visualization (VIZ-06).
Thin conveniences over the IR (VIZ-01) and HTML (VIZ-03) layers. Standard
library only; everything heavy stays behind function calls so
import gestaltdb never pays for visualization.
- class gestaltdb.viz.api.VizFigure(viz, options=<factory>)[source]
Bases:
objectA built visualization: save it, embed it, or display it in Jupyter.
- Parameters:
viz (Any)
options (VizOptions)
- as_svg(*, width=1200, height=800, layout='circle', positions=None, background=True)[source]
Return dependency-free SVG using a static layout or supplied coordinates.
This renders the Python graph, not the current notebook iframe state. Set
edge_labels='all'to include relationship labels in static output.
- options: VizOptions
- class gestaltdb.viz.api.VizOptions(max_nodes=2000, max_edges=5000, absolute_max_nodes=None, absolute_max_edges=None, height=600, theme='light', charge=-300.0, link_distance=60.0, title='GestaltDB graph', show_labels=True, show_properties=True, node_label_property=None, edge_label_property=None, edge_labels='selected', label_max_length=40, node_size_property=None, node_color_property=None, edge_width_property=None, edge_color_property=None)[source]
Bases:
objectLabels, property styling, caps, and initial notebook-view settings.
node_label_property/edge_label_propertychoose readable labels with ID/type fallbacks.edge_labelsisoff,selected(also hovered/focused), orall. Size/width properties use bounded numeric scales; color properties use categorical values. These mappings also apply to static SVG, where relationship labels requireedge_labels='all'.- Parameters:
max_nodes (int)
max_edges (int)
absolute_max_nodes (int | None)
absolute_max_edges (int | None)
height (int)
theme (str)
charge (float)
link_distance (float)
title (str)
show_labels (bool)
show_properties (bool)
node_label_property (str | None)
edge_label_property (str | None)
edge_labels (str)
label_max_length (int)
node_size_property (str | None)
node_color_property (str | None)
edge_width_property (str | None)
edge_color_property (str | None)
- gestaltdb.viz.api.visualize_nodes_edges(nodes, edges, *, options=None, **caps)[source]
Visualize explicit node/edge collections.
- gestaltdb.viz.api.visualize_query(graph, cypher, parameters=None, *, options=None)[source]
Run a Cypher query and visualize the matched entities.
- gestaltdb.viz.api.visualize_sample(graph, seeds, pattern, *, rng=None, options=None)[source]
Sample a typed subgraph around
seedsand visualize it.
- gestaltdb.viz.api.visualize_sampler_batch(batch, snapshot, *, options=None)[source]
Visualize an ML training batch mapped back to external IDs.
Local batch rows resolve through
batch.node_ids_globalto compact global IDs and then viasnapshot.external_node_id(...)/snapshot.external_relation_id(...). The sampling-first path for inspectingSamplerEngineoutput without dumping whole snapshots.
Self-contained offline HTML artifacts (VIZ-03).
Builds shareable .html files that inline the committed JS/CSS bundle
plus the visualization payload. Standard library only: no new runtime
dependency, no network, no template engine.
- gestaltdb.viz.html.build_html(viz_graph, *, title='GestaltDB graph', warn_on_stale_bundle=True, view_overrides=None)[source]
Build a self-contained offline HTML document for a payload.
- Parameters:
viz_graph (Any) –
VizGraph(or any object exposingto_dict()) or an already-serialized payload mapping.title (str) – Document title (HTML-escaped).
warn_on_stale_bundle (bool) – Emit a
RuntimeWarningwhen the committed bundle does not match its manifest.view_overrides (Mapping[str, Any] | None) – Optional front-end initial-state overrides (theme, charge, link distance, label/property visibility) injected as
window.__GESTALTDB_VIZ_OPTIONS__.
- Returns:
Complete HTML document as a string.
- Return type:
- gestaltdb.viz.html.ensure_bundle_fresh(*, warn=True)[source]
Warn (once per call) when the bundle does not match the manifest.
- Returns:
stale output is still preferable to no output, but it must be loud.
- Return type:
True when fresh, False otherwise. Never raises for staleness
- Parameters:
warn (bool)
- gestaltdb.viz.html.is_bundle_fresh()[source]
Verify asset hashes and, in a checkout, frontend source provenance.
- Return type:
- gestaltdb.viz.html.read_manifest()[source]
Return the committed bundle manifest.
- Raises:
FileNotFoundError – If the manifest is absent (e.g. source checkout where
web/was never built).- Return type: