Skip to content

Commit 035c396

Browse files
committed
More work towards NIO client
1 parent fb4b527 commit 035c396

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

src/com/danga/MemCached/MemCachedClient.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ public class MemCachedClient {
165165
private static Logger log =
166166
Logger.getLogger( MemCachedClient.class.getName() );
167167

168-
// default charset
169-
private static final Charset charSet =
170-
Charset.defaultCharset();
171-
172168
// return codes
173169
private static final String VALUE = "VALUE"; // start of value line from server
174170
private static final String STATS = "STAT"; // start of stats line from server
@@ -1457,14 +1453,9 @@ private void loadItemsNIO( Map<String,StringBuilder> cmdMap, Map<String,Object>
14571453
Map<String,SockIOPool.SockIO> sockMap =
14581454
new HashMap<String,SockIOPool.SockIO>( cmdMap.keySet().size() );
14591455

1460-
// Convenience to move our Strings to CharBuffers
1461-
Map<String,CharBuffer> writeMap =
1462-
new HashMap<String,CharBuffer>( cmdMap.keySet().size() );
1463-
1464-
// map to store server response
1465-
// keyed off of the cache key
1466-
Map<String,byte[]> response =
1467-
new HashMap<String,byte[]>( keys.length );
1456+
// map of data we need to write per host
1457+
Map<String,ByteBuffer> writeMap =
1458+
new HashMap<String,ByteBuffer>( cmdMap.keySet().size() );
14681459

14691460
// iterate through and flip the sockets to nonblocking
14701461
// get a selector and register the socket
@@ -1484,8 +1475,8 @@ private void loadItemsNIO( Map<String,StringBuilder> cmdMap, Map<String,Object>
14841475
sockMap.put( host, sock );
14851476

14861477
// store the string in a charbuffer and remove this entry
1487-
CharBuffer cb = CharBuffer.wrap( cmdMap.get( host ).append( "\r\n" ) );
1488-
writeMap.put( host, cb );
1478+
ByteBuffer bb = ByteBuffer.wrap( cmdMap.get( host ).append( "\r\n" ).toString().getBytes() );
1479+
writeMap.put( host, bb );
14891480
i.remove();
14901481
}
14911482
catch ( IOException e ) {
@@ -1516,12 +1507,14 @@ private void loadItemsNIO( Map<String,StringBuilder> cmdMap, Map<String,Object>
15161507
// will be reused in loop
15171508
SocketChannel socket = null;
15181509

1519-
// grab a buffer to work with
1510+
// grab a buffer to work with for reading results
15201511
ByteBuffer buf =
15211512
ByteBuffer.allocateDirect( 8192 );
15221513

1523-
// get an encoder
1524-
CharsetEncoder encoder = charSet.newEncoder();
1514+
// map to store server response
1515+
// keyed off of host
1516+
Map<String,byte[]> response =
1517+
new HashMap<String,byte[]>( keys.length );
15251518

15261519
// now lets start looping
15271520
while ( true ) {
@@ -1585,30 +1578,19 @@ private void loadItemsNIO( Map<String,StringBuilder> cmdMap, Map<String,Object>
15851578
buf.clear();
15861579

15871580
if ( writeMap.containsKey( host ) ) {
1588-
CharBuffer in = writeMap.get( host );
1581+
ByteBuffer in = writeMap.get( host );
15891582
log.error( "remaining bytes to send before encoding: " + in.remaining() );
15901583

1591-
// write what we need to write
1592-
encoder.reset();
1593-
CoderResult cr = encoder.encode( in, buf, false );
1594-
encoder.flush( buf );
1595-
1596-
log.error( "remaining bytes to send after encoding: " + in.remaining() );
1597-
1598-
// check to see if we are done?
1599-
if ( cr.isUnderflow() ) {
1600-
encoder.encode( in, buf, true );
1601-
encoder.flush( buf );
1602-
1603-
// and clear out the map
1604-
writeMap.remove( host );
1605-
}
16061584

16071585
// now send
16081586
try {
1609-
while ( buf.hasRemaining() ) {
1610-
int rc = socket.write( buf );
1611-
log.error( String.format( "wrote %d bytes to the server for host %s", rc, host ) );
1587+
int rc = socket.write( buf );
1588+
log.error( String.format( "wrote %d bytes to the server for host %s", rc, host ) );
1589+
1590+
if ( !buf.hasRemaining() ) {
1591+
// we read it all so we can stop worrying about writing to this host
1592+
writeMap.remove( host );
1593+
sk.interestOps( SelectionKey.OP_READ );
16121594
}
16131595
}
16141596
catch ( IOException e ) {
@@ -1690,7 +1672,19 @@ private void loadItemsNIO( Map<String,StringBuilder> cmdMap, Map<String,Object>
16901672
}
16911673
else {
16921674
// we got some data
1675+
byte[] tmp = new byte[ sz ];
1676+
buf.get( tmp );
1677+
1678+
if ( response.containsKey( host ) ) {
1679+
byte[] oldData = response.get( host );
1680+
byte[] newData = new byte[ oldData.length + sz ];
16931681

1682+
System.arraycopy( oldData, 0, newData, 0, oldData.length );
1683+
System.arraycopy( tmp, 0, newData, oldData.length, sz );
1684+
}
1685+
else {
1686+
response.put( host, tmp );
1687+
}
16941688
}
16951689

16961690
continue;

0 commit comments

Comments
 (0)