graphcast.db
¶
Database connection and management components.
This package provides database connection implementations and management utilities for different graph databases (ArangoDB, Neo4j). It includes connection interfaces, query execution, and database operations.
Key Components
- Connection: Abstract database connection interface
- ConnectionManager: Database connection management
- ArangoDB: ArangoDB-specific implementation
- Neo4j: Neo4j-specific implementation
- Query: Query generation and execution utilities
Example
from graphcast.db import ConnectionManager from graphcast.db.arango import ArangoConnection manager = ConnectionManager( ... connection_config={"url": "http://localhost:8529"}, ... conn_class=ArangoConnection ... ) with manager as conn: ... conn.init_db(schema)
Connection
¶
Bases: ABC
Abstract base class for database connections.
This class defines the interface that all database connection implementations must follow. It provides methods for database operations, collection management, and data manipulation.
Note
All methods marked with @abc.abstractmethod must be implemented by concrete connection classes.
Source code in graphcast/db/connection.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 |
|
__init__()
¶
aggregate(class_name, aggregation_function, discriminant=None, aggregated_field=None, filters=None)
abstractmethod
¶
Perform aggregation on a collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
Name of the collection |
required | |
aggregation_function
|
AggregationType
|
Type of aggregation to perform |
required |
discriminant
|
str | None
|
Field to group by |
None
|
aggregated_field
|
str | None
|
Field to aggregate |
None
|
filters
|
list | dict | None
|
Query filters |
None
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Aggregation results |
Source code in graphcast/db/connection.py
close()
abstractmethod
¶
create_database(name)
abstractmethod
¶
Create a new database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the database to create |
required |
define_collections(schema)
abstractmethod
¶
Define collections based on the schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Schema
|
Schema containing collection definitions |
required |
define_edge_indices(edges)
abstractmethod
¶
Define indices for edge collections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edges
|
list[Edge]
|
List of edge configurations containing index definitions |
required |
define_indexes(schema)
¶
Define indexes for vertices and edges in the schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Schema
|
Schema containing vertex and edge configurations |
required |
Source code in graphcast/db/connection.py
define_vertex_indices(vertex_config)
abstractmethod
¶
Define indices for vertex collections.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vertex_config
|
VertexConfig
|
Vertex configuration containing index definitions |
required |
delete_collections(cnames=(), gnames=(), delete_all=False)
abstractmethod
¶
Delete collections from the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cnames
|
Collection names to delete |
()
|
|
gnames
|
Graph names to delete |
()
|
|
delete_all
|
Whether to delete all collections |
False
|
Source code in graphcast/db/connection.py
delete_database(name)
abstractmethod
¶
Delete a database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the database to delete |
required |
execute(query, **kwargs)
abstractmethod
¶
Execute a database query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
Query to execute |
required | |
**kwargs
|
Additional query parameters |
{}
|
fetch_docs(class_name, filters, limit, return_keys, unset_keys)
abstractmethod
¶
Fetch documents from a collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_name
|
Name of the collection |
required | |
filters
|
Query filters |
required | |
limit
|
Maximum number of documents to return |
required | |
return_keys
|
Keys to return |
required | |
unset_keys
|
Keys to unset |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
Fetched documents |
Source code in graphcast/db/connection.py
fetch_present_documents(batch, class_name, match_keys, keep_keys, flatten=False, filters=None)
abstractmethod
¶
Fetch documents that exist in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Batch of documents to check |
required | |
class_name
|
Name of the collection |
required | |
match_keys
|
Keys to match |
required | |
keep_keys
|
Keys to keep in result |
required | |
flatten
|
Whether to flatten the result |
False
|
|
filters
|
list | dict | None
|
Additional query filters |
None
|
Returns:
Name | Type | Description |
---|---|---|
list |
Documents that exist in the database |
Source code in graphcast/db/connection.py
init_db(schema, clean_start)
abstractmethod
¶
Initialize the database with the given schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Schema
|
Schema to initialize the database with |
required |
clean_start
|
Whether to clean existing data |
required |
Source code in graphcast/db/connection.py
insert_edges_batch(docs_edges, source_class, target_class, relation_name, collection_name, match_keys_source, match_keys_target, filter_uniques=True, uniq_weight_fields=None, uniq_weight_collections=None, upsert_option=False, head=None, **kwargs)
abstractmethod
¶
Insert a batch of edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs_edges
|
Edge documents to insert |
required | |
source_class
|
Source vertex class |
required | |
target_class
|
Target vertex class |
required | |
relation_name
|
Name of the relation |
required | |
collection_name
|
Name of the edge collection |
required | |
match_keys_source
|
Keys to match source vertices |
required | |
match_keys_target
|
Keys to match target vertices |
required | |
filter_uniques
|
Whether to filter unique edges |
True
|
|
uniq_weight_fields
|
Fields to consider for uniqueness |
None
|
|
uniq_weight_collections
|
Collections to consider for uniqueness |
None
|
|
upsert_option
|
Whether to upsert existing edges |
False
|
|
head
|
Optional head document |
None
|
|
**kwargs
|
Additional insertion parameters |
{}
|
Source code in graphcast/db/connection.py
insert_return_batch(docs, class_name)
abstractmethod
¶
Insert documents and return the inserted documents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs
|
Documents to insert |
required | |
class_name
|
Name of the collection |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
Inserted documents |
Source code in graphcast/db/connection.py
keep_absent_documents(batch, class_name, match_keys, keep_keys, filters=None)
abstractmethod
¶
Keep documents that don't exist in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Batch of documents to check |
required | |
class_name
|
Name of the collection |
required | |
match_keys
|
Keys to match |
required | |
keep_keys
|
Keys to keep in result |
required | |
filters
|
list | dict | None
|
Additional query filters |
None
|
Returns:
Name | Type | Description |
---|---|---|
list |
Documents that don't exist in the database |
Source code in graphcast/db/connection.py
upsert_docs_batch(docs, class_name, match_keys, **kwargs)
abstractmethod
¶
Upsert a batch of documents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs
|
Documents to upsert |
required | |
class_name
|
Name of the collection |
required | |
match_keys
|
Keys to match for upsert |
required | |
**kwargs
|
Additional upsert parameters |
{}
|
Source code in graphcast/db/connection.py
ConnectionManager
¶
Manager for database connections.
This class manages database connections to different graph database implementations. It provides a context manager interface for safe connection handling and automatic cleanup.
Attributes:
Name | Type | Description |
---|---|---|
conn_class_mapping |
Mapping of connection types to connection classes |
|
config |
ProtoConnectionConfig
|
Connection configuration |
working_db |
Current working database name |
|
conn |
Active database connection |
Source code in graphcast/db/manager.py
__enter__()
¶
Enter the context manager.
Creates and returns a new database connection.
Returns:
Name | Type | Description |
---|---|---|
Connection |
Database connection instance |
Source code in graphcast/db/manager.py
__exit__(exc_type, exc_value, exc_traceback)
¶
Exit the context manager.
Ensures the connection is properly closed when exiting the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc_type
|
Exception type if an exception occurred |
required | |
exc_value
|
Exception value if an exception occurred |
required | |
exc_traceback
|
Exception traceback if an exception occurred |
required |
Source code in graphcast/db/manager.py
__init__(secret_path=None, args=None, connection_config=None, **kwargs)
¶
Initialize the connection manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
secret_path
|
Path to configuration file |
None
|
|
args
|
Command line arguments |
None
|
|
connection_config
|
Optional[ProtoConnectionConfig]
|
Optional connection configuration |
None
|
**kwargs
|
Additional configuration parameters |
{}
|
Source code in graphcast/db/manager.py
close()
¶
Close the database connection.
Closes the active connection and performs any necessary cleanup.