Class MibEmbeddingIndex

java.lang.Object
org.snmp4j.mcp.tool.mib.semantic.MibEmbeddingIndex

public class MibEmbeddingIndex extends Object
In-memory vector index over MIB objects with cosine-similarity search and optional GZIP'd JSON persistence.

Each MIB object's DESCRIPTION is split into one or more paragraph-aligned chunks by the MibChunkFormatter, and each chunk is embedded and indexed independently. Search results are deduplicated to one hit per object, keeping the best-scoring chunk.

Usage pattern (lazy build):

  index.buildAsync(smiManager, embeddingClient, formatter, cachePath);
  // later …
  List<SearchResult> hits = index.search(queryVector, 10, null);
Since:
1.0.0
  • Constructor Details

    • MibEmbeddingIndex

      public MibEmbeddingIndex()
      Creates a new, empty embedding index.
  • Method Details

    • build

      public void build(com.snmp4j.smi.SmiManager smiManager, EmbeddingClient client, MibChunkFormatter formatter) throws IOException
      Builds the index synchronously by embedding all chunks of every object returned by smiManager.findSmiObject(null, ACCEPT_ALL).

      Clears any previously indexed content first.

      Parameters:
      smiManager - source of MIB objects
      client - embedding backend
      formatter - splits SmiObjects into one or more text chunks
      Throws:
      IOException - if the embedding client reports an error
    • upsert

      public void upsert(com.snmp4j.smi.SmiObject obj, EmbeddingClient client, MibChunkFormatter formatter) throws IOException
      Incrementally upserts (adds or replaces) a single object and all of its chunks.
      Parameters:
      obj - the MIB object to index
      client - embedding backend used to generate the vectors
      formatter - splits the object into one or more embeddable text chunks
      Throws:
      IOException - if the embedding client reports a transport or API error
    • remove

      public void remove(String oid)
      Removes all chunk entries for the given OID, if present.
      Parameters:
      oid - dotted-decimal OID string key to remove
    • search

      public List<MibEmbeddingIndex.SearchResult> search(float[] queryVector, int limit, String typeFilter, String syntaxFilter)
      Searches the index by cosine similarity to queryVector.

      When an object has multiple chunks, only its best-scoring chunk contributes one result; the response contains at most one entry per OID.

      Parameters:
      queryVector - pre-embedded query
      limit - maximum number of results (clamped to 1..1000)
      typeFilter - optional SmiType name substring filter, e.g. "OBJECT_TYPE"
      syntaxFilter - optional SmiSyntax name substring filter, e.g. "OBJECT IDENTIFIER"
      Returns:
      results sorted by descending score, one per OID
    • save

      public void save(Path path) throws IOException
      Saves the index to a GZIP'd JSON file at path. The file can be reloaded with load(Path).
      Parameters:
      path - destination file path
      Throws:
      IOException - if the file cannot be written
    • load

      public static MibEmbeddingIndex load(Path path) throws IOException
      Loads a previously saved index from a GZIP'd JSON file. Both the legacy 1.0 (one entry per OID) and the current chunked format are accepted.
      Parameters:
      path - the file written by save(Path)
      Returns:
      a populated index (the caller should verify modelId matches the current client)
      Throws:
      IOException - if the file cannot be read or parsed
    • size

      public int size()
      Returns the number of indexed chunks.
      Returns:
      number of indexed chunks (an object may contribute more than one)
    • isEmpty

      public boolean isEmpty()
      Checks if the index contains no chunks.
      Returns:
      true if the index contains no chunks
    • getModelId

      public String getModelId()
      Returns the embedding model ID that was active when the index was built or loaded.
      Returns:
      model identifier string, or an empty string if unknown
    • copyFrom

      public void copyFrom(MibEmbeddingIndex source)
      Replaces all content of this index with the content of source. Used to update the live-shared instance from a freshly loaded or rebuilt index.
      Parameters:
      source - the index whose data should be copied into this instance