org.jgroups.blocks

Class ReplicatedTree

Implemented Interfaces:
Runnable, MembershipListener, MessageListener

public class ReplicatedTree
extends java.lang.Object
implements Runnable, MessageListener, MembershipListener

A tree-like structure that is replicated across several members. Updates will be multicast to all group members reliably and in the same order.
Authors:
Bela Ban Jan 17 2002
Alfonso Olias-Sanz

Nested Class Summary

static class
ReplicatedTree.Node
static interface
ReplicatedTree.ReplicatedTreeListener

Field Summary

static String
SEPARATOR
protected Log
log

Constructor Summary

ReplicatedTree()
ReplicatedTree(String groupname, String props, long state_fetch_timeout)
Creates a channel with the given properties.
ReplicatedTree(String groupname, String props, long state_fetch_timeout, boolean jmx)
ReplicatedTree(JChannel channel)
Expects an already connected channel.

Method Summary

void
_put(String fqn, HashMap data)
void
_put(String fqn, String key, Object value)
void
_remove(String fqn)
void
_remove(String fqn, String key)
void
_removeData(String fqn)
void
addReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)
void
block()
Block sending and receiving of messages until viewAccepted() is called
boolean
exists(String fqn)
Checks whether a given node exists in the tree
void
fetchState(long timeout)
Fetch the group state from the current coordinator.
Object
get(String fqn, String key)
Finds a node given its name and returns the value associated with a given key in its data map.
Channel
getChannel()
Returns the Channel the DistributedTree is connected to
Set
getChildrenNames(String fqn)
Returns all children of a given node
int
getGroupMembersNumber()
Returns the number of current members joined to the group
String
getGroupName()
Returns the name of the group that the DistributedTree is connected to
Set
getKeys(String fqn)
Gets the keys of the data map.
Address
getLocalAddress()
Vector
getMembers()
byte[]
getState()
Return a copy of the current cache (tree)
static void
main(String[] args)
String
print(String fqn)
Prints a representation of the node defined by fqn.
void
put(String fqn, HashMap data)
Adds a new node to the tree and sets its data.
void
put(String fqn, String key, Object value)
Adds a key and value to a given node.
void
receive(Message msg)
Callback.
void
remove(String fqn)
Removes the node from the tree.
void
remove(String fqn, String key)
Removes key from the node's hashmap
void
removeReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)
void
run()
Request handler thread
void
setRemoteCalls(boolean flag)
void
setRootNode(ReplicatedTree.Node n)
void
setState(byte[] new_state)
Set the cache (tree) to this value
void
start()
void
stop()
void
suspect(Address suspected_mbr)
Called when a member is suspected
String
toString()
void
viewAccepted(View new_view)
Called when a change in membership has occurred.

Field Details

SEPARATOR

public static final String SEPARATOR

log

protected final Log log

Constructor Details

ReplicatedTree

public ReplicatedTree()

ReplicatedTree

public ReplicatedTree(String groupname,
                      String props,
                      long state_fetch_timeout)
            throws Exception
Creates a channel with the given properties. Connects to the channel, then creates a PullPushAdapter and starts it

ReplicatedTree

public ReplicatedTree(String groupname,
                      String props,
                      long state_fetch_timeout,
                      boolean jmx)
            throws Exception

ReplicatedTree

public ReplicatedTree(JChannel channel)
            throws Exception
Expects an already connected channel. Creates a PullPushAdapter and starts it

Method Details

_put

public void _put(String fqn,
                 HashMap data)

_put

public void _put(String fqn,
                 String key,
                 Object value)

_remove

public void _remove(String fqn)

_remove

public void _remove(String fqn,
                    String key)

_removeData

public void _removeData(String fqn)

addReplicatedTreeListener

public void addReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)

block

public void block()
Block sending and receiving of messages until viewAccepted() is called
Specified by:
block in interface MembershipListener

exists

public boolean exists(String fqn)
Checks whether a given node exists in the tree
Parameters:
fqn - The fully qualified name of the node
Returns:
boolean Whether or not the node exists

fetchState

public void fetchState(long timeout)
            throws ChannelClosedException,
                   ChannelNotConnectedException
Fetch the group state from the current coordinator. If successful, this will trigger setState().

get

public Object get(String fqn,
                  String key)
Finds a node given its name and returns the value associated with a given key in its data map. Returns null if the node was not found in the tree or the key was not found in the hashmap.
Parameters:
fqn - The fully qualified name of the node.
key - The key.

getChannel

public Channel getChannel()
Returns the Channel the DistributedTree is connected to
Returns:
Channel

getChildrenNames

public Set getChildrenNames(String fqn)
Returns all children of a given node
Parameters:
fqn - The fully qualified name of the node
Returns:
Set A list of child names (as Strings)

getGroupMembersNumber

public int getGroupMembersNumber()
Returns the number of current members joined to the group
Returns:
int

getGroupName

public String getGroupName()
Returns the name of the group that the DistributedTree is connected to
Returns:
String

getKeys

public Set getKeys(String fqn)
Gets the keys of the data map. Returns all keys as Strings. Returns null if node does not exist.
Parameters:
fqn - The fully qualified name of the node
Returns:
Set A set of keys (as Strings)

getLocalAddress

public Address getLocalAddress()

getMembers

public Vector getMembers()

getState

public byte[] getState()
Return a copy of the current cache (tree)
Specified by:
getState in interface MessageListener

main

public static void main(String[] args)

print

public String print(String fqn)
Prints a representation of the node defined by fqn. Output includes name, fqn and data.

put

public void put(String fqn,
                HashMap data)
Adds a new node to the tree and sets its data. If the node doesn not yet exist, it will be created. Also, parent nodes will be created if not existent. If the node already has data, then the new data will override the old one. If the node already existed, a nodeModified() notification will be generated. Otherwise a nodeCreated() motification will be emitted.
Parameters:
fqn - The fully qualified name of the new node
data - The new data. May be null if no data should be set in the node.

put

public void put(String fqn,
                String key,
                Object value)
Adds a key and value to a given node. If the node doesn't exist, it will be created. If the node already existed, a nodeModified() notification will be generated. Otherwise a nodeCreated() motification will be emitted.
Parameters:
fqn - The fully qualified name of the node
key - The key
value - The value

receive

public void receive(Message msg)
Callback. Process the contents of the message; typically an _add() or _set() request
Specified by:
receive in interface MessageListener

remove

public void remove(String fqn)
Removes the node from the tree.
Parameters:
fqn - The fully qualified name of the node.

remove

public void remove(String fqn,
                   String key)
Removes key from the node's hashmap
Parameters:
fqn - The fullly qualified name of the node
key - The key to be removed

removeReplicatedTreeListener

public void removeReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)

run

public void run()
Request handler thread

setRemoteCalls

public void setRemoteCalls(boolean flag)

setRootNode

public void setRootNode(ReplicatedTree.Node n)

setState

public void setState(byte[] new_state)
Set the cache (tree) to this value
Specified by:
setState in interface MessageListener

start

public void start()
            throws Exception

stop

public void stop()

suspect

public void suspect(Address suspected_mbr)
Called when a member is suspected
Specified by:
suspect in interface MembershipListener

toString

public String toString()

viewAccepted

public void viewAccepted(View new_view)
Called when a change in membership has occurred. No long running actions should be done in this callback. If some long running action needs to be performed, it should be done in a separate thread.
Specified by:
viewAccepted in interface MembershipListener

Copyright B) 1998-2005 Bela Ban. All Rights Reserved.