Skip to content

Commit

Permalink
Remove non-working array element diff computation.
Browse files Browse the repository at this point in the history
Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Jun 22, 2022
1 parent ff17c53 commit d934455
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,49 +116,21 @@ private static List<Element> diffAsElementList(final JsonPointer key,
final int maxWireVersion,
final BiFunction<BsonDocument, Integer, Integer> mostSimilarIndex) {

final BsonSizeVisitor bsonSizeVisitor = new BsonSizeVisitor();
final BsonString subtrahendExpr = getPathExpr(key);
final Map<BsonValue, Integer> subtrahendIndexMap = IntStream.range(0, subtrahend.size()).boxed()
.collect(Collectors.toMap(subtrahend::get, Function.identity(), (i, j) -> i));
final List<Element> result = new ArrayList<>(minuend.size());
for (int j = 0; j < minuend.size(); ++j) {
final var element = minuend.get(j);
for (final BsonValue element : minuend) {
final Integer i = subtrahendIndexMap.get(element);
if (i != null) {
result.add(new Pointer(subtrahendExpr, i));
} else if (element.isDocument()) {
final var elementDoc = element.asDocument();
final int k = mostSimilarIndex.apply(elementDoc, j);
if (isMostSimilarElementADocument(subtrahend, k)) {
final int replaceSize = bsonSizeVisitor.eval(elementDoc);
final BsonDiff diff =
BsonDiff.minus(element.asDocument(), subtrahend.get(k).asDocument(), false, maxWireVersion);
final BsonDiffList diffList = diff.consumeAndExportToList();
final boolean isUnsetAllowed = maxWireVersion >= MIN_UNSET_WIRE_VERSION;
final var diffInPipeline =
diffList.toBsonInPipeline(getSubtrahendElement(subtrahendExpr, k), isUnsetAllowed);
final boolean diffSizeIsBetter = diffInPipeline.map(bsonSizeVisitor::eval)
.map(diffSize -> diffSize < replaceSize)
.orElse(false);
if (diffSizeIsBetter) {
result.add(new Replace(diffInPipeline.orElseThrow()));
} else {
result.add(new Replace(element));
}
} else {
result.add(new Replace(element));
}
} else {
result.add(new Replace(element));
}
}
return result;
}

private static boolean isMostSimilarElementADocument(final BsonArray subtrahend, final int k) {
return k >= 0 && k < subtrahend.size() && subtrahend.get(k).isDocument();
}

private static BsonDocument getSubtrahendElement(final BsonValue subtrahendExpr, final int i) {
final BsonArray args = new BsonArray();
args.add(subtrahendExpr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,6 @@ public boolean isDiffSmaller() {
return diffSize < replacementSize;
}

/**
* Destroy this object and convert the set and unset fields into lists.
*
* @return exported set and unset lists.
*/
BsonDiffList consumeAndExportToList() {
return new BsonDiffList(setPointers.collect(Collectors.toList()), unsetPointers.collect(Collectors.toList()));
}

private BsonDocument consumeAndExportSet() {
final BsonDocument setDocument = new BsonDocument();
setPointers.forEach(pair -> setDocument.append(getPathString(pair.first()), pair.second()));
Expand Down

This file was deleted.

0 comments on commit d934455

Please sign in to comment.