API Reference

Application integration routes

Filtering and Sampling Entry Points

  • Property-graph filtering: GraphDB label/type, exact/range property, ID-iterator, and count helpers. See Property-Graph Filtering and Indexes for index setup and cohort-to-snapshot examples, and Cypher Queries for query predicates.

  • Live sampling: GraphDB.sample_neighbors, sample_typed_paths, and sample_typed_subgraph with SamplingHop / SamplingPattern.

  • Repeated array-native sampling: SamplerSnapshot / SamplerEngine for seed, weighted-neighbor, layered, walk, multihop, and negative sampling in RAM or memmap mode. See Graph sampling API and policy reference for the API map and ID rules, and Performance and Benchmarks for cost boundaries and benchmarks.

Graph Models and Database

class gestaltdb.graphdb.Edge(edge_id=None, source=None, target=None, properties=None)

Bases: object

Directed graph edge with source, target, and properties.

Identity is the get_id property (also available as the id alias and to_dict()["id"]). get_id is a property, not a method: use edge.get_id, not edge.get_id().

Parameters:
  • edge_id (str | None) – Optional stable edge identifier. A UUID is generated when omitted.

  • source (str | Node | None) – Source node ID or Node instance.

  • target (str | Node | None) – Target node ID or Node instance.

  • properties (PropertyMap | None) – 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.

Parameters:
Return type:

None

classmethod from_dict(data)[source]

Factory from dictionary.

Parameters:

data (Mapping[str, object])

Return type:

Edge

property get_id: str

Unique identifier for this edge (property, not a method).

property get_id_bytes: 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: str | None

Return the typed traversal edge type.

Examples

>>> Edge(properties={"type": "drug-to-protein"}).get_type
'drug-to-protein'
property id: str

Alias for get_id for Cypher-familiar attribute access.

to_dict()[source]

Convert to a dictionary for serialization.

Return type:

dict[str, object]

class gestaltdb.graphdb.GraphDB(store, serializer, indexed_node_properties=None, indexed_edge_properties=None)[source]

Bases: object

Persistent current-graph records, indexes, queries, and scoped services.

Choose an installed backend and serializer with create(); open() restores those choices from the database manifest. Always close the handle.

Temporal history lives on graph.temporal, sourced claims and modal evaluation on graph.epistemic, and Horn rules on graph.rules. Those services own their state and operations; this class keeps no wrapper methods for them. Ordinary node/edge writes stay here.

Examples

Create, query, and reopen a disposable database (requires lmdb):

 1>>> from tempfile import TemporaryDirectory
 2>>> from pathlib import Path
 3>>> from gestaltdb.graphdb import GraphDB, Node
 4>>> with TemporaryDirectory() as root:
 5...     path = Path(root) / "graph"
 6...     graph = GraphDB.create(path, backend="lmdb", serializer="json")
 7...     try:
 8...         _ = graph.create_node_property_index("name")
 9...         graph.put_node(Node("alice", labels=["Person"], properties={"name": "Alice"}))
10...         result = graph.query('MATCH (n:Person {name: "Alice"}) RETURN n.name AS name')
11...         assert result.records == [{"name": "Alice"}]
12...     finally:
13...         graph.close()
14...     reopened = GraphDB.open(path)
15...     try:
16...         assert reopened.get_node(b"alice").properties["name"] == "Alice"
17...     finally:
18...         reopened.close()

See the architecture and temporal-versioning guides for component handoffs and historical reads; use ingest_arrow for a columnar loading example.

Parameters:
__init__(store, serializer, indexed_node_properties=None, indexed_edge_properties=None)[source]

Initialize a graph database wrapper.

Parameters:
  • store (KVStore) – KVStore instance such as LMDBStore, LevelDBStore, or PyRexStore.

  • 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"])
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.

Parameters:

start_node_id (bytes)

Return type:

list[bytes]

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.

  • source_db_reference (bool) – Record the managed source location automatically.

  • source_db_path – Override location (cwd-relative, except project mode).

  • source_db_path_mode (str) – Snapshot-relative (default), absolute, or project-relative.

  • 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.

Returns:

A loaded SamplerSnapshot instance.

close()[source]

Close the underlying key-value store.

Examples

>>> graph_db.close()
Return type:

None

count_edges_by_property(property_name, value)[source]

Return the number of edges currently indexed for an exact property value.

Parameters:

property_name (str)

Return type:

int

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.

Parameters:
  • property_name (str)

  • include_start (bool)

  • include_end (bool)

Return type:

int

count_edges_by_type(edge_type)[source]

Return the number of edges currently indexed for a relationship type.

Parameters:

edge_type (str)

Return type:

int

count_edges_by_type_property(edge_type, property_name, value)[source]

Return the number of edges indexed for a type and exact property value.

Parameters:
  • edge_type (str)

  • property_name (str)

Return type:

int

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.

Parameters:
  • edge_type (str)

  • property_name (str)

  • include_start (bool)

  • include_end (bool)

Return type:

int

count_nodes_by_label(label)[source]

Return the number of nodes currently indexed for a label.

Parameters:

label (str)

Return type:

int

count_nodes_by_label_property(label, property_name, value)[source]

Return the number of nodes indexed for a label and exact property value.

Parameters:
  • label (str)

  • property_name (str)

Return type:

int

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.

Parameters:
  • label (str)

  • property_name (str)

  • include_start (bool)

  • include_end (bool)

Return type:

int

count_nodes_by_property(property_name, value)[source]

Return the number of nodes currently indexed for an exact property value.

Parameters:

property_name (str)

Return type:

int

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.

Parameters:
  • property_name (str)

  • include_start (bool)

  • include_end (bool)

Return type:

int

count_typed_adjacency(node_id, edge_type, direction='out')[source]

Count typed adjacency records without materializing traversal records.

Parameters:
  • node_id (NodeKeyInput) – Node ID as string or bytes.

  • edge_type (str) – Edge type to traverse.

  • direction (Direction) – "out", "in", or "any".

Returns:

Number of typed adjacency records matching the node, edge type, and direction.

Return type:

int

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:

GraphDB

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
current_read_view(*, require_verifiable=False, capture_provenance=True)[source]

Yield current graph reads pinned to one backend snapshot.

Parameters:
  • require_verifiable (bool)

  • capture_provenance (bool)

Return type:

Iterator[CurrentGraphReadView]

property database_id: str

Return the stable UUID persisted with this database.

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:
Return type:

None

delete_node(node_id)[source]

Delete a node by byte key.

Parameters:

node_id (str | bytes) – Node ID bytes.

Return type:

None

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'
property edges: EdgeRepository

Current-edge repository bound to this handle’s store and lifetime.

edges_by_edge_type(node_id, edge_type, direction='out')[source]

Return edge IDs connected by a specific edge type.

Parameters:
  • node_id (NodeKeyInput) – Node ID as string or bytes.

  • edge_type (str) – Edge type to traverse.

  • direction (Direction) – "out", "in", or "any".

Returns:

List of edge ID bytes.

Return type:

list[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 Edge objects.

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.

Parameters:
  • property_name (str)

  • include_start (bool)

  • include_end (bool)

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 Edge objects.

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.

Parameters:
  • edge_type (str)

  • property_name (str)

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.

Parameters:
  • edge_type (str)

  • property_name (str)

  • include_start (bool)

  • include_end (bool)

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:

list[str]

get_edge(edge_id)[source]

Return an edge by key; str IDs are UTF-8 encoded automatically.

Parameters:

edge_id (str | bytes)

Return type:

Edge | None

get_node(node_id)[source]

Return a node by key; str IDs are UTF-8 encoded automatically.

Parameters:

node_id (str | bytes)

Return type:

Node | 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_nodes(node_ids)[source]

Return decoded nodes in request order; accepts str or byte IDs.

Parameters:

node_ids (Iterable[str | bytes])

Return type:

list[Node | 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.

Parameters:
  • node_id (NodeKeyInput)

  • edge_type (str)

  • direction (Direction)

Return type:

list[dict[str, bytes]]

index_statistics()[source]

Return persisted index definitions visible to the query planner.

Return type:

dict[str, object]

property indexes: IndexService

Current index lookup, DDL and rebuild authority for this store scope.

property ingest: IngestionService

Arrow/Polars ingestion capabilities bound to this current store scope.

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_COLUMNS builds payloads from structured node/edge columns. SERIALIZED_PAYLOADS expects node_values and edge_values to contain serializer-compatible payload bytes.

Examples

Load two entities and a typed edge (requires lmdb and pyarrow):

 1>>> from tempfile import TemporaryDirectory
 2>>> import pyarrow as pa
 3>>> from gestaltdb.graphdb import GraphDB
 4>>> with TemporaryDirectory() as root:
 5...     graph = GraphDB.create(root + "/db", backend="lmdb", serializer="json")
 6...     try:
 7...         _ = graph.ingest_arrow(
 8...             pa.array(["a", "b"]), pa.array(["ab"]),
 9...             pa.array(["a"]), pa.array(["b"]), pa.array(["knows"]),
10...             node_properties={"name": pa.array(["Alice", "Bob"])})
11...         assert graph.get_edge(b"ab").properties["type"] == "knows"
12...         assert graph.get_node(b"a").properties["name"] == "Alice"
13...     finally:
14...         graph.close()

The default rebuilds deferred indexes before returning. Native columnar writes are available on plain PyRex; transactional PyRex uses a different write path. See the operations/performance guides for backend trade-offs.

Parameters:
Return type:

dict[str, object]

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_values is required and must contain serialized edge payloads compatible with the current GraphDB serializer. 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.

Parameters:
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_value column is required and must contain serialized edge payload bytes compatible with the current GraphDB serializer.

Parameters:
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 Python Edge serialization.

Parameters:
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_values is required and must contain serialized node payloads compatible with the current GraphDB serializer.

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.

Parameters:
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_value column is required and must contain serialized node payload bytes compatible with the current GraphDB serializer.

Parameters:
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 Python Node serialization.

Parameters:
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"])
Parameters:
Return type:

dict[str, object]

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.

Parameters:
  • property_name (str)

  • include_start (bool)

  • include_end (bool)

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.

Parameters:
  • edge_type (str)

  • property_name (str)

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.

Parameters:
  • edge_type (str)

  • property_name (str)

  • include_start (bool)

  • include_end (bool)

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.

Parameters:
  • label (str)

  • property_name (str)

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.

Parameters:
  • label (str)

  • property_name (str)

  • include_start (bool)

  • include_end (bool)

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.

Parameters:
  • property_name (str)

  • include_start (bool)

  • include_end (bool)

iter_typed_adjacency(node_id, edge_type, direction='out')[source]

Yield typed adjacency records with clean direction semantics.

Parameters:
  • node_id (NodeKeyInput) – Node ID as string or bytes.

  • edge_type (str) – Edge type to traverse.

  • direction (Direction) – "out", "in", or "any".

Yields:

Typed adjacency records containing edge, neighbor, source, target, edge type, and concrete direction fields.

Return type:

Iterator[dict[str, bytes]]

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'
property lookup: EntityLookup

Index-backed decoded node/edge reads bound to this store scope.

property manifest: dict

Return the loaded or generated manifest for this graph.

neighbors_by_edge_type(node_id, edge_type, direction='out')[source]

Return neighbor IDs connected by a specific edge type.

Parameters:
  • node_id (NodeKeyInput) – Node ID as string or bytes.

  • edge_type (str) – Edge type to traverse.

  • direction (Direction) – "out", "in", or "any".

Returns:

List of neighbor ID bytes.

Return type:

list[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'
property nodes: NodeRepository

Current-node repository; see the architecture guide for standalone use.

Borrows this handle’s lifetime and store scope. Temporal records and schema changes remain separate capabilities.

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 Node objects.

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.

Parameters:
  • label (str)

  • property_name (str)

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.

Parameters:
  • label (str)

  • property_name (str)

  • include_start (bool)

  • include_end (bool)

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 Node objects.

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.

Parameters:
  • property_name (str)

  • include_start (bool)

  • include_end (bool)

classmethod open(path, *, backend_options=None, validate_manifest=True)[source]

Open a self-describing graph store from a directory.

Return type:

GraphDB

put_adjacency_list(node_id, edges_list)[source]

Stores the adjacency list for node_id.

Parameters:
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_edges_bulk(edges, check_existing=True)[source]

Store multiple edges and update adjacency indexes in bulk.

Parameters:
  • edges (List[Edge]) – Edges to write.

  • check_existing (bool) – When true, read existing edges first and remove stale typed adjacency and sorted index records before replacement. Set to false for append-only ingestion with new edge IDs.

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_nodes(nodes)[source]

Store multiple nodes and maintain label/property indexes.

Parameters:

nodes (list[Node]) – Nodes to serialize and write.

Examples

>>> graph_db.put_nodes([Node(node_id="drug-1", labels=["Drug"])])
query(cypher, parameters=None, *, read_snapshot=None, label_hierarchy=None, relation_hierarchy=None)[source]

Execute a supported Cypher query.

Read queries run directly; queries with CREATE/SET/REMOVE run inside transaction() 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 (Optional[dict[str, object]]) – 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.

  • label_hierarchy (LabelHierarchy | None) – Optional LabelHierarchy. When supplied, a required label also matches nodes carrying any transitive sub-type label, including in label expressions, endpoint filters and indexed lookups. Default matching stays exact.

  • relation_hierarchy (RelationHierarchy | None) – Optional RelationHierarchy. When supplied, a required relationship type also matches edges carrying any transitive sub-property type, including property maps, variable-length paths, and index-backed lookups. Default matching stays exact.

Returns:

gestaltdb.QueryResult containing projected records.

Return type:

QueryResult

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]

Deprecated; use nodes_by_property_range instead.

Parameters:

property_name (str)

Return type:

Iterator[Node | None]

rebuild_deferred_indexes()[source]

Rebuild secondary indexes marked stale by deferred bulk ingestion.

Returns:

Mapping from rebuilt index family/property to entries written.

Return type:

dict[str, int]

rebuild_edge_indexes(*, edge_types=True, properties=None, batch_size=100000)[source]

Rebuild requested edge secondary indexes in one edge scan.

Parameters:
Return type:

dict[str, int]

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.

Parameters:
Return type:

dict[str, int]

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_typed_adjacency()[source]

Rebuild typed adjacency indexes from stored edge records.

Returns:

Number of typed edges indexed.

Examples

>>> graph_db.rebuild_typed_adjacency()
sample_neighbors(node_id, edge_type, direction='out', sample_size=10, rng=None, *, relation_hierarchy=None)[source]

Sample typed neighbors using reservoir sampling.

Parameters:
  • node_id (NodeKeyInput) – Node ID as string or bytes.

  • edge_type (str) – Edge type to traverse. With relation_hierarchy, this is the requested parent type.

  • direction (Direction) – "out", "in", or "any".

  • sample_size (int) – Maximum number of records to return.

  • rng (RandomLike | None) – Optional random number generator with randrange.

  • relation_hierarchy (RelationHierarchy | None) – Optional RelationHierarchy. When supplied, edge_type expands to itself plus all transitive sub-properties and the reservoir is drawn from their union, so sample_size remains the total fanout. Default traversal stays literal.

Returns:

List of typed adjacency records.

Return type:

list[dict[str, bytes]]

Examples

>>> graph_db.sample_neighbors("drug-1", "drug-to-protein", sample_size=2)
sample_nodes(sample_size, *, label=None, node_filter=None, rng=None)[source]

Sample live node IDs using reservoir sampling.

Memory is bounded by sample_size rather than by graph size, but eligible keys are still scanned. label restricts the scan to one native-label index; otherwise all live node keys are visited. No property-index pushdown is performed: node_filter receives each decoded Node and runs during the scan. Use snapshot SamplerEngine.sample_nodes for array-native, index-free sampling.

Parameters:
  • sample_size (int) – Maximum number of node IDs to return.

  • label (str | None) – Optional native label restricting the candidate set.

  • node_filter (Callable[[Node], bool] | None) – Optional predicate over decoded Node objects.

  • rng – Optional random number generator with randrange.

Returns:

Sampled node IDs as strings, in reservoir order.

Raises:
  • ValueError – If sample_size is not a non-negative integer.

  • TypeError – If node_filter is not callable.

Return type:

list[str]

Examples

>>> graph_db.sample_nodes(5, label="Drug", rng=random.Random(7))
['drug-3', 'drug-1', 'drug-7', 'drug-2', 'drug-5']
sample_typed_paths(seed_ids, pattern, rng=None, *, relation_hierarchy=None)[source]

Sample paths that follow an ordered typed edge pattern.

Parameters:
  • seed_ids (Iterable[NodeKeyInput]) – Starting node IDs as strings or bytes.

  • pattern (SamplingPattern | Iterable[SamplingHop | Mapping[str, object]]) – SamplingPattern or list of dictionaries such as {"edge_type": "drug-to-protein", "direction": "out", "sample_size": 2}.

  • rng (RandomLike | None) – Optional random number generator with randrange.

  • relation_hierarchy (RelationHierarchy | None) – Optional RelationHierarchy applied to every hop. Each hop’s edge_type expands to its transitive sub-properties, with sample_size shared across the union.

Returns:

List of dictionaries with seed and sampled path records.

Return type:

list[SampledPath]

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, *, relation_hierarchy=None)[source]

Sample and materialize a typed subgraph around seed nodes.

Parameters:
  • seed_ids (Iterable[NodeKeyInput]) – Starting node IDs as strings or bytes.

  • pattern (Union[SamplingPattern, list[dict]]) – SamplingPattern or list of dictionary hop configs.

  • rng (RandomLike | None) – Optional random number generator with randrange.

  • relation_hierarchy (RelationHierarchy | None) – Optional RelationHierarchy applied to every hop before sampling.

Returns:

Dictionary with nodes, edges, and paths entries.

Return type:

SampledSubgraph

Examples

>>> pattern = [{"edge_type": "drug-to-protein", "direction": "out", "sample_size": 2}]
>>> graph_db.sample_typed_subgraph(["drug-1"], pattern)
save_manifest(path=None)[source]

Write or update the root manifest for this graph.

Return type:

dict

serialize_edge_value(edge)[source]

Serialize an edge for use with columnar edge ingestion.

Parameters:

edge (Edge)

Return type:

bytes

serialize_node_value(node)[source]

Serialize a node for use with columnar node ingestion.

Parameters:

node (Node)

Return type:

bytes

set_node_constraints(constraints)[source]

Replace and persist the node constraint catalog.

Parameters:

constraints (list[dict[str, str]])

Return type:

None

stale_indexes()[source]

Return index families requiring rebuild after deferred ingestion.

Return type:

tuple[str, …]

property store

The bound backend store; assigning rebinds scope-owned engines.

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.

Parameters:

options (Any)

Return type:

Iterator[GraphDB]

property traversal: TraversalService

Typed adjacency and live sampling bound to this current store scope.

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.

Parameters:
Return type:

Edge

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)

Parameters:
Return type:

Node

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 .html artifacts 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 – SamplingPattern or hop dicts used with seeds.

  • parameters – Optional Cypher parameters for cypher.

  • options – Optional gestaltdb.viz.api.VizOptions or 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)

Bases: object

Serialize 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.

Return type:

None

deserialize(val: bytes, entity_type: Literal['Node']) → Node[source]
deserialize(val: bytes, entity_type: Literal['Edge']) → Edge
deserialize(val: bytes, entity_type: Literal['AdjacencyList']) → object

Deserializer (conditional on entity type)

Parameters:
  • val (bytes) – bytes containing the data

  • entity_type (Literal['Node', 'Edge', 'AdjacencyList']) – (str) is Edge, Node, AdjacencyList

Return type:

object

serialize(entity: Node, entity_type: Literal['Node']) → bytes[source]
serialize(entity: Edge, entity_type: Literal['Edge']) → bytes
serialize(entity: object, entity_type: Literal['AdjacencyList']) → bytes

Serialize a graph entity by entity type.

Parameters:
  • entity (object) – Node, Edge, or adjacency-list object.

  • entity_type (Literal['Node', 'Edge', 'AdjacencyList']) – One of "Node", "Edge", or "AdjacencyList".

Returns:

Serialized bytes.

Return type:

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)

Bases: object

Graph node with an ID, native labels, and arbitrary properties.

Identity is the get_id property (also available as the id alias and to_dict()["id"]). get_id is a property, not a method: use node.get_id, not node.get_id(). There is no node.id = ... setter; pass node_id= to the constructor.

Parameters:
  • node_id (str | None) – Optional stable node identifier. A UUID is generated when omitted.

  • properties (PropertyMap | None) – Optional dictionary of node attributes.

  • labels (Iterable[str] | None) – 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_id is omitted.

Parameters:
Return type:

None

classmethod from_dict(data)[source]

Create a node from serialized dictionary data.

Parameters:

data (Mapping[str, object]) – Dictionary produced by to_dict. Older dictionaries without labels deserialize with an empty label tuple.

Returns:

Node instance.

Return type:

Node

Examples

>>> Node.from_dict({"id": "n1", "properties": {}}).labels
()
property get_id: str

Unique identifier for this node.

This is a property: use node.get_id (or the id alias), not node.get_id().

property get_id_bytes: bytes

Return the node ID encoded as UTF-8 bytes.

Examples

>>> Node(node_id="drug-1").get_id_bytes
b'drug-1'
property id: str

Alias for get_id for Cypher-familiar attribute access.

to_dict()[source]

Convert to a dictionary form for serialization.

Returns:

Dictionary containing id, properties, and labels.

Return type:

dict[str, object]

Examples

>>> Node(node_id="n1", labels=["Drug"]).to_dict()["labels"]
['Drug']
class gestaltdb.graphdb.TimeIndexedEdge(timestamp_dat, *args, **kwargs)

Bases: Edge

Deprecated 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.datetime) – Datetime used as the sortable key prefix.

  • *args (Any) – Positional arguments passed to Edge.

  • **kwargs (Any) – 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) – Datetime used as the sortable key prefix.

  • *args (Any) – Positional arguments passed to Edge.

  • **kwargs (Any) – Keyword arguments passed to Edge.

Return type:

None

classmethod from_dict(data)[source]

Factory from dictionary.

Parameters:

data (Mapping[str, object])

Return type:

TimeIndexedEdge

property get_id_bytes: 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
to_dict()[source]

Convert to a dictionary for serialization.

Return type:

dict[str, object]

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: object

A point-in-time or finite-window temporal selection.

Parameters:
matches(value)[source]

Test a point event or validity interval against this selection.

Parameters:

value (TemporalInterval | TemporalInstant | datetime | int)

Return type:

bool

class gestaltdb.temporal.TemporalDate(value)[source]

Bases: object

A calendar date without time or timezone.

Parameters:

value (date)

class gestaltdb.temporal.TemporalDuration(total_microseconds)[source]

Bases: object

An exact signed duration represented as total microseconds.

Parameters:

total_microseconds (int)

class gestaltdb.temporal.TemporalInstant(epoch_microseconds)[source]

Bases: object

An 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:

TemporalInstant

encode_sortable()[source]

Encode the instant so lexicographic byte order is chronological.

Return type:

bytes

classmethod parse(value)[source]

Parse an extended ISO-8601 datetime with an explicit UTC offset.

Parameters:

value (str)

Return type:

TemporalInstant

class gestaltdb.temporal.TemporalInterval(start, end=None)[source]

Bases: object

A non-empty half-open interval [start, end).

end=None denotes positive infinity.

Parameters:
intersection(other)[source]

Return the non-empty intersection, or None for disjoint intervals.

Parameters:

other (TemporalInterval)

Return type:

TemporalInterval | None

class gestaltdb.temporal.TemporalLocalDateTime(value)[source]

Bases: object

A timezone-free local date and time.

Parameters:

value (datetime)

class gestaltdb.temporal.TemporalLocalTime(microseconds)[source]

Bases: object

A timezone-free time represented as microseconds since midnight.

Parameters:

microseconds (int)

class gestaltdb.temporal.TemporalTime(local_microseconds, offset_seconds)[source]

Bases: object

A fixed-offset time of day.

Parameters:
  • local_microseconds (int)

  • offset_seconds (int)

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:

TemporalContext

gestaltdb.temporal.as_temporal_instant(value)[source]

Normalize an instant, aware datetime, or epoch-microsecond integer.

Parameters:

value (TemporalInstant | datetime | int)

Return type:

TemporalInstant

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:

TemporalInterval

gestaltdb.temporal.datetime_to_epoch_microseconds(value)[source]

Normalize an aware datetime to exact UTC epoch microseconds.

Parameters:

value (datetime)

Return type:

int

gestaltdb.temporal.epoch_microseconds_to_datetime(value)[source]

Convert epoch microseconds to a timezone-aware UTC datetime.

Parameters:

value (int)

Return type:

datetime

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: TemporalVersionMetadata

An immutable metadata envelope with an optional epistemic claim.

Parameters:
class gestaltdb.versioning.ClaimVersionWrite(operation, logical_id, valid, claim=None, version_id=None, supersedes_version_id=None, reason=None)[source]

Bases: object

Validated input describing an epistemic claim-version operation.

Parameters:
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: TemporalVersionMetadata

An immutable metadata envelope with an optional edge payload.

Parameters:
class gestaltdb.versioning.EdgeVersionWrite(operation, logical_id, valid, edge=None, version_id=None, supersedes_version_id=None, reason=None)[source]

Bases: object

Validated input describing an edge-version operation.

Parameters:
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: TemporalVersionMetadata

An immutable metadata envelope with an optional node payload.

Parameters:
class gestaltdb.versioning.NodeVersionWrite(operation, logical_id, valid, node=None, version_id=None, supersedes_version_id=None, reason=None)[source]

Bases: object

Validated input describing a node-version operation.

Parameters:
class gestaltdb.versioning.TemporalCommit(commit_id, system_time, metadata, versions)[source]

Bases: object

One visible temporal commit and its ordered entity versions.

Parameters:
exception gestaltdb.versioning.TemporalCorruptionError[source]

Bases: TemporalVersionError

Raised when marker-backed temporal history fails integrity checks.

exception gestaltdb.versioning.TemporalVersionError[source]

Bases: ValueError

Base 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: object

Metadata common to node and edge versions.

Parameters:
class gestaltdb.versioning.VersionOperation(*values)[source]

Bases: str, Enum

Operation represented by an immutable entity version.

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: object

A sourced assertion or denial, independent of temporal version metadata.

statement_id identifies only the subject-predicate-object proposition. claim_id additionally identifies the asserting context (agent, source, world, and polarity), allowing contradictory claims to coexist.

Parameters:
classmethod from_dict(value)[source]

Decode and authenticate a canonical claim payload.

Parameters:

value (Mapping[str, object])

Return type:

Claim

to_dict()[source]

Return the canonical JSON-compatible claim payload.

Return type:

dict[str, object]

class gestaltdb.epistemic.ClaimObjectKind(*values)[source]

Bases: str, Enum

How a claim object is interpreted.

class gestaltdb.epistemic.ClaimPolarity(*values)[source]

Bases: str, Enum

Whether an agent asserts or denies a proposition.

class gestaltdb.epistemic.ClaimStatus(*values)[source]

Bases: str, Enum

Open-world four-valued status of a proposition at a read view.

gestaltdb.epistemic.claim_statement_id(subject, predicate, object, *, object_kind=ClaimObjectKind.ENTITY)[source]

Return the deterministic identity of a subject-predicate-object proposition.

Parameters:
Return type:

str

Positive Horn Rules

Immutable Horn rules, stratified negation values and evaluation results.

class gestaltdb.rules.ClaimExplanation(claim_id, root_version_id, valid_time, system_time, nodes, edges, truncated=False)[source]

Bases: object

A finite historical explanation rooted at one visible claim version.

Parameters:
class gestaltdb.rules.ExplanationEdge(source_id, target_id, relationship, premise_ordinal=None)[source]

Bases: object

A directed derivation or premise edge in an explanation graph.

Parameters:
  • source_id (str)

  • target_id (str)

  • relationship (str)

  • premise_ordinal (int | None)

class gestaltdb.rules.ExplanationNode(node_id, kind, value)[source]

Bases: object

One immutable claim or rule version in an explanation graph.

Parameters:
class gestaltdb.rules.RuleAbsence(position, predicate, subject, object, scope)[source]

Bases: object

One ground closed-world absence check used as a negated premise.

This is recorded evidence that no matching positive claim was visible at the pinned evaluation horizon. It is not a negative claim, a denial, or a claim version, and it carries no version identity.

Parameters:
class gestaltdb.rules.RuleAtom(subject, predicate, object)[source]

Bases: object

One (subject, predicate, object) atom in a positive Horn rule.

Parameters:
exception gestaltdb.rules.RuleError[source]

Bases: TemporalVersionError

Base error for invalid rule catalogs and evaluations.

Sibling of gestaltdb.reasoning.model.ReasoningError under TemporalVersionError (both are ValueError). Callers that need to catch every bounded-evaluation failure must catch (ReasoningError, TemporalVersionError) or ValueError; except ReasoningError alone misses rule/modal bounds.

exception gestaltdb.rules.RuleEvaluationLimitError[source]

Bases: RuleError

Raised before persistence when a rule run exceeds a resource bound.

class gestaltdb.rules.RuleJustification(rule_version_id, premise_version_ids, absences=())[source]

Bases: object

A rule version, ordered positive premises and any absence checks.

Parameters:
class gestaltdb.rules.RuleRunResult(iterations, derived_count, justification_count, commit_id, versions)[source]

Bases: object

Summary of one bounded rule run and its persisted claim versions.

Parameters:
  • iterations (int)

  • derived_count (int)

  • justification_count (int)

  • commit_id (int | None)

  • versions (tuple[object, ...])

class gestaltdb.rules.RuleVersion(rule_id, version_id, when, then, system_time, catalog_ordinal, not_when=(), closed=())[source]

Bases: object

One immutable system-time version of a named safe Horn rule.

when holds positive body atoms. not_when holds closed-world absence atoms whose variables must already be bound by when. closed declares predicates treated as complete for negation; the active program’s effective scope is the union of these declarations, checked at creation.

Parameters:
class gestaltdb.rules.TruthMaintenanceResult(input_commit_id, commit_id, asserted_count, corrected_count, retracted_count, support_count, versions)[source]

Bases: object

Summary of one bounded incremental truth-maintenance pass.

Parameters:
  • input_commit_id (int)

  • commit_id (int | None)

  • asserted_count (int)

  • corrected_count (int)

  • retracted_count (int)

  • support_count (int)

  • versions (tuple[object, ...])

gestaltdb.rules.absence_node_id(rule_version_id, absence)[source]

Return the deterministic explanation node ID for one absence check.

Parameters:
Return type:

str

gestaltdb.rules.stratify_rules(rules)[source]

Return a deterministic stratum schedule or raise on unstratifiable rules.

Positive dependencies form the recursion components. Every negative edge must point strictly downward: a negative edge inside one positive component, or any remaining cycle after ordering, fails closed. Predicates without rules keep their positions as zero-round components. The effective closed-world scope is the union of all active rule declarations.

Parameters:

rules (Iterable[RuleVersion])

Return type:

tuple[tuple[str, …], …]

Modal Evaluation

Bounded four-valued modal evaluation over temporal epistemic claims.

class gestaltdb.modal.AccessibilityKind(*values)[source]

Bases: str, Enum

Independent accessibility frames used by modal operators.

class gestaltdb.modal.ModalEntailmentResult(status, confidence, explanation)[source]

Bases: object

Four-valued result and deterministic finite evaluation evidence.

Parameters:
exception gestaltdb.modal.ModalError[source]

Bases: TemporalVersionError

Base error for invalid modal formulas and evaluations.

Sibling of gestaltdb.reasoning.model.ReasoningError under TemporalVersionError (both are ValueError). Callers that need to catch every bounded-evaluation failure must catch (ReasoningError, TemporalVersionError) or ValueError; except ReasoningError alone misses rule/modal bounds.

exception gestaltdb.modal.ModalEvaluationLimitError[source]

Bases: ModalError

Raised 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: object

A validated finite atom, connective, or nested modal expression.

Mapping forms use subject/predicate/object for atoms, formula for unary operators, and operands for AND/OR. Every modal operator requires an explicit agent.

Parameters:
class gestaltdb.modal.ModalOperator(*values)[source]

Bases: str, Enum

Operators accepted by ModalExpression.

gestaltdb.modal.evaluate_modal(view, expression, *, world, max_depth=4, max_states=1000)[source]

Evaluate one formula under a pinned read view and explicit finite bounds.

Parameters:
Return type:

ModalEntailmentResult

Consistent Read Views

Consistent temporal read views and reproducible source provenance.

class gestaltdb.readview.CurrentGraphReadView(graph, provenance)[source]

Bases: object

Graph facade whose current-state reads share one backend snapshot.

Parameters:

provenance (CurrentReadProvenance | None)

property edges

Read-only edge capabilities bound to this view’s snapshot.

property indexes

Current index reads without DDL authority on this snapshot.

property lookup

Index-backed decoded entity reads bound to this snapshot.

property nodes

Read-only node capabilities bound to this view’s snapshot.

property traversal

Typed adjacency reads bound to this snapshot, without rebuilds.

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: object

Identity of one verifiable mutable-state backend snapshot.

Parameters:
  • database_id (str)

  • snapshot_sequence (int)

  • backend_name (str)

  • backend_layout_version (int)

  • serializer_name (str)

  • serializer_format_version (int)

  • backend_snapshot_kind (str)

  • state_sha256 (str)

  • snapshot_kind (str)

  • format_version (int)

verify_snapshot_store(view)[source]

Verify a provenance-free view is pinned to the captured sequence.

Return type:

None

verify_source(graph)[source]

Verify that the mutable source has not advanced since capture.

Return type:

None

verify_view(view)[source]

Verify a newly opened current read view has this exact identity.

Return type:

None

class gestaltdb.readview.GraphReadView(graph, provenance)[source]

Bases: object

A fixed temporal commit horizon and default valid-time instant.

Parameters:

provenance (ReadViewProvenance)

build_sampler_snapshot(output_path, **kwargs)[source]

Build a sampler snapshot from this exact source view.

evaluate_modal(expression, *, world, max_depth=4, max_states=1000)[source]

Evaluate a bounded modal expression at this view’s fixed horizon.

exception gestaltdb.readview.ProvenanceMismatchError[source]

Bases: ValueError

Raised 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: object

JSON-serializable identity of one immutable temporal graph view.

Parameters:
  • database_id (str)

  • commit_horizon (int)

  • commit_system_time_us (int | None)

  • commit_marker_sha256 (str | None)

  • visibility_sha256 (str)

  • valid_time_us (int)

  • backend_name (str)

  • backend_layout_version (int)

  • serializer_name (str)

  • serializer_format_version (int)

  • snapshot_kind (str)

  • format_version (int)

classmethod from_dict(value)[source]

Parse and authenticate a provenance mapping.

Parameters:

value (dict)

Return type:

ReadViewProvenance

to_dict()[source]

Return canonical JSON-compatible provenance including its token.

Return type:

dict

to_json()[source]

Serialize provenance deterministically.

Return type:

str

property token: str

Return a stable digest over all provenance fields.

verify_source(graph)[source]

Verify that graph still represents this source view.

Return type:

None

gestaltdb.readview.parse_read_provenance(value)[source]

Parse temporal or mutable provenance by its explicit snapshot kind.

Parameters:

value (dict)

Type Hierarchies

Immutable validated type hierarchies for relations and node labels.

RelationHierarchy maps child relation names to their direct parents, so a sampler asked for Transport_Edge can expand to Ocean_Freight, Air_Freight and Rail. LabelHierarchy applies the same contract to native node labels, so an opt-in Cypher query for :Transport_Edge can match nodes labelled with any transitive sub-type.

Both classes are standalone: they validate an explicit schema of external name strings and couple to no ontology compiler, storage backend or query planner. Names absent as keys are roots. Lookups of unknown names return the name itself, so a hierarchy only adds matches for declared children.

class gestaltdb.hierarchy.LabelHierarchy(parents=None)[source]

Bases: TypeHierarchy

Immutable validated sub-type hierarchy for native node labels.

Parameters:

parents (Mapping[str, Sequence[str]] | None)

class gestaltdb.hierarchy.RelationHierarchy(parents=None)[source]

Bases: TypeHierarchy

Immutable validated sub-property hierarchy for relation names.

Parameters:

parents (Mapping[str, Sequence[str]] | None)

class gestaltdb.hierarchy.TypeHierarchy(parents=None)[source]

Bases: object

Immutable validated parent/child name hierarchy.

Parameters:

parents (Mapping[str, Sequence[str]] | None) – Mapping of child name to its direct parent names. Names absent as keys are roots. Duplicate parents for one child are deduplicated; duplicate child entries are rejected.

Raises:

ValueError – On empty names, self-parenting, duplicate child entries, or parent/child cycles.

closure(name)[source]

Return a name plus all its transitive sub-types.

Parameters:

name (str)

Return type:

tuple[str, …]

expand(names)[source]

Expand parent names to their closures, preserving first-seen order.

Parameters:

names (Sequence[str])

Return type:

tuple[str, …]

classmethod from_definitions(definitions)[source]

Build a hierarchy from (child, parents) pairs in order.

Parameters:

definitions (Sequence[tuple[str, Sequence[str]]])

Return type:

TypeHierarchy

parents_of(name)[source]

Return the direct parents of a name (empty for roots).

Parameters:

name (str)

Return type:

tuple[str, …]

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: object

Configuration for one typed sampling hop.

Parameters:
  • edge_type (str)

  • direction (Literal['out', 'in', 'any'])

  • sample_size (int)

class gestaltdb.sampling.SamplingPattern(hops)[source]

Bases: object

Ordered 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:

SamplingHop

gestaltdb.sampling.as_sampling_pattern(pattern)[source]

Normalize a sampling pattern to SamplingPattern.

Parameters:

pattern (SamplingPattern | Iterable[SamplingHop | Mapping[str, object]])

Return type:

SamplingPattern

Sampling Engine

Array-native sampler engine.

class gestaltdb.sampling.engine.LayeredSampleBatch(seed_nodes, layers)[source]

Bases: object

Neighbor samples with one result block preserved per requested layer.

Parameters:
to_blocks(snapshot)[source]

Convert layer blocks to bipartite message-passing blocks.

Each layer’s SampledNeighbors groups sampled edges by input node. This method repackages each layer as one bipartite block with plain NumPy arrays, consumable by DGL/PyG-style gather-scatter loops without a framework dependency.

Every block maps edge_index columns to unique sorted node frames: row 0 indexes src_nodes (sampled neighbors, the message sources) and row 1 indexes dst_nodes (layer inputs, the message destinations). One column is emitted per sampled edge occurrence, so duplicate draws under replace=True are preserved. edge_ids and relation_ids align with the columns for weight or relation-specific lookups.

The blocks are oriented by message flow (neighbor to input), not by graph-edge orientation: for direction="out" layers the underlying graph edges point from input to neighbor, while for direction="in" layers they point from neighbor to input. Graph orientation is not encoded; callers that need it must consult the NeighborSamplingSpec values they passed to sample_layers.

Parameters:

snapshot (SamplerSnapshot) – Snapshot providing relation IDs for sampled edges.

Returns:

One block dict per layer with src_nodes, dst_nodes, edge_index, edge_ids, and relation_ids. Empty layers yield zero-column blocks with edge_index shaped (2, 0).

Raises:

ValueError – If a layer’s offsets do not align with its inputs and sampled edges.

Return type:

list[dict[str, TypeAliasForwardRef(‘gestaltdb.sampling._arrays.IntArray’)]]

Examples

Aggregate one message-passing step per layer:

1for block in batch.to_blocks(snapshot):
2    messages = features[block["src_nodes"]][block["edge_index"][0]]
to_blocks_external(snapshot)[source]

Convert to_blocks() output to external IDs.

src_nodes, dst_nodes, edge_ids, and relation_ids are mapped through the snapshot’s external ID arrays; edge_index local positions are unchanged.

Parameters:

snapshot (SamplerSnapshot)

Return type:

list[dict[str, TypeAliasForwardRef(‘gestaltdb.sampling._arrays.Array’)]]

to_external(snapshot)[source]

Convert compact IDs in every layer to external IDs.

Parameters:

snapshot (SamplerSnapshot)

Return type:

dict[str, object]

to_pyg_blocks(snapshot)[source]

Convert to_blocks() output to PyTorch tensors.

Each layer becomes a PyG-style bipartite block dictionary with src_nodes, dst_nodes, edge_index, edge_ids, and relation_ids tensors plus the layer’s temporal edge-version arrays when present. ID tensors use torch.long; temporal version IDs stay string-array metadata, matching SampledSubgraphBatch.to_pyg. The dictionaries convert directly into torch_geometric bipartite blocks; no torch_geometric dependency is required or imported here.

Parameters:

snapshot (SamplerSnapshot) – Snapshot providing relation IDs for sampled edges.

Returns:

One tensor dictionary per layer, in layer order.

Raises:

ImportError – If torch is not installed.

Return type:

list[dict[str, object]]

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: object

Batched neighbor-sampling result.

Parameters:
  • input_nodes (gestaltdb.sampling._arrays.IntArray)

  • edge_indices (gestaltdb.sampling._arrays.IntArray)

  • neighbor_nodes (gestaltdb.sampling._arrays.IntArray)

  • offsets (gestaltdb.sampling._arrays.IntArray)

  • edge_version_ids (TypeAliasForwardRef('gestaltdb.sampling._arrays.StringArray') | None)

  • valid_from_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • valid_to_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • valid_to_open (TypeAliasForwardRef('gestaltdb.sampling._arrays.BoolArray') | None)

input_nodes

Input compact node IDs, one per requested seed node.

Type:

IntArray

edge_indices

Sampled global compact edge IDs concatenated across all input nodes.

Type:

IntArray

neighbor_nodes

Neighbor compact node IDs aligned with edge_indices.

Type:

IntArray

offsets

Prefix-sum offsets into edge_indices and neighbor_nodes. Samples for input_nodes[i] are in offsets[i]:offsets[i + 1].

Type:

IntArray

edge_version_ids

Sampled edge-version IDs for temporal snapshots.

Type:

StringArray | None

valid_from_us

Inclusive valid starts aligned with sampled edges.

Type:

IntArray | None

valid_to_us

Exclusive valid ends, with zero for open intervals.

Type:

IntArray | None

valid_to_open

Open-end masks aligned with sampled edges.

Type:

BoolArray | None

Examples

Iterate over sampled neighbors for each input node:

1result = engine.sample_neighbors([0, 1], fanout=5)
2for row, node in enumerate(result.input_nodes):
3    start, end = result.offsets[row], result.offsets[row + 1]
4    neighbors = result.neighbor_nodes[start:end]
to_external(snapshot)[source]

Convert compact IDs to external IDs; -1 padding maps to None.

Parameters:

snapshot (SamplerSnapshot)

Return type:

dict[str, TypeAliasForwardRef(‘gestaltdb.sampling._arrays.Array’)]

class gestaltdb.sampling.engine.SamplerEngine(snapshot, *, seed=None)[source]

Bases: object

High-throughput sampler over a static SamplerSnapshot.

The engine samples compact integer arrays instead of materializing GraphDB Node/Edge objects. It supports direction-aware traversal, relation-aware traversal, heterogeneous layers, first-order random walks, 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

Build a tiny snapshot and sample by external IDs (no KV backend needed):

 1>>> from tempfile import TemporaryDirectory
 2>>> from gestaltdb.sampling import SamplerEngine, SamplerSnapshot
 3>>> with TemporaryDirectory() as root:
 4...     snapshot = SamplerSnapshot.from_edge_arrays(
 5...         root + "/snapshot", external_node_ids=["a", "b"],
 6...         external_edge_ids=["ab"], external_relation_ids=["knows"],
 7...         src_int=[0], dst_int=[1], rel_int=[0])
 8...     engine = SamplerEngine.load(snapshot.path, mode="ram", seed=7)
 9...     batch = engine.sample_neighbors_external(["a"], fanout=1, relations=["knows"])
10...     assert batch.to_external(engine.snapshot)["neighbor_nodes"].tolist() == ["b"]

Reuse the engine across batches. Use GraphDB.build_sampler_snapshot to build from database records. Sampling outputs use compact integer IDs until explicitly converted; see the typed-sampling guide for layers, walks, negative sampling, and RAM versus memory-mapped lifetimes.

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.

Parameters:
  • src (int) – Compact source node ID.

  • rel (int) – Compact relation ID.

  • dst (int) – Compact target node ID.

  • temporal (TemporalContext | None)

  • policy (str)

  • window_policy (str)

Returns:

True if (src, rel, dst) exists in the snapshot positives.

Return type:

bool

iter_random_walks(start_nodes=None, walk_length=40, *, walks_per_node=1, batch_size=1024, shuffle=False, walk_type='random', **walk_options)[source]

Stream walk batches, one seed pass per round.

None seeds means every snapshot node; explicit compact IDs retain order and duplicates. Each round optionally shuffles seed positions. walk_type selects random, node2vec or policy; extra keywords go to that kernel. Validation is eager, generation lazy. Unshuffled all-node iteration uses batch-sized seed buffers; explicit seeds are copied once, and global shuffling needs O(seed count) order storage. Results need O(batch_size * walk_length) storage, in addition to existing kernel candidate buffers. Consume/discard batches to bound corpus memory. Keep the snapshot alive throughout iteration.

Uses this engine’s RNG: replay requires the same initial state, seed cohort, options and consumption order. Batch size alone does not change concatenated walks. Do not interleave other RNG users for this promise. Use worker_engine for independent streams; no parallel execution or invariance across worker counts is implied.

Parameters:
Return type:

Iterator[RandomWalkBatch]

classmethod load(path, *, mode='ram', seed=None, **_kwargs)[source]

Load a sampler engine from a snapshot path.

Parameters:
  • path (str | Path) – Snapshot directory created by SamplerSnapshot.

  • mode (str) – "ram" eagerly loads arrays; "memmap" memory maps them.

  • seed (int | None) – Optional RNG seed.

  • **_kwargs – Reserved for future cache/prefetch options.

Returns:

Initialized SamplerEngine.

Raises:

ValueError – If mode is not "ram" or "memmap".

Return type:

SamplerEngine

Examples

Load a RAM-backed engine:

1engine = SamplerEngine.load("snapshot", mode="ram", seed=13)
sample_edges(count, *, relations=None, replace=False, strategy='uniform', weights=None, balanced=False, temporal=None, window_policy='overlap')[source]

Sample compact edge IDs, optionally restricted by relation.

Parameters:
  • count (int) – Maximum number of edge IDs to return.

  • relations (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional compact relation IDs to restrict candidates.

  • replace (bool) – Whether to sample with replacement.

  • strategy (str) – "uniform" or "weighted".

  • weights (Sequence[float] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Per-edge weights for strategy="weighted".

  • balanced (bool) – When true, split count into deterministic equal shares across the distinct relation IDs present in the candidate set. Groups smaller than their share return fewer edges when replace=False; unused quota is not borrowed from other relations.

  • temporal (TemporalContext | None) – Optional point or finite-window edge validity filter. Requires a temporal format-v2 snapshot.

  • window_policy (str) – "overlap" keeps edge versions whose validity intersects the window; "contained" keeps versions fully inside it. Ignored for point contexts.

Return type:

gestaltdb.sampling._arrays.IntArray

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]] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – 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] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional example instants aligned with positives. Required by example-time policies and by leakage-safe candidate filtering unless temporal provides 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_triples is not shaped (n, 3).

  • RuntimeError – If a valid non-positive negative cannot be found within the configured retry budget.

Return type:

TypeAliasForwardRef(‘gestaltdb.sampling._arrays.IntArray’) | tuple[TypeAliasForwardRef(‘gestaltdb.sampling._arrays.IntArray’), dict[str, int]]

sample_induced_subgraph(num_nodes, *, max_edges, relations=None, temporal=None, window_policy='overlap', normalize=False)[source]

Sample a bounded node-induced subgraph.

Nodes are drawn uniformly without replacement from the snapshot. Every eligible edge whose endpoints are both in the sampled node set is included after optional relation and temporal filters. If the eligible edge count exceeds max_edges, the call fails closed instead of truncating or silently sampling edges. The result contains no positive or negative triples; use sample_subgraph for edge-seeded training batches.

Parameters:
  • num_nodes (int) – Number of nodes to draw without replacement.

  • max_edges (int) – Maximum eligible induced edges allowed. Exceeding it raises ValueError.

  • relations (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional compact relation IDs to restrict induced edges.

  • temporal (TemporalContext | None) – Optional point or finite-window validity filter applied to induced edges.

  • window_policy (str) – Window matching policy, "overlap" or "contained".

  • normalize (bool) – When true, attach GraphSAINT-style unbiased node_weights and edge_weights. Uniform without-replacement node sampling includes each node with probability num_nodes / num_nodes_snapshot and each distinct node pair with probability num_nodes * (num_nodes - 1) / (num_nodes_snapshot * (num_nodes_snapshot - 1)), so node weights are the reciprocal of the first probability and non-loop edge weights the reciprocal of the second. Self-loops are node-inclusion events and carry the node weight. Weights are exact for the unfiltered node draw; relation and temporal filters restrict which edges are returned but do not change the draw probabilities, so filtered batches keep the same weights.

Returns:

SampledSubgraphBatch with sampled nodes and all eligible induced edges. positives and negatives are empty.

Raises:

ValueError – If counts are invalid, num_nodes exceeds the snapshot, the induced edge count exceeds max_edges, or normalize is not a boolean.

Return type:

SampledSubgraphBatch

Examples

Draw a bounded GraphSAINT-style node subgraph:

1batch = engine.sample_induced_subgraph(
2    256, max_edges=50_000, normalize=True
3)
sample_layers(seed_nodes, layers, *, temporal=None, window_policy='overlap')[source]

Sample layered neighborhoods; per-hop config lives in NeighborSamplingSpec.

Parameters:
Return type:

LayeredSampleBatch

sample_layers_external(seed_nodes, layers, *, temporal=None, window_policy='overlap')[source]

External-ID layered sampling; returns a compact batch needing to_external.

Parameters:
Return type:

LayeredSampleBatch

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] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – 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] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | 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:

SampledSubgraphBatch containing one merged sampled graph.

Return type:

SampledSubgraphBatch

Examples

Sample a two-hop undirected context:

1batch = 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', strategy='uniform', balanced=False)[source]

Sample neighbors for each input node.

Parameters:
  • nodes (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – 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] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | 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.

  • strategy (str) – "uniform" or "weighted". Weighted sampling uses the snapshot’s edge weights.

  • balanced (bool) – When true, split fanout into deterministic equal shares across the relation IDs present for each input node. Groups smaller than their share return fewer edges when replace=False; unused quota is not borrowed from other relations. Default uniform allocation is unchanged.

Returns:

SampledNeighbors with concatenated edge and neighbor arrays.

Raises:

ValueError – If fanout is negative or direction is invalid.

Return type:

SampledNeighbors

Examples

Relation-aware neighbor sampling:

1sampled = engine.sample_neighbors(
2    [drug_id], fanout=10, direction="out", relations=[binds_id]
3)
sample_neighbors_external(nodes, fanout, *, direction='out', relations=None, replace=False, strategy='uniform', temporal=None, window_policy='overlap', causal_policy='none', balanced=False)[source]

External-ID neighbor sampling; returns a compact batch needing to_external.

Parameters:
Return type:

SampledNeighbors

sample_node2vec_walks(start_nodes, walk_length, *, return_factor=1.0, inout_factor=1.0, direction='out', relations=None, dead_end='terminate', temporal=None, window_policy='overlap')[source]

Sample second-order biased walks in the node2vec style.

From the second transition on, each candidate edge is weighted by the shortest-path distance d between the previous node and the candidate endpoint: 1 / return_factor for d == 0 (return), 1 for d == 1, and 1 / inout_factor for d >= 2. Small inout_factor values push walks outward (DFS-like); large values keep them local (BFS-like). Large return_factor values discourage immediately revisiting the previous node. The first transition of every walk is uniform over the directed candidates. Static snapshot edge weights multiply the bias factors.

Candidates follow direction and the optional relations and temporal filters. Distances are measured on the undirected incident adjacency of the static snapshot, ignoring relation and temporal filters; document this when mixing biased walks with filters.

Parameters:
  • start_nodes (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – Compact node IDs where walks begin.

  • walk_length (int) – Transitions per walk.

  • return_factor (float) – Positive finite p; the return weight is 1 / return_factor.

  • inout_factor (float) – Positive finite q; the outward weight is 1 / inout_factor.

  • direction (str) – "out", "in", or "any" traversal.

  • relations (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional compact relation IDs to restrict candidates.

  • dead_end (str) – "terminate" stops the walk, "restart" jumps back to the start node with -1 edge and relation IDs.

  • temporal (TemporalContext | None) – Optional point or finite-window validity filter.

  • window_policy (str) – "overlap" or "contained" window matching.

Returns:

RandomWalkBatch with the same -1 gap conventions as sample_random_walks().

Raises:

ValueError – If factors are not positive finite numbers, options are invalid, or IDs are out of range.

Return type:

RandomWalkBatch

sample_node2vec_walks_external(start_nodes, walk_length, *, return_factor=1.0, inout_factor=1.0, direction='out', relations=None, dead_end='terminate', temporal=None, window_policy='overlap')[source]

External-ID node2vec input; returns a compact batch needing to_external.

Parameters:
Return type:

RandomWalkBatch

sample_nodes(count, *, node_types=None, replace=False, strategy='uniform', weights=None, balanced=False, temporal=None, window_policy='overlap')[source]

Sample compact node IDs, optionally restricted by node type.

Parameters:
  • count (int) – Maximum number of node IDs to return.

  • node_types (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional compact node type IDs to restrict candidates.

  • replace (bool) – Whether to sample with replacement.

  • strategy (str) – "uniform", "weighted", or "degree".

  • weights (Sequence[float] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Per-node weights for strategy="weighted".

  • balanced (bool) – When true, split count into deterministic equal shares across the distinct node type IDs present in the candidate set. Groups smaller than their share return fewer nodes when replace=False; unused quota is not borrowed from other types. Untyped nodes (compact type -1) form their own group when no node_types filter excludes them.

  • temporal (TemporalContext | None) – Optional point or finite-window node availability filter. Requires a temporal format-v2 snapshot.

  • window_policy (str) – "overlap" keeps nodes whose availability intersects the window; "contained" keeps nodes whose availability is fully inside it. Ignored for point contexts.

Return type:

gestaltdb.sampling._arrays.IntArray

sample_packed_subgraphs(seed_edge_groups, fanouts, *, direction='any', relations=None, negative_config=None, temporal=None, window_policy='overlap', causal_policy='none')[source]

Sample one independent subgraph per seed group in a single batch.

Each group is sampled exactly as sample_subgraph() would sample it alone, then repackaged into one batch with per-component frames: graph_node_offsets and graph_edge_offsets mark each component’s node and edge rows, and all local IDs (senders, receivers, positives, negatives) are rebased to the concatenated node frame. Global node and edge IDs may repeat across components; each component’s slice is self-contained. Positive rows appear in seed-group order (len(group) rows per component when no temporal filtering drops seed edges) and negative rows align with positive rows. Temporal arrays, example times, and negative diagnostics are concatenated or summed component-wise.

Parameters:
  • seed_edge_groups (Sequence[Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')]) – One compact seed-edge sequence per component. Groups are validated exactly as in sample_subgraph().

  • fanouts (Sequence[int]) – Fanout per hop, shared by every component.

  • direction (str) – Traversal direction for context expansion.

  • relations (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional relation filter for context expansion.

  • negative_config (HardNegativeConfig | None) – Optional hard-negative configuration shared by every component.

  • 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 propagated through context hops.

Returns:

SampledSubgraphBatch with graph_node_offsets and graph_edge_offsets of length len(seed_edge_groups) + 1.

Raises:

ValueError – If options are invalid or a group fails sample_subgraph() validation.

Return type:

SampledSubgraphBatch

Examples

Sample two disjoint training neighborhoods at once:

1batch = engine.sample_packed_subgraphs(
2    [[10, 11], [42]], fanouts=[8, 4]
3)
4first_nodes = batch.node_ids_global[
5    batch.graph_node_offsets[0]:batch.graph_node_offsets[1]
6]
sample_partition_block(num_partitions, *, max_edges, relations=None, temporal=None, window_policy='overlap', normalize=False)[source]

Sample one uniformly chosen block of a random balanced partition.

Nodes are randomly permuted with the engine RNG, split into num_partitions contiguous chunks with sizes differing by at most one, and one chunk is chosen uniformly at random. Every eligible edge induced by the chosen chunk is returned after optional relation and temporal filters; exceeding max_edges fails closed. Partitions are uniformly random, not cut-optimized: unlike METIS-based Cluster-GCN, no cut quality is claimed. The result contains no positive or negative triples.

Parameters:
  • num_partitions (int) – Number of balanced partitions, at least one.

  • max_edges (int) – Maximum eligible induced edges allowed. Exceeding it raises ValueError.

  • relations (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional compact relation IDs to restrict induced edges.

  • temporal (TemporalContext | None) – Optional point or finite-window validity filter applied to induced edges.

  • window_policy (str) – Window matching policy, "overlap" or "contained".

  • normalize (bool) – When true, attach exact unbiased node_weights and edge_weights. Every node is selected with probability 1 / num_partitions by symmetry, so node weights equal num_partitions. A distinct node pair is selected with probability (sum_c C(size_c, 2) / C(N, 2)) / num_partitions over the realized chunk sizes, and non-loop edges carry its reciprocal. Self-loops are node-inclusion events and carry the node weight. Relation and temporal filters do not change the draw probabilities.

Returns:

SampledSubgraphBatch with the chosen partition’s nodes and all eligible induced edges.

Raises:

ValueError – If counts are invalid, the induced edge count exceeds max_edges, normalize is not a boolean, or the partition invariant is violated.

Return type:

SampledSubgraphBatch

Examples

Sample a Cluster-GCN-style random partition block:

1batch = engine.sample_partition_block(
2    8, max_edges=50_000, normalize=True
3)
sample_policy_walks(start_nodes, walk_length, *, policy=None, **walk_options)[source]

Sample opt-in constrained walks without changing legacy defaults.

WalkPolicy composes avoidance, typed/state-machine schemas, explicit chronology and transition units. Direction/relation/query-time/restart options go to the kernel. Stream with iter_random_walks(walk_type="policy", policy=...). Temporal policies add masked times/version IDs/provenance.

Parameters:
  • start_nodes (Sequence[int] | ndarray)

  • walk_length (int)

  • policy (WalkPolicy | None)

Return type:

RandomWalkBatch

sample_random_walks(start_nodes, walk_length, *, direction='out', relations=None, metapath=None, restart_probability=0.0, strategy='uniform', dead_end='terminate', temporal=None, window_policy='overlap')[source]

Sample fixed-width first-order walks (compact IDs; -1 pads gaps/restarts).

metapath repeats cyclically, advancing only on edge steps, and is mutually exclusive with relations. lengths counts populated transitions including restarts; edge_indices are compact edge rows.

Parameters:
  • start_nodes (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array'))

  • walk_length (int)

  • direction (str)

  • relations (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None)

  • metapath (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None)

  • restart_probability (float)

  • strategy (str)

  • dead_end (str)

  • temporal (TemporalContext | None)

  • window_policy (str)

Return type:

RandomWalkBatch

sample_random_walks_external(start_nodes, walk_length, *, direction='out', relations=None, metapath=None, restart_probability=0.0, strategy='uniform', dead_end='terminate', temporal=None, window_policy='overlap')[source]

External-ID walk input (incl. metapath); returns compact batch needing to_external.

Parameters:
Return type:

RandomWalkBatch

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] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – 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] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | 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:

SampledSubgraphBatch with local node IDs in positive and negative triples.

Return type:

SampledSubgraphBatch

Examples

Sample positives with hard negatives:

1batch = engine.sample_subgraph(
2    seed_edges,
3    fanouts=[12, 8],
4    negative_config=HardNegativeConfig(negatives_per_positive=8),
5)
sample_subgraph_external(seed_edges, fanouts, *, direction='any', relations=None, negative_config=None, temporal=None, window_policy='overlap', causal_policy='none')[source]

External-ID subgraph seeds; temporal dup edge IDs raise, use compact rows.

Parameters:
Return type:

SampledSubgraphBatch

sample_subgraphs(*args, **kwargs)[source]

Batched alias for sample_subgraph.

Return type:

SampledSubgraphBatch

worker_engine(worker_id, *, num_workers)[source]

Build a sibling engine holding this worker’s RNG substream.

The snapshot object is shared, not copied; only the RNG stream is independent and deterministic per (seed, worker_id, num_workers). Requires the engine to have been constructed with an integer seed (or None for entropy-derived independence without reproducibility).

Parameters:
  • worker_id (int)

  • num_workers (int)

Return type:

SamplerEngine

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, edge_weights=<factory>, node_lookup_order=<factory>, edge_lookup_order=<factory>, relation_lookup_order=<factory>, node_lookup_ids=<factory>, edge_lookup_ids=<factory>, relation_lookup_ids=<factory>, positive_membership_triples=None)[source]

Bases: object

Read-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 (gestaltdb.sampling._arrays.StringArray)

  • node_type_ids (gestaltdb.sampling._arrays.IntArray)

  • external_edge_ids (gestaltdb.sampling._arrays.StringArray)

  • external_relation_ids (gestaltdb.sampling._arrays.StringArray)

  • relation_src_type_ids (gestaltdb.sampling._arrays.IntArray)

  • relation_dst_type_ids (gestaltdb.sampling._arrays.IntArray)

  • src_int (gestaltdb.sampling._arrays.IntArray)

  • dst_int (gestaltdb.sampling._arrays.IntArray)

  • rel_int (gestaltdb.sampling._arrays.IntArray)

  • out (CSRAdjacency)

  • in_ (CSRAdjacency)

  • incident (CSRAdjacency)

  • relation_out (CSRAdjacency)

  • relation_in (CSRAdjacency)

  • positive_triples (gestaltdb.sampling._arrays.IntArray)

  • edge_version_ids (TypeAliasForwardRef('gestaltdb.sampling._arrays.StringArray') | None)

  • valid_from_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • valid_to_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • valid_to_open (TypeAliasForwardRef('gestaltdb.sampling._arrays.BoolArray') | None)

  • edge_system_time_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • edge_commit_ids (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • temporal_out (CSRAdjacency | None)

  • temporal_in (CSRAdjacency | None)

  • positive_history_triples (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • positive_history_indptr (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • positive_history_edge_indices (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • node_history_indptr (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • node_valid_from_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • node_valid_to_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • node_valid_to_open (TypeAliasForwardRef('gestaltdb.sampling._arrays.BoolArray') | None)

  • edge_weights (gestaltdb.sampling._arrays.FloatArray)

  • node_lookup_order (gestaltdb.sampling._arrays.IntArray)

  • edge_lookup_order (gestaltdb.sampling._arrays.IntArray)

  • relation_lookup_order (gestaltdb.sampling._arrays.IntArray)

  • node_lookup_ids (gestaltdb.sampling._arrays.StringArray)

  • edge_lookup_ids (gestaltdb.sampling._arrays.StringArray)

  • relation_lookup_ids (gestaltdb.sampling._arrays.StringArray)

  • positive_membership_triples (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

path

Directory containing the persisted snapshot arrays and metadata.

Type:

Path

metadata

JSON-compatible snapshot metadata.

Type:

dict

external_node_ids

External node identifiers ordered by compact node ID.

Type:

StringArray

node_type_ids

Integer node type ID per compact node, or -1 when unknown.

Type:

IntArray

external_edge_ids

External edge identifiers ordered by compact edge ID.

Type:

StringArray

external_relation_ids

External relation identifiers ordered by compact relation ID.

Type:

StringArray

relation_src_type_ids

Expected source node type ID per relation, or -1 when the source type is heterogeneous or unknown.

Type:

IntArray

relation_dst_type_ids

Expected target node type ID per relation, or -1 when the target type is heterogeneous or unknown.

Type:

IntArray

src_int

Source compact node ID for each edge.

Type:

IntArray

dst_int

Target compact node ID for each edge.

Type:

IntArray

rel_int

Relation compact ID for each edge.

Type:

IntArray

edge_weights

Non-negative sampling weight for each edge.

Type:

FloatArray

out

Source-to-edge CSR adjacency.

Type:

CSRAdjacency

in_

Target-to-edge CSR adjacency.

Type:

CSRAdjacency

incident

Undirected incident edge CSR adjacency.

Type:

CSRAdjacency

relation_out

Source/relation CSR keyed by node*relations+relation.

Type:

CSRAdjacency

relation_in

Target/relation CSR (sparse dirs on new snapshots).

Type:

CSRAdjacency

positive_triples

Array of (src, rel, dst) positives.

Type:

IntArray

positive_membership_triples

Sorted unique static membership keys; temporal snapshots reuse positive_history_triples instead.

Type:

IntArray | None

edge_version_ids

Temporal edge-version UUIDs aligned with edge arrays, or None for non-temporal and legacy v1 snapshots.

Type:

StringArray | None

valid_from_us

Inclusive valid-time starts in UTC epoch microseconds.

Type:

IntArray | None

valid_to_us

Exclusive valid-time ends; open ends use zero together with valid_to_open.

Type:

IntArray | None

temporal_out

Source/relation CSR ordered by valid start.

Type:

CSRAdjacency | None

temporal_in

Target/relation CSR ordered by valid start.

Type:

CSRAdjacency | None

positive_history_triples

Unique temporal positive triples.

Type:

IntArray | None

positive_history_indptr

Group offsets into positive history edge rows.

Type:

IntArray | None

positive_history_edge_indices

Edge rows ordered by triple and valid start.

Type:

IntArray | None

node_history_indptr

Node offsets into availability intervals.

Type:

IntArray | None

node_valid_from_us

Node availability interval starts.

Type:

IntArray | None

node_valid_to_us

Node availability interval ends.

Type:

IntArray | None

node_valid_to_open

Node availability open-end masks.

Type:

BoolArray | None

node_lookup_order

Compact node IDs sorted by external node ID.

Type:

IntArray

edge_lookup_order

Compact edge IDs sorted by external edge ID.

Type:

IntArray

relation_lookup_order

Compact relation IDs sorted by external relation ID.

Type:

IntArray

node_lookup_ids

External node IDs in sorted lookup order.

Type:

StringArray

edge_lookup_ids

External edge IDs in sorted lookup order.

Type:

StringArray

relation_lookup_ids

External relation IDs in sorted lookup order.

Type:

StringArray

Examples

Persist and reload compact arrays without an embedded backend:

 1>>> from tempfile import TemporaryDirectory
 2>>> from gestaltdb.sampling import SamplerSnapshot
 3>>> with TemporaryDirectory() as root:
 4...     snapshot = SamplerSnapshot.from_edge_arrays(
 5...         root + "/snapshot", external_node_ids=["drug-1", "protein-1"],
 6...         external_edge_ids=["edge-1"], external_relation_ids=["binds"],
 7...         src_int=[0], dst_int=[1], rel_int=[0],
 8...         node_type_values=["drug", "protein"])
 9...     loaded = SamplerSnapshot.load(snapshot.path)
10...     assert loaded.external_node_id(loaded.node_int("drug-1")) == "drug-1"
11...     assert loaded.num_edges == 1

Use a fresh output directory; SamplerEngine continues to sampling.

append_from_graph(graph, edge_keys, *, edge_type_property='type', edge_weight_property=None, node_type_property='kind')[source]

Append graph edges to this snapshot without a full rebuild.

Node and relation maps grow append-only: existing compact IDs never change, so stored batches stay valid. Adjacency indexes are rebuilt from the extended rows (documented O(E) cost — measure before assuming append beats rebuild at scale). The snapshot identity is preserved while append_generation advances and the source version is restamped from the graph.

Only snapshots built by build carry the recorded interpretation (type properties) required to encode new rows; array-built snapshots are rejected. Temporal snapshots are rejected. Duplicate external edge IDs fail closed. No live readers may hold this snapshot path (including memmap loads) across the call.

Parameters:
  • edge_type_property (str)

  • edge_weight_property (str | None)

  • node_type_property (str)

Return type:

dict

classmethod build(graph, output_path, *, node_filter=None, edge_filter=None, edge_type_property='type', edge_weight_property=None, 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, and key_to_string.

  • output_path (str | Path) – Directory where snapshot metadata and .npy arrays are written.

  • node_filter (Callable[[object], bool] | None) – Optional predicate receiving each node object. Nodes for which the predicate returns False are omitted.

  • edge_filter (Callable[[object], bool] | None) – Optional predicate receiving each edge object. Edges for which the predicate returns False are omitted.

  • edge_type_property (str) – Edge property containing the relation identifier.

  • edge_weight_property (str | None) – Optional edge property containing a finite, non-negative sampling weight. Missing values default to 1.0.

  • 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 .npy files.

  • layout (str) – Adjacency layout. Only "csr" is currently supported.

  • source_db (dict | None)

  • source_artifacts (dict | None)

  • source_provenance (dict | None)

Returns:

Loaded SamplerSnapshot pointing at output_path.

Raises:

ValueError – If reverse_relation_policy or layout is invalid.

Return type:

SamplerSnapshot

Examples

Build a snapshot from a graph database:

1snapshot = graph.build_sampler_snapshot(
2    "data/sampler",
3    edge_type_property="type",
4    node_type_property="kind",
5)
classmethod build_temporal(read_view, output_path, *, node_filter=None, edge_filter=None, edge_type_property='type', edge_weight_property=None, node_type_property='kind', time_bucket='day', source_db=None, **metadata_options)[source]

Build a temporal-history snapshot from one authenticated read view.

Parameters:
Return type:

SamplerSnapshot

check_fresh(graph)[source]

Return the recorded source version if the graph still matches it.

Raises ValueError naming the recorded and observed versions when the source moved or when either side is unverifiable. Call this before training runs that must not silently reuse invalidated data.

Return type:

dict

compact(graph=None)[source]

Canonically rebuild this snapshot in place and verify integrity.

Rebuilds every adjacency index from the current edge rows, rewrites all arrays atomically, and re-authenticates the result on load. Compact IDs for nodes and relations are never reordered: map extension stays append-only across compactions.

With graph (unfiltered snapshots only), edge rows whose external ID no longer resolves in the source are pruned first — the incremental answer to source deletions. Pruned edge rows disappear, so edge compact IDs after pruned rows shift; node and relation IDs do not. Filtered snapshots reject graph pruning: recorded filters are callables that cannot be re-applied, so pruning would silently drop intentionally excluded edges. No live readers may hold this snapshot path across the call.

Return type:

dict

edge_int(external_edge_id)[source]

Resolve one external edge ID; ambiguous under temporal dup rows.

Return type:

int

edge_ints(external_edge_ids)[source]

Resolve external edge IDs to compact integer IDs.

Parameters:

external_edge_ids (Sequence[object])

Return type:

gestaltdb.sampling._arrays.IntArray

expand_node_types(node_types, hierarchy)[source]

Expand semantic node-type names through a sub-type hierarchy.

Each name resolves to itself plus all transitive sub-types, then to the compact node-type IDs recorded in snapshot metadata. Unknown snapshot types fail closed; hierarchy-undefined names expand to themselves. Snapshots built from raw node_type_ids without recorded node_type_values cannot be expanded safely and fail closed.

Parameters:
Return type:

gestaltdb.sampling._arrays.IntArray

expand_relations(relations, hierarchy)[source]

Expand parents to transitive sub-properties, then to compact IDs.

Unknown snapshot relations fail closed; hierarchy-undefined names expand to themselves. Fanout applies to the union, not per relation.

Parameters:
Return type:

gestaltdb.sampling._arrays.IntArray

external_edge_id(edge_int)[source]

Resolve one compact edge row to its external string ID.

Return type:

str

external_node_id(node_int)[source]

Resolve one compact node row to its external string ID.

Return type:

str

external_relation_id(rel_int)[source]

Resolve one compact relation ID to its external string ID.

Return type:

str

classmethod from_arrays(output_path, *, external_node_ids, node_type_ids, external_edge_ids, external_relation_ids, src_int, dst_int, rel_int, edge_weights=None, 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.

  • edge_weights – Optional finite, non-negative sampling weight 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:

SamplerSnapshot

Examples

Use this lower-level API when all type IDs are already encoded:

 1snapshot = SamplerSnapshot.from_arrays(
 2    "snapshot",
 3    external_node_ids=["n0", "n1"],
 4    node_type_ids=[0, 1],
 5    external_edge_ids=["e0"],
 6    external_relation_ids=["r0"],
 7    src_int=[0],
 8    dst_int=[1],
 9    rel_int=[0],
10)
classmethod from_edge_arrays(output_path, *, external_node_ids, external_edge_ids, external_relation_ids, src_int, dst_int, rel_int, edge_weights=None, 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_values or node_type_ids may be provided. If node_type_values is 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] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – External node IDs ordered by compact node ID.

  • external_edge_ids (Sequence[object] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – External edge IDs ordered by compact edge ID.

  • external_relation_ids (Sequence[object] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – External relation IDs ordered by compact relation ID.

  • src_int (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – Source compact node ID per edge.

  • dst_int (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – Target compact node ID per edge.

  • rel_int (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array')) – Relation compact ID per edge.

  • edge_weights (Sequence[float] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional finite, non-negative sampling weight 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 (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – 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 (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – Optional precomputed relation source type IDs.

  • relation_dst_type_ids (Sequence[int] | TypeAliasForwardRef('gestaltdb.sampling._arrays.Array') | None) – 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_values and node_type_ids are provided.

Return type:

SamplerSnapshot

Examples

Build from NumPy arrays without any dataframe dependency:

 1snapshot = SamplerSnapshot.from_edge_arrays(
 2    "snapshot",
 3    external_node_ids=["n0", "n1", "n2"],
 4    external_edge_ids=["e0", "e1"],
 5    external_relation_ids=["r0"],
 6    src_int=np.array([0, 1]),
 7    dst_int=np.array([1, 2]),
 8    rel_int=np.array([0, 0]),
 9    node_type_values=["drug", "protein", "protein"],
10)
is_stale(graph)[source]

Return whether the source graph moved past the recorded version.

Snapshots without a recorded source version (legacy builds, graphs without verifiable read identity) always report stale: freshness cannot be proven, so it is never assumed. Temporal-only counters are deliberately not consulted — they are blind to plain mutations.

Return type:

bool

classmethod load(path, *, mmap=False)[source]

Load a sampler snapshot from disk.

Parameters:
  • path (str | Path) – Snapshot directory containing metadata.json and array files.

  • mmap (bool) – If True, arrays are loaded using NumPy memory mapping.

Returns:

Loaded SamplerSnapshot.

Raises:

ValueError – If the snapshot format version is unsupported.

Return type:

SamplerSnapshot

Examples

Load eagerly into RAM:

1snapshot = SamplerSnapshot.load("snapshot")

Load arrays through memory maps:

1snapshot = SamplerSnapshot.load("snapshot", mmap=True)
node_int(external_node_id)[source]

Resolve one external node ID to its compact integer ID.

Return type:

int

node_ints(external_node_ids)[source]

Resolve external node IDs to compact integer IDs.

Parameters:

external_node_ids (Sequence[object])

Return type:

gestaltdb.sampling._arrays.IntArray

property num_edges: int

Number of compact edge rows in this snapshot.

property num_nodes: int

Number of compact node rows in this snapshot.

property num_relations: int

Number of compact relation IDs in this snapshot.

open_source_graph(*, base_path=None, backend_options=None)[source]

Open the GraphDB referenced by this snapshot’s source metadata.

relation_int(external_relation_id)[source]

Resolve one external relation ID to its compact integer ID.

Return type:

int

relation_ints(external_relation_ids)[source]

Resolve external relation IDs to compact integer IDs.

Parameters:

external_relation_ids (Sequence[object])

Return type:

gestaltdb.sampling._arrays.IntArray

set_edge_weights(weights, *, source)[source]

Persist caller-derived per-edge sampling weights in place.

Validates length, finiteness and non-negativity, rewrites the snapshot atomically (staging directory, authenticated load, backup-swap with restore), then refreshes this object. The snapshot identity, compact IDs, adjacency and source version are untouched — weights are a derived view, not new data — while edge_weight_source records their provenance (for example "policy:degree" or "manual:confidence-v3"). No live readers may hold this snapshot path across the call.

Parameters:

source (str)

Return type:

dict

source_graph_exists(*, base_path=None)[source]

Return whether the referenced source graph path exists without opening it.

Return type:

bool

property temporal: bool

Return whether this snapshot carries validated temporal arrays.

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_time is omitted, while edges starting after the selected bucket are pruned. SamplerEngine applies the exact half-open point or window predicate before random selection.

Parameters:
Return type:

gestaltdb.sampling._arrays.IntArray

verify_source(graph)[source]

Verify provenance-bearing snapshots against a source graph.

Return type:

None

Sampled Batches

Framework-neutral sampled batch containers.

class gestaltdb.sampling.batch.NumpyBatch[source]

Bases: _RequiredNumpyBatch

Array export; temporal companions may be absent or explicitly None.

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, node_weights=None, edge_weights=None)[source]

Bases: object

Framework-neutral sampled subgraph batch.

Node IDs inside senders, receivers, positives, and negatives are local to this batch. node_ids_global maps each local node row back to the compact global node ID in the SamplerSnapshot.

Parameters:
  • node_ids_global (gestaltdb.sampling._arrays.IntArray)

  • node_type_ids (gestaltdb.sampling._arrays.IntArray)

  • senders (gestaltdb.sampling._arrays.IntArray)

  • receivers (gestaltdb.sampling._arrays.IntArray)

  • edge_ids_global (gestaltdb.sampling._arrays.IntArray)

  • edge_relation_ids (gestaltdb.sampling._arrays.IntArray)

  • positives (gestaltdb.sampling._arrays.IntArray)

  • negatives (gestaltdb.sampling._arrays.IntArray)

  • graph_node_offsets (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • graph_edge_offsets (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • edge_version_ids (TypeAliasForwardRef('gestaltdb.sampling._arrays.StringArray') | None)

  • valid_from_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • valid_to_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • valid_to_open (TypeAliasForwardRef('gestaltdb.sampling._arrays.BoolArray') | None)

  • positive_time_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • negative_time_us (TypeAliasForwardRef('gestaltdb.sampling._arrays.IntArray') | None)

  • negative_diagnostics (dict[str, int] | None)

  • node_weights (TypeAliasForwardRef('gestaltdb.sampling._arrays.FloatArray') | None)

  • edge_weights (TypeAliasForwardRef('gestaltdb.sampling._arrays.FloatArray') | None)

node_ids_global

Compact global node IDs included in this batch.

Type:

IntArray

node_type_ids

Node type IDs aligned with node_ids_global.

Type:

IntArray

senders

Local source node IDs for sampled edges.

Type:

IntArray

receivers

Local target node IDs for sampled edges.

Type:

IntArray

edge_ids_global

Compact global edge IDs included in this batch.

Type:

IntArray

edge_relation_ids

Relation IDs aligned with edge_ids_global.

Type:

IntArray

positives

Local positive triples shaped (num_positives, 3).

Type:

IntArray

negatives

Local negative triples, usually shaped (num_positives, negatives_per_positive, 3).

Type:

IntArray

graph_node_offsets

Optional graph component offsets for packed batches.

Type:

IntArray | None

graph_edge_offsets

Optional edge component offsets for packed batches.

Type:

IntArray | None

edge_version_ids

Edge-version IDs aligned with edge_ids_global for temporal snapshots, otherwise None.

Type:

StringArray | None

valid_from_us

Inclusive valid starts aligned with sampled edges.

Type:

IntArray | None

valid_to_us

Exclusive valid ends, with zero for open intervals.

Type:

IntArray | None

valid_to_open

Open-end masks aligned with sampled edges.

Type:

BoolArray | None

positive_time_us

Example times aligned with positive triples.

Type:

IntArray | None

negative_time_us

Example times aligned with grouped negatives.

Type:

IntArray | None

negative_diagnostics

Deterministic rejection and exhaustion counters.

Type:

dict[str, int] | None

node_weights

Optional per-node training weights aligned with node_ids_global, e.g. GraphSAINT inclusion-probability normalization.

Type:

FloatArray | None

edge_weights

Optional per-edge training weights aligned with edge_ids_global.

Type:

FloatArray | None

Examples

Convert to plain NumPy arrays:

1arrays = batch.to_numpy()
2edge_index = np.stack([arrays["senders"], arrays["receivers"]])
property n_edges: int

Number of edge rows in this batch (global edge ID space size).

property n_nodes: int

Number of node rows in this batch (local ID space size).

to_arrow()[source]

Return a PyArrow table for nodes and edges plus triple arrays.

Returns:

Dictionary with Arrow tables for nodes, edges, positives, and negatives. If negatives are grouped, a negative_group_offsets Arrow array is included.

Raises:

ImportError – If pyarrow is not installed.

Examples

Export node and edge tables:

1arrow_batch = batch.to_arrow()
2node_table = arrow_batch["nodes"]
to_dgl()[source]

Return a DGL graph with node/edge features and label tensors.

Returns:

Tuple (graph, labels) where graph is a DGL graph and labels contains positive and negative PyTorch tensors. Temporal graphs keep string edge-version IDs on graph.edge_version_ids.

Raises:

ImportError – If dgl or torch is not installed.

to_numpy()[source]

Return arrays without framework-specific conversion.

Returns:

Dictionary containing NumPy arrays for nodes, edges, positives, and negatives. Partially populated temporal companions can be None.

Return type:

NumpyBatch

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 torch is 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 tensorflow is 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', in_batch=False, candidate_distribution='uniform', degree_exponent=0.75)[source]

Bases: object

Configuration for KG hard-negative generation.

IDs are compact snapshot IDs; use *_external engine entry points to pass application string IDs, then resolve results with to_external.

Parameters:
  • negatives_per_positive (int)

  • source (Literal['random', 'non_visited_relation_neighbors', 'context_relation_neighbors'])

  • 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 (Literal['any_time', 'at_positive_time', 'window'])

  • temporal_positive_window_policy (Literal['overlap', 'contained'])

  • temporal_candidate_window_days (float | None)

  • allow_future_candidates (bool)

  • exhaustion_policy (Literal['raise', 'repeat'])

  • in_batch (bool)

  • candidate_distribution (Literal['uniform', 'degree'])

  • degree_exponent (float)

negatives_per_positive

Negatives drawn per positive triple.

Type:

int

source

Candidate source ("random" default). "in-batch" is enabled separately via in_batch=True below.

Type:

Literal[‘random’, ‘non_visited_relation_neighbors’, ‘context_relation_neighbors’]

reject_known_positives

Skip candidates that match a known positive.

Type:

bool

same_endpoint_type

Restrict corrupted endpoints to the positive’s type.

Type:

bool

relation_endpoint_types

Restrict candidates by relation endpoint types.

Type:

bool

candidate_fanout

Candidate pool size per draw; None uses the engine default.

Type:

int | None

head_probability

Probability of corrupting the head (vs tail).

Type:

float

max_retries

Attempts per negative before exhaustion handling.

Type:

int

temporal_positive_policy

"any_time" (default), "at_positive_time", or "window". Time-aware policies need positive_times_us or a temporal context unless allow_future_candidates=True.

Type:

Literal[‘any_time’, ‘at_positive_time’, ‘window’]

temporal_positive_window_policy

"overlap" or "contained" for "window" membership.

Type:

Literal[‘overlap’, ‘contained’]

temporal_candidate_window_days

Trailing candidate window size; None disables windowing.

Type:

float | None

allow_future_candidates

Opt in to candidates after the positive time.

Type:

bool

exhaustion_policy

"raise" (default) or "repeat" an accepted negative when the pool is exhausted.

Type:

Literal[‘raise’, ‘repeat’]

in_batch

Also try other in-batch positives as negatives first.

Type:

bool

candidate_distribution

"uniform" or "degree" (weights the random fallback by incident_degree ** degree_exponent).

Type:

Literal[‘uniform’, ‘degree’]

degree_exponent

Exponent for "degree" weighting (default 0.75).

Type:

float

Examples

Leakage-safe time-aware negatives:

1config = HardNegativeConfig(
2    temporal_positive_policy="at_positive_time",
3    temporal_candidate_window_days=90,
4)

See also

docs/typed-sampling.rst “Time-Aware Hard Negatives” for policies, diagnostics, and exhaustion semantics.

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

start()[source]

Start the background producer thread.

Return type:

AsyncBatchFeeder[T]

stop()[source]

Signal the producer to stop and unblock waiting consumers.

Return type:

None

Columnar Ingestion

Columnar ingestion containers for GestaltDB.

class gestaltdb.ingestion.ColumnarIngestionMode(*values)[source]

Bases: str, Enum

How high-level columnar ingestion should interpret input columns.

ENTITY_COLUMNS means node and edge payloads are built from structured entity columns such as IDs, labels, sources, targets, types, and properties. With JSONSerializer, this enables the fast Polars/Arrow JSON payload path.

SERIALIZED_PAYLOADS means inputs already contain serializer-compatible node_value and edge_value byte 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: object

Columnar typed edges with caller-provided serialized edge values.

Parameters:
chunks(chunk_size)[source]

Yield fixed-size EdgeList chunks.

Parameters:

chunk_size (int)

edge_ids: list[bytes]
edge_ids_column: object = None
edge_types: list[str]
edge_types_column: object = None
edge_values: list[bytes]
edge_values_column: object = None
classmethod from_arrow(edge_ids, sources, targets, edge_types, edge_values)[source]

Create an edge list from Arrow-like or Python columns.

classmethod from_polars(df, *, edge_id='edge_id', source='source', target='target', edge_type='edge_type', edge_value='edge_value')[source]

Create an edge list from a Polars DataFrame.

sources: list[bytes]
sources_column: object = None
targets: list[bytes]
targets_column: object = None
class gestaltdb.ingestion.IndexMaintenanceMode(*values)[source]

Bases: str, Enum

Secondary-index maintenance policy for columnar ingestion.

MAINTAIN updates secondary indexes during ingestion. This is safest for incremental writes, but expensive for large append-only bulk loads.

DEFER writes canonical graph records and traversal-critical typed adjacency, marks secondary indexes stale, and requires an explicit GraphDB.rebuild_deferred_indexes() before index-backed queries.

DEFER_REBUILD is 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: object

Columnar nodes with caller-provided serialized node values.

Parameters:
chunks(chunk_size)[source]

Yield fixed-size NodeList chunks.

Parameters:

chunk_size (int)

classmethod from_arrow(node_ids, node_values)[source]

Create a node list from Arrow-like or Python columns.

classmethod from_polars(df, *, node_id='node_id', node_value='node_value')[source]

Create a node list from a Polars DataFrame.

node_ids: list[bytes]
node_ids_column: object = None
node_values: list[bytes]
node_values_column: object = None

Cypher Queries

Canonical Cypher query engine API.

class gestaltdb.query_engine.cypher.QueryResult(columns, records)[source]

Bases: object

Tabular query result returned by GraphDB.query.

columns contains projected column names in return order. records is a list of dictionaries keyed by column name.

Examples

>>> result = QueryResult(columns=("n",), records=[{"n": "node"}])
>>> len(result)
1
>>> list(result)[0]["n"]
'node'
Parameters:
columns: tuple[str, ...]
records: list[dict[str, object]]
gestaltdb.query_engine.cypher.execute(graph, query, parameters=None, *, read_snapshot=None, label_hierarchy=None, relation_hierarchy=None)[source]

Execute a supported Cypher query against a GraphDB instance.

Parameters:
  • graph – GraphDB instance used for indexed lookups and traversal.

  • query (str) – Cypher query text.

  • parameters (dict[str, object] | None)

  • read_snapshot (bool | None)

Returns:

QueryResult with projected records.

Return type:

QueryResult

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, UNION queries 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: object

Abstract interface for a simple key-value store.

close()[source]

Close any resources owned by the store.

Return type:

None

count_typed_adjacency(node_id, edge_type, direction='out')[source]

Count typed adjacency records for a node and edge type.

Parameters:
Return type:

int

delete(key)[source]

Delete a raw key/value pair.

Parameters:

key (bytes)

Return type:

None

delete_adjacency(node_id)[source]

Delete a serialized adjacency list for a node.

Parameters:

node_id (bytes)

Return type:

None

delete_edge(edge_id)[source]

Delete an edge.

Parameters:

edge_id (bytes)

Return type:

None

delete_index_entry(index_name, key_parts, value)[source]

Delete one sorted index entry.

Parameters:
Return type:

None

delete_metadata(key)[source]

Delete a metadata key/value pair.

Parameters:

key (bytes)

Return type:

None

delete_node(node_id)[source]

Delete a node.

Parameters:

node_id (bytes)

Return type:

None

delete_range_index_entry(index_name, key_parts, range_value, value)[source]

Delete one sorted range index entry.

Parameters:
Return type:

None

delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Delete typed adjacency records for an edge.

Parameters:
Return type:

None

get(key)[source]

Return a raw value by key.

Parameters:

key (bytes)

Return type:

bytes | None

get_adjacency(node_id)[source]
Parameters:

node_id (bytes)

Return type:

bytes | None

get_edge(edge_id)[source]

Retrieve an edge.

Parameters:

edge_id (bytes)

Return type:

bytes | None

get_edge_keys_generator(num_edges=None, key_offset=None)[source]
Parameters:
  • num_edges (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_edges_bulk(edge_ids)[source]

Retrieve multiple serialized edges by key.

Parameters:

edge_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

get_metadata(key)[source]

Return a metadata value by key.

Parameters:

key (bytes)

Return type:

bytes | None

get_node(node_id)[source]

Retrieve a node by ID.

Parameters:

node_id (bytes)

Return type:

bytes | None

get_node_keys_generator(num_nodes=None, key_offset=None)[source]
Parameters:
  • num_nodes (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_nodes_bulk(node_ids)[source]

Retrieve multiple serialized nodes by key.

Parameters:

node_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

ingest_edges_columnar(edge_list, *, append_only=True, native=True, maintain_indexes=True)[source]

Store columnar typed edges with caller-provided serialized values.

Parameters:
Return type:

None

ingest_nodes_columnar(node_list, *, native=True)[source]

Store columnar nodes with caller-provided serialized values.

Parameters:
Return type:

None

iter_index_prefix(index_name, key_parts)[source]

Yield values whose index key starts with key_parts.

Parameters:
Return type:

Iterator[bytes]

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.

Parameters:
Return type:

Iterator[bytes]

iter_typed_adjacency(node_id, edge_type, direction='out')[source]

Yield typed adjacency records for a node and edge type.

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

put(key, value)[source]

Store a raw key/value pair.

Parameters:
Return type:

None

put_adjacency(node_id, value)[source]
Parameters:
Return type:

None

put_adjacency_bulk(adjacencies)[source]
Parameters:

adjacencies (dict[bytes, bytes])

Return type:

None

put_edge(edge_id, value)[source]

Store an edge (serialized).

Parameters:
Return type:

None

put_edges_bulk(keys_and_values)[source]

Store multiple serialized edges.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_index_entries_bulk(entries)[source]

Store many sorted index entries.

Parameters:

entries (list[tuple[str, list[bytes], bytes]])

Return type:

None

put_index_entry(index_name, key_parts, value)[source]

Store one sorted index entry.

Parameters:
Return type:

None

put_metadata(key, value)[source]

Store a metadata key/value pair.

Parameters:
Return type:

None

put_node(node_id, value)[source]

Store a node (serialized).

Parameters:
Return type:

None

put_nodes_bulk(keys_and_values)[source]

Store multiple node (serialized) values in a single batch/transaction if possible.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_range_index_entries_bulk(entries)[source]

Store many sorted range index entries.

Parameters:

entries (list[tuple[str, list[bytes], bytes, bytes]])

Return type:

None

put_range_index_entry(index_name, key_parts, range_value, value)[source]

Store one sorted range index entry.

Parameters:
Return type:

None

put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Store typed adjacency records for an edge.

Parameters:
Return type:

None

put_typed_adjacency_bulk(records)[source]

Store typed adjacency records for multiple edges.

Parameters:

records (list[tuple[bytes, bytes, str, bytes]])

Return type:

None

range_iter(start_key, end_key)[source]

Iterate over keys from start_key to end_key (inclusive).

Parameters:
Return type:

Iterable[tuple[bytes, bytes]]

read_snapshot()[source]

Return a context manager yielding a unified read-only store view.

Return type:

ContextManager[LMDBReadSnapshotStore | PyRexReadSnapshotStore]

supports_read_snapshots = False
supports_transactions = False
supports_verifiable_read_snapshots = False
transaction(**options)[source]

Return a transaction-bound store when supported.

Parameters:

options (Any)

Return type:

KVTransaction | LMDBTransactionStore | PyRexTransactionStore

class gestaltdb.kvstores.KVTransaction[source]

Bases: KVStore

A transaction-bound store with explicit commit/rollback coordination.

commit()[source]
Return type:

None

rollback()[source]
Return type:

None

class gestaltdb.kvstores.LMDBReadSnapshotStore(parent)[source]

Bases: LMDBTransactionStore

Read-only LMDB transaction with a verifiable MVCC identity.

Parameters:

parent (LMDBStore)

commit()[source]
Return type:

NoReturn

delete(*args, **kwargs)

Delete a raw key/value pair.

Parameters:
Return type:

NoReturn

delete_adjacency(*args, **kwargs)

Delete a serialized adjacency list for a node.

Parameters:
Return type:

NoReturn

delete_edge(*args, **kwargs)

Delete an edge.

Parameters:
Return type:

NoReturn

delete_index_entry(*args, **kwargs)

Delete one sorted index entry.

Parameters:
Return type:

NoReturn

delete_metadata(*args, **kwargs)

Delete a metadata key/value pair.

Parameters:
Return type:

NoReturn

delete_node(*args, **kwargs)

Delete a node.

Parameters:
Return type:

NoReturn

delete_range_index_entry(*args, **kwargs)

Delete one sorted range index entry.

Parameters:
Return type:

NoReturn

delete_typed_adjacency(*args, **kwargs)

Delete typed adjacency records for an edge.

Parameters:
Return type:

NoReturn

put(*args, **kwargs)

Store a raw key/value pair.

Parameters:
Return type:

NoReturn

put_adjacency(*args, **kwargs)
Parameters:
Return type:

NoReturn

put_adjacency_bulk(*args, **kwargs)
Parameters:
Return type:

NoReturn

put_edge(*args, **kwargs)

Store an edge (serialized).

Parameters:
Return type:

NoReturn

put_edges_bulk(*args, **kwargs)

Store multiple serialized edges.

Parameters:
Return type:

NoReturn

put_index_entries_bulk(*args, **kwargs)

Store many sorted index entries.

Parameters:
Return type:

NoReturn

put_index_entry(*args, **kwargs)

Store one sorted index entry.

Parameters:
Return type:

NoReturn

put_metadata(*args, **kwargs)

Store a metadata key/value pair.

Parameters:
Return type:

NoReturn

put_node(*args, **kwargs)

Store a node (serialized).

Parameters:
Return type:

NoReturn

put_nodes_bulk(*args, **kwargs)

Store multiple node (serialized) values in a single batch/transaction if possible.

Parameters:
Return type:

NoReturn

put_range_index_entries_bulk(*args, **kwargs)

Store many sorted range index entries.

Parameters:
Return type:

NoReturn

put_range_index_entry(*args, **kwargs)

Store one sorted range index entry.

Parameters:
Return type:

NoReturn

put_typed_adjacency(*args, **kwargs)

Store typed adjacency records for an edge.

Parameters:
Return type:

NoReturn

put_typed_adjacency_bulk(*args, **kwargs)

Store typed adjacency records for multiple edges.

Parameters:
Return type:

NoReturn

state_sha256()[source]

Digest every mutable namespace visible at this MVCC horizon.

Return type:

str

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: KVStore

LMDB implementation of the GestaltDB key-value store.

Examples

Work with raw bytes in a disposable store (requires lmdb):

1>>> from tempfile import TemporaryDirectory
2>>> from gestaltdb.kvstores import LMDBStore
3>>> with TemporaryDirectory() as root:
4...     store = LMDBStore(path=root + "/db")
5...     try:
6...         store.put_node(b"n", b"payload")
7...         assert store.get_node(b"n") == b"payload"
8...     finally:
9...         store.close()

Most applications use GraphDB.create(..., backend="lmdb") to add serialization, indexes, and graph semantics above this byte interface.

Parameters:
__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

Parameters:
Return type:

None

close()[source]

Close the LMDB environment.

Return type:

None

current_snapshot_sequence()[source]

Return the latest committed LMDB transaction ID.

Return type:

int

delete(key)[source]

Placeholder generic delete; graph code uses specialized methods.

Parameters:

key (bytes)

Return type:

None

delete_adjacency(node_id)[source]

Delete a serialized adjacency list for a node.

Parameters:

node_id (bytes)

Return type:

None

delete_edge(edge_id)[source]

Delete an edge by byte key.

Parameters:

edge_id (bytes)

Return type:

None

delete_index_entry(index_name, key_parts, value)[source]

Delete one sorted index entry.

Parameters:
Return type:

None

delete_metadata(key)[source]

Delete a metadata key/value pair.

Parameters:

key (bytes)

Return type:

None

delete_node(node_id)[source]

Delete a node by byte key.

Parameters:

node_id (bytes)

Return type:

None

delete_range_index_entry(index_name, key_parts, range_value, value)[source]

Delete one sorted range index entry.

Parameters:
Return type:

None

delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Delete forward and reverse typed adjacency records.

Parameters:
Return type:

None

static ensure_available()[source]

Import and return LMDB, or raise installation guidance.

Return type:

Any

get(key)[source]

Placeholder generic get; graph code uses specialized methods.

Parameters:

key (bytes)

Return type:

bytes | None

get_adjacency(node_id)[source]

Return a serialized adjacency list for a node.

Parameters:

node_id (bytes | str)

Return type:

bytes | None

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.

Parameters:

node_ids (List[bytes])

Return type:

Dict[bytes, bytes]

get_edge(edge_id)[source]

Return serialized edge bytes by key, or None.

Parameters:

edge_id (bytes)

Return type:

bytes | None

get_edge_keys_generator(num_edges=None, key_offset=None)[source]

Yield edge keys from the edge database.

Parameters:
  • num_edges (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_edges_bulk(edge_ids)[source]

Return serialized edges for the requested keys.

Parameters:

edge_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

get_metadata(key)[source]

Return a metadata value by key, or None.

Parameters:

key (bytes)

Return type:

bytes | None

get_node(node_id)[source]

Return serialized node bytes by key, or None.

Parameters:

node_id (bytes)

Return type:

bytes | None

get_node_keys_generator(num_nodes=None, key_offset=None)[source]

Yield node keys from the node database.

Parameters:
  • num_nodes (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_nodes_bulk(node_ids)[source]

Retrieve multiple nodes in one read transaction.

Parameters:

node_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

iter_index_prefix(index_name, key_parts)[source]

Yield values whose index key starts with key_parts.

Parameters:
Return type:

Iterator[bytes]

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.

Parameters:
Return type:

Iterator[bytes]

iter_typed_adjacency(node_id, edge_type, direction='out')[source]

Yield typed adjacency (edge_id, neighbor_id) pairs.

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

put(key, value)[source]

Placeholder generic put; graph code uses specialized methods.

Parameters:
Return type:

None

put_adjacency(node_id, value)[source]

Store a serialized adjacency list for a node.

Parameters:
Return type:

None

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)

Parameters:

adj_dict (Dict[bytes, bytes])

Return type:

None

put_edge(edge_id, value)[source]

Store a serialized edge by byte key.

Parameters:
Return type:

None

put_edges_bulk(keys_and_values)[source]

Store many serialized edges in one transaction.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_index_entries_bulk(entries)[source]

Store many sorted index entries in one transaction.

Parameters:

entries (list[tuple[str, list[bytes], bytes]])

Return type:

None

put_index_entry(index_name, key_parts, value)[source]

Store one sorted index entry.

Parameters:
Return type:

None

put_metadata(key, value)[source]

Store a metadata key/value pair.

Parameters:
Return type:

None

put_node(node_id, value)[source]

Store a serialized node by byte key.

Parameters:
Return type:

None

put_nodes_bulk(keys_and_values)[source]

Write a batch of nodes in a single transaction.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_range_index_entries_bulk(entries)[source]

Store many sorted range index entries in one transaction.

Parameters:

entries (list[tuple[str, list[bytes], bytes, bytes]])

Return type:

None

put_range_index_entry(index_name, key_parts, range_value, value)[source]

Store one sorted range index entry.

Parameters:
Return type:

None

put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Store forward and reverse typed adjacency records.

Parameters:
Return type:

None

put_typed_adjacency_bulk(records)[source]

Store many typed adjacency records in one transaction.

Parameters:

records (list[tuple[bytes, bytes, str, bytes]])

Return type:

None

range_iter(start_key, end_key)[source]

Yield node records whose keys fall within an inclusive range.

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

read_snapshot()[source]

Pin all named databases to one LMDB MVCC transaction.

Return type:

Iterator[LMDBReadSnapshotStore]

supports_read_snapshots = True
supports_transactions = True
supports_verifiable_read_snapshots = True
transaction(write=True, **options)[source]

Open a transaction spanning all LMDB named databases.

Parameters:
Return type:

LMDBTransactionStore

class gestaltdb.kvstores.LMDBTransactionStore(parent, write=True, **options)[source]

Bases: KVStore

Transaction-bound LMDB store using one environment transaction.

Parameters:
close()[source]

Close any resources owned by the store.

Return type:

None

commit()[source]
Return type:

None

delete(key)[source]

Delete a raw key/value pair.

Parameters:

key (bytes)

Return type:

None

delete_adjacency(node_id)[source]

Delete a serialized adjacency list for a node.

Parameters:

node_id (bytes)

Return type:

None

delete_edge(edge_id)[source]

Delete an edge.

Parameters:

edge_id (bytes)

Return type:

None

delete_index_entry(index_name, key_parts, value)[source]

Delete one sorted index entry.

Parameters:
Return type:

None

delete_metadata(key)[source]

Delete a metadata key/value pair.

Parameters:

key (bytes)

Return type:

None

delete_node(node_id)[source]

Delete a node.

Parameters:

node_id (bytes)

Return type:

None

delete_range_index_entry(index_name, key_parts, range_value, value)[source]

Delete one sorted range index entry.

Parameters:
Return type:

None

delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Delete typed adjacency records for an edge.

Parameters:
Return type:

None

get(key)[source]

Return a raw value by key.

Parameters:

key (bytes)

Return type:

bytes | None

get_adjacency(node_id)[source]
Parameters:

node_id (bytes)

Return type:

bytes | None

get_adjacency_bulk(node_ids)[source]
Parameters:

node_ids (List[bytes])

Return type:

Dict[bytes, bytes]

get_edge(edge_id)[source]

Retrieve an edge.

Parameters:

edge_id (bytes)

Return type:

bytes | None

get_edge_keys_generator(num_edges=None, key_offset=None)[source]
Parameters:
  • num_edges (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_edges_bulk(edge_ids)[source]

Retrieve multiple serialized edges by key.

Parameters:

edge_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

get_metadata(key)[source]

Return a metadata value by key.

Parameters:

key (bytes)

Return type:

bytes | None

get_node(node_id)[source]

Retrieve a node by ID.

Parameters:

node_id (bytes)

Return type:

bytes | None

get_node_keys_generator(num_nodes=None, key_offset=None)[source]
Parameters:
  • num_nodes (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_nodes_bulk(node_ids)[source]

Retrieve multiple serialized nodes by key.

Parameters:

node_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

iter_index_prefix(index_name, key_parts)[source]

Yield values whose index key starts with key_parts.

Parameters:
Return type:

Iterator[bytes]

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.

Parameters:
Return type:

Iterator[bytes]

iter_typed_adjacency(node_id, edge_type, direction='out')[source]

Yield typed adjacency records for a node and edge type.

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

put(key, value)[source]

Store a raw key/value pair.

Parameters:
Return type:

None

put_adjacency(node_id, value)[source]
Parameters:
Return type:

None

put_adjacency_bulk(adj_dict)[source]
Parameters:

adj_dict (Dict[bytes, bytes])

Return type:

None

put_edge(edge_id, value)[source]

Store an edge (serialized).

Parameters:
Return type:

None

put_edges_bulk(keys_and_values)[source]

Store multiple serialized edges.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_index_entries_bulk(entries)[source]

Store many sorted index entries.

Parameters:

entries (list[tuple[str, list[bytes], bytes]])

Return type:

None

put_index_entry(index_name, key_parts, value)[source]

Store one sorted index entry.

Parameters:
Return type:

None

put_metadata(key, value)[source]

Store a metadata key/value pair.

Parameters:
Return type:

None

put_node(node_id, value)[source]

Store a node (serialized).

Parameters:
Return type:

None

put_nodes_bulk(keys_and_values)[source]

Store multiple node (serialized) values in a single batch/transaction if possible.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_range_index_entries_bulk(entries)[source]

Store many sorted range index entries.

Parameters:

entries (list[tuple[str, list[bytes], bytes, bytes]])

Return type:

None

put_range_index_entry(index_name, key_parts, range_value, value)[source]

Store one sorted range index entry.

Parameters:
Return type:

None

put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Store typed adjacency records for an edge.

Parameters:
Return type:

None

put_typed_adjacency_bulk(records)[source]

Store typed adjacency records for multiple edges.

Parameters:

records (list[tuple[bytes, bytes, str, bytes]])

Return type:

None

range_iter(start_key, end_key)[source]

Iterate over keys from start_key to end_key (inclusive).

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

rollback()[source]
Return type:

None

supports_transactions = True
transaction(**options)[source]

Return a transaction-bound store when supported.

Parameters:

options (Any)

Return type:

NoReturn

class gestaltdb.kvstores.LevelDBStore(path='graph_leveldb')[source]

Bases: KVStore

LevelDB implementation backed by plyvel.

Parameters:

path (str | PathLike[str]) – Directory that will contain the LevelDB sub-databases.

Examples

Work with raw bytes in a disposable store (requires plyvel):

1>>> from tempfile import TemporaryDirectory
2>>> from gestaltdb.kvstores import LevelDBStore
3>>> with TemporaryDirectory() as root:
4...     store = LevelDBStore(path=root + "/db")
5...     try:
6...         store.put_node(b"n", b"payload")
7...         assert store.get_node(b"n") == b"payload"
8...     finally:
9...         store.close()

Most applications use GraphDB.create(..., backend="leveldb"); see the storage-backends guide for snapshot/transaction capabilities.

__init__(path='graph_leveldb')[source]

Create or open a LevelDB store. We’ll store nodes/edges by prefix.

Parameters:

path (str | PathLike[str])

Return type:

None

close()[source]

Close all LevelDB sub-databases.

Return type:

None

delete_adjacency(node_id)[source]

Delete a serialized adjacency list for a node.

Parameters:

node_id (bytes)

Return type:

None

delete_edge(edge_id)[source]

Delete an edge by byte key.

Parameters:

edge_id (bytes)

Return type:

None

delete_index_entry(index_name, key_parts, value)[source]

Delete one sorted index entry.

Parameters:
Return type:

None

delete_metadata(key)[source]

Delete a metadata key/value pair.

Parameters:

key (bytes)

Return type:

None

delete_node(node_id)[source]

Delete a node by byte key.

Parameters:

node_id (bytes)

Return type:

None

delete_range_index_entry(index_name, key_parts, range_value, value)[source]

Delete one sorted range index entry.

Parameters:
Return type:

None

delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Delete forward and reverse typed adjacency records.

Parameters:
Return type:

None

static ensure_available()[source]

Import and return Plyvel, or raise installation guidance.

Return type:

Any

get_adjacency(node_id)[source]

Return a serialized adjacency list for a node.

Parameters:

node_id (bytes)

Return type:

bytes | None

get_adjacency_bulk(node_ids)[source]

Return serialized adjacency lists for the requested nodes.

Parameters:

node_ids (List[bytes])

Return type:

Dict[bytes, bytes]

get_db_iterator(which_db='nodes')[source]

Yield all records from a named sub-database.

Parameters:

which_db (str)

Return type:

Iterator[tuple[bytes, bytes]]

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'
Parameters:

db_string (str)

Return type:

str

get_edge(edge_id)[source]

Return serialized edge bytes by key, or None.

Parameters:

edge_id (bytes)

Return type:

bytes | None

get_edge_keys_generator(num_edges=None, key_offset=None)[source]

Yield edge keys from the edge database.

Parameters:
  • num_edges (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_edges_bulk(edge_ids)[source]

Return serialized edges for the requested keys.

Parameters:

edge_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

get_metadata(key)[source]

Return a metadata value by key, or None.

Parameters:

key (bytes)

Return type:

bytes | None

get_node(node_id)[source]

Return serialized node bytes by key, or None.

Parameters:

node_id (bytes)

Return type:

bytes | None

get_node_keys_generator(num_nodes=None, key_offset=None)[source]

Yield node keys from the node database.

Parameters:
  • num_nodes (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_node_keys_iterator()[source]

Yield node database records.

Return type:

Iterator[tuple[bytes, bytes]]

get_nodes_bulk(node_ids)[source]

Return serialized nodes for the requested keys.

Parameters:

node_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

iter_index_prefix(index_name, key_parts)[source]

Yield values whose index key starts with key_parts.

Parameters:
Return type:

Iterator[bytes]

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.

Parameters:
Return type:

Iterator[bytes]

iter_typed_adjacency(node_id, edge_type, direction='out')[source]

Yield typed adjacency (edge_id, neighbor_id) pairs.

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

put_adjacency(node_id, value)[source]

Store a serialized adjacency list for a node.

Parameters:
Return type:

None

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

Parameters:

adj_dict (Dict[bytes, bytes])

Return type:

None

put_edge(edge_id, value)[source]

Store a serialized edge by byte key.

Parameters:
Return type:

None

put_edges_bulk(keys_and_values)[source]

Store many serialized edges in one write batch.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_index_entries_bulk(entries)[source]

Store many sorted index entries in one write batch.

Parameters:

entries (list[tuple[str, list[bytes], bytes]])

Return type:

None

put_index_entry(index_name, key_parts, value)[source]

Store one sorted index entry.

Parameters:
Return type:

None

put_metadata(key, value)[source]

Store a metadata key/value pair.

Parameters:
Return type:

None

put_node(node_id, value)[source]

Store a serialized node by byte key.

Parameters:
Return type:

None

put_nodes_bulk(keys_and_values)[source]

Use a WriteBatch for atomic bulk updates.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_range_index_entries_bulk(entries)[source]

Store many sorted range index entries in one write batch.

Parameters:

entries (list[tuple[str, list[bytes], bytes, bytes]])

Return type:

None

put_range_index_entry(index_name, key_parts, range_value, value)[source]

Store one sorted range index entry.

Parameters:
Return type:

None

put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Store forward and reverse typed adjacency records.

Parameters:
Return type:

None

put_typed_adjacency_bulk(records)[source]

Store many typed adjacency records in one write batch.

Parameters:

records (list[tuple[bytes, bytes, str, bytes]])

Return type:

None

range_iter(start_key, end_key)[source]

Yield records whose keys fall within a range.

Note

This generic iterator is not used by the main graph APIs.

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

read_snapshot()[source]

Return a context manager yielding a unified read-only store view.

Return type:

NoReturn

class gestaltdb.kvstores.PyRexReadSnapshotStore(parent, txn, *, sequence)[source]

Bases: PyRexTransactionStore

Read-only transaction-bound PyRex store.

Parameters:
commit()[source]
Return type:

NoReturn

delete(*args, **kwargs)

Delete a raw key/value pair.

Parameters:
Return type:

NoReturn

delete_adjacency(*args, **kwargs)

Delete a serialized adjacency list for a node.

Parameters:
Return type:

NoReturn

delete_edge(*args, **kwargs)

Delete an edge by byte key.

Parameters:
Return type:

NoReturn

delete_index_entry(*args, **kwargs)

Delete one sorted index entry.

Parameters:
Return type:

NoReturn

delete_metadata(*args, **kwargs)

Delete a metadata key/value pair.

Parameters:
Return type:

NoReturn

delete_node(*args, **kwargs)

Delete a node by byte key.

Parameters:
Return type:

NoReturn

delete_range_index_entry(*args, **kwargs)

Delete one sorted range index entry.

Parameters:
Return type:

NoReturn

delete_typed_adjacency(*args, **kwargs)

Delete forward and reverse typed adjacency records.

Parameters:
Return type:

NoReturn

put(*args, **kwargs)

Store a raw key/value pair in the shared RocksDB keyspace.

Parameters:
Return type:

NoReturn

put_adjacency(*args, **kwargs)

Store a serialized adjacency list for a node.

Parameters:
Return type:

NoReturn

put_adjacency_bulk(*args, **kwargs)

Store many serialized adjacency lists in one RocksDB write batch.

Parameters:
Return type:

NoReturn

put_edge(*args, **kwargs)

Store a serialized edge by byte key.

Parameters:
Return type:

NoReturn

put_edges_bulk(*args, **kwargs)

Store many serialized edges in one RocksDB write batch.

Parameters:
Return type:

NoReturn

put_index_entries_bulk(*args, **kwargs)

Store many sorted index entries in one write batch.

Parameters:
Return type:

NoReturn

put_index_entry(*args, **kwargs)

Store one sorted index entry.

Parameters:
Return type:

NoReturn

put_metadata(*args, **kwargs)

Store a metadata key/value pair.

Parameters:
Return type:

NoReturn

put_node(*args, **kwargs)

Store a serialized node by byte key.

Parameters:
Return type:

NoReturn

put_nodes_bulk(*args, **kwargs)

Store many serialized nodes in one RocksDB write batch.

Parameters:
Return type:

NoReturn

put_range_index_entries_bulk(*args, **kwargs)

Store many sorted range index entries in one write batch.

Parameters:
Return type:

NoReturn

put_range_index_entry(*args, **kwargs)

Store one sorted range index entry.

Parameters:
Return type:

NoReturn

put_typed_adjacency(*args, **kwargs)

Store forward and reverse typed adjacency records.

Parameters:
Return type:

NoReturn

put_typed_adjacency_bulk(*args, **kwargs)

Store many typed adjacency records in one RocksDB write batch.

Parameters:
Return type:

NoReturn

state_sha256()[source]
Return type:

str

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: KVStore

RocksDB implementation backed by pyrex-rocksdb.

PyRexStore uses 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 (str | PathLike[str]) – Directory for the RocksDB database.

  • parallelism (int | None) – Optional number of RocksDB background threads.

  • max_background_jobs (int | None) – Optional RocksDB background job limit.

  • write_buffer_size (int | None) – Optional write buffer size in bytes.

  • bloom_bits_per_key (float | None) – Optional block-based Bloom filter bits per key.

  • disable_wal (bool) – Disable RocksDB’s write-ahead log for faster but less durable ingestion benchmarks.

  • transactional (bool)

  • transaction_db_options (Any)

Examples

Work with raw bytes in a disposable store (requires pyrex-rocksdb):

1>>> from tempfile import TemporaryDirectory
2>>> from gestaltdb.kvstores import PyRexStore
3>>> with TemporaryDirectory() as root:
4...     store = PyRexStore(path=root + "/db")
5...     try:
6...         store.put_node(b"n", b"payload")
7...         assert store.get_node(b"n") == b"payload"
8...     finally:
9...         store.close()

GraphDB.create(..., backend="pyrex") adds graph semantics. Plain and transactional PyRex have different ingestion/snapshot capabilities; see the storage-backends guide before selecting transaction mode.

__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.

Parameters:
  • path (str | PathLike[str])

  • parallelism (int | None)

  • max_background_jobs (int | None)

  • write_buffer_size (int | None)

  • bloom_bits_per_key (float | None)

  • disable_wal (bool)

  • transactional (bool)

  • transaction_db_options (Any)

Return type:

None

close()[source]

Close the RocksDB database.

Return type:

None

count_typed_adjacency(node_id, edge_type, direction='out')[source]

Count typed adjacency records without constructing record dictionaries.

Parameters:
Return type:

int

current_snapshot_sequence()[source]
Return type:

int | None

delete(key)[source]

Delete a raw key/value pair.

Parameters:

key (bytes)

Return type:

None

delete_adjacency(node_id)[source]

Delete a serialized adjacency list for a node.

Parameters:

node_id (bytes)

Return type:

None

delete_edge(edge_id)[source]

Delete an edge by byte key.

Parameters:

edge_id (bytes)

Return type:

None

delete_index_entry(index_name, key_parts, value)[source]

Delete one sorted index entry.

Parameters:
Return type:

None

delete_metadata(key)[source]

Delete a metadata key/value pair.

Parameters:

key (bytes)

Return type:

None

delete_node(node_id)[source]

Delete a node by byte key.

Parameters:

node_id (bytes)

Return type:

None

delete_range_index_entry(index_name, key_parts, range_value, value)[source]

Delete one sorted range index entry.

Parameters:
Return type:

None

delete_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Delete forward and reverse typed adjacency records.

Parameters:
Return type:

None

static ensure_available()[source]

Import and return PyRex, or raise installation guidance.

Return type:

Any

get(key)[source]

Return a raw value by key.

Parameters:

key (bytes)

Return type:

bytes | None

get_adjacency(node_id)[source]

Return a serialized adjacency list for a node.

Parameters:

node_id (bytes)

Return type:

bytes | None

get_adjacency_bulk(node_ids)[source]

Return serialized adjacency lists for the requested nodes.

Parameters:

node_ids (List[bytes])

Return type:

Dict[bytes, bytes]

get_edge(edge_id)[source]

Return serialized edge bytes by key, or None.

Parameters:

edge_id (bytes)

Return type:

bytes | None

get_edge_keys_generator(num_edges=None, key_offset=None)[source]

Yield edge keys from the shared RocksDB keyspace.

Parameters:
  • num_edges (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_edges_bulk(edge_ids)[source]

Return serialized edges for the requested keys.

Parameters:

edge_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

get_metadata(key)[source]

Return a metadata value by key, or None.

Parameters:

key (bytes)

Return type:

bytes | None

get_node(node_id)[source]

Return serialized node bytes by key, or None.

Parameters:

node_id (bytes)

Return type:

bytes | None

get_node_keys_generator(num_nodes=None, key_offset=None)[source]

Yield node keys from the shared RocksDB keyspace.

Parameters:
  • num_nodes (int | None)

  • key_offset (bytes | None)

Return type:

Iterator[bytes]

get_nodes_bulk(node_ids)[source]

Return serialized nodes for the requested keys.

Parameters:

node_ids (Iterable[bytes])

Return type:

dict[bytes, bytes]

has_native_arrow_scan()[source]

Return whether this PyRex runtime exposes native Arrow scans.

Requires scan_arrow plus create_snapshot on the underlying handle. Transactional handles do not expose them in 0.5.0; 0.6.0 adds native committed-state scans of the default column family. This probe is not authenticated claim-source or named-column-family admission.

Return type:

bool

has_native_columnar_ingestion()[source]

Return whether this PyRex runtime exposes native columnar writes.

Return type:

bool

ingest_edges_columnar(edge_list, *, append_only=True, native=True, maintain_indexes=True)[source]

Store columnar typed edges, using native PyRex ingestion when available.

Parameters:
Return type:

None

ingest_nodes_columnar(node_list, *, native=True)[source]

Store columnar nodes, using native PyRex ingestion when available.

Parameters:
Return type:

None

iter_index_prefix(index_name, key_parts)[source]

Yield values whose index key starts with key_parts.

Parameters:
Return type:

Iterator[bytes]

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.

Parameters:
Return type:

Iterator[bytes]

iter_typed_adjacency(node_id, edge_type, direction='out')[source]

Yield typed adjacency (edge_id, neighbor_id) pairs.

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

put(key, value)[source]

Store a raw key/value pair in the shared RocksDB keyspace.

Parameters:
Return type:

None

put_adjacency(node_id, value)[source]

Store a serialized adjacency list for a node.

Parameters:
Return type:

None

put_adjacency_bulk(adj_dict)[source]

Store many serialized adjacency lists in one RocksDB write batch.

Parameters:

adj_dict (Dict[bytes, bytes])

Return type:

None

put_edge(edge_id, value)[source]

Store a serialized edge by byte key.

Parameters:
Return type:

None

put_edges_bulk(keys_and_values)[source]

Store many serialized edges in one RocksDB write batch.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_index_entries_bulk(entries)[source]

Store many sorted index entries in one write batch.

Parameters:

entries (list[tuple[str, list[bytes], bytes]])

Return type:

None

put_index_entry(index_name, key_parts, value)[source]

Store one sorted index entry.

Parameters:
Return type:

None

put_metadata(key, value)[source]

Store a metadata key/value pair.

Parameters:
Return type:

None

put_node(node_id, value)[source]

Store a serialized node by byte key.

Parameters:
Return type:

None

put_nodes_bulk(keys_and_values)[source]

Store many serialized nodes in one RocksDB write batch.

Parameters:

keys_and_values (dict[bytes, bytes])

Return type:

None

put_range_index_entries_bulk(entries)[source]

Store many sorted range index entries in one write batch.

Parameters:

entries (list[tuple[str, list[bytes], bytes, bytes]])

Return type:

None

put_range_index_entry(index_name, key_parts, range_value, value)[source]

Store one sorted range index entry.

Parameters:
Return type:

None

put_typed_adjacency(source_id, target_id, edge_type, edge_id)[source]

Store forward and reverse typed adjacency records.

Parameters:
Return type:

None

put_typed_adjacency_bulk(records)[source]

Store many typed adjacency records in one RocksDB write batch.

Parameters:

records (list[tuple[bytes, bytes, str, bytes]])

Return type:

None

range_iter(start_key, end_key)[source]

Yield raw records whose keys fall within an inclusive range.

Parameters:
Return type:

Iterator[tuple[bytes, bytes]]

read_snapshot()[source]

Pin reads to one RocksDB transaction snapshot when available.

Return type:

Iterator[PyRexReadSnapshotStore]

transaction(transaction_options=None, **options)[source]

Open a RocksDB transaction-bound store.

Parameters:
  • transaction_options (Any)

  • options (Any)

Return type:

PyRexTransactionStore

class gestaltdb.kvstores.PyRexTransactionStore(parent, txn)[source]

Bases: PyRexStore

Transaction-bound PyRex/RocksDB store.

Parameters:
close()[source]

Close the RocksDB database.

Return type:

None

commit()[source]
Return type:

None

has_native_columnar_ingestion()[source]

Return whether this PyRex runtime exposes native columnar writes.

Return type:

bool

rollback()[source]
Return type:

None

supports_transactions = True
transaction(**options)[source]

Open a RocksDB transaction-bound store.

Parameters:

options (Any)

Return type:

NoReturn

class gestaltdb.kvstores.ReadSnapshotIdentity(backend, kind, sequence, verifiable)[source]

Bases: object

Backend identity for one pinned mutable-state read horizon.

Parameters:
  • backend (str)

  • kind (str)

  • sequence (int | None)

  • verifiable (bool)

backend: str
kind: str
sequence: int | None
verifiable: bool
class gestaltdb.kvstores.SimpleIndexCounterKVStore(dbenv=None, db_path=b'nodes')[source]

Bases: object

This 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.

decode_db_key(key)[source]

Return the stored encoded key bytes for a user key.

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.

get(key)[source]

Read a counter metadata value by key.

get_num_keys()[source]

Return the number of keys already assigned.

put(key, value)[source]

Store a counter metadata key/value pair.

Parameters:
put_num_keys(num_keys)[source]

Persist the number of keys already assigned.

class gestaltdb.kvstores.SimpleKV(db_path)[source]

Bases: object

Small LMDB-backed helper for metadata key/value access.

Parameters:

db_path – LMDB database handle or name used by transactions.

__init__(db_path)[source]

Initialize the helper with an LMDB database path or handle.

decode_db_key(key)[source]

Return the encoded database key for a user key.

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.

get(key)[source]

Read a metadata value by key.

get_num_keys()[source]

Return the stored key counter.

put(key, value)[source]

Write a metadata key/value pair.

Parameters:
put_num_keys(num_keys)[source]

Store the key counter.

Serializers

class gestaltdb.serializers.JSONSerializer[source]

Bases: Serializer

Encode supported property payloads as UTF-8 JSON bytes.

Examples

Round-trip a dictionary independently of any storage backend:

1>>> from gestaltdb.serializers import JSONSerializer
2>>> serializer = JSONSerializer()
3>>> payload = {"name": "Alice", "scores": [1, 2]}
4>>> encoded = serializer.serialize(payload)
5>>> assert isinstance(encoded, bytes)
6>>> assert serializer.deserialize(encoded) == payload

GraphDB.create(..., serializer="json") selects this implementation. See the serializers guide for temporal property values and optional encodings.

deserialize(data)[source]

Deserialize JSON bytes.

Examples

>>> JSONSerializer().deserialize(b'{"a": 1}')
{'a': 1}
Parameters:

data (bytes)

Return type:

dict

serialize(obj)[source]

Serialize a JSON-compatible object.

Examples

>>> JSONSerializer().serialize({"a": 1})
b'{"a": 1}'
Parameters:

obj (dict)

Return type:

bytes

class gestaltdb.serializers.MessagePackSerializer[source]

Bases: Serializer

Uses MessagePack for serialization.

deserialize(data)[source]

Deserialize MessagePack bytes.

Raises:

ImportError – If the optional msgpack package is missing.

Parameters:

data (bytes)

Return type:

dict

Examples

>>> MessagePackSerializer().deserialize(MessagePackSerializer().serialize({"a": 1}))
{'a': 1}
static ensure_available()[source]

Import and return MessagePack, or raise installation guidance.

Return type:

Any

serialize(obj)[source]

Serialize an object with MessagePack.

Raises:

ImportError – If the optional msgpack package is missing.

Parameters:

obj (dict)

Return type:

bytes

Examples

>>> MessagePackSerializer().deserialize(MessagePackSerializer().serialize({"a": 1}))
{'a': 1}
class gestaltdb.serializers.PickleSerializer[source]

Bases: Serializer

Uses Python’s pickle for serialization.

deserialize(data)[source]

Deserialize pickle bytes.

Examples

>>> PickleSerializer().deserialize(PickleSerializer().serialize({"a": 1}))
{'a': 1}
Parameters:

data (bytes)

Return type:

dict

serialize(obj)[source]

Serialize an object with pickle.

Examples

>>> PickleSerializer().deserialize(PickleSerializer().serialize({"a": 1}))
{'a': 1}
Parameters:

obj (dict)

Return type:

bytes

class gestaltdb.serializers.ProtobufSerializer[source]

Bases: Serializer

Uses 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 protobuf package is missing.

Return type:

dict

static ensure_available()[source]

Import and return protobuf helpers, or raise installation guidance.

Return type:

Any

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 protobuf package is missing.

Return type:

bytes

class gestaltdb.serializers.Serializer[source]

Bases: object

Abstract base for serialization/deserialization.

deserialize(data)[source]

Deserialize bytes into a dictionary-like object.

Parameters:

data (bytes) – Serialized bytes.

Returns:

Decoded object.

Return type:

dict

serialize(obj)[source]

Serialize a dictionary-like object to bytes.

Parameters:

obj (dict) – Object to serialize.

Returns:

Serialized bytes.

Return type:

bytes

gestaltdb.serializers.temporal_tagged_value(value, *, canonical_time=False)[source]

Return a portable tagged representation for a temporal scalar.

Parameters:
Return type:

dict[str, object]

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_edges to 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.NODE_SHAPES = ('circle', 'triangle', 'square')

Node shapes honored by the static SVG renderer and the interactive viewer. circle marks concrete data (graph nodes, asserted claims, input facts), triangle marks inferred conclusions (derived facts and claims), and square marks schema (rules, axioms, and ontology terms).

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: object

Exact accounting of deterministic cap enforcement.

Parameters:
  • nodes_dropped (int)

  • edges_dropped (int)

edges_dropped: int = 0
nodes_dropped: int = 0
to_dict()[source]

Return the JSON-safe truncation payload.

Return type:

dict[str, Any]

property truncated: bool

Whether any node or edge was dropped.

exception gestaltdb.viz.ir.TruncationWarning[source]

Bases: UserWarning

Emitted 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: ValueError

Raised when a payload exceeds its absolute ceiling (VIZ-07).

class gestaltdb.viz.ir.VizEdge(id, source, target, type='', properties=<factory>, extra=<factory>, dash=None)[source]

Bases: object

A single directed edge in the visualization IR.

Parameters:
dash: str | None = None
extra: dict[str, Any]
id: str
properties: dict[str, Any]
source: str
target: str
to_dict()[source]

Return the JSON-safe edge payload.

Return type:

dict[str, Any]

type: str = ''
class gestaltdb.viz.ir.VizGraph(nodes=(), edges=(), highlight_nodes=(), highlight_edges=(), truncation=<factory>, meta=<factory>)[source]

Bases: object

Deterministic, JSON-serializable graph payload for the front end.

Parameters:
property edge_ids: tuple[str, ...]

Edge IDs in payload order.

edges: tuple[VizEdge, ...] = ()
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/PathValue cell 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 in meta so the outcome stays explainable.

Parameters:
  • result (Any)

  • max_nodes (int)

  • max_edges (int)

  • absolute_max_nodes (int | None)

  • absolute_max_edges (int | None)

  • meta (Mapping[str, Any] | None)

Return type:

VizGraph

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 first max_edges. Edges touching a dropped node are dropped as well and counted in edges_dropped. Duplicate IDs keep their first occurrence.

Payloads with more distinct nodes than absolute_max_nodes (or edges than absolute_max_edges) raise VizCapExceededError instead of freezing the browser; both default to twice their respective cap. Sample a subgraph or narrow the query instead.

Parameters:
  • nodes (Any)

  • edges (Any)

  • max_nodes (int)

  • max_edges (int)

  • absolute_max_nodes (int | None)

  • absolute_max_edges (int | None)

  • highlight_nodes (Any)

  • highlight_edges (Any)

  • meta (Mapping[str, Any] | None)

Return type:

VizGraph

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_subgraph mapping.

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.

Parameters:
Return type:

VizGraph

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 SampledSubgraphBatch and snapshot.

Local batch rows map back through node_ids_global to compact global IDs and then to external IDs via snapshot.external_node_id(...); relation IDs resolve via snapshot.external_relation_id(...). numpy is used only through duck typing (tolist/indexing), so this module keeps no mandatory third-party imports.

Parameters:
  • batch (Any)

  • snapshot (Any)

  • max_nodes (int)

  • max_edges (int)

  • absolute_max_nodes (int | None)

  • absolute_max_edges (int | None)

  • meta (Mapping[str, Any] | None)

Return type:

VizGraph

highlight_edges: tuple[str, ...] = ()
highlight_nodes: tuple[str, ...] = ()
meta: dict[str, Any]
property node_ids: tuple[str, ...]

Node IDs in payload order.

nodes: tuple[VizNode, ...] = ()
to_dict()[source]

Return the full JSON-safe payload consumed by the front end.

Return type:

dict[str, Any]

to_json()[source]

Return the canonical JSON encoding (sorted keys, compact).

Return type:

str

truncation: TruncationInfo
class gestaltdb.viz.ir.VizNode(id, labels=(), properties=<factory>, extra=<factory>, shape='circle')[source]

Bases: object

A single node in the visualization IR.

Parameters:
extra: dict[str, Any]
property group: str

Primary group used for coloring (first label, or "").

id: str
labels: tuple[str, ...] = ()
properties: dict[str, Any]
shape: str = 'circle'
to_dict()[source]

Return the JSON-safe node payload.

Return type:

dict[str, Any]

gestaltdb.viz.ir.warn_if_truncated(viz_graph, *, source)[source]

Emit a TruncationWarning when caps dropped anything.

Parameters:
  • viz_graph (VizGraph) – Built payload to inspect.

  • source (str) – Human context (e.g. builder name) included in the message.

Return type:

None

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: object

A built visualization: save it, embed it, or display it in Jupyter.

Parameters:
as_html()[source]

Return the self-contained offline HTML document.

Return type:

str

as_svg(*, width=1200, height=800, layout=None, 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. layout defaults to the payload’s suggested layout (reasoning figures suggest "tree"; everything else defaults to "circle").

Parameters:
Return type:

str

options: VizOptions
save(path)[source]

Write the artifact to path and return the resolved path.

Parameters:

path (str | Path)

Return type:

Path

save_svg(path, **kwargs)[source]

Write as_svg() output and return the resolved destination.

Parameters:
Return type:

Path

viz: Any
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: object

Labels, property styling, caps, and initial notebook-view settings.

node_label_property / edge_label_property choose readable labels with ID/type fallbacks. edge_labels is off, selected (also hovered/focused), or all. Size/width properties use bounded numeric scales; color properties use categorical values. These mappings also apply to static SVG, where relationship labels require edge_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)

absolute_max_edges: int | None = None
absolute_max_nodes: int | None = None
charge: float = -300.0
edge_color_property: str | None = None
edge_label_property: str | None = None
edge_labels: str = 'selected'
edge_width_property: str | None = None
height: int = 600
label_max_length: int = 40
max_edges: int = 5000
max_nodes: int = 2000
node_color_property: str | None = None
node_label_property: str | None = None
node_size_property: str | None = None
show_labels: bool = True
show_properties: bool = True
theme: str = 'light'
title: str = 'GestaltDB graph'
view_overrides()[source]

Return the front-end initial-state overrides for this options set.

Return type:

dict[str, Any]

gestaltdb.viz.api.visualize_claim_explanation(explanation, *, options=None, **caps)[source]

Visualize a ClaimExplanation derivation graph from graph.rules.explain.

Claim, rule, and absence versions become color-grouped nodes; stored derived_by / premise / absent_premise edges read as “depends on” from the highlighted root outward. The inspector carries the statement triple, agent/polarity, rule head, and absence scope. SVG output needs no browser and keeps the same topology.

Parameters:
Return type:

VizFigure

gestaltdb.viz.api.visualize_inference_audit(report, *, options=None, **caps)[source]

Visualize an inference-audit document from export_inference.

Accepts an InferenceExplanation or its to_dict() mapping, so archived audits render without a live database handle. Node kinds (claim/model_output/rule/absence) become groups and recorded justifications become derivation edges.

Parameters:
Return type:

VizFigure

gestaltdb.viz.api.visualize_inference_chain(nodes, edges, explanation=None, ontology=None, *, link_layers=True, options=None, **caps)[source]

Visualize the whole inference chain in one figure.

Circles mark the data layer (property-graph nodes plus asserted/input facts), triangles mark inferred conclusions, and squares mark schema (rules, absences, axiom nodes, ontology terms). Stored and structural edges are solid; inference steps are dotted. With link_layers=True, statements link to graph nodes they mention and to ontology properties they instantiate, both by exact ID equality only.

Parameters:
Return type:

VizFigure

gestaltdb.viz.api.visualize_nodes_edges(nodes, edges, *, options=None, **caps)[source]

Visualize explicit node/edge collections.

Parameters:
  • nodes (Any) – Node objects or node-shaped dicts.

  • edges (Any) – Edge objects or edge-shaped dicts.

  • options (VizOptions | Mapping[str, Any] | None) – VizOptions or equivalent mapping.

  • caps (Any) – Optional max_nodes/max_edges overrides.

Return type:

VizFigure

Examples

Create a portable SVG without a database, browser, or notebook:

 1>>> from tempfile import TemporaryDirectory
 2>>> from pathlib import Path
 3>>> from gestaltdb.graphdb import Node, Edge
 4>>> from gestaltdb.viz.api import VizOptions, visualize_nodes_edges
 5>>> figure = visualize_nodes_edges(
 6...     [Node("a", properties={"name": "Alice"}), Node("b", properties={"name": "Bob"})],
 7...     [Edge("ab", "a", "b", properties={"type": "knows"})],
 8...     options=VizOptions(node_label_property="name"))
 9>>> assert "Alice" in figure.as_svg()
10>>> with TemporaryDirectory() as root:
11...     _ = figure.save_svg(Path(root) / "graph.svg")
12...     assert (Path(root) / "graph.svg").is_file()

figure.as_html() produces the interactive viewer. The visualization guide covers notebook display, query/sample builders, caps, and browser export.

gestaltdb.viz.api.visualize_ontology(ontology, *, options=None, **caps)[source]

Visualize an ontology revision as a class/property hierarchy graph.

Binary axioms become single typed edges (subclass, subproperty, equivalence, domain, range, inverses, characteristics); property chains and intersections become explicit axiom nodes so n-ary premises are never dropped. Union members each imply the superclass and stay direct edges.

Parameters:
Return type:

VizFigure

gestaltdb.viz.api.visualize_proof_explanation(explanation, *, options=None, **caps)[source]

Visualize a standalone Datalog proof witness as a fact tree.

Each ground fact is one node (input vs derived groups) and each witness premise is one directed premise → conclusion edge typed by the deriving rule ID, with the explained root highlighted. Works for ProofTrace.explain and EvaluationResult.explain results.

Parameters:
Return type:

VizFigure

gestaltdb.viz.api.visualize_query(graph, cypher, parameters=None, *, options=None)[source]

Run a Cypher query and visualize the matched entities.

Parameters:
Return type:

VizFigure

gestaltdb.viz.api.visualize_sample(graph, seeds, pattern, *, rng=None, options=None)[source]

Sample a typed subgraph around seeds and visualize it.

Parameters:
Return type:

VizFigure

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_global to compact global IDs and then via snapshot.external_node_id(...) / snapshot.external_relation_id(...). The sampling-first path for inspecting SamplerEngine output without dumping whole snapshots.

Parameters:
Return type:

VizFigure

gestaltdb.viz.api.visualize_support_set(fact, supports, witness=None, *, options=None, **caps)[source]

Visualize every alternative support for one typed fact.

The conclusion fans out to one support node per derivation, each fanning out to its premises. A supplied RankedWitness (from minimal_witness / top_k) highlights the chosen derivation against the full alternative set instead of hiding the alternatives.

Parameters:
Return type:

VizFigure

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 exposing to_dict()) or an already-serialized payload mapping.

  • title (str) – Document title (HTML-escaped).

  • warn_on_stale_bundle (bool) – Emit a RuntimeWarning when 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:

str

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:

bool

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:

dict[str, Any]

gestaltdb.viz.html.save_html(viz_graph, path, *, title='GestaltDB graph', warn_on_stale_bundle=True, view_overrides=None)[source]

Write a self-contained HTML artifact to path.

Creates parent directories as needed. Returns the resolved path.

Parameters:
Return type:

Path