Skip to main content

Database API Reference

The Database is the main interface for interacting with EngramDB. This document provides a detailed reference for the Database API.

Creating a Database

Database::in_memory()

Creates a new in-memory database with linear vector search (volatile, for testing and development). Returns:
  • A new Database instance using memory storage and linear vector index
Example:

Database::in_memory_with_hnsw()

Creates a new in-memory database with HNSW vector search for faster similarity queries. Returns:
  • A new Database instance using memory storage and HNSW vector index
Example:

Database::file_based(dir)

Creates a file-based database at the specified directory with linear vector search. Parameters:
  • dir: &str or Path - Path to the storage directory
Returns:
  • A new Database instance using file storage, or an error if initialization failed
Example:

Database::file_based_with_hnsw(dir)

Creates a file-based database at the specified directory with HNSW vector search. Parameters:
  • dir: &str or Path - Path to the storage directory
Returns:
  • A new Database instance using file storage and HNSW vector index, or an error if initialization failed
Example:

Database::new(config)

Creates a new database with the given configuration. Parameters:
  • config: DatabaseConfig - Configuration options for the database
Returns:
  • A new Database instance, or an error if initialization failed
Example:

DatabaseConfig

The DatabaseConfig struct provides configuration options for the database:
Fields:
  • storage_type: The type of storage to use (StorageType::Memory, StorageType::MultiFile, or StorageType::SingleFile)
  • storage_path: Directory path for file storage (ignored if using memory storage)
  • cache_size: Size of the query result cache (0 to disable caching)
  • vector_index_config: Configuration for the vector index

StorageType

VectorIndexConfig

Fields:
  • algorithm: The vector indexing algorithm to use (VectorAlgorithm::Linear or VectorAlgorithm::HNSW)
  • hnsw: Configuration for the HNSW algorithm (if used)

Basic Operations

initialize()

Initializes the database by loading existing memories into the vector index. Returns:
  • Result<()> - Success or an error
Example:

save(node)

Saves a memory node to the database. Parameters:
  • node: &MemoryNode - The memory node to save
Returns:
  • Result<Uuid> - The ID of the saved memory node, or an error
Example:

load(id)

Loads a memory node by its ID. Parameters:
  • id: Uuid - The ID of the memory node to load
Returns:
  • Result<MemoryNode> - The loaded memory node, or an error if not found
Example:

delete(id)

Deletes a memory node by its ID. Parameters:
  • id: Uuid - The ID of the memory node to delete
Returns:
  • Result<()> - Success or an error
Example:

list_all()

Lists all memory node IDs in the database. Returns:
  • Result<Vec<Uuid>> - A vector of all memory node IDs, or an error
Example:

search_similar(query_vector, limit, threshold)

Searches for memory nodes with similar vector embeddings. Parameters:
  • query_vector: &[f32] - The query vector
  • limit: usize - Maximum number of results to return
  • threshold: f32 - Minimum similarity threshold (0.0 to 1.0)
Returns:
  • Result<Vec<(Uuid, f32)>> - A vector of (ID, similarity) pairs, sorted by descending similarity
Example:

Query Builder

query()

Creates a new query builder for complex queries. Returns:
  • A new QueryBuilder instance
Example:

Connection Management

get_connections(id)

Gets all connections for a memory node. Parameters:
  • id: Uuid - The ID of the memory node
Returns:
  • Result<Vec<ConnectionInfo>> - A vector of connection information, or an error
Example:

add_connection(source_id, target_id, relationship_type, strength)

Adds a connection between two memory nodes. Parameters:
  • source_id: Uuid - The ID of the source memory node
  • target_id: Uuid - The ID of the target memory node
  • relationship_type: RelationshipType - The type of relationship
  • strength: f32 - The strength of the connection (0.0 to 1.0)
Returns:
  • Result<()> - Success or an error
Example:

remove_connection(source_id, target_id)

Removes a connection between two memory nodes. Parameters:
  • source_id: Uuid - The ID of the source memory node
  • target_id: Uuid - The ID of the target memory node
Returns:
  • Result<bool> - True if a connection was removed, false otherwise, or an error
Example:

Background Processing

EngramDB includes a background processing system that can perform operations during idle periods:

Enabling Background Processing

Scheduling Background Tasks

Available Task Types

EngramDB supports several types of background tasks:

Error Handling

The database operations return a Result type that can contain various error types:
Example: