Class InMemoryKnowledgeStore<C>

java.lang.Object
dev.agenor.runtime.knowledge.InMemoryKnowledgeStore<C>
Type Parameters:
C - category type
All Implemented Interfaces:
KnowledgeStore<C>

public class InMemoryKnowledgeStore<C> extends Object implements KnowledgeStore<C>
In-memory KnowledgeStore backed by a ConcurrentHashMap.

Retrieval is powered by KnowledgeDocument.relevanceScore(java.lang.String), which performs keyword matching on title, keywords, and content. This is suitable for development, testing, and small single-JVM knowledge bases where vector similarity is not required.

For production RAG use cases with dense-vector search, use an embedding-backed store from agenor-adapters.

This implementation is thread-safe.

Example:


 KnowledgeStore<SupportIntent> store = new InMemoryKnowledgeStore<>();
 store.add(new KnowledgeDocument<>("faq-001", "Return policy",
     "Items can be returned within 30 days.",
     SupportIntent.RETURNS, Set.of("return", "refund")));

 store.search("refund", 3).forEach(doc -> System.out.println(doc.title()));
 
  • Constructor Details

    • InMemoryKnowledgeStore

      public InMemoryKnowledgeStore()
  • Method Details

    • add

      public void add(KnowledgeDocument<C> document)
      Adds or replaces a document.

      If a document with the same id already exists it is silently replaced (last-write-wins semantics).

      Specified by:
      add in interface KnowledgeStore<C>
      Parameters:
      document - document to store (non-null)
    • getById

      public Optional<KnowledgeDocument<C>> getById(String id)
      Returns the document with the given ID, or Optional.empty() if no such document exists.
      Specified by:
      getById in interface KnowledgeStore<C>
      Parameters:
      id - document identifier (non-null)
      Returns:
      matching document or empty
    • search

      public List<KnowledgeDocument<C>> search(String query, int topK)
      Returns up to topK documents ordered by descending relevance to query across all categories.

      Documents with a KnowledgeDocument.relevanceScore(java.lang.String) of 0.0 are excluded from the results.

      Specified by:
      search in interface KnowledgeStore<C>
      Parameters:
      query - search query (non-null)
      topK - maximum number of results to return (> 0)
      Returns:
      ordered list of matching documents; never null
    • searchByCategory

      public List<KnowledgeDocument<C>> searchByCategory(String query, C category, int topK)
      Returns up to topK documents restricted to category, ordered by descending relevance to query.

      Documents with a KnowledgeDocument.relevanceScore(java.lang.String) of 0.0 are excluded from the results.

      Specified by:
      searchByCategory in interface KnowledgeStore<C>
      Parameters:
      query - search query (non-null)
      category - category filter (non-null)
      topK - maximum number of results to return (> 0)
      Returns:
      ordered list of matching documents; never null
    • getByCategory

      public List<KnowledgeDocument<C>> getByCategory(C category)
      Returns all documents belonging to category in unspecified order.
      Specified by:
      getByCategory in interface KnowledgeStore<C>
      Parameters:
      category - category filter (non-null)
      Returns:
      list of documents; never null, may be empty
    • size

      public int size()
      Returns the total number of documents currently stored.
      Specified by:
      size in interface KnowledgeStore<C>
      Returns:
      document count (>= 0)