Skip to content

Commit

Permalink
Amend search indices.
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 Apr 11, 2022
1 parent 4a3d317 commit ed3d196
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public final class Index {
private final boolean unique;
private final boolean sparse;
private final boolean background;
private final BsonDocument partialFilterExpression;
private final BsonDocument wildcardProjection;

@Nullable
private final Long expireAfterSeconds;

private Index(final BsonDocument keys, final String name, final boolean unique, final boolean sparse,
final boolean background, final BsonDocument partialFilterExpression,
final boolean background, final BsonDocument wildcardProjection,
@Nullable final Long expireAfterSeconds) {

this.keys = requireNonNull(keys);
Expand All @@ -53,7 +53,7 @@ private Index(final BsonDocument keys, final String name, final boolean unique,
this.sparse = sparse;
this.background = background;
this.expireAfterSeconds = expireAfterSeconds;
this.partialFilterExpression = partialFilterExpression;
this.wildcardProjection = wildcardProjection;
}

/**
Expand Down Expand Up @@ -91,15 +91,15 @@ public static Index indexInfoOf(final Document document) {
? ((Number) expireAfterSecondsObject).longValue()
: null;

final String partialFilterExpressionName = "partialFilterExpression";
final BsonDocument partialFilterExpression;
if (document.containsKey(partialFilterExpressionName)) {
partialFilterExpression = BsonUtil.toBsonDocument((Document) document.get(partialFilterExpressionName));
final String wildcardProjectionName = "wildcardProjection";
final BsonDocument wildcardProjection;
if (document.containsKey(wildcardProjectionName)) {
wildcardProjection = BsonUtil.toBsonDocument((Document) document.get(wildcardProjectionName));
} else {
partialFilterExpression = new BsonDocument();
wildcardProjection = new BsonDocument();
}

return new Index(keyDbObject, name, unique, sparse, background, partialFilterExpression, expireAfterSeconds);
return new Index(keyDbObject, name, unique, sparse, background, wildcardProjection, expireAfterSeconds);
}

/**
Expand All @@ -110,17 +110,17 @@ public static Index indexInfoOf(final Document document) {
* @return a copy of this object with expiration set.
*/
public Index withExpireAfterSeconds(final long expireAfterSeconds) {
return new Index(keys, name, unique, sparse, background, partialFilterExpression, expireAfterSeconds);
return new Index(keys, name, unique, sparse, background, wildcardProjection, expireAfterSeconds);
}

/**
* Create a copy of this object with a partial filter expression.
* Create a copy of this object with a wildcard projection.
*
* @param partialFilterExpression the partial filter expression.
* @return a copy of this object with partial filter expression set.
* @param wildcardProjection the wildcard projection.
* @return a copy of this object with wildcard projection.
*/
public Index withPartialFilterExpression(final BsonDocument partialFilterExpression) {
return new Index(keys, name, unique, sparse, background, partialFilterExpression, expireAfterSeconds);
public Index withWildcardProjection(final BsonDocument wildcardProjection) {
return new Index(keys, name, unique, sparse, background, wildcardProjection, expireAfterSeconds);
}

/**
Expand All @@ -135,8 +135,8 @@ public IndexModel toIndexModel() {
.sparse(sparse)
.background(background);

if (!partialFilterExpression.isEmpty()) {
options.partialFilterExpression(partialFilterExpression);
if (!wildcardProjection.isEmpty()) {
options.wildcardProjection(wildcardProjection);
}

getExpireAfterSeconds().ifPresent(n -> options.expireAfter(n, TimeUnit.SECONDS));
Expand Down Expand Up @@ -212,13 +212,13 @@ public boolean equals(final Object o) {
background == indexInfo.background &&
Objects.equals(keys, indexInfo.keys) &&
Objects.equals(name, indexInfo.name) &&
Objects.equals(partialFilterExpression, indexInfo.partialFilterExpression) &&
Objects.equals(wildcardProjection, indexInfo.wildcardProjection) &&
Objects.equals(expireAfterSeconds, indexInfo.expireAfterSeconds);
}

@Override
public int hashCode() {
return Objects.hash(keys, name, unique, sparse, background, partialFilterExpression, expireAfterSeconds);
return Objects.hash(keys, name, unique, sparse, background, wildcardProjection, expireAfterSeconds);
}

@Override
Expand All @@ -229,7 +229,7 @@ public String toString() {
", unique=" + unique +
", sparse=" + sparse +
", background=" + background +
", partialFilterExpression=" + partialFilterExpression +
", wildcardProjection=" + wildcardProjection +
", expireAfterSeconds=" + expireAfterSeconds +
']';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@

import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_DELETE_AT;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_GLOBAL_READ;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_GRANTED_PATH;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_ID;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_NAMESPACE;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_PATH_KEY;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_PATH_VALUE;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_POLICY_ID;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_POLICY_REVISION;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.eclipse.ditto.internal.utils.persistence.mongo.indices.Index;
import org.eclipse.ditto.internal.utils.persistence.mongo.indices.IndexFactory;

Expand All @@ -41,37 +38,41 @@ private Indices() {
/**
* Index for queries with effective filters.
*/
private static final Index KEY_VALUE = IndexFactory.newInstance("key-value",
Arrays.asList(FIELD_GRANTED_PATH, FIELD_PATH_KEY, FIELD_PATH_VALUE, FIELD_ID), false);
private static final Index WILDCARD = IndexFactory.newInstance("v_wildcard", List.of("$**"), false)
.withWildcardProjection(new BsonDocument()
.append("t", new BsonInt32(1))
.append("f", new BsonInt32(1)));

/**
* Index for queries without effective filters to be executed as scans over all visible things.
*/
private static final Index GLOBAL_READ = IndexFactory.newInstance("global-read",
Collections.singletonList(FIELD_GLOBAL_READ), false);
private static final Index GLOBAL_READ =
IndexFactory.newInstance("global_read", List.of(FIELD_GLOBAL_READ, FIELD_ID), false);

/**
* Index for dispatching policy events.
*/
private static final Index POLICY = IndexFactory.newInstance("policyId",
Arrays.asList(FIELD_POLICY_ID, FIELD_POLICY_REVISION), false);

private static final Index DELETE_AT = IndexFactory.newExpirationIndex(FIELD_DELETE_AT, FIELD_DELETE_AT, 0L);
private static final Index POLICY =
IndexFactory.newInstance("policyId", List.of(FIELD_POLICY_ID, FIELD_POLICY_REVISION), false);

/**
* Index for namespace.
*/
private static final Index NAMESPACE = IndexFactory.newInstance("namespace",
Arrays.asList(FIELD_NAMESPACE, FIELD_ID), false);
private static final Index NAMESPACE =
IndexFactory.newInstance("_namespace", List.of(FIELD_NAMESPACE, FIELD_ID), false);

/**
* Index for namespace purging.
*/
private static final Index DELETE_AT = IndexFactory.newExpirationIndex(FIELD_DELETE_AT, FIELD_DELETE_AT, 0L);

/**
* Gets all defined indices.
*
* @return the indices
*/
public static List<Index> all() {
return Collections.unmodifiableList(
Arrays.asList(KEY_VALUE, GLOBAL_READ, POLICY, NAMESPACE, DELETE_AT));
return List.of(NAMESPACE, GLOBAL_READ, WILDCARD, POLICY, DELETE_AT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Checks the initialization of Mongo indices.
*/
public class IndexInitializationIT extends AbstractThingSearchPersistenceITBase {
public final class IndexInitializationIT extends AbstractThingSearchPersistenceITBase {

@Test
public void indicesAreCorrectlyInitialized() {
Expand Down

0 comments on commit ed3d196

Please sign in to comment.