Skip to content

Commit

Permalink
INTERNAL: refactoring groupingKeys method logic in ArcusClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Jul 17, 2023
1 parent 5ab583c commit 53dcdb2
Showing 1 changed file with 22 additions and 36 deletions.
58 changes: 22 additions & 36 deletions src/main/java/net/spy/memcached/ArcusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2146,48 +2146,34 @@ public SMGetFuture<List<SMGetElement<Object>>> asyncBopSortMergeGet(
* @return list of grouped (memcached node + keys) in the group
*/
private Collection<Entry<MemcachedNode, List<String>>> groupingKeys(List<String> keyList, int groupSize) {
Map<String, Integer> chunkCount = new HashMap<String, Integer>();
Map<String, Entry<MemcachedNode, List<String>>> result = new HashMap<String,
Entry<MemcachedNode, List<String>>>();

Map<Integer, Entry<MemcachedNode, List<String>>> result =
new HashMap<Integer, Entry<MemcachedNode, List<String>>>();
Map<MemcachedNode, List<String>> temp = new HashMap<MemcachedNode, List<String>>();
MemcachedConnection conn = getMemcachedConnection();
Integer index = 0;

for (String key : keyList) {
MemcachedNode qa = conn.findNodeByKey(key);

for (String k : keyList) {
MemcachedNode qa = conn.findNodeByKey(k);
String node;
if (qa == null) {
node = "fake_node";
} else {
node = qa.getSocketAddress().toString();
throw new IllegalStateException("Could not find node for key: " + key);
}
int cc;
if (chunkCount.containsKey(node)) {
cc = chunkCount.get(node);
} else {
cc = 0;
chunkCount.put(node, 0);
if (!temp.containsKey(qa)) {
temp.put(qa, new ArrayList<String>());
}

String resultKey = node + cc;

List<String> arrangedKeyList = null;

if (result.containsKey(resultKey)) {
if (result.get(resultKey).getValue().size() >= groupSize) {
arrangedKeyList = new ArrayList<String>();
cc++;
result.put(node + cc, new AbstractMap.SimpleEntry<MemcachedNode,
List<String>>(qa, arrangedKeyList));
chunkCount.put(node, cc);
} else {
arrangedKeyList = result.get(resultKey).getValue();
}
} else {
arrangedKeyList = new ArrayList<String>();
result.put(resultKey, new AbstractMap.SimpleEntry<MemcachedNode,
List<String>>(qa, arrangedKeyList));
if (temp.get(qa).size() >= groupSize) {
result.put(index, new AbstractMap.SimpleEntry<MemcachedNode, List<String>>(qa, temp.get(qa)));
temp.put(qa, new ArrayList<String>());
index++;
}
arrangedKeyList.add(k);
temp.get(qa).add(key);
}
/*
* Add the new Entry instance which is not full(smaller than groupSize) to the result.
* */
for (Entry<MemcachedNode, List<String>> entry : temp.entrySet()) {
result.put(index, new AbstractMap.SimpleEntry<MemcachedNode, List<String>>(entry.getKey(), entry.getValue()));
index++;
}
return result.values();
}
Expand Down

0 comments on commit 53dcdb2

Please sign in to comment.