Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compound Wildcard Indexes #4790

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.0-4471-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.0-4471-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.0-4471-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.0-4471-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.index;

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Mark a class to use compound wildcard indexes. <br />
*
* <pre class="code">
* &#64;Document
* &#64;CompoundWildcardIndexed(wildcardFieldName = "address", fields = "{'firstname': 1}")
* class Person {
* String firstname;
* Address address;
* }
*
* db.product.createIndex({"address.$**": 1, "firstname": 1})
* </pre>
*
* {@literal wildcardProjection} can be used to specify keys to in-/exclude in the index.
*
* <pre class="code">
*
* &#64;Document
* &#64;CompoundWildcardIndexed(wildcardProjection = "{'address.zip': 0}", fields = "{'firstname': 1}")
* class Person {
* String firstname;
* Address address;
* }
*
* db.user.createIndex({"$**": 1, "firstname": 1}, {"wildcardProjection": {"address.zip": 0}})
* </pre>
*
* @author Julia Lee
* @author Marcin Grzejszczak
* @since 4.4
*/
@Target({ ElementType.TYPE })
@Documented
@CompoundIndex
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(CompoundWildcardIndexes.class)
public @interface CompoundWildcardIndex {

/**
* Represents wildcard for all fields starting from the root od the document.
*/
String ALL_FIELDS = "$**";

/**
* The name of the sub-field to which a wildcard index is applied. The default value scans all fields.
*
* @return {@link #ALL_FIELDS} by default.
*/
String wildcardFieldName() default ALL_FIELDS;

/**
* Explicitly specify sub-fields to be in-/excluded as a {@link org.bson.Document#parse(String) parsable} String.
* <br />
* <strong>NOTE:</strong> Can only be applied on when wildcard term is {@link #ALL_FIELDS}
*
* @return empty by default.
*/
String wildcardProjection() default "";

/**
* Definition of non-wildcard index(es) in JSON format, wherein the keys are the fields to be indexed and the values
* define the index direction (1 for ascending, -1 for descending). <br />
*
* <pre class="code">
* &#64;Document
* &#64;CompoundWildcardIndexed(wildcardProjection = "{ 'address.zip' : 0 }", fields = "{'firstname': 1}")
* class Person {
* String firstname;
* Address address;
* }
* </pre>
*
* @return empty String by default.
*/
@AliasFor(annotation = CompoundIndex.class, attribute = "def")
String fields();

/**
* Index name either as plain value or as {@link org.springframework.expression.spel.standard.SpelExpression template
* expression}. <br />
*
* @return empty by default.
*/
@AliasFor(annotation = CompoundIndex.class, attribute = "name")
String name() default "";

/**
* If set to {@literal true} then MongoDB will ignore the given index name and instead generate a new name. Defaults
* to {@literal false}.
*
* @return {@literal false} by default
*/
@AliasFor(annotation = CompoundIndex.class, attribute = "useGeneratedName")
boolean useGeneratedName() default false;

/**
* Only index the documents in a collection that meet a specified {@link IndexFilter filter expression}. <br />
*
* @return empty by default.
*/
@AliasFor(annotation = CompoundIndex.class, attribute = "partialFilter")
String partialFilter() default "";

/**
* Defines the collation to apply.
*
* @return an empty {@link String} by default.
*/
@AliasFor(annotation = CompoundIndex.class, attribute = "collation")
String collation() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.index;

import org.bson.Document;
import org.springframework.util.Assert;

/**
* {@link CompoundWildcardIndexDefinition} is a specific {@link Index} that includes one {@link WildcardIndex} and
* one or more non-wildcard fields.
*
* @author Julia Lee
* @since 4.4
*/
public class CompoundWildcardIndexDefinition extends WildcardIndex {

private final Document indexKeys;

/**
* Creates a new {@link CompoundWildcardIndexDefinition} for the given {@literal wildcardPath} and {@literal keys}.
* If {@literal wildcardPath} is empty, the wildcard index will apply to the root entity, using {@code $**}.
* <br />
*
* @param wildcardPath can be a {@literal empty} {@link String}.
*/
public CompoundWildcardIndexDefinition(String wildcardPath, Document indexKeys) {

super(wildcardPath);
this.indexKeys = indexKeys;
}

@Override
public Document getIndexKeys() {

Document document = new Document();
document.putAll(indexKeys);
document.putAll(super.getIndexKeys());
return document;
}

@Override
public Document getIndexOptions() {

Document options = super.getIndexOptions();
return options;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.index;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Container annotation that allows to collect multiple {@link CompoundWildcardIndex} annotations.
* <p>
* Can be used natively, declaring several nested {@link CompoundWildcardIndex} annotations. Can also be used in conjunction
* with Java 8's support for <em>repeatable annotations</em>, where {@link CompoundWildcardIndex} can simply be declared several
* times on the same {@linkplain ElementType#TYPE type}, implicitly generating this container annotation.
*
* @author Marcin Grzejszczak
* @since 4.4
*/
@Target({ ElementType.TYPE })
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CompoundWildcardIndexes {

CompoundWildcardIndex[] value();

}
Loading