org.metastatic.rsync
Class MatcherStream

java.lang.Object
  |
  +--org.metastatic.rsync.MatcherStream

public class MatcherStream
extends Object

A streaming version of Matcher. The idea here is that the hashtable search is the most expensive operation in the rsync algorithm, and it is at times undesirable to wait for this to finish while whoever on the other end of the wire is waiting for our data. With this construction, we can send Deltas as soon as they are generated.

To implement the outgoing stream of Deltas, this class makes use of a callback interface, MatcherListener. Pass a concrete implementation of this interface to the addListener(MatcherListener) method, and a List of ChecksumPairs before calling any of the update methods. Once the data have been passed to these methods, call doFinal() to finish the process.


Field Summary
protected  byte[] buffer
          The intermediate byte buffer.
protected  Configuration config
          The configuration.
protected  long count
          The number of bytes summed thusfar.
protected  TwoKeyMap hashtable
          The current hashtable.
protected  List listeners
          The list of MatcherListeners.
protected  int ndx
          The current index in buffer.
 
Constructor Summary
MatcherStream(Configuration config)
          Create a new MatcherStream.
 
Method Summary
 void addListener(MatcherListener listener)
          Add a MatcherListener to the list of listeners.
 void doFinal()
          Flush any buffered data and reset this instance.
protected  Long hashSearch(byte[] block, int off, int len)
          Search if a portion of the given byte array is in the map, returning its original offset if it is.
 boolean removeListener(MatcherListener listener)
          Remove a MatcherListener from the list of listeners.
 void reset()
          Reset this matcher, to be used for another data set.
 void setChecksums(List sums)
          Set the list of checksums that will be searched by this matcher.
 void update(byte b)
          Update this matcher with a single byte.
 void update(byte[] buf)
          Update this matcher with a byte array.
 void update(byte[] buf, int off, int len)
          Update this matcher with a portion of a byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

config

protected final Configuration config
The configuration.


listeners

protected final List listeners
The list of MatcherListeners.


hashtable

protected final TwoKeyMap hashtable
The current hashtable.


buffer

protected final byte[] buffer
The intermediate byte buffer.


ndx

protected int ndx
The current index in buffer.


count

protected long count
The number of bytes summed thusfar.

Constructor Detail

MatcherStream

public MatcherStream(Configuration config)
Create a new MatcherStream.

Parameters:
config - The current configuration.
Method Detail

addListener

public void addListener(MatcherListener listener)
Add a MatcherListener to the list of listeners.

Parameters:
listener - The listener to add.

removeListener

public boolean removeListener(MatcherListener listener)
Remove a MatcherListener from the list of listeners.

Parameters:
listener - The listener to add.
Returns:
True if a listener was really removed (i.e. that the listener was in the list to begin with).

setChecksums

public void setChecksums(List sums)
Set the list of checksums that will be searched by this matcher. This method must be called at least once before calling update.

Parameters:
sums - The checksums.

reset

public void reset()
Reset this matcher, to be used for another data set.


update

public void update(byte b)
            throws ListenerException
Update this matcher with a single byte.

Parameters:
b - The next byte
ListenerException

update

public void update(byte[] buf,
                   int off,
                   int len)
            throws ListenerException
Update this matcher with a portion of a byte array.

Parameters:
buf - The next bytes.
off - The offset to begin at.
len - The number of bytes to update.
ListenerException

update

public void update(byte[] buf)
            throws ListenerException
Update this matcher with a byte array.

Parameters:
buf - The next bytes.
ListenerException

doFinal

public void doFinal()
             throws ListenerException
Flush any buffered data and reset this instance.

ListenerException

hashSearch

protected Long hashSearch(byte[] block,
                          int off,
                          int len)
Search if a portion of the given byte array is in the map, returning its original offset if it is.

Parameters:
block - The block of bytes to search for.
off - The offset in the block to begin.
len - The number of bytes to read from the block.
Returns:
The original offset of the given block if it was found in the map. null if it was not found.