org.apache.jackrabbit.mk.blobs
Class AbstractBlobStore

java.lang.Object
  extended by org.apache.jackrabbit.mk.blobs.AbstractBlobStore
All Implemented Interfaces:
BlobStore, Cache.Backend<AbstractBlobStore.BlockId,AbstractBlobStore.Data>
Direct Known Subclasses:
DbBlobStore, FileBlobStore, MemoryBlobStore

public abstract class AbstractBlobStore
extends Object
implements BlobStore, 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 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 clearCache()
           
 void clearInUse()
           
 long getBlobLength(String blobId)
          Get the length of the blob.
 int getBlockSize()
           
 long getBlockSizeMin()
           
protected abstract  boolean isMarkEnabled()
           
 AbstractBlobStore.Data load(AbstractBlobStore.BlockId id)
          Load the object.
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.
 void setBlockSize(int x)
           
 void setBlockSizeMin(int x)
           
abstract  void startMark()
           
protected abstract  void storeBlock(byte[] digest, int level, byte[] data)
           
abstract  int sweep()
           
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
 

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()

setBlockSize

public void setBlockSize(int x)

getBlockSize

public int getBlockSize()

writeBlob

public String writeBlob(String tempFilePath)
                 throws Exception
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.

Parameters:
tempFilePath - the temporary file
Returns:
the blob id
Throws:
Exception

writeBlob

public String writeBlob(InputStream in)
                 throws Exception
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:
Exception

usesBlobId

protected void usesBlobId(String blobId)

clearInUse

public void clearInUse()

clearCache

public void clearCache()

storeBlock

protected abstract void storeBlock(byte[] digest,
                                   int level,
                                   byte[] data)
                            throws Exception
Throws:
Exception

startMark

public abstract void startMark()
                        throws Exception
Throws:
Exception

sweep

public abstract int sweep()
                   throws Exception
Throws:
Exception

isMarkEnabled

protected abstract boolean isMarkEnabled()

mark

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

markInUse

protected void markInUse()
                  throws Exception
Throws:
Exception

readBlob

public int readBlob(String blobId,
                    long pos,
                    byte[] buff,
                    int off,
                    int length)
             throws Exception
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:
Exception

load

public AbstractBlobStore.Data load(AbstractBlobStore.BlockId id)
Description copied from interface: Cache.Backend
Load the object. The method does not need to be synchronized (it is synchronized in the cache)

Specified by:
load in interface Cache.Backend<AbstractBlobStore.BlockId,AbstractBlobStore.Data>
Parameters:
id - the key
Returns:
the value

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


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