-
Notifications
You must be signed in to change notification settings - Fork 215
/
GetSortBsonVisitor.java
176 lines (155 loc) · 6.55 KB
/
GetSortBsonVisitor.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (c) 2017 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.read.expression.visitors;
import static java.util.Objects.requireNonNull;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.eclipse.ditto.internal.utils.persistence.mongo.DittoBsonJson;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonArrayBuilder;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.rql.query.SortDirection;
import org.eclipse.ditto.rql.query.SortOption;
import org.eclipse.ditto.rql.query.expression.SortFieldExpression;
import org.eclipse.ditto.rql.query.expression.visitors.SortFieldExpressionVisitor;
import org.eclipse.ditto.thingsearch.service.persistence.MongoSortKeyMappingFunction;
import org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants;
import com.mongodb.client.model.Sorts;
/**
* Creates a Bson sort option based on an expression.
*/
public final class GetSortBsonVisitor implements SortFieldExpressionVisitor<String> {
private GetSortBsonVisitor() {}
/**
* Creates a Bson sort option based on an expression.
*
* @param expression the expression to generate the Bson from.
* @param sortDirection the {@link SortDirection} needed to create a valid Bson for sorting.
* @return the Bson.
*/
public static List<Bson> apply(final SortFieldExpression expression, final SortDirection sortDirection) {
return getSortBson(sortDirection, path(expression));
}
/**
* Translate sort options into projections for the sort keys.
*
* @param sortOptions sort options.
* @return projection of the sort keys.
*/
public static Document projections(final List<SortOption> sortOptions) {
final Document document = new Document().append(PersistenceConstants.FIELD_PATH_MODIFIED, true);
sortOptions.stream()
.map(SortOption::getSortExpression)
.map(GetSortBsonVisitor::path)
.forEach(path -> document.append(path, true));
return document;
}
/**
* Project values of sort keys into an array.
*
* @param document document containing the sort keys.
* @param sortOptions sort options.
* @return values of sort keys as a JSON array.
*/
public static JsonArray sortValuesAsArray(final Document document, final List<SortOption> sortOptions) {
final JsonArrayBuilder builder = JsonFactory.newArrayBuilder();
sortOptions.stream()
.map(SortOption::getSortExpression)
.map(GetSortBsonVisitor::path)
.map(seekToPath(document))
.forEach(value -> builder.add(toJsonValue(value)));
return builder.build();
}
/**
* Translate a sort field expression into a path of the Bson document.
*
* @param expression the sort field expression.
* @return the path.
*/
public static String path(final SortFieldExpression expression) {
return expression.acceptSortVisitor(new GetSortBsonVisitor());
}
@Override
public String visitAttribute(final String key) {
return MongoSortKeyMappingFunction.mapSortKey(
PersistenceConstants.FIELD_THING, PersistenceConstants.FIELD_ATTRIBUTES, key);
}
@Override
public String visitFeatureIdProperty(final String featureId, final String property) {
return MongoSortKeyMappingFunction.mapSortKey(
PersistenceConstants.FIELD_THING, PersistenceConstants.FIELD_FEATURES, featureId,
PersistenceConstants.FIELD_PROPERTIES, property);
}
@Override
public String visitFeatureIdDesiredProperty(final CharSequence featureId, final CharSequence desiredProperty) {
return MongoSortKeyMappingFunction.mapSortKey(
PersistenceConstants.FIELD_THING, PersistenceConstants.FIELD_FEATURES, featureId.toString(),
PersistenceConstants.FIELD_DESIRED_PROPERTIES,
desiredProperty.toString());
}
@Override
public String visitSimple(final String fieldName) {
return fieldName.startsWith(PersistenceConstants.SLASH)
? MongoSortKeyMappingFunction.mapSortKey(PersistenceConstants.FIELD_THING + fieldName)
: fieldName;
}
/**
* Builds singleton List (with always 1 entry) of a Bson document used for sorting based on the passed
* {@code sortDirection} and {@code fieldName}.
*
* @param sortDirection the {@link SortDirection} to apply.
* @param sortKey the sorting key.
* @return the singleton List of a Bson sort document.
*/
private static List<Bson> getSortBson(final SortDirection sortDirection, final String sortKey) {
requireNonNull(sortDirection);
final Bson sort;
switch (sortDirection) {
case ASC:
sort = Sorts.ascending(sortKey);
break;
case DESC:
sort = Sorts.descending(sortKey);
break;
default:
throw new IllegalStateException("Unknown SortDirection=" + sortDirection);
}
return Collections.singletonList(sort);
}
private static Function<String, Object> seekToPath(final Document document) {
return path -> seekToPathImpl(document, path.split("\\."), 0);
}
private static Object seekToPathImpl(final Document document, final String[] segments, final int i) {
if (document == null) {
return null;
} else if (i + 1 == segments.length) {
return document.get(segments[i]);
} else {
return seekToPathImpl(document.get(segments[i], Document.class), segments, i + 1);
}
}
private static JsonValue toJsonValue(final Object object) {
if (object instanceof Document) {
return JsonFactory.readFrom(((Document) object).toJson());
} else if (object instanceof BsonValue) {
return DittoBsonJson.getInstance().serialize((BsonValue) object);
} else {
return JsonValue.of(object);
}
}
}