Package dev.agenor.runtime.knowledge
Class InMemoryKnowledgeStore<C>
java.lang.Object
dev.agenor.runtime.knowledge.InMemoryKnowledgeStore<C>
- Type Parameters:
C- category type
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(KnowledgeDocument<C> document) Adds or replaces a document.getByCategory(C category) Returns all documents belonging tocategoryin unspecified order.Returns the document with the given ID, orOptional.empty()if no such document exists.Returns up totopKdocuments ordered by descending relevance toqueryacross all categories.searchByCategory(String query, C category, int topK) Returns up totopKdocuments restricted tocategory, ordered by descending relevance toquery.intsize()Returns the total number of documents currently stored.
-
Constructor Details
-
InMemoryKnowledgeStore
public InMemoryKnowledgeStore()
-
-
Method Details
-
add
Adds or replaces a document.If a document with the same
idalready exists it is silently replaced (last-write-wins semantics).- Specified by:
addin interfaceKnowledgeStore<C>- Parameters:
document- document to store (non-null)
-
getById
Returns the document with the given ID, orOptional.empty()if no such document exists.- Specified by:
getByIdin interfaceKnowledgeStore<C>- Parameters:
id- document identifier (non-null)- Returns:
- matching document or empty
-
search
Returns up totopKdocuments ordered by descending relevance toqueryacross all categories.Documents with a
KnowledgeDocument.relevanceScore(java.lang.String)of0.0are excluded from the results.- Specified by:
searchin interfaceKnowledgeStore<C>- Parameters:
query- search query (non-null)topK- maximum number of results to return (> 0)- Returns:
- ordered list of matching documents; never
null
-
searchByCategory
Returns up totopKdocuments restricted tocategory, ordered by descending relevance toquery.Documents with a
KnowledgeDocument.relevanceScore(java.lang.String)of0.0are excluded from the results.- Specified by:
searchByCategoryin interfaceKnowledgeStore<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
Returns all documents belonging tocategoryin unspecified order.- Specified by:
getByCategoryin interfaceKnowledgeStore<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:
sizein interfaceKnowledgeStore<C>- Returns:
- document count (
>= 0)
-