Class BindexRepository

java.lang.Object
org.machanism.machai.bindex.BindexRepository

public class BindexRepository extends Object
MongoDB-backed repository for persisting and retrieving Bindex documents.

The repository stores the serialized Bindex JSON in a dedicated field (see BINDEX_PROPERTY_NAME) and provides helper operations commonly needed by higher-level components such as Picker and tool integrations.

Connection details are resolved from configuration/environment:

  • When BINDEX_REPO_URL is configured, it is used as the MongoDB connection URI.
  • Otherwise a default cluster URI is used, with credentials optionally sourced from BINDEX_REG_PASSWORD.
Since:
0.0.2
Author:
Viktor Tovstyi
  • Field Details

  • Constructor Details

    • BindexRepository

      public BindexRepository(Configurator config)
      Creates a repository instance backed by a MongoDB collection.
      Parameters:
      config - configurator used to resolve BINDEX_REPO_URL
      Throws:
      IllegalArgumentException - if config is null
    • BindexRepository

      BindexRepository(com.mongodb.client.MongoCollection<org.bson.Document> collection)
      Creates a repository using an existing collection.

      Package-private constructor used for tests to avoid MongoDB driver initialization.

  • Method Details

    • getCollection

      public static com.mongodb.client.MongoCollection<org.bson.Document> getCollection(Configurator config)
      Provides direct access to the underlying MongoDB collection.

      Used by components such as Picker which operate on aggregation pipelines and need the raw MongoCollection.

      Parameters:
      config - configurator (kept for backward compatibility with callers)
      Returns:
      MongoDB collection handle
    • close

      public void close()
      Closes the underlying MongoClient if this repository created it.

      This allows callers to use try-with-resources:

       try (BindexRepository repo = new BindexRepository(config)) { ... }
       
    • getRegistredId

      public String getRegistredId(Bindex bindex)
      Looks up the registered database ID for a Bindex (if it exists).
      Parameters:
      bindex - Bindex instance
      Returns:
      MongoDB object id as string, or null if not present
      Throws:
      IllegalArgumentException - if bindex is null
    • getBindex

      public Bindex getBindex(String id)
      Retrieves a Bindex instance from the database by its Bindex id.
      Parameters:
      id - Bindex id (the id field in the stored document)
      Returns:
      parsed Bindex, or null if not present
      Throws:
      IllegalArgumentException - if id is null or the stored JSON cannot be parsed
    • deleteBindex

      public String deleteBindex(Bindex bindex)
      Deletes a Bindex document from the database.
      Parameters:
      bindex - Bindex to delete (by Bindex.getId())
      Returns:
      the deleted Bindex id
      Throws:
      IllegalArgumentException - if bindex is null
    • findFirst

      org.bson.Document findFirst(org.bson.conversions.Bson filter)
      Single overridable seam for querying the first matching document.
    • findFirst

      org.bson.Document findFirst(org.bson.Document filter)