GraphEngine and GraphRepository¶
GraphEngine is the single entry-point for all graph mutations. It owns an in-memory cache of loaded graphs and delegates all persistence I/O through the injected GraphRepository.
Architecture¶
graph TD
Service["GraphService / OutlineService"] --> Engine["GraphEngine"]
Engine --> Cache["_cache: GraphId to Graph dict"]
Engine --> Repo["GraphRepository (interface)"]
Repo --> SQLite["SqliteGraphRepository"]
The engine follows the Repository + Cache pattern:
- On
get_graph(), check the cache first. On a cache miss, load from the repository and populate the cache. - On any mutation, update both the in-memory graph and the repository so they stay in sync.
- On
delete_graph(), evict from cache and delete from repository.
GraphRepository Interface¶
GraphRepository is an abstract base class that any persistence backend must implement. The engine depends only on this interface, never on SQLAlchemy or any other framework.
GraphRepository
├── save_graph(graph)
├── load_graph(graph_id) -> Graph | None
├── list_graphs(workspace_id) -> list[Graph]
├── delete_graph(graph_id)
├── save_node(node)
├── delete_node(node_id)
├── save_edge(edge)
└── delete_edge(edge_id)
To provide a custom persistence backend (e.g. PostgreSQL, a remote API), subclass GraphRepository and implement all eight methods.
Single-Operation Helpers vs. Transactions¶
The engine provides two ways to mutate graphs:
Single-operation helpers (auto-commit each change immediately):
engine.add_node(node)
engine.update_node(updated_node)
engine.remove_node(graph_id, node_id)
engine.add_edge(edge)
engine.remove_edge(graph_id, edge_id)
Explicit transactions (batch multiple operations):
from knowledge_platform.core.transaction import transaction
with transaction(graph.id) as tx:
tx.record_add_node(parent)
tx.record_add_node(child)
tx.record_add_edge(edge)
engine.commit(tx)
Use the single-operation helpers for simple, independent changes. Use transactions when you need atomicity across multiple related changes.
Graph Lifecycle¶
# Create
graph = engine.create_graph(workspace_id, "outline", "My Notes")
# Read (from cache or repo)
graph = engine.get_graph(graph_id)
# List all in a workspace
graphs = engine.list_graphs(workspace_id)
# Delete (evicts cache + removes from repo)
engine.delete_graph(graph_id)
Error Handling¶
| Operation | Raises | Condition |
|---|---|---|
get_graph(id) |
KeyError |
Graph not found in cache or repository |
commit(tx) |
RuntimeError |
Transaction already committed or rolled back |
commit(tx) |
ValueError |
Unknown operation in a ChangeRecord |
API Reference¶
knowledge_platform.core.engine.GraphRepository ¶
Abstract repository interface that persistence implementations must satisfy.
The engine delegates all I/O through this interface, keeping the engine itself free of SQLAlchemy or file-system concerns.
Source code in src/knowledge_platform/core/engine.py
Functions¶
delete_edge ¶
Delete an edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_id
|
EdgeId
|
Target identifier. |
required |
delete_graph ¶
Delete a graph and all its nodes/edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Target identifier. |
required |
delete_node ¶
Delete a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
NodeId
|
Target identifier. |
required |
list_graphs ¶
Return all graphs in a workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_id
|
WorkspaceId
|
Owning workspace. |
required |
Returns:
| Type | Description |
|---|---|
list[Graph]
|
List of :class: |
Source code in src/knowledge_platform/core/engine.py
load_graph ¶
Load a graph by ID, or return None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Target identifier. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Loaded |
Graph | None
|
class: |
save_edge ¶
Persist an edge (insert or update).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge
|
Edge
|
The edge to save. |
required |
save_graph ¶
Persist a graph record (insert or update).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph to save. |
required |
knowledge_platform.core.engine.GraphEngine ¶
Orchestrates graph operations against an in-memory cache + repository.
:class:GraphEngine is the single entry-point for all graph mutations.
It keeps a live in-memory cache of loaded graphs and persists changes
through the injected :class:GraphRepository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repository
|
GraphRepository
|
The persistence back-end. |
required |
Source code in src/knowledge_platform/core/engine.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
Functions¶
add_edge ¶
Add an edge to its owning graph without an explicit transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge
|
Edge
|
Edge to add. |
required |
Source code in src/knowledge_platform/core/engine.py
add_node ¶
Add a node to its owning graph without an explicit transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Node to add; its :attr: |
required |
Source code in src/knowledge_platform/core/engine.py
commit ¶
Apply all changes recorded in tx to the graph and repository.
Changes are applied in recording order. On any error the in-memory graph is left in a partially-applied state; callers should discard and reload the graph if recovery is required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tx
|
Transaction
|
A completed (not yet committed) :class: |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the transaction is already committed or rolled back. |
KeyError
|
If the target graph is not found. |
Source code in src/knowledge_platform/core/engine.py
create_graph ¶
Create and persist a new empty graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_id
|
WorkspaceId
|
Owning workspace. |
required |
type_name
|
str
|
Semantic graph type name. |
required |
name
|
str
|
Optional display name. |
''
|
Returns:
| Type | Description |
|---|---|
Graph
|
Newly created :class: |
Source code in src/knowledge_platform/core/engine.py
delete_graph ¶
Delete a graph and remove it from cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Target identifier. |
required |
Source code in src/knowledge_platform/core/engine.py
get_graph ¶
Return a graph from cache or repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Target identifier. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
Graph
|
class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the graph does not exist. |
Source code in src/knowledge_platform/core/engine.py
list_graphs ¶
List all graphs in a workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_id
|
WorkspaceId
|
Owning workspace. |
required |
Returns:
| Type | Description |
|---|---|
list[Graph]
|
List of :class: |
Source code in src/knowledge_platform/core/engine.py
remove_edge ¶
Remove an edge from a graph without an explicit transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Owning graph. |
required |
edge_id
|
EdgeId
|
Edge to remove. |
required |
Source code in src/knowledge_platform/core/engine.py
remove_node ¶
Remove a node from a graph without an explicit transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Owning graph. |
required |
node_id
|
NodeId
|
Node to remove. |
required |
Source code in src/knowledge_platform/core/engine.py
update_node ¶
Update a node in its owning graph without an explicit transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Updated node. |
required |