Skip to content

Commit

Permalink
INTERNAL: Add comments to old SMGetResult merge method.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhm0311 committed Dec 26, 2023
1 parent 5ad8753 commit f24932f
Showing 1 changed file with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.spy.memcached.ops.OperationStatus;

public final class SMGetResultOldImpl<T> extends SMGetResult<T> {
private final AtomicBoolean mergedTrim = new AtomicBoolean(false);
private final AtomicBoolean isMergedResultTrimmed = new AtomicBoolean(false);
private final int offset;
private final boolean isMultiNode;

Expand Down Expand Up @@ -44,53 +44,72 @@ public void addFailedOperationStatus(OperationStatus status) {
}

public void mergeSMGetElements(final List<SMGetElement<T>> eachResult,
final boolean isTrimmed) {
final boolean isEachResultTrimmed) {

if (mergedResult.isEmpty()) {
// merged result is empty, add all.
mergedResult.addAll(eachResult);
mergedTrim.set(isTrimmed);
isMergedResultTrimmed.set(isEachResultTrimmed);

while (mergedResult.size() > totalResultElementCount) {
mergedResult.remove(totalResultElementCount);
}
} else {
boolean addAll = true;
int pos = 0;
boolean allAdded = true; // Is all element of eachResult added to mergedResult?
int comp, pos = 0;
for (SMGetElement<T> result : eachResult) {
for (; pos < mergedResult.size(); pos++) {
if ((reverse) ? (0 < result.compareTo(mergedResult.get(pos)))
: (0 > result.compareTo(mergedResult.get(pos)))) {
// Search proper position of current eachResult in mergedResult.

comp = result.compareTo(mergedResult.get(pos));
if ((reverse) ? (comp > 0) : (comp < 0)) {
break;
}
}
if (pos >= totalResultElementCount) {
addAll = false;
// From current eachResult, elements of eachResult can NOT be inserted
// since proper position is over the totalResultElementCount.

allAdded = false;
break;
}
if (pos >= mergedResult.size() && mergedTrim.get() &&
if (pos >= mergedResult.size() && isMergedResultTrimmed.get() &&
result.compareBkeyTo(mergedResult.get(pos - 1)) != 0) {
addAll = false;

// When proper position is over the size of mergedResult
// and mergedResult is trimmed,
// old smget stops since this result is trimmed.

allAdded = false;
break;
}

// Insert current eachResult to proper position of mergedResult.
mergedResult.add(pos, result);
if (mergedResult.size() > totalResultElementCount) {
mergedResult.remove(totalResultElementCount);
}
pos += 1;
}
if (isTrimmed && addAll && pos > 0) {
if (isEachResultTrimmed && allAdded && pos > 0) {
// If eachResult is trimmed and all element of eachResult added,
// then mergedResult includes trimmed elements.
// Trim the mergedResult.

while (pos < mergedResult.size()) {
if (mergedResult.get(pos).compareBkeyTo(mergedResult.get(pos - 1)) == 0) {
pos += 1;
} else {
mergedResult.remove(pos);
}
}
mergedTrim.set(true);
isMergedResultTrimmed.set(true);
}
if (mergedResult.size() >= totalResultElementCount) {
mergedTrim.set(false);
// If size of mergedResult is reached to totalResultElementCount,
// then mergedResult is NOT trimmed.

isMergedResultTrimmed.set(false);
}
}
}
Expand All @@ -99,7 +118,7 @@ public void mergeSMGetElements(final List<SMGetElement<T>> eachResult,
public void makeResultOperationStatus() {
boolean isDuplicated = hasDuplicatedBKeyResult();

if (mergedTrim.get()) {
if (isMergedResultTrimmed.get()) {
if (isDuplicated) {
resultOperationStatus.add(new OperationStatus(true, "DUPLICATED_TRIMMED"));
} else {
Expand Down

0 comments on commit f24932f

Please sign in to comment.