org.alliance.core.file.blockstorage
Class BlockStorage

java.lang.Object
  extended by java.lang.Thread
      extended by org.alliance.core.file.blockstorage.BlockStorage
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
CacheStorage, DownloadStorage

public abstract class BlockStorage
extends java.lang.Thread

The BlockStorage keeps track of a list of incomplete files. Information about these files (what blocks of the file are complete and where they're located on disk) is contained here.

This information can be serialized/deserialized to disk in order to resume downloads after a restart.

When downloading a file the portions of the file (called slices) that are received over the netword are directly sent here using the saveSlice method. The caller of saveSlice needs very little information about the file and what's complete in it. It just sends slices of the file here until there's nothing more to send.

The _incomplete_ directory is the representation of the BlockStorage on disk.

It's the job of this class to keep track of when the file is complete (the check is made at the end of saveSlice) and to move the file out of the incomplete files directory.

Created by IntelliJ IDEA. User: maciek Date: 2006-jan-20 Time: 13:01:14


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  CoreSubsystem core
           
protected  boolean isSequential
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
BlockStorage(java.lang.String storagePath, java.lang.String completeFilePath, CoreSubsystem core)
           
 
Method Summary
 boolean contains(Hash root)
           
 boolean containsBlock(Hash rootHash, int blockNumber)
           
 BlockFile getBlockFile(Hash root)
           
 BlockMask getBlockMaskFor(Hash root)
           
static BlockStorage getById(CoreSubsystem core, int id)
           
 java.io.File getCompleteFilePath()
           
 CoreSubsystem getCore()
           
 FileDescriptor getFD(Hash root)
           
 java.io.File getStoragePath()
           
abstract  int getStorageTypeId()
           
 boolean isRecentlyDownloaded(Hash rootHash)
           
 boolean isSequential()
           
 void removePermanently(Hash root)
           
 java.util.Set<Hash> rootHashes()
           
 void run()
           
 int saveSlice(Hash root, int blockNumber, int sliceOffset, java.nio.ByteBuffer slice, FileDescriptor fd)
           
 void shutdown()
           
protected abstract  void signalFileComplete(BlockFile bf)
           
 void waitForUnfinishedTasks()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

core

protected CoreSubsystem core

isSequential

protected boolean isSequential
Constructor Detail

BlockStorage

public BlockStorage(java.lang.String storagePath,
                    java.lang.String completeFilePath,
                    CoreSubsystem core)
Method Detail

signalFileComplete

protected abstract void signalFileComplete(BlockFile bf)

getStorageTypeId

public abstract int getStorageTypeId()

run

public void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

saveSlice

public int saveSlice(Hash root,
                     int blockNumber,
                     int sliceOffset,
                     java.nio.ByteBuffer slice,
                     FileDescriptor fd)
              throws java.io.IOException
Parameters:
sliceOffset - The offset into this block at wich the slice should be saved
Returns:
Number of bytes written
Throws:
java.io.IOException

waitForUnfinishedTasks

public void waitForUnfinishedTasks()

containsBlock

public boolean containsBlock(Hash rootHash,
                             int blockNumber)
                      throws java.io.IOException
Throws:
java.io.IOException

getBlockFile

public BlockFile getBlockFile(Hash root)
                       throws java.io.IOException
Throws:
java.io.IOException

contains

public boolean contains(Hash root)

rootHashes

public java.util.Set<Hash> rootHashes()

getBlockMaskFor

public BlockMask getBlockMaskFor(Hash root)
                          throws java.io.IOException
Throws:
java.io.IOException

getFD

public FileDescriptor getFD(Hash root)
                     throws java.io.IOException
Throws:
java.io.IOException

shutdown

public void shutdown()
              throws java.io.IOException
Throws:
java.io.IOException

isRecentlyDownloaded

public boolean isRecentlyDownloaded(Hash rootHash)

removePermanently

public void removePermanently(Hash root)
                       throws java.io.IOException
Throws:
java.io.IOException

getCore

public CoreSubsystem getCore()

getStoragePath

public java.io.File getStoragePath()

getCompleteFilePath

public java.io.File getCompleteFilePath()

isSequential

public boolean isSequential()

getById

public static BlockStorage getById(CoreSubsystem core,
                                   int id)