Class ObjectDatabase

java.lang.Object
io.bdeploy.bhive.objects.LockableDatabase
io.bdeploy.bhive.objects.ObjectDatabase
Direct Known Subclasses:
MarkerDatabase

public class ObjectDatabase extends LockableDatabase
A simple key-value data store. For each object that is added to the database a ObjectId is calculated that can be used later to retrieve the content again. The identifier is based on the content. Two objects having the same identifier will be stored once. Additional metadata like the name or type of the added object is not stored.

Each object is stored internally as single file named with the object identifier. Files are placed in sub-directories to keep the overall amount of files per directory small. The first four characters of the identifier are used to determine the target directory. Two levels of directories are used. The first level is based on the first two characters and the second level on the next two characters.

  • Constructor Details

    • ObjectDatabase

      public ObjectDatabase(Path root, Path tmp, ActivityReporter reporter, BHiveTransactions transactions)
      Create a new ObjectDatabase at the given root. The database is not required to exist yet, it will be created initially empty in this case.
      Parameters:
      root - the root Path where the database is located.
      tmp - directory to store temporary files into.
      reporter - an ActivityReporter used to report possibly long running operations.
  • Method Details

    • getStream

      public InputStream getStream(ObjectId id) throws IOException
      Retrieves an InputStream from which the actual content of an object with the given ObjectId can be read.
      Parameters:
      id - the ObjectId of the object to lookup.
      Returns:
      an InputStream to the object.
      Throws:
      IOException - in case of an error.
    • hasObject

      public boolean hasObject(ObjectId id)
      Checks whether the object with the given ObjectId exists in the database.
      Parameters:
      id - the ObjectId to check
      Returns:
      true if it exists, false otherwise.
    • addObject

      public ObjectId addObject(Path file) throws IOException
      Add a new object to the database from an existing file. This method will delegate to addObject(byte[]) or addObject(InputStream) depending on the size of the file to add.
      Parameters:
      file - Path to the file to add
      Returns:
      the calculated ObjectId under which the object has been persisted.
      Throws:
      IOException - in case of an error.
    • addObject

      public ObjectId addObject(byte[] bytes) throws IOException
      Add a new object to the database from in-memory data.
      Parameters:
      bytes - the objects content
      Returns:
      the calculated ObjectId under which the object has been persisted.
      Throws:
      IOException - in case of an error.
    • addObject

      public ObjectId addObject(InputStream stream) throws IOException
      Add a new object to the database from the given InputStream. The ObjectId is calculated while writing the file contents to a temporary file, which is then moved to the final location.

      Attention: this can have a huge performance impact when operating on a FileSystem which does not support moving.

      Parameters:
      stream - the raw object's data
      Returns:
      the calculated ObjectId under which the object has been persisted.
      Throws:
      IOException - in case of an error.
    • internalAddObject

      protected ObjectId internalAddObject(ObjectWriter writer) throws IOException
      Throws:
      IOException
    • checkObject

      public boolean checkObject(ObjectId id) throws IOException
      Verifies that a given ObjectIds backing file still hashes to the given ObjectId. This can be used to detect corruption of objects.
      Throws:
      IOException
    • removeObject

      public void removeObject(ObjectId id)
      Removes the given ObjectIds backing file from the database.

      WARNING: use with care, this can cause corruption!

    • getObjectFile

      public Path getObjectFile(ObjectId id)
      Calculate the Path where a certain ObjectId can be found in the database. Use with caution.
    • getObjectSize

      public long getObjectSize(ObjectId id) throws IOException
      Retrieve the file size for the file backing ObjectId.
      Throws:
      IOException
    • getAllObjects

      public SortedSet<ObjectId> getAllObjects() throws IOException, InterruptedException
      Scan for and retrieve all objects in the database. This is potentially an expensive operation, as object presence is not cached.
      Throws:
      IOException - in case of an error.
      InterruptedException - when interrupted.