org.tranche.network
Class StatusTableRow

java.lang.Object
  extended by org.tranche.commons.Debuggable
      extended by org.tranche.network.StatusTableRow
All Implemented Interfaces:
java.io.Serializable

public class StatusTableRow
extends org.tranche.commons.Debuggable
implements java.io.Serializable

A row in the status table. Each row represents a server of the network.

Author:
James "Augie" Hill - augman85@gmail.com, Bryan Smith - bryanesmith@gmail.com
See Also:
Serialized Form

Field Summary
static int CAN_READ_BIT
          The location of the "Can Read" bit in the set of flags.
static int CAN_WRITE_BIT
          The location of the "Can Write" bit in the set of flags.
static int DATA_STORE_BIT
          The location of the "Data Store" bit in the set of flags.
static long DEFAULT_FLAGS
          The default value for the flags.
static java.util.Collection<HashSpan> DEFAULT_HASH_SPANS
          The default set of hash spans.
static java.util.Collection<HashSpan> DEFAULT_TARGET_HASH_SPANS
          The default set of target hash spans.
static int LENGTH_MAX_GROUP
          The maximum length of the group name.
static int LENGTH_MAX_NAME
          The maximum length of the name.
static int ONLINE_BIT
          The location of the Online bit in the set of flags.
static int SSL_BIT
          The location of the SSL bit in the set of flags.
static int VERSION_LATEST
          The value for the latest version of the object.
static int VERSION_ONE
          The value for the first version of the object.
static int VERSION_TWO
          
 
Constructor Summary
StatusTableRow(java.io.InputStream in)
          Deserializes the row based on the given stream.
StatusTableRow(Server server)
          Constructs a row based on a local server.
StatusTableRow(java.lang.String host)
          Starts the row with a host name.
StatusTableRow(java.lang.String host, int port, boolean isSSL)
          Starts the row with a host name.
StatusTableRow(java.lang.String host, int port, boolean isSSL, boolean isOnline)
          Starts the row with a host name.
 
Method Summary
static boolean areEqual(java.util.Collection<StatusTableRow> c1, java.util.Collection<StatusTableRow> c2)
          Returns whether the two collections of rows are equivalent.
protected static java.util.Collection<StatusTableRow> calculateFullHashSpan(java.util.Collection<StatusTableRow> allRows)
          Given a collection of StatusTableRows, randomizes the rows and returns the first found full hash span.
protected static java.util.Collection<StatusTableRow> calculateFullHashSpan(java.util.Collection<StatusTableRow> seedRows, java.util.Collection<StatusTableRow> allRows)
          Given two collections of StatusTableRows, randomizes the rows and returns the first found full hash span with read and write priveleges and core servers containing as many of the seedRows as possible.
 StatusTableRow clone()
          Creates a duplication of this row.
protected  void deserialize(java.io.InputStream in)
          Sets the values of the row based on the given serialized row.
protected  void deserializeVersionOne(java.io.InputStream in)
          Sets the values of the row based the given version one serialized row.
protected  void deserializeVersionTwo(java.io.InputStream in)
           
 boolean equals(java.lang.Object o)
          Evaluates all the attributes except the update timestamp of the given object against this one.
 long getFlags()
          Returns the flags object.
 java.lang.String getGroup()
          Gets the group name.
 java.util.Collection<HashSpan> getHashSpans()
          Gets the hash spans.
 java.lang.String getHost()
          Gets the host name.
 java.lang.String getName()
          Gets the name.
 int getPort()
          Gets the port number.
 long getResponseTimestamp()
           
 java.util.Collection<HashSpan> getTargetHashSpans()
          Gets the target hash spans.
 long getUpdateTimestamp()
          Gets the timestamp when the row's information was last updated.
 java.lang.String getURL()
          Gets the URL of the row.
 int getVersion()
          Gets the version.
 int hashCode()
          Hashes the host name only.
 boolean isCore()
          Returns whether this row is a core server.
 boolean isDataStore()
          Returns whether the server stores data.
 boolean isLocalServer()
          Returns whether this row is for the local server.
 boolean isOnline()
          Returns whether the server is online.
 boolean isReadable()
          Returns whether the server allows its data to be read.
 boolean isSSL()
          Returns whether the server communicates over SSL.
 boolean isWritable()
          Gets whether the server can be written to.
 void responseReceived()
           
 void serialize(int version, java.io.OutputStream out)
          Outputs the values of this row in the structure defined by the given version to the given output stream.
protected  boolean setGroup(java.lang.String group)
          Sets the group name.
protected  boolean setHashSpans(java.util.Collection<HashSpan> hashSpans)
          Sets the hash spans.
protected  boolean setHost(java.lang.String host)
          Sets the host name.
protected  boolean setIsDataStore(boolean dataStore)
          Sets whether the server stores data.
protected  boolean setIsOnline(boolean online)
          Sets whether the server is online.
protected  boolean setIsReadable(boolean readable)
          Sets whether the server allows its data to be read.
protected  boolean setIsSSL(boolean ssl)
          Sets whether the server communicates over SSL.
protected  boolean setIsWritable(boolean writable)
          Sets whether the server can be written to.
protected  boolean setName(java.lang.String name)
          Sets the name.
protected  boolean setPort(int port)
          Sets the port number.
protected  boolean setResponseTimestamp(long responseTimestamp)
           
protected  boolean setTargetHashSpans(java.util.Collection<HashSpan> hashSpans)
          Sets the target hash spans.
protected  boolean setUpdateTimestamp(long updateTimestamp)
          Sets the timestamp when the row's information was last updated.
protected  boolean setVersion(int version)
          Sets the version.
 byte[] toByteArray(int version)
           
 java.lang.String toString()
          Prints out the values of the attributes in the row.
 void update(Configuration config)
          Updates the information in the row based on the given configuration.
protected  void update(Server server)
          Updates the information in the row based on the given local server.
protected  boolean update(StatusTableRow row)
           
 
Methods inherited from class org.tranche.commons.Debuggable
debugErr, debugErr, debugOut, isDebug, setDebug
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION_ONE

public static final int VERSION_ONE

The value for the first version of the object.

See Also:
Constant Field Values

VERSION_TWO

public static final int VERSION_TWO

See Also:
Constant Field Values

VERSION_LATEST

public static final int VERSION_LATEST

The value for the latest version of the object.

See Also:
Constant Field Values

SSL_BIT

public static final int SSL_BIT

The location of the SSL bit in the set of flags.


ONLINE_BIT

public static final int ONLINE_BIT

The location of the Online bit in the set of flags.


CAN_READ_BIT

public static final int CAN_READ_BIT

The location of the "Can Read" bit in the set of flags.


CAN_WRITE_BIT

public static final int CAN_WRITE_BIT

The location of the "Can Write" bit in the set of flags.


DATA_STORE_BIT

public static final int DATA_STORE_BIT

The location of the "Data Store" bit in the set of flags.


DEFAULT_FLAGS

public static final long DEFAULT_FLAGS

The default value for the flags.


DEFAULT_HASH_SPANS

public static final java.util.Collection<HashSpan> DEFAULT_HASH_SPANS

The default set of hash spans.


DEFAULT_TARGET_HASH_SPANS

public static final java.util.Collection<HashSpan> DEFAULT_TARGET_HASH_SPANS

The default set of target hash spans.


LENGTH_MAX_NAME

public static final int LENGTH_MAX_NAME

The maximum length of the name.

See Also:
Constant Field Values

LENGTH_MAX_GROUP

public static final int LENGTH_MAX_GROUP

The maximum length of the group name.

See Also:
Constant Field Values
Constructor Detail

StatusTableRow

public StatusTableRow(java.lang.String host)

Starts the row with a host name. All other values are the defaults.

Parameters:
host - Host name.

StatusTableRow

public StatusTableRow(java.io.InputStream in)
               throws java.io.IOException

Deserializes the row based on the given stream.

Parameters:
in - The stream containing a serialized row
Throws:
java.io.IOException

StatusTableRow

public StatusTableRow(Server server)

Constructs a row based on a local server.

Parameters:
server - A server

StatusTableRow

public StatusTableRow(java.lang.String host,
                      int port,
                      boolean isSSL)

Starts the row with a host name.

Parameters:
host -
port -
isSSL -

StatusTableRow

public StatusTableRow(java.lang.String host,
                      int port,
                      boolean isSSL,
                      boolean isOnline)

Starts the row with a host name.

Parameters:
host -
port -
isSSL -
isOnline -
Method Detail

update

public void update(Configuration config)

Updates the information in the row based on the given configuration.

This is updated by the network status table and related utilities, as well as by tests.

Parameters:
config - A configuration

update

protected void update(Server server)

Updates the information in the row based on the given local server.

Parameters:
server - A server

update

protected boolean update(StatusTableRow row)
Parameters:
row -
Returns:
Whether any information was changed.

isCore

public boolean isCore()

Returns whether this row is a core server. A core server is defined a server that exists in the startup list of servers.

Returns:
Whether this row is a core server.

isLocalServer

public boolean isLocalServer()

Returns whether this row is for the local server.

Returns:
Whether this row is for the local server

getURL

public java.lang.String getURL()

Gets the URL of the row. Uses IOUtil.createURL(String, int, boolean).

Returns:
The URL

getVersion

public int getVersion()

Gets the version.

Returns:
The version

setVersion

protected boolean setVersion(int version)

Sets the version.

Parameters:
version - The new version
Returns:

getUpdateTimestamp

public long getUpdateTimestamp()

Gets the timestamp when the row's information was last updated.

Returns:
The timestamp when the row's information was last updated.

setUpdateTimestamp

protected boolean setUpdateTimestamp(long updateTimestamp)

Sets the timestamp when the row's information was last updated.

Parameters:
updateTimestamp - The timestamp when the row's information was last updated.
Returns:

getResponseTimestamp

public long getResponseTimestamp()
Returns:

responseReceived

public void responseReceived()

setResponseTimestamp

protected boolean setResponseTimestamp(long responseTimestamp)
Parameters:
responseTimestamp -
Returns:

getFlags

public long getFlags()

Returns the flags object.

Returns:
The flags

getHost

public java.lang.String getHost()

Gets the host name.

Returns:
The host name

setHost

protected boolean setHost(java.lang.String host)

Sets the host name.

Parameters:
host - The new host name
Returns:

getPort

public int getPort()

Gets the port number.

Returns:
The port number

setPort

protected boolean setPort(int port)

Sets the port number.

Parameters:
port - The new port number
Returns:

getName

public java.lang.String getName()

Gets the name.

Returns:
The name

setName

protected boolean setName(java.lang.String name)

Sets the name.

Parameters:
name - The new name
Returns:

getGroup

public java.lang.String getGroup()

Gets the group name.

Returns:
The group name

setGroup

protected boolean setGroup(java.lang.String group)

Sets the group name.

Parameters:
group - The new group name
Returns:

isSSL

public boolean isSSL()

Returns whether the server communicates over SSL.

Returns:
Whether the server communicates over SSL

setIsSSL

protected boolean setIsSSL(boolean ssl)

Sets whether the server communicates over SSL.

Parameters:
ssl - Whether the server communicates over SSL

isOnline

public boolean isOnline()

Returns whether the server is online.

Returns:
Whether the server is online.

setIsOnline

protected boolean setIsOnline(boolean online)

Sets whether the server is online.

Parameters:
online - Whether the server is online.
Returns:

isReadable

public boolean isReadable()

Returns whether the server allows its data to be read.

Returns:
Whether the server allows its data to be read.

setIsReadable

protected boolean setIsReadable(boolean readable)

Sets whether the server allows its data to be read.

Parameters:
readable - Whether the server allows its data to be read.
Returns:

isWritable

public boolean isWritable()

Gets whether the server can be written to.

Returns:
Whether the server can be written to.

setIsWritable

protected boolean setIsWritable(boolean writable)

Sets whether the server can be written to.

Parameters:
writable - Whether the server can be written to.

isDataStore

public boolean isDataStore()

Returns whether the server stores data.

Returns:
Whether the server stores data.

setIsDataStore

protected boolean setIsDataStore(boolean dataStore)

Sets whether the server stores data.

Parameters:
dataStore - Whether the server stores data.
Returns:

getHashSpans

public java.util.Collection<HashSpan> getHashSpans()

Gets the hash spans.

Returns:
The hash spans

setHashSpans

protected boolean setHashSpans(java.util.Collection<HashSpan> hashSpans)

Sets the hash spans.

Parameters:
hashSpans - The hash spans
Returns:

getTargetHashSpans

public java.util.Collection<HashSpan> getTargetHashSpans()

Gets the target hash spans.

Returns:
The hash spans

setTargetHashSpans

protected boolean setTargetHashSpans(java.util.Collection<HashSpan> hashSpans)

Sets the target hash spans.

Parameters:
hashSpans - The hash spans
Returns:

toByteArray

public byte[] toByteArray(int version)
                   throws java.io.IOException
Returns:
Throws:
java.io.IOException

serialize

public void serialize(int version,
                      java.io.OutputStream out)
               throws java.io.IOException

Outputs the values of this row in the structure defined by the given version to the given output stream.

Parameters:
out - The output stream
Throws:
java.io.IOException

deserialize

protected void deserialize(java.io.InputStream in)
                    throws java.io.IOException

Sets the values of the row based on the given serialized row.

Parameters:
in - An input stream containing a serialized row
Throws:
java.io.IOException

deserializeVersionOne

protected void deserializeVersionOne(java.io.InputStream in)
                              throws java.io.IOException

Sets the values of the row based the given version one serialized row.

Parameters:
in - An input stream containing a version one serialized row
Throws:
java.io.IOException

deserializeVersionTwo

protected void deserializeVersionTwo(java.io.InputStream in)
                              throws java.io.IOException
Parameters:
in -
Throws:
java.io.IOException

equals

public boolean equals(java.lang.Object o)

Evaluates all the attributes except the update timestamp of the given object against this one.

Overrides:
equals in class java.lang.Object
Parameters:
o - A StatusTableRow object
Returns:
Whether all the attributes except the update timestamp of the given object are equal to this one.

clone

public StatusTableRow clone()

Creates a duplication of this row.

Overrides:
clone in class java.lang.Object
Returns:
The duplicate row

hashCode

public int hashCode()

Hashes the host name only.

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code

toString

public java.lang.String toString()

Prints out the values of the attributes in the row.

Overrides:
toString in class java.lang.Object
Returns:
The printed value of the row

areEqual

public static boolean areEqual(java.util.Collection<StatusTableRow> c1,
                               java.util.Collection<StatusTableRow> c2)

Returns whether the two collections of rows are equivalent.

Parameters:
c1 - A collection of rows
c2 - A collection of rows
Returns:
Whether the two collections of rows are equivalent

calculateFullHashSpan

protected static java.util.Collection<StatusTableRow> calculateFullHashSpan(java.util.Collection<StatusTableRow> allRows)
                                                                     throws java.lang.Exception

Given a collection of StatusTableRows, randomizes the rows and returns the first found full hash span.

It is important that the rows are randomized before finding the full hash span because the status table sorts all the rows alphabetically, and we don't want all clients connecting to the same servers.

Parameters:
allRows - The rows to use in trying to put together a full hash span.
Returns:
A collection of status table rows that together make up a full hash span.
Throws:
java.lang.Exception

calculateFullHashSpan

protected static java.util.Collection<StatusTableRow> calculateFullHashSpan(java.util.Collection<StatusTableRow> seedRows,
                                                                            java.util.Collection<StatusTableRow> allRows)
                                                                     throws java.lang.Exception

Given two collections of StatusTableRows, randomizes the rows and returns the first found full hash span with read and write priveleges and core servers containing as many of the seedRows as possible.

It is important that the rows are randomized before finding the full hash span because the status table sorts all the rows alphabetically, and we don't want all clients connecting to the same servers.

Parameters:
seedRows - The function should try to keep as many of these in the full hash span as possible.
allRows - All the rows to use in trying to put together a full hash span, possibly including the seed rows.
Returns:
A collection of status table rows that together make up a full hash span.
Throws:
java.lang.Exception - TODO: allow for partial priveleges -- read on one server with a hash span plus write on another server with the same hash span


This code is free for use both commercially and non-commercially as stated in the project's license