From 7902d9bcbc36d54a4c9da8297d65af9ae5f53526 Mon Sep 17 00:00:00 2001 From: uhm0311 Date: Fri, 22 Dec 2023 15:42:02 +0900 Subject: [PATCH] INTERNAL: Add comments to new SMGetResult merge method. --- .../internal/result/SMGetResultImpl.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/spy/memcached/internal/result/SMGetResultImpl.java b/src/main/java/net/spy/memcached/internal/result/SMGetResultImpl.java index 5e758f31d..ecd8a5e18 100644 --- a/src/main/java/net/spy/memcached/internal/result/SMGetResultImpl.java +++ b/src/main/java/net/spy/memcached/internal/result/SMGetResultImpl.java @@ -43,32 +43,34 @@ public void mergeSMGetElements(final List> eachResult, } } else { // do sort merge - boolean duplicated; + boolean doInsert; // Is current eachResult could be inserted? int comp, pos = 0; for (SMGetElement result : eachResult) { - duplicated = false; + doInsert = true; for (; pos < mergedResult.size(); pos++) { - // compare b+tree key comp = result.compareBkeyTo(mergedResult.get(pos)); - if ((reverse) ? (0 < comp) : (0 > comp)) { + if ((reverse) ? (comp > 0) : (comp < 0)) { break; } - if (comp == 0) { // compare key string + if (comp == 0) { + // Duplicated bkey. Compare the "cache key". int keyComp = result.compareKeyTo(mergedResult.get(pos)); - if ((reverse) ? (0 < keyComp) : (0 > keyComp)) { + if ((reverse) ? (keyComp > 0) : (keyComp < 0)) { if (unique) { - mergedResult.remove(pos); // remove dup bkey + // Remove duplicated bkey. + mergedResult.remove(pos); } break; } else { if (unique) { - duplicated = true; + // NOT the first cache key with the same bkey. do NOT insert. + doInsert = false; break; } } } } - if (duplicated) { // UNIQUE + if (!doInsert) { // UNIQUE continue; } if (pos >= count) { @@ -83,7 +85,6 @@ public void mergeSMGetElements(final List> eachResult, } mergedResult.add(pos, result); if (mergedResult.size() > count) { - // Remove elements that exceed the requested count. mergedResult.remove(count); } pos += 1; @@ -95,11 +96,11 @@ public void mergeSMGetElements(final List> eachResult, mergedTrimmedKeys.addAll(eachTrimmedResult); } else { // do sort merge trimmed list - int pos = 0; + int comp, pos = 0; for (SMGetTrimKey result : eachTrimmedResult) { for (; pos < mergedTrimmedKeys.size(); pos++) { - if ((reverse) ? (0 < result.compareTo(mergedTrimmedKeys.get(pos))) - : (0 > result.compareTo(mergedTrimmedKeys.get(pos)))) { + comp = result.compareTo(mergedTrimmedKeys.get(pos)); + if ((reverse) ? (comp > 0) : (comp < 0)) { break; } }