Skip to content

Commit

Permalink
Exclude policy objects from wildcard index to prevent inefficient que…
Browse files Browse the repository at this point in the history
…ry plans.

Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Apr 25, 2022
1 parent 58149d9 commit 83aed4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Expand Up @@ -13,13 +13,17 @@
package org.eclipse.ditto.thingsearch.service.persistence;

import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_DELETE_AT;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_FEATURE_POLICY;
import static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_GLOBAL_READ;
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_POLICY;
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 static org.eclipse.ditto.thingsearch.service.persistence.PersistenceConstants.FIELD_REVISION;

import java.util.List;
import java.util.stream.Stream;

import org.bson.BsonDocument;
import org.bson.BsonInt32;
Expand All @@ -37,11 +41,23 @@ private Indices() {

/**
* Index for queries with effective filters.
* <p>
* All fields not included in the wildcard index are enumerated.
* Policy objects are excluded on thing and feature levels to prevent MongoDB from choosing an inefficient query
* plan according to the nested clauses for authorization.
* It is not possible to list the included fields because the feature array 'f' is the parent of the excluded
* feature policy field 'f.p'.
*/
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)));
.withWildcardProjection(Stream.of(FIELD_ID, FIELD_NAMESPACE, FIELD_GLOBAL_READ, FIELD_REVISION,
FIELD_POLICY_ID, FIELD_POLICY_REVISION, FIELD_POLICY, FIELD_FEATURE_POLICY)
.reduce(new BsonDocument(),
(doc, field) -> doc.append(field, new BsonInt32(0)),
(doc1, doc2) -> {
doc2.forEach(doc1::append);
return doc1;
})
);

/**
* Index for queries without effective filters to be executed as scans over all visible things.
Expand Down
Expand Up @@ -141,6 +141,11 @@ public final class PersistenceConstants {
*/
public static final String FIELD_POLICY = "p";

/**
* Field name for policies of features.
*/
public static final String FIELD_FEATURE_POLICY = FIELD_F_ARRAY + DOT + FIELD_POLICY;

/**
* Special character used as prefix for grant (g) and revoke (r) fields in index document to avoid conflict
* actual fields in a thing. The character is part of the restricted fields (see
Expand Down

0 comments on commit 83aed4c

Please sign in to comment.