-
Notifications
You must be signed in to change notification settings - Fork 215
/
BsonDiffList.java
112 lines (100 loc) · 4 KB
/
BsonDiffList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.mapping;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.eclipse.ditto.json.JsonKey;
import org.eclipse.ditto.json.JsonPointer;
import akka.japi.Pair;
/**
* Exported diffs of a document.
*/
public final class BsonDiffList {
private static final String MERGE_OBJECTS = "$mergeObjects";
/**
* $unsetField only possible for top-level fields
*/
private static final String SET_FIELD = "$setField";
private static final BsonString SET_FIELD_REMOVE = new BsonString("$$REMOVE");
private static final String FIELD = "field";
private static final String INPUT = "input";
private static final String VALUE = "value";
final List<Pair<JsonPointer, BsonValue>> set;
final List<JsonPointer> unset;
BsonDiffList(final List<Pair<JsonPointer, BsonValue>> set, final List<JsonPointer> unset) {
this.set = set;
this.unset = unset;
}
/**
* Create a BSON document to use in an aggregation pipeline.
* This is not trivial if there is a nested unset pointer, in which case we give up.
*
* @return the diff document, or an empty optional if we gave up.
*/
Optional<BsonValue> toBsonInPipeline(final BsonValue previousDocumentExpression) {
final boolean hasNestedUnset = unset.stream().anyMatch(pointer -> pointer.getLevelCount() > 1);
if (hasNestedUnset) {
return Optional.empty();
}
final BsonValue beforeUnset;
if (set.isEmpty()) {
beforeUnset = previousDocumentExpression;
} else {
final var doc = new BsonDocument();
for (final var pair : set) {
setBsonDocument(doc, pair.first(), pair.second());
}
beforeUnset = mergeObjects(previousDocumentExpression, doc);
}
return Optional.of(buildUnsetDocument(beforeUnset, unset.iterator()));
}
private static BsonDocument mergeObjects(final BsonValue prev, final BsonValue next) {
final BsonArray args = new BsonArray();
args.add(prev);
args.add(next);
return new BsonDocument().append(MERGE_OBJECTS, args);
}
private static BsonValue buildUnsetDocument(final BsonValue beforeUnset, final Iterator<JsonPointer> keys) {
if (keys.hasNext()) {
final String key = keys.next().getRoot().map(JsonKey::toString).orElseThrow();
final BsonDocument nextDoc = new BsonDocument()
.append(SET_FIELD, new BsonDocument()
.append(FIELD, new BsonString(key))
.append(INPUT, beforeUnset)
.append(VALUE, SET_FIELD_REMOVE)
);
return buildUnsetDocument(nextDoc, keys);
} else {
return beforeUnset;
}
}
private static void setBsonDocument(final BsonDocument doc, final JsonPointer pointer, final BsonValue value) {
final String key = pointer.getRoot().map(JsonKey::toString).orElseThrow();
if (pointer.getLevelCount() > 1) {
if (doc.containsKey(key)) {
setBsonDocument(doc.getDocument(key), pointer.nextLevel(), value);
} else {
final var newDocument = new BsonDocument();
setBsonDocument(newDocument, pointer.nextLevel(), value);
doc.append(key, newDocument);
}
} else {
doc.append(key, value);
}
}
}