Skip to content

Commit

Permalink
INTERNAL: Add comments to new SMGetResult merge method.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhm0311 committed Dec 22, 2023
1 parent 5ad8753 commit c15a6e9
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,52 @@ public void mergeSMGetElements(final List<SMGetElement<T>> eachResult,
}
} else {
// do sort merge
boolean duplicated;
boolean duplicated; // !(Is current eachResult could be inserted?)
int comp, pos = 0;
for (SMGetElement<T> result : eachResult) {
duplicated = false;
for (; pos < mergedResult.size(); pos++) {
// compare b+tree key
// Compare bkey to search proper position of current eachResult in mergedResult.

comp = result.compareBkeyTo(mergedResult.get(pos));
if ((reverse) ? (0 < comp) : (0 > comp)) {
// If reverse, mergedResult is sorted as desc. (4 -> 3 -> 2 -> 1)
// Otherwise, mergedResult is sorted as asc. (1 -> 2 -> 3 -> 4)

// If reverse and bkey of current eachResult is greater than
// bkey of mergedResult.get(position),
// current eachResult should be inserted to position of mergedResult.

// If not reverse and bkey of current eachResult is less than
// bkey of mergedResult.get(position),
// current eachResult should be inserted to position of mergedResult.

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 (unique) {
mergedResult.remove(pos); // remove dup bkey
// If reverse, remove less cache key.
// Therefore, greater cache key is remained in mergedResult.
// Less cache key is key of mergedResult.get(position),
// and greater cache key is key of current eachResult.
// EX: key0 vs key1, remove key0. key1 is remained.

// Otherwise, remove greater cache key.
// Therefore, less cache key is remain in mergedResult.
// Greater cache key is cache key of mergedResult.get(position).
// and less cache key is key of current eachResult.
// EX: key0 vs. key1, remove key1. key0 is remained.

mergedResult.remove(pos);
}
break;
} else {
if (unique) {
// In this case, current eachResult is prevented to be inserted.

duplicated = true;
break;
}
Expand All @@ -78,6 +105,8 @@ public void mergeSMGetElements(final List<SMGetElement<T>> eachResult,
// So, stop the current sort-merge.
break;
}

// Insert current eachResult to proper position of mergedResult.
mergedResult.add(pos, result);
if (mergedResult.size() > count) {
// Remove elements that exceed the requested count.
Expand All @@ -94,9 +123,27 @@ public void mergeSMGetElements(final List<SMGetElement<T>> eachResult,
// do sort merge trimmed list
int pos = 0;
for (SMGetTrimKey result : eachTrimmedResult) {
// Search proper position of current eachTrimmedResult in mergedTrimmedKeys.

for (; pos < mergedTrimmedKeys.size(); pos++) {
if ((reverse) ? (0 < result.compareTo(mergedTrimmedKeys.get(pos)))
: (0 > result.compareTo(mergedTrimmedKeys.get(pos)))) {

// If reverse, mergedTrimmedKeys is sorted as desc. (4 -> 3 -> 2 -> 1)
// Otherwise, mergedTrimmedKeys is sorted as asc. (1 -> 2 -> 3 -> 4)

// SMGetTrimKey.compareTo() compares bkey first, and cache key second.
// If bkey is same, then compares cache key.

// If reverse, mergedTrimmedKeys is sorted as desc for bkey,
// and in same bkey, it is sorted as desc for cache key.
// Otherwise, mergedTrimmedKeys is sorted as asc for bkey,
// and in same bkey, it is sorted as asc for cache key.

// Same bkey and cache key?
// For current server, there is NO duplicated bkey in one btree.
// Server rejects duplicated bkey insertion in one btree.

break;
}
}
Expand Down

0 comments on commit c15a6e9

Please sign in to comment.