edu.harvard.deas.hyperenc
Class DBHyperStorage

java.lang.Object
  extended by edu.harvard.deas.hyperenc.HyperStorage
      extended by edu.harvard.deas.hyperenc.DBHyperStorage

public class DBHyperStorage
extends HyperStorage

Stores unreconciled pages and corresponding hash values, reconciled pages, system blocks, and encryption blocks for a single contact. This storage is backed by a PersistentMap, so insertions and removals of blocks/pages in this storage are synchronously written to disk, and can persist across instances of the application.

Thread safety: This implementation is unconditionally thread-safe.


Constructor Summary
DBHyperStorage(Contact contact, Direction direction, RandomSource rs, MessageDigest md, PageShuffler ps)
          Equivalent to calling DBHyperStorage(contact, direction, rs, md, ps, null).
DBHyperStorage(Contact contact, Direction direction, RandomSource rs, MessageDigest md, PageShuffler ps, File envPath)
          Constructs a new DBHyperStorage.
 
Method Summary
 int addEncBlock(byte[] block)
          Adds an encryption block.
 void addRPage(byte[] page)
          Store a page that has been reconciled.
 int addSysBlock(byte[][] blocks)
          Adds a system block.
 void addUPage(int id, byte[] page, byte[] hash)
          Adds a page of randomness that has yet to be reconciled, along with its hash.
 Contact getContact()
          Get the communication partner for this HyperStorage
 MessageDigest getDigest()
          Gets the stored MessageDigest object for this HyperStorage.
 Direction getDirection()
          The direction of communication for this HyperStorage.
 byte[] getEncBlock(int id)
          Retrieve the encryption block with the given ID.
 List<Integer> getEncBlockList()
          Gets a List of IDs of available encryption blocks.
 PageShuffler getShuffler()
           
 RandomSource getSource()
          Gets the stored RandomSource object for this HyperStorage.
 byte[][] getSysBlock(int id)
          Retrieve the system block with the given ID.
 List<Integer> getSysBlockList()
          Gets a List of IDs of available system blocks.
 byte[] getUHash(int id)
          Retrieve hash of the unreconciled page with the given ID.
 byte[] getUPage(int id)
          Retrieve the unreconciled page with the given ID.
 List<Integer> getUPageList()
          Gets a List of IDs of available unreconciled pages.
 int numEncBlocks()
           
 int numRPages()
           
 int numSysBlocks()
           
 int numUPages()
           
 byte[] remEncBlock(int id)
          Removes and returns encryption block with the given ID.
 List<byte[]> remEncBlockList(List<Integer> idlist)
          Removes and returns all the encryption blocks with the given IDs.
 byte[] remRPage()
          Removes and returns the first reconciled page from the FIFO queue.
 byte[][] remSysBlock(int id)
          Removes and returns system block with the given ID.
 byte[] remUPage(int id)
          Removes and returns unreconciled page with the given ID, and remove the corresponding hash.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DBHyperStorage

public DBHyperStorage(Contact contact,
                      Direction direction,
                      RandomSource rs,
                      MessageDigest md,
                      PageShuffler ps,
                      File envPath)
Constructs a new DBHyperStorage. If the given database environment already contains blocks/pages for contact, they are loaded, restoring the state of this object to the state of the last DBHyperStorage that used the database. Otherwise, the new DBHyperStorage is empty.

Parameters:
contact - The partner for whom this storage stores things
direction - Indicates if this storage is the master of the communication path
rs - The random source used to create pages for this path of communication
md - The hash function used as a component of computing the hash of a page.
ps - The page shuffler used to shuffle pages fetched for this storage.
envPath - The path on disk of the database environment underlying the database backing this map. This File is passed directly to the constructor for PersistentMap.

DBHyperStorage

public DBHyperStorage(Contact contact,
                      Direction direction,
                      RandomSource rs,
                      MessageDigest md,
                      PageShuffler ps)
Equivalent to calling DBHyperStorage(contact, direction, rs, md, ps, null).

Method Detail

addSysBlock

public int addSysBlock(byte[][] blocks)
Description copied from class: HyperStorage
Adds a system block. Each system block is comprised of HyperCollector.NUM_SYSBLOCKS subblocks.

Specified by:
addSysBlock in class HyperStorage
Parameters:
blocks - Data for block.
Returns:
ID number for block.

addEncBlock

public int addEncBlock(byte[] block)
Description copied from class: HyperStorage
Adds an encryption block.

Specified by:
addEncBlock in class HyperStorage
Parameters:
block - Data for block.
Returns:
ID number for block.

addUPage

public void addUPage(int id,
                     byte[] page,
                     byte[] hash)
Description copied from class: HyperStorage
Adds a page of randomness that has yet to be reconciled, along with its hash.

Specified by:
addUPage in class HyperStorage
Parameters:
id - ID number for the Page. This is the same as the ID for the block from which it was generated.
page - The unreconciled page.
hash - The hash of page.

addRPage

public void addRPage(byte[] page)
Description copied from class: HyperStorage
Store a page that has been reconciled. The page is placed at the end of a FIFO queue, so there is no ID (unlike unreconciled pages).

Specified by:
addRPage in class HyperStorage
Parameters:
page - The reconciled page.

getSysBlockList

public List<Integer> getSysBlockList()
Description copied from class: HyperStorage
Gets a List of IDs of available system blocks. The list is a copy; callers are free to mutate the returned list.

Specified by:
getSysBlockList in class HyperStorage
Returns:
List of IDs of system blocks.

getEncBlockList

public List<Integer> getEncBlockList()
Description copied from class: HyperStorage
Gets a List of IDs of available encryption blocks. The list is a copy; callers are free to mutate the returned list.

Specified by:
getEncBlockList in class HyperStorage
Returns:
List of IDs of encryption blocks.

getUPageList

public List<Integer> getUPageList()
Description copied from class: HyperStorage
Gets a List of IDs of available unreconciled pages. The list is a copy; callers are free to mutate the returned list. The list can also be used as a List of IDs for all unreconciled page hashes, because every page has a hash stored with the same ID.

Specified by:
getUPageList in class HyperStorage
Returns:
List of IDs of unreconciled pages.

getSysBlock

public byte[][] getSysBlock(int id)
Description copied from class: HyperStorage
Retrieve the system block with the given ID.

Specified by:
getSysBlock in class HyperStorage
Parameters:
id - ID number of desired system block.
Returns:
The system block with the given ID (or null if no such block).

getEncBlock

public byte[] getEncBlock(int id)
Description copied from class: HyperStorage
Retrieve the encryption block with the given ID.

Specified by:
getEncBlock in class HyperStorage
Parameters:
id - ID number of desired encryption block.
Returns:
The encryption block with the given ID (or null if no such block).

getUPage

public byte[] getUPage(int id)
Description copied from class: HyperStorage
Retrieve the unreconciled page with the given ID.

Specified by:
getUPage in class HyperStorage
Parameters:
id - ID of desired page.
Returns:
The unreconciled page with the given ID (or null if no such page).

getUHash

public byte[] getUHash(int id)
Description copied from class: HyperStorage
Retrieve hash of the unreconciled page with the given ID.

Specified by:
getUHash in class HyperStorage
Parameters:
id - ID of desired page.
Returns:
The hash of the page with the given ID (or null if no such page).

remSysBlock

public byte[][] remSysBlock(int id)
Description copied from class: HyperStorage
Removes and returns system block with the given ID.

Specified by:
remSysBlock in class HyperStorage
Parameters:
id - ID of block.
Returns:
system block with the given ID

remEncBlock

public byte[] remEncBlock(int id)
Description copied from class: HyperStorage
Removes and returns encryption block with the given ID.

Specified by:
remEncBlock in class HyperStorage
Parameters:
id - ID of block.
Returns:
encryption block with the given ID

remEncBlockList

public List<byte[]> remEncBlockList(List<Integer> idlist)
                             throws BlockMissingException
Description copied from class: HyperStorage
Removes and returns all the encryption blocks with the given IDs. This is an atomic, all-or-nothing operation: if all of the specified block IDs exist in this storage, then they are all removed and returned. If any of the specified IDs is missing, no blocks are removed, and a BlockMissingException is thrown.

Specified by:
remEncBlockList in class HyperStorage
Parameters:
idlist - a list of integer encryption block IDs
Returns:
a list containing the encryption blocks with the corresponding IDs, in the same order in which the IDs were given
Throws:
BlockMissingException - if any of the specified IDs are missing. The IDs of the missing blocks can be accessed from the getMissingIDList method of BlockMissingException.

remUPage

public byte[] remUPage(int id)
Description copied from class: HyperStorage
Removes and returns unreconciled page with the given ID, and remove the corresponding hash.

Specified by:
remUPage in class HyperStorage
Parameters:
id - ID of page.
Returns:
unreconciled page with the given ID

remRPage

public byte[] remRPage()
Description copied from class: HyperStorage
Removes and returns the first reconciled page from the FIFO queue.

Specified by:
remRPage in class HyperStorage
Returns:
a reconciled page (or null if there are none)

numSysBlocks

public int numSysBlocks()
Specified by:
numSysBlocks in class HyperStorage
Returns:
The number of system blocks contained in this HyperStorage.

numEncBlocks

public int numEncBlocks()
Specified by:
numEncBlocks in class HyperStorage
Returns:
The number of encryption blocks contained in this HyperStorage.

numUPages

public int numUPages()
Specified by:
numUPages in class HyperStorage
Returns:
The number of unreconciled pages contained in this HyperStorage.

numRPages

public int numRPages()
Specified by:
numRPages in class HyperStorage
Returns:
The number of reconciled pages contained in this HyperStorage.

getContact

public Contact getContact()
Description copied from class: HyperStorage
Get the communication partner for this HyperStorage

Specified by:
getContact in class HyperStorage
Returns:
the contact with whom this HyperStorage's blocks and pages are used to communicate

getSource

public RandomSource getSource()
Description copied from class: HyperStorage
Gets the stored RandomSource object for this HyperStorage. This RandomSource object is used to create pages.

Specified by:
getSource in class HyperStorage
Returns:
The RandomSource for this HyperStorage.

getDigest

public MessageDigest getDigest()
Description copied from class: HyperStorage
Gets the stored MessageDigest object for this HyperStorage. This MessageDigest object is a component of the hash function used to compute the hash of a page.

Specified by:
getDigest in class HyperStorage
Returns:
The MessageDigest for this HyperStorage.

getShuffler

public PageShuffler getShuffler()
Specified by:
getShuffler in class HyperStorage
Returns:
The PageShuffler for this HyperStorage.

getDirection

public Direction getDirection()
Description copied from class: HyperStorage
The direction of communication for this HyperStorage. If MASTER, this storage's blocks and pages are used for outgoing communication: sending messages to the partner (as given by getContact()). If SLAVE, this storage is used for incoming communication: receiving messages from the partner.

Specified by:
getDirection in class HyperStorage
Returns:
the direction of communication for this storage