@@ -176,11 +176,15 @@ public class MemCachedClient {
176
176
private static final String NOTSTORED = "NOT_STORED" ; // data not stored
177
177
private static final String OK = "OK" ; // success
178
178
private static final String END = "END" ; // end of data from server
179
+
179
180
private static final String ERROR = "ERROR" ; // invalid command name from client
180
181
private static final String CLIENT_ERROR = "CLIENT_ERROR" ; // client error in input line - invalid protocol
181
182
private static final String SERVER_ERROR = "SERVER_ERROR" ; // server error
182
183
183
- private static final byte [] THE_END = "END\r \n " .getBytes ();
184
+ private static final byte [] B_END = "END\r \n " .getBytes ();
185
+ private static final byte [] B_NOTFOUND = "NOT_FOUND\r \n " .getBytes ();
186
+ private static final byte [] B_DELETED = "DELETED\r \r " .getBytes ();
187
+ private static final byte [] B_STORED = "STORED\r \r " .getBytes ();
184
188
185
189
// default compression threshold
186
190
private static final int COMPRESS_THRESH = 30720 ;
@@ -384,7 +388,7 @@ public void setCompressThreshold( long compressThreshold ) {
384
388
public boolean keyExists ( String key ) {
385
389
return ( this .get ( key , null , true ) != null );
386
390
}
387
-
391
+
388
392
/**
389
393
* Deletes an object from cache given cache key.
390
394
*
@@ -1266,15 +1270,15 @@ public Object get( String key, Integer hashCode, boolean asString ) {
1266
1270
new HashMap <String ,StringBuilder >();
1267
1271
1268
1272
cmdMap .put ( sock .getHost (),
1269
- new StringBuilder ( String .format ( "get %s\r \n " , key ) ) );
1273
+ new StringBuilder ( String .format ( "get %s" , key ) ) );
1270
1274
1271
1275
sock .close ();
1272
1276
1273
1277
// build empty map
1274
1278
// and fill it from server
1275
1279
Map <String ,Object > hm =
1276
1280
new HashMap <String ,Object >();
1277
- (new NIOLoader ()).loadItemsNIO ( asString , cmdMap , new String [] { key }, hm );
1281
+ (new NIOLoader ()).doMulti ( asString , cmdMap , new String [] { key }, hm );
1278
1282
1279
1283
// return the value for this key if we found it
1280
1284
// else return null
@@ -1440,7 +1444,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
1440
1444
new HashMap <String ,Object >( keys .length );
1441
1445
1442
1446
// now use new NIO implementation
1443
- (new NIOLoader ()).loadItemsNIO ( asString , cmdMap , keys , ret );
1447
+ (new NIOLoader ()).doMulti ( asString , cmdMap , keys , ret );
1444
1448
1445
1449
// fix the return array in case we had to rewrite any of the keys
1446
1450
for ( String key : keys ) {
@@ -1484,7 +1488,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
1484
1488
* @param asString if true, and if we are using NativehHandler, return string val
1485
1489
* @throws IOException if io exception happens while reading from socket
1486
1490
*/
1487
- private void loadItems ( LineInputStream input , Map <String ,Object > hm , boolean asString ) throws IOException {
1491
+ private void loadMulti ( LineInputStream input , Map <String ,Object > hm , boolean asString ) throws IOException {
1488
1492
1489
1493
while ( true ) {
1490
1494
String line = input .readLine ();
@@ -1961,16 +1965,22 @@ public boolean isDone() {
1961
1965
1962
1966
// else find out the hard way
1963
1967
int maxBuf = incoming .size ()-1 ;
1964
- int strPos = THE_END .length -1 ;
1968
+ int strPos = B_END .length -1 ;
1969
+
1970
+ // Need to check if last bytes are:
1971
+ // - END\r\n
1972
+ // - NOT_FOUND\r\n
1973
+ // - DELETED\r\n
1965
1974
1966
1975
int bi = maxBuf ;
1967
1976
while ( bi >= 0 && strPos >= 0 ) {
1968
1977
ByteBuffer buf = incoming .get ( bi );
1969
1978
int pos = buf .position ()-1 ;
1970
- while ( pos >= 0 && strPos >= 0 ) {
1971
- if ( buf .get ( pos -- ) != THE_END [strPos --] )
1979
+ while ( pos >= 0 && strPos >= 0 ) {
1980
+ if ( buf .get ( pos -- ) != B_END [strPos --] )
1972
1981
return false ;
1973
1982
}
1983
+
1974
1984
bi --;
1975
1985
}
1976
1986
@@ -1995,7 +2005,7 @@ public String toString() {
1995
2005
}
1996
2006
}
1997
2007
1998
- public void loadItemsNIO ( boolean asString , Map <String , StringBuilder > sockKeys , String [] keys , Map <String , Object > ret ) {
2008
+ public void doMulti ( boolean asString , Map <String , StringBuilder > sockKeys , String [] keys , Map <String , Object > ret ) {
1999
2009
2000
2010
long timeRemaining = 0 ;
2001
2011
try {
@@ -2076,7 +2086,7 @@ public void loadItemsNIO( boolean asString, Map<String, StringBuilder> sockKeys,
2076
2086
for ( Connection c : conns ) {
2077
2087
try {
2078
2088
if ( c .incoming .size () > 0 && c .isDone () )
2079
- loadItems ( new ByteBufArrayInputStream ( c .incoming ), ret , asString );
2089
+ loadMulti ( new ByteBufArrayInputStream ( c .incoming ), ret , asString );
2080
2090
}
2081
2091
catch ( Exception e ) {
2082
2092
// shouldn't happen; we have all the data already
0 commit comments