Skip to content

Commit 860c80d

Browse files
committed
additional tests and use same method for single vs multi gets
1 parent ce214c1 commit 860c80d

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

Diff for: src/com/danga/MemCached/MemCachedClient.java

+21-11
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,15 @@ public class MemCachedClient {
176176
private static final String NOTSTORED = "NOT_STORED"; // data not stored
177177
private static final String OK = "OK"; // success
178178
private static final String END = "END"; // end of data from server
179+
179180
private static final String ERROR = "ERROR"; // invalid command name from client
180181
private static final String CLIENT_ERROR = "CLIENT_ERROR"; // client error in input line - invalid protocol
181182
private static final String SERVER_ERROR = "SERVER_ERROR"; // server error
182183

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();
184188

185189
// default compression threshold
186190
private static final int COMPRESS_THRESH = 30720;
@@ -384,7 +388,7 @@ public void setCompressThreshold( long compressThreshold ) {
384388
public boolean keyExists( String key ) {
385389
return ( this.get( key, null, true ) != null );
386390
}
387-
391+
388392
/**
389393
* Deletes an object from cache given cache key.
390394
*
@@ -1266,15 +1270,15 @@ public Object get( String key, Integer hashCode, boolean asString ) {
12661270
new HashMap<String,StringBuilder>();
12671271

12681272
cmdMap.put( sock.getHost(),
1269-
new StringBuilder( String.format( "get %s\r\n", key ) ) );
1273+
new StringBuilder( String.format( "get %s", key ) ) );
12701274

12711275
sock.close();
12721276

12731277
// build empty map
12741278
// and fill it from server
12751279
Map<String,Object> hm =
12761280
new HashMap<String,Object>();
1277-
(new NIOLoader()).loadItemsNIO( asString, cmdMap, new String[] { key }, hm );
1281+
(new NIOLoader()).doMulti( asString, cmdMap, new String[] { key }, hm );
12781282

12791283
// return the value for this key if we found it
12801284
// else return null
@@ -1440,7 +1444,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14401444
new HashMap<String,Object>( keys.length );
14411445

14421446
// now use new NIO implementation
1443-
(new NIOLoader()).loadItemsNIO( asString, cmdMap, keys, ret );
1447+
(new NIOLoader()).doMulti( asString, cmdMap, keys, ret );
14441448

14451449
// fix the return array in case we had to rewrite any of the keys
14461450
for ( String key : keys ) {
@@ -1484,7 +1488,7 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
14841488
* @param asString if true, and if we are using NativehHandler, return string val
14851489
* @throws IOException if io exception happens while reading from socket
14861490
*/
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 {
14881492

14891493
while ( true ) {
14901494
String line = input.readLine();
@@ -1961,16 +1965,22 @@ public boolean isDone() {
19611965

19621966
// else find out the hard way
19631967
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
19651974

19661975
int bi = maxBuf;
19671976
while ( bi >= 0 && strPos >= 0 ) {
19681977
ByteBuffer buf = incoming.get( bi );
19691978
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--] )
19721981
return false;
19731982
}
1983+
19741984
bi--;
19751985
}
19761986

@@ -1995,7 +2005,7 @@ public String toString() {
19952005
}
19962006
}
19972007

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 ) {
19992009

20002010
long timeRemaining = 0;
20012011
try {
@@ -2076,7 +2086,7 @@ public void loadItemsNIO( boolean asString, Map<String, StringBuilder> sockKeys,
20762086
for ( Connection c : conns ) {
20772087
try {
20782088
if ( c.incoming.size() > 0 && c.isDone() )
2079-
loadItems( new ByteBufArrayInputStream( c.incoming ), ret, asString );
2089+
loadMulti( new ByteBufArrayInputStream( c.incoming ), ret, asString );
20802090
}
20812091
catch ( Exception e ) {
20822092
// shouldn't happen; we have all the data already

Diff for: src/com/danga/MemCached/test/UnitTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,26 @@ public static void test23() {
246246
log.error( "+ store/retrieve serialized object test passed" );
247247
}
248248

249+
public static void test24() {
250+
251+
String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
252+
String[] setKeys = { "key1", "key3", "key5", "key7" };
253+
254+
for ( String key : setKeys ) {
255+
mc.set( key, key );
256+
}
257+
258+
Map<String,Object> results = mc.getMulti( allKeys );
259+
260+
assert allKeys.length == results.size();
261+
for ( String key : setKeys ) {
262+
String val = (String)results.get( key );
263+
assert key.equals( val );
264+
}
265+
266+
log.error( "+ getMulti w/ keys that don't exist test passed" );
267+
}
268+
249269
public static void runAlTests( MemCachedClient mc ) {
250270
test14();
251271
for ( int t = 0; t < 2; t++ ) {
@@ -270,6 +290,7 @@ public static void runAlTests( MemCachedClient mc ) {
270290
test21();
271291
test22();
272292
test23();
293+
test24();
273294

274295
for ( int i = 0; i < 3; i++ )
275296
test19();
@@ -341,7 +362,6 @@ public static void main(String[] args) {
341362
pool.setWeights( weights );
342363
pool.setMaxConn( 250 );
343364
pool.setNagle( false );
344-
pool.setMaintSleep( 1000 );
345365
pool.setHashingAlg( SockIOPool.CONSISTENT_HASH );
346366
pool.initialize();
347367

0 commit comments

Comments
 (0)