Class VectorMemoryStore

java.lang.Object
com.tnsai.memory.advanced.VectorMemoryStore

public final class VectorMemoryStore extends Object
In-memory vector store for semantic search over memory entries.

Stores entries alongside their embedding vectors and supports cosine similarity search. Uses brute-force comparison, which is sufficient for typical agent memory sizes (<10k entries).

The first admitted vector pins the store's embedding dimension for its lifetime. Later additions and non-empty searches must use that dimension. Thread-safe via ConcurrentHashMap for the entry and vector maps.

  • Constructor Details

    • VectorMemoryStore

      public VectorMemoryStore(EmbeddingFunction embeddingFunction)
      Creates a new in-memory vector memory store.
      Parameters:
      embeddingFunction - the function to compute embeddings
    • VectorMemoryStore

      public VectorMemoryStore(EmbeddingFunction embeddingFunction, VectorIndex index)
      Creates a store over an explicit backend.
      Parameters:
      embeddingFunction - the function to compute embeddings
      index - storage backend
      Since:
      0.14.0
  • Method Details

    • add

      public void add(MemoryEntry entry)
      Adds a memory entry and computes its embedding.
      Parameters:
      entry - the memory entry to add
    • add

      public void add(MemoryEntry entry, float[] vector)
      Adds a memory entry with a pre-computed embedding.
      Parameters:
      entry - the memory entry
      vector - the pre-computed embedding vector
    • search

      public List<ScoredEntry> search(String query, int topK)
      Searches for the top-K most similar entries to the query.
      Parameters:
      query - the search query text
      topK - maximum number of results
      Returns:
      scored entries sorted by descending cosine similarity
    • searchByVector

      public List<ScoredEntry> searchByVector(float[] queryVector, int topK)
      Searches for the top-K most similar entries to a pre-computed query vector.
      Parameters:
      queryVector - the query embedding vector
      topK - maximum number of results
      Returns:
      scored entries sorted by descending cosine similarity
    • remove

      public boolean remove(String id)
      Removes an entry by ID.
      Parameters:
      id - the entry ID
      Returns:
      true if the entry was present and removed
    • size

      public int size()
      Returns the number of stored entries.
    • clear

      public void clear()
      Clears all entries and vectors without resetting the pinned dimension.