Package io.bdeploy.bhive.objects
Class ObjectDatabase
java.lang.Object
io.bdeploy.bhive.objects.LockableDatabase
io.bdeploy.bhive.objects.ObjectDatabase
- Direct Known Subclasses:
MarkerDatabase
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.
-
Nested Class Summary
Nested classes/interfaces inherited from class io.bdeploy.bhive.objects.LockableDatabase
LockableDatabase.LockedOperation -
Constructor Summary
ConstructorsConstructorDescriptionObjectDatabase(Path root, Path tmp, ActivityReporter reporter, BHiveTransactions transactions) Create a newObjectDatabaseat the given root. -
Method Summary
Modifier and TypeMethodDescriptionaddObject(byte[] bytes) Add a new object to the database from in-memory data.addObject(InputStream stream) Add a new object to the database from the givenInputStream.Add a new object to the database from an existing file.booleancheckObject(ObjectId id) Scan for and retrieve all objects in the database.longRetrieve the file size for the file backingObjectId.Retrieves an InputStream from which the actual content of an object with the givenObjectIdcan be read.booleanChecks whether the object with the givenObjectIdexists in the database.protected ObjectIdinternalAddObject(ObjectWriter writer) voidremoveObject(ObjectId id) Removes the givenObjectIds backing file from the database.Methods inherited from class io.bdeploy.bhive.objects.LockableDatabase
locked
-
Constructor Details
-
ObjectDatabase
public ObjectDatabase(Path root, Path tmp, ActivityReporter reporter, BHiveTransactions transactions) Create a newObjectDatabaseat the given root. The database is not required to exist yet, it will be created initially empty in this case.- Parameters:
root- the rootPathwhere the database is located.tmp- directory to store temporary files into.reporter- anActivityReporterused to report possibly long running operations.
-
-
Method Details
-
getStream
Retrieves an InputStream from which the actual content of an object with the givenObjectIdcan be read.- Parameters:
id- theObjectIdof the object to lookup.- Returns:
- an
InputStreamto the object. - Throws:
IOException- in case of an error.
-
hasObject
Checks whether the object with the givenObjectIdexists in the database.- Parameters:
id- theObjectIdto check- Returns:
trueif it exists,falseotherwise.
-
addObject
Add a new object to the database from an existing file. This method will delegate toaddObject(byte[])oraddObject(InputStream)depending on the size of the file to add.- Parameters:
file-Pathto the file to add- Returns:
- the calculated
ObjectIdunder which the object has been persisted. - Throws:
IOException- in case of an error.
-
addObject
Add a new object to the database from in-memory data.- Parameters:
bytes- the objects content- Returns:
- the calculated
ObjectIdunder which the object has been persisted. - Throws:
IOException- in case of an error.
-
addObject
Add a new object to the database from the givenInputStream. TheObjectIdis 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
FileSystemwhich does not support moving.- Parameters:
stream- the raw object's data- Returns:
- the calculated
ObjectIdunder which the object has been persisted. - Throws:
IOException- in case of an error.
-
internalAddObject
- Throws:
IOException
-
checkObject
Verifies that a givenObjectIds backing file still hashes to the givenObjectId. This can be used to detect corruption of objects.- Throws:
IOException
-
removeObject
Removes the givenObjectIds backing file from the database.WARNING: use with care, this can cause corruption!
-
getObjectFile
-
getObjectSize
Retrieve the file size for the file backingObjectId.- Throws:
IOException
-
getAllObjects
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.
-