Skip to content

Commit

Permalink
Restore base element selection logic for feature array 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 Mar 31, 2022
1 parent 57695e4 commit 9a2956e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.mapping;

import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_FEATURE_ID;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_F_ARRAY;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.bson.BsonArray;
Expand All @@ -28,6 +32,8 @@
import org.bson.BsonValue;
import org.eclipse.ditto.json.JsonPointer;

import akka.japi.Pair;

/**
* Diff between 2 BSON arrays. Only used for the flattened key-value array because Ditto has no API for array
* operations.
Expand All @@ -45,6 +51,32 @@ static BsonValue diff(final JsonPointer key,
return diff(key, minuend, subtrahend, (v, j) -> j);
}

static BsonDiff diffFeaturesArray(final BsonArray minuend, final BsonArray subtrahend) {
final BsonSizeVisitor bsonSizeVisitor = new BsonSizeVisitor();
final int replacementSize = bsonSizeVisitor.eval(subtrahend);
if (minuend.equals(subtrahend)) {
return BsonDiff.empty(replacementSize);
}
final JsonPointer internalArrayKey = JsonPointer.of(FIELD_F_ARRAY);
final Map<BsonValue, Integer> kMap = IntStream.range(0, subtrahend.size())
.boxed()
.collect(Collectors.toMap(
i -> subtrahend.get(i).asDocument().get(FIELD_FEATURE_ID),
Function.identity(),
(x, y) -> x
));
final BiFunction<BsonDocument, Integer, Integer> kMapGet =
// use 0 as default value to re-use root grant/revoke
(doc, j) -> kMap.getOrDefault(doc.get(FIELD_FEATURE_ID), 0);
final BsonValue difference = diff(internalArrayKey, minuend, subtrahend, kMapGet);
return new BsonDiff(
replacementSize,
bsonSizeVisitor.eval(difference),
Stream.of(Pair.create(internalArrayKey, difference)),
Stream.empty()
);
}

private static BsonValue diff(final JsonPointer key,
final BsonArray minuend,
final BsonArray subtrahend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.mapping;

import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_F_ARRAY;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -93,7 +95,17 @@ public static BsonDiff minus(final BsonDocument minuend,
* @return the difference.
*/
public static BsonDiff minusThingDocs(final BsonDocument minuend, final BsonDocument subtrahend) {
return minus(minuend, subtrahend, true);
// compute the internal array diff especially to find similar elements by internal key
final var minuendFeatures = minuend.getArray(FIELD_F_ARRAY);
final var subtrahendFeatures = subtrahend.getArray(FIELD_F_ARRAY);
final var diffFeatures = BsonArrayDiff.diffFeaturesArray(minuendFeatures, subtrahendFeatures);
// compute the rest of the diff without the internal array
final var minuendWithoutInternal = minuend.clone();
final var subtrahendWithoutInternal = subtrahend.clone();
minuendWithoutInternal.remove(FIELD_F_ARRAY);
subtrahendWithoutInternal.remove(FIELD_F_ARRAY);
final var diffWithoutInternal = minus(minuendWithoutInternal, subtrahendWithoutInternal, true);
return diffWithoutInternal.concat(diffFeatures);
}

/**
Expand Down

0 comments on commit 9a2956e

Please sign in to comment.