net.spy.memcached.protocol
Class TCPMemcachedNodeImpl

java.lang.Object
  extended by net.spy.memcached.compat.SpyObject
      extended by net.spy.memcached.protocol.TCPMemcachedNodeImpl
All Implemented Interfaces:
MemcachedNode
Direct Known Subclasses:
AsciiMemcachedNodeImpl, BinaryMemcachedNodeImpl

public abstract class TCPMemcachedNodeImpl
extends SpyObject
implements MemcachedNode

Represents a node with the memcached cluster, along with buffering and operation queues.


Field Summary
protected  Operation optimizedOp
           
protected  BlockingQueue<Operation> writeQ
           
 
Constructor Summary
TCPMemcachedNodeImpl(SocketAddress sa, SocketChannel c, int bufSize, BlockingQueue<Operation> rq, BlockingQueue<Operation> wq, BlockingQueue<Operation> iq)
           
 
Method Summary
 void addOp(Operation op)
          Add an operation to the queue.
 void connected()
          Notify this node that it has reconnected.
 void copyInputQueue()
          Move all of the operations delivered via addOperation into the internal write queue.
 Collection<Operation> destroyInputQueue()
          Extract all queued items for this node destructively.
 void fillWriteBuffer(boolean shouldOptimize)
          Fill the write buffer with data from the next operations in the queue.
 void fixupOps()
          Fix up the selection ops on the selection key.
 int getBytesRemainingToWrite()
          Get the number of bytes remaining to write.
 SocketChannel getChannel()
          Get the SocketChannel for this connection.
 Operation getCurrentReadOp()
          Get the operation at the top of the queue that is requiring input.
 Operation getCurrentWriteOp()
          Get the operation at the top of the queue that has information available to write.
 ByteBuffer getRbuf()
          Get the buffer used for reading data from this node.
 int getReconnectCount()
          Get the current reconnect count.
 int getSelectionOps()
          Compute the appropriate selection operations for the channel this MemcachedNode holds to the server.
 SelectionKey getSk()
          Get the selection key from this node.
 SocketAddress getSocketAddress()
          Get the SocketAddress of the server to which this node is connected.
 ByteBuffer getWbuf()
          Get the buffer used for writing data to this node.
 boolean hasReadOp()
          True if an operation is available to read.
 boolean hasWriteOp()
          True if an operation is available to write.
 boolean isActive()
          True if this node is active. i.e.
protected abstract  void optimize()
           
 void reconnecting()
          Notify this node that it will be reconnecting.
 void registerChannel(SocketChannel ch, SelectionKey skey)
          Register a channel with this node.
 Operation removeCurrentReadOp()
          Remove the operation at the top of the queue that is requiring input.
 Operation removeCurrentWriteOp()
          Remove the operation at the top of the queue that has information available to write.
 void setChannel(SocketChannel to)
          Set the SocketChannel this node uses.
 void setSk(SelectionKey to)
          Set the selection key for this node.
 void setupResend()
          Clear the queue of currently processing operations by either cancelling them or setting them up to be reapplied after a reconnect.
 String toString()
           
 void transitionWriteItem()
          Transition the current write item into a read state.
 int writeSome()
          Write some bytes and return the number of bytes written.
 
Methods inherited from class net.spy.memcached.compat.SpyObject
getLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

writeQ

protected final BlockingQueue<Operation> writeQ

optimizedOp

protected Operation optimizedOp
Constructor Detail

TCPMemcachedNodeImpl

public TCPMemcachedNodeImpl(SocketAddress sa,
                            SocketChannel c,
                            int bufSize,
                            BlockingQueue<Operation> rq,
                            BlockingQueue<Operation> wq,
                            BlockingQueue<Operation> iq)
Method Detail

copyInputQueue

public final void copyInputQueue()
Description copied from interface: MemcachedNode
Move all of the operations delivered via addOperation into the internal write queue.

Specified by:
copyInputQueue in interface MemcachedNode

destroyInputQueue

public Collection<Operation> destroyInputQueue()
Description copied from interface: MemcachedNode
Extract all queued items for this node destructively. This is useful for redistributing items.

Specified by:
destroyInputQueue in interface MemcachedNode

setupResend

public final void setupResend()
Description copied from interface: MemcachedNode
Clear the queue of currently processing operations by either cancelling them or setting them up to be reapplied after a reconnect.

Specified by:
setupResend in interface MemcachedNode

fillWriteBuffer

public final void fillWriteBuffer(boolean shouldOptimize)
Description copied from interface: MemcachedNode
Fill the write buffer with data from the next operations in the queue.

Specified by:
fillWriteBuffer in interface MemcachedNode
Parameters:
shouldOptimize - if true, combine sequential gets into a single multi-key get

transitionWriteItem

public final void transitionWriteItem()
Description copied from interface: MemcachedNode
Transition the current write item into a read state.

Specified by:
transitionWriteItem in interface MemcachedNode

optimize

protected abstract void optimize()

getCurrentReadOp

public final Operation getCurrentReadOp()
Description copied from interface: MemcachedNode
Get the operation at the top of the queue that is requiring input.

Specified by:
getCurrentReadOp in interface MemcachedNode

removeCurrentReadOp

public final Operation removeCurrentReadOp()
Description copied from interface: MemcachedNode
Remove the operation at the top of the queue that is requiring input.

Specified by:
removeCurrentReadOp in interface MemcachedNode

getCurrentWriteOp

public final Operation getCurrentWriteOp()
Description copied from interface: MemcachedNode
Get the operation at the top of the queue that has information available to write.

Specified by:
getCurrentWriteOp in interface MemcachedNode

removeCurrentWriteOp

public final Operation removeCurrentWriteOp()
Description copied from interface: MemcachedNode
Remove the operation at the top of the queue that has information available to write.

Specified by:
removeCurrentWriteOp in interface MemcachedNode

hasReadOp

public final boolean hasReadOp()
Description copied from interface: MemcachedNode
True if an operation is available to read.

Specified by:
hasReadOp in interface MemcachedNode

hasWriteOp

public final boolean hasWriteOp()
Description copied from interface: MemcachedNode
True if an operation is available to write.

Specified by:
hasWriteOp in interface MemcachedNode

addOp

public final void addOp(Operation op)
Description copied from interface: MemcachedNode
Add an operation to the queue.

Specified by:
addOp in interface MemcachedNode

getSelectionOps

public final int getSelectionOps()
Description copied from interface: MemcachedNode
Compute the appropriate selection operations for the channel this MemcachedNode holds to the server.

Specified by:
getSelectionOps in interface MemcachedNode

getRbuf

public final ByteBuffer getRbuf()
Description copied from interface: MemcachedNode
Get the buffer used for reading data from this node.

Specified by:
getRbuf in interface MemcachedNode

getWbuf

public final ByteBuffer getWbuf()
Description copied from interface: MemcachedNode
Get the buffer used for writing data to this node.

Specified by:
getWbuf in interface MemcachedNode

getSocketAddress

public final SocketAddress getSocketAddress()
Description copied from interface: MemcachedNode
Get the SocketAddress of the server to which this node is connected.

Specified by:
getSocketAddress in interface MemcachedNode

isActive

public final boolean isActive()
Description copied from interface: MemcachedNode
True if this node is active. i.e. is is currently connected and expected to be able to process requests

Specified by:
isActive in interface MemcachedNode

reconnecting

public final void reconnecting()
Description copied from interface: MemcachedNode
Notify this node that it will be reconnecting.

Specified by:
reconnecting in interface MemcachedNode

connected

public final void connected()
Description copied from interface: MemcachedNode
Notify this node that it has reconnected.

Specified by:
connected in interface MemcachedNode

getReconnectCount

public final int getReconnectCount()
Description copied from interface: MemcachedNode
Get the current reconnect count.

Specified by:
getReconnectCount in interface MemcachedNode

toString

public final String toString()
Overrides:
toString in class Object

registerChannel

public final void registerChannel(SocketChannel ch,
                                  SelectionKey skey)
Description copied from interface: MemcachedNode
Register a channel with this node.

Specified by:
registerChannel in interface MemcachedNode

setChannel

public final void setChannel(SocketChannel to)
Description copied from interface: MemcachedNode
Set the SocketChannel this node uses.

Specified by:
setChannel in interface MemcachedNode

getChannel

public final SocketChannel getChannel()
Description copied from interface: MemcachedNode
Get the SocketChannel for this connection.

Specified by:
getChannel in interface MemcachedNode

setSk

public final void setSk(SelectionKey to)
Description copied from interface: MemcachedNode
Set the selection key for this node.

Specified by:
setSk in interface MemcachedNode

getSk

public final SelectionKey getSk()
Description copied from interface: MemcachedNode
Get the selection key from this node.

Specified by:
getSk in interface MemcachedNode

getBytesRemainingToWrite

public final int getBytesRemainingToWrite()
Description copied from interface: MemcachedNode
Get the number of bytes remaining to write.

Specified by:
getBytesRemainingToWrite in interface MemcachedNode

writeSome

public final int writeSome()
                    throws IOException
Description copied from interface: MemcachedNode
Write some bytes and return the number of bytes written.

Specified by:
writeSome in interface MemcachedNode
Returns:
the number of bytes written
Throws:
IOException - if there's a problem writing

fixupOps

public final void fixupOps()
Description copied from interface: MemcachedNode
Fix up the selection ops on the selection key.

Specified by:
fixupOps in interface MemcachedNode