org.apache.jackrabbit.oak.spi.blob
Class AbstractBlobStore

java.lang.Object
  extended by org.apache.jackrabbit.oak.spi.blob.AbstractBlobStore
All Implemented Interfaces:
org.apache.jackrabbit.oak.commons.cache.Cache.Backend<AbstractBlobStore.BlockId,AbstractBlobStore.Data>, BlobStore, GarbageCollectableBlobStore
Direct Known Subclasses:
FileBlobStore, MemoryBlobStore

public abstract class AbstractBlobStore
extends Object
implements GarbageCollectableBlobStore, org.apache.jackrabbit.oak.commons.cache.Cache.Backend<AbstractBlobStore.BlockId,AbstractBlobStore.Data>

An abstract data store that splits the binaries in relatively small blocks, so that each block fits in memory.

Each data store id is a list of zero or more entries. Each entry is either

Thanks to the indirection, blocks can be kept relatively small, so that caching is simpler, and so that the storage backend doesn't need to support arbitrary size blobs (some storage backends buffer blobs in memory) and fast seeks (some storage backends re-read the whole blob when seeking).

The format of a 'data' entry is: type (one byte; 0 for data), length (variable size int), data (bytes).

The format of a 'hash of content' entry is: type (one byte; 1 for hash), level (variable size int, 0 meaning not nested), size (variable size long), hash code length (variable size int), hash code.

The format of a 'hash of data store id' entry is: type (one byte; 1 for hash), level (variable size int, nesting level), total size (variable size long), size of data store id (variable size long), hash code length (variable size int), hash code.


Nested Class Summary
static class AbstractBlobStore.BlockId
          A block id.
static class AbstractBlobStore.Data
          The data for a block.
 
Field Summary
protected static int BLOCK_SIZE_LIMIT
           
protected static String HASH_ALGORITHM
           
protected  Map<String,WeakReference<String>> inUse
           
protected static int TYPE_DATA
           
protected static int TYPE_HASH
           
protected static int TYPE_HASH_COMPRESSED
           
 
Constructor Summary
AbstractBlobStore()
           
 
Method Summary
 void clearInUse()
          Clear all objects marked as "transiently in use".
 String getBlobId(String reference)
          Returns the blobId that referred by the given binary reference.
 long getBlobLength(String blobId)
          Get the length of the blob.
 int getBlockSize()
           
 long getBlockSizeMin()
          Get the minimum block size (if there is any).
 InputStream getInputStream(String blobId)
          Returns a new stream for given blobId.
protected  byte[] getOrCreateReferenceKey()
          Returns the reference key of this blob store.
 String getReference(String blobId)
          Returns a secure reference to blob referred by blobid, or null if no such reference is available.
protected abstract  boolean isMarkEnabled()
           
 AbstractBlobStore.Data load(AbstractBlobStore.BlockId id)
           
protected abstract  void mark(AbstractBlobStore.BlockId id)
           
protected  void mark(String blobId)
           
protected  void markInUse()
           
 int readBlob(String blobId, long pos, byte[] buff, int off, int length)
          Read a number of bytes from a blob.
protected abstract  byte[] readBlockFromBackend(AbstractBlobStore.BlockId id)
          Load the block from the storage backend.
 Iterator<String> resolveChunks(String blobId)
          Resolve chunks from the given Id.
 void setBlockSize(int x)
          Set the block size used by this blob store, if the blob store splits binaries into blocks.
 void setBlockSizeMin(int x)
           
 void setReferenceKey(byte[] referenceKey)
           
 void setReferenceKeyEncoded(String encodedKey)
          Set the referenceKey from Base64 encoded byte array
 void setReferenceKeyPlainText(String textKey)
          Set the referenceKey from plain text.
abstract  void startMark()
          Start the mark phase.
protected abstract  void storeBlock(byte[] digest, int level, byte[] data)
          Store a block of data.
abstract  int sweep()
          Remove all unused blocks.
protected  void usesBlobId(String blobId)
           
 String writeBlob(InputStream in)
          Write a blob from an input stream.
 String writeBlob(String tempFilePath)
          Write a blob from a temporary file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.jackrabbit.oak.spi.blob.GarbageCollectableBlobStore
clearCache, deleteChunks, getAllChunkIds
 

Field Detail

HASH_ALGORITHM

protected static final String HASH_ALGORITHM
See Also:
Constant Field Values

TYPE_DATA

protected static final int TYPE_DATA
See Also:
Constant Field Values

TYPE_HASH

protected static final int TYPE_HASH
See Also:
Constant Field Values

TYPE_HASH_COMPRESSED

protected static final int TYPE_HASH_COMPRESSED
See Also:
Constant Field Values

BLOCK_SIZE_LIMIT

protected static final int BLOCK_SIZE_LIMIT
See Also:
Constant Field Values

inUse

protected Map<String,WeakReference<String>> inUse
Constructor Detail

AbstractBlobStore

public AbstractBlobStore()
Method Detail

setBlockSizeMin

public void setBlockSizeMin(int x)

getBlockSizeMin

public long getBlockSizeMin()
Description copied from interface: GarbageCollectableBlobStore
Get the minimum block size (if there is any).

Specified by:
getBlockSizeMin in interface GarbageCollectableBlobStore
Returns:
the block size

setBlockSize

public void setBlockSize(int x)
Description copied from interface: GarbageCollectableBlobStore
Set the block size used by this blob store, if the blob store splits binaries into blocks. If not, this setting is ignored.

Specified by:
setBlockSize in interface GarbageCollectableBlobStore
Parameters:
x - the block size in bytes.

getBlockSize

public int getBlockSize()

writeBlob

public String writeBlob(String tempFilePath)
                 throws IOException
Description copied from interface: GarbageCollectableBlobStore
Write a blob from a temporary file. The temporary file is removed afterwards. A file based blob stores might simply rename the file, so that no additional writes are necessary.

Specified by:
writeBlob in interface GarbageCollectableBlobStore
Parameters:
tempFilePath - the temporary file name
Returns:
the blob id
Throws:
IOException

writeBlob

public String writeBlob(InputStream in)
                 throws IOException
Description copied from interface: BlobStore
Write a blob from an input stream. This method closes the input stream.

Specified by:
writeBlob in interface BlobStore
Parameters:
in - the input stream
Returns:
the blob id
Throws:
IOException

getInputStream

public InputStream getInputStream(String blobId)
                           throws IOException
Description copied from interface: BlobStore
Returns a new stream for given blobId. The streams returned from multiple calls to this method are byte wise equals. That is, subsequent calls to read return the same sequence of bytes as long as neither call throws an exception.

Specified by:
getInputStream in interface BlobStore
Parameters:
blobId - the blob id
Returns:
a new stream for given blobId
Throws:
IOException

getReference

public String getReference(String blobId)
Description copied from interface: BlobStore
Returns a secure reference to blob referred by blobid, or null if no such reference is available.

Specified by:
getReference in interface BlobStore
Parameters:
blobId - blobId referring the blob for which reference is required
Returns:
binary reference, or null

getBlobId

public String getBlobId(String reference)
Description copied from interface: BlobStore
Returns the blobId that referred by the given binary reference. Returns null if the reference is invalid, for example if it points to a blob that does not exist.

Specified by:
getBlobId in interface BlobStore
Parameters:
reference - binary reference
Returns:
matching blobId, or null

getOrCreateReferenceKey

protected byte[] getOrCreateReferenceKey()
Returns the reference key of this blob store. If one does not already exist, it is automatically created in an implementation-specific way. The default implementation simply creates a temporary random key that's valid only until the data store gets restarted. Subclasses can override and/or decorate this method to support a more persistent reference key.

This method is called only once during the lifetime of a data store instance and the return value is cached in memory, so it's no problem if the implementation is slow.

Returns:
reference key

setReferenceKey

public void setReferenceKey(byte[] referenceKey)

setReferenceKeyEncoded

public void setReferenceKeyEncoded(String encodedKey)
Set the referenceKey from Base64 encoded byte array

Parameters:
encodedKey - base64 encoded key

setReferenceKeyPlainText

public void setReferenceKeyPlainText(String textKey)
Set the referenceKey from plain text. Key content would be UTF-8 encoding of the string.

This is useful when setting key via generic bean property manipulation from string properties. User can specify the key in plain text and that would be passed on this object via PropertiesUtil.populate(Object, java.util.Map, boolean)

Parameters:
textKey - base64 encoded key
See Also:
PropertiesUtil.populate(Object, java.util.Map, boolean)

usesBlobId

protected void usesBlobId(String blobId)

clearInUse

public void clearInUse()
Description copied from interface: GarbageCollectableBlobStore
Clear all objects marked as "transiently in use".

Specified by:
clearInUse in interface GarbageCollectableBlobStore

storeBlock

protected abstract void storeBlock(byte[] digest,
                                   int level,
                                   byte[] data)
                            throws IOException
Store a block of data.

Parameters:
digest - the content hash
level - the indirection level (0 is for user data, 1 is a list of digests that point to user data, 2 is a list of digests that point to digests, and so on). This parameter is for informational use only, and it is not required to store it unless that's easy to achieve
data - the data to be stored
Throws:
IOException

startMark

public abstract void startMark()
                        throws IOException
Description copied from interface: GarbageCollectableBlobStore
Start the mark phase.

Specified by:
startMark in interface GarbageCollectableBlobStore
Throws:
IOException

sweep

public abstract int sweep()
                   throws IOException
Description copied from interface: GarbageCollectableBlobStore
Remove all unused blocks.

Specified by:
sweep in interface GarbageCollectableBlobStore
Returns:
the number of removed blocks
Throws:
IOException

isMarkEnabled

protected abstract boolean isMarkEnabled()

mark

protected abstract void mark(AbstractBlobStore.BlockId id)
                      throws Exception
Throws:
Exception

markInUse

protected void markInUse()
                  throws IOException
Throws:
IOException

readBlob

public int readBlob(String blobId,
                    long pos,
                    byte[] buff,
                    int off,
                    int length)
             throws IOException
Description copied from interface: BlobStore
Read a number of bytes from a blob.

Specified by:
readBlob in interface BlobStore
Parameters:
blobId - the blob id
pos - the position within the blob
buff - the target byte array
off - the offset within the target array
length - the number of bytes to read
Returns:
the number of bytes read
Throws:
IOException

load

public AbstractBlobStore.Data load(AbstractBlobStore.BlockId id)
Specified by:
load in interface org.apache.jackrabbit.oak.commons.cache.Cache.Backend<AbstractBlobStore.BlockId,AbstractBlobStore.Data>

readBlockFromBackend

protected abstract byte[] readBlockFromBackend(AbstractBlobStore.BlockId id)
                                        throws Exception
Load the block from the storage backend. Returns null if the block was not found.

Parameters:
id - the block id
Returns:
the block data, or null
Throws:
Exception

getBlobLength

public long getBlobLength(String blobId)
                   throws IOException
Description copied from interface: BlobStore
Get the length of the blob.

Specified by:
getBlobLength in interface BlobStore
Parameters:
blobId - the blob id
Returns:
the length
Throws:
IOException

mark

protected void mark(String blobId)
             throws IOException
Throws:
IOException

resolveChunks

public Iterator<String> resolveChunks(String blobId)
                               throws IOException
Description copied from interface: GarbageCollectableBlobStore
Resolve chunks from the given Id.

Specified by:
resolveChunks in interface GarbageCollectableBlobStore
Parameters:
blobId - the blob id
Returns:
the iterator
Throws:
IOException - Signals that an I/O exception has occurred.


Copyright © 2012-2014 The Apache Software Foundation. All Rights Reserved.