Skip to content

Commit 01fe32e

Browse files
Ted Crossmaningenthr
authored andcommitted
Add Iterator versions of getAsyncBulk and getBulk
Add the following methods to Memcached/MemcachedIF: - <T> BulkFuture<Map<String, T>> asyncGetBulk(Iterator<String> keys, Iterator<Transcoder<T>> tcs); - <T> BulkFuture<Map<String, T>> asyncGetBulk(Iterator<String> keys, Transcoder<T> tc); - BulkFuture<Map<String, Object>> asyncGetBulk(Iterator<String> keys); - <T> Map<String, T> getBulk(Iterator<String> keys, Transcoder<T> tc); - Map<String, Object> getBulk(Iterator<String> keys); Some times it is more convenient/memory efficent to pass in an Iterator of keys than it is to have a Collection of them. Example: public PrefixAdderIterator implements Iterator<String> { private final String prefix; private final Iterator<String> iterator; public PrefixAdderIterator(String prefix, Iterator<String> iterator) { this.prefix = prefix; this.iterator = iterator; } ... public String next() { return prefix+iterator.next(); } ... } rather than List<String> prefixedKeys = new ArrayList<String>(); for (String key: keys) { prefixedKeys.add(prefix+key); } Change-Id: I15821983ea2ebd07fd98feeb968ce9fa578ded0b Reviewed-on: http://review.couchbase.org/12283 Reviewed-by: Ted Crossman <[email protected]> Reviewed-by: Matt Ingenthron <[email protected]> Tested-by: Matt Ingenthron <[email protected]>
1 parent 3740d61 commit 01fe32e

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

src/main/java/net/spy/memcached/MemcachedClient.java

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ public Object get(String key) {
10221022
* Asynchronously get a bunch of objects from the cache.
10231023
*
10241024
* @param <T>
1025-
* @param keys the keys to request
1025+
* @param keyIter Iterator that produces keys.
10261026
* @param tcIter an iterator of transcoders to serialize and unserialize
10271027
* values; the transcoders are matched with the keys in the same
10281028
* order. The minimum of the key collection length and number of
@@ -1032,7 +1032,7 @@ public Object get(String key) {
10321032
* @throws IllegalStateException in the rare circumstance where queue is too
10331033
* full to accept any more requests
10341034
*/
1035-
public <T> BulkFuture<Map<String, T>> asyncGetBulk(Collection<String> keys,
1035+
public <T> BulkFuture<Map<String, T>> asyncGetBulk(Iterator<String> keyIter,
10361036
Iterator<Transcoder<T>> tcIter) {
10371037
final Map<String, Future<T>> m = new ConcurrentHashMap<String, Future<T>>();
10381038

@@ -1046,7 +1046,7 @@ public <T> BulkFuture<Map<String, T>> asyncGetBulk(Collection<String> keys,
10461046
final Map<MemcachedNode, Collection<String>> chunks =
10471047
new HashMap<MemcachedNode, Collection<String>>();
10481048
final NodeLocator locator = mconn.getLocator();
1049-
Iterator<String> keyIter = keys.iterator();
1049+
10501050
while (keyIter.hasNext() && tcIter.hasNext()) {
10511051
String key = keyIter.next();
10521052
tcMap.put(key, tcIter.next());
@@ -1113,6 +1113,41 @@ public void complete() {
11131113
return rv;
11141114
}
11151115

1116+
/**
1117+
* Asynchronously get a bunch of objects from the cache.
1118+
*
1119+
* @param <T>
1120+
* @param keys the keys to request
1121+
* @param tcIter an iterator of transcoders to serialize and unserialize
1122+
* values; the transcoders are matched with the keys in the same
1123+
* order. The minimum of the key collection length and number of
1124+
* transcoders is used and no exception is thrown if they do not
1125+
* match
1126+
* @return a Future result of that fetch
1127+
* @throws IllegalStateException in the rare circumstance where queue is too
1128+
* full to accept any more requests
1129+
*/
1130+
public <T> BulkFuture<Map<String, T>> asyncGetBulk(Collection<String> keys,
1131+
Iterator<Transcoder<T>> tcIter) {
1132+
return asyncGetBulk(keys.iterator(), tcIter);
1133+
}
1134+
1135+
/**
1136+
* Asynchronously get a bunch of objects from the cache.
1137+
*
1138+
* @param <T>
1139+
* @param keyIter Iterator for the keys to request
1140+
* @param tc the transcoder to serialize and unserialize values
1141+
* @return a Future result of that fetch
1142+
* @throws IllegalStateException in the rare circumstance where queue is too
1143+
* full to accept any more requests
1144+
*/
1145+
public <T> BulkFuture<Map<String, T>> asyncGetBulk(Iterator<String> keyIter,
1146+
Transcoder<T> tc) {
1147+
return asyncGetBulk(keyIter,
1148+
new SingleElementInfiniteIterator<Transcoder<T>>(tc));
1149+
}
1150+
11161151
/**
11171152
* Asynchronously get a bunch of objects from the cache.
11181153
*
@@ -1129,6 +1164,20 @@ public <T> BulkFuture<Map<String, T>> asyncGetBulk(Collection<String> keys,
11291164
tc));
11301165
}
11311166

1167+
/**
1168+
* Asynchronously get a bunch of objects from the cache and decode them with
1169+
* the given transcoder.
1170+
*
1171+
* @param keyIter Iterator that produces the keys to request
1172+
* @return a Future result of that fetch
1173+
* @throws IllegalStateException in the rare circumstance where queue is too
1174+
* full to accept any more requests
1175+
*/
1176+
public BulkFuture<Map<String, Object>> asyncGetBulk(
1177+
Iterator<String> keyIter) {
1178+
return asyncGetBulk(keyIter, transcoder);
1179+
}
1180+
11321181
/**
11331182
* Asynchronously get a bunch of objects from the cache and decode them with
11341183
* the given transcoder.
@@ -1228,18 +1277,18 @@ public void gotData(String k, int flags, long cas, byte[] data) {
12281277
* Get the values for multiple keys from the cache.
12291278
*
12301279
* @param <T>
1231-
* @param keys the keys
1280+
* @param keyIter Iterator that produces the keys
12321281
* @param tc the transcoder to serialize and unserialize value
12331282
* @return a map of the values (for each value that exists)
12341283
* @throws OperationTimeoutException if the global operation timeout is
12351284
* exceeded
12361285
* @throws IllegalStateException in the rare circumstance where queue is too
12371286
* full to accept any more requests
12381287
*/
1239-
public <T> Map<String, T> getBulk(Collection<String> keys,
1288+
public <T> Map<String, T> getBulk(Iterator<String> keyIter,
12401289
Transcoder<T> tc) {
12411290
try {
1242-
return asyncGetBulk(keys, tc).get(operationTimeout,
1291+
return asyncGetBulk(keyIter, tc).get(operationTimeout,
12431292
TimeUnit.MILLISECONDS);
12441293
} catch (InterruptedException e) {
12451294
throw new RuntimeException("Interrupted getting bulk values", e);
@@ -1250,6 +1299,37 @@ public <T> Map<String, T> getBulk(Collection<String> keys,
12501299
}
12511300
}
12521301

1302+
/**
1303+
* Get the values for multiple keys from the cache.
1304+
*
1305+
* @param keyIter Iterator that produces the keys
1306+
* @return a map of the values (for each value that exists)
1307+
* @throws OperationTimeoutException if the global operation timeout is
1308+
* exceeded
1309+
* @throws IllegalStateException in the rare circumstance where queue is too
1310+
* full to accept any more requests
1311+
*/
1312+
public Map<String, Object> getBulk(Iterator<String> keyIter) {
1313+
return getBulk(keyIter, transcoder);
1314+
}
1315+
1316+
/**
1317+
* Get the values for multiple keys from the cache.
1318+
*
1319+
* @param <T>
1320+
* @param keys the keys
1321+
* @param tc the transcoder to serialize and unserialize value
1322+
* @return a map of the values (for each value that exists)
1323+
* @throws OperationTimeoutException if the global operation timeout is
1324+
* exceeded
1325+
* @throws IllegalStateException in the rare circumstance where queue is too
1326+
* full to accept any more requests
1327+
*/
1328+
public <T> Map<String, T> getBulk(Collection<String> keys,
1329+
Transcoder<T> tc) {
1330+
return getBulk(keys.iterator(), tc);
1331+
}
1332+
12531333
/**
12541334
* Get the values for multiple keys from the cache.
12551335
*

src/main/java/net/spy/memcached/MemcachedClientIF.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,27 @@ <T> Future<CASValue<T>> asyncGetAndTouch(final String key, final int exp,
107107

108108
Object get(String key);
109109

110+
<T> BulkFuture<Map<String, T>> asyncGetBulk(Iterator<String> keys,
111+
Iterator<Transcoder<T>> tcs);
110112
<T> BulkFuture<Map<String, T>> asyncGetBulk(Collection<String> keys,
111113
Iterator<Transcoder<T>> tcs);
112114

115+
<T> BulkFuture<Map<String, T>> asyncGetBulk(Iterator<String> keys,
116+
Transcoder<T> tc);
113117
<T> BulkFuture<Map<String, T>> asyncGetBulk(Collection<String> keys,
114118
Transcoder<T> tc);
115119

120+
BulkFuture<Map<String, Object>> asyncGetBulk(Iterator<String> keys);
116121
BulkFuture<Map<String, Object>> asyncGetBulk(Collection<String> keys);
117122

118123
<T> BulkFuture<Map<String, T>> asyncGetBulk(Transcoder<T> tc, String... keys);
119124

120125
BulkFuture<Map<String, Object>> asyncGetBulk(String... keys);
121126

127+
<T> Map<String, T> getBulk(Iterator<String> keys, Transcoder<T> tc);
122128
<T> Map<String, T> getBulk(Collection<String> keys, Transcoder<T> tc);
123129

130+
Map<String, Object> getBulk(Iterator<String> keys);
124131
Map<String, Object> getBulk(Collection<String> keys);
125132

126133
<T> Map<String, T> getBulk(Transcoder<T> tc, String... keys);

0 commit comments

Comments
 (0)