Index.java

/*
 * Copyright 2010-present 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.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.bson.Document;
import org.jspecify.annotations.Nullable;
import org.springframework.data.core.TypedPropertyPath;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.index.IndexOptions.Unique;
import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.lang.Contract;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * @author Oliver Gierke
 * @author Christoph Strobl
 * @author Mark Paluch
 */
@SuppressWarnings("deprecation")
public class Index implements IndexDefinition {

	private final Map<String, Direction> fieldSpec = new LinkedHashMap<String, Direction>();
	private @Nullable String name;
	private boolean sparse = false;
	private boolean background = false;
	private final IndexOptions options = IndexOptions.none();
	private Optional<IndexFilter> filter = Optional.empty();
	private Optional<Collation> collation = Optional.empty();

	public Index() {}

	public Index(String key, Direction direction) {
		fieldSpec.put(key, direction);
	}

	/**
	 * Create a new {@link Index} definition for the given {@link TypedPropertyPath property} and {@link Direction}
	 * 
	 * @param property must not be {@literal null}.
	 * @param direction index order
	 * @param <T> Property owing root type
	 * @param <P> Target property reachable via path.
	 * @since 5.1
	 */
	public <T, P> Index(TypedPropertyPath<T, P> property, Direction direction) {
		this(property.toDotPath(), direction);
	}

	@Contract("_, _ -> this")
	public Index on(String key, Direction direction) {
		fieldSpec.put(key, direction);
		return this;
	}

	/**
	 * Append the {@link TypedPropertyPath path} to the target property to the index definition.
	 *
	 * @param property the property to include.
	 * @param direction the direction to order values within the index.
	 * @return this.
	 * @param <T> Property owing root type
	 * @param <P> Target property reachable via path.
	 * @since 5.1
	 */
	@Contract("_, _ -> this")
	public <T, P> Index on(TypedPropertyPath<T, P> property, Direction direction) {
		return on(TypedPropertyPath.of(property).toDotPath(), direction);
	}

	@Contract("_ -> this")
	public Index named(String name) {
		this.name = name;
		return this;
	}

	/**
	 * Reject all documents that contain a duplicate value for the indexed field.
	 *
	 * @return this.
	 * @see <a href=
	 *      "https://docs.mongodb.org/manual/core/index-unique/">https://docs.mongodb.org/manual/core/index-unique/</a>
	 */
	@Contract("-> this")
	public Index unique() {

		this.options.setUnique(Unique.YES);
		return this;
	}

	/**
	 * Skip over any document that is missing the indexed field.
	 *
	 * @return this.
	 * @see <a href=
	 *      "https://docs.mongodb.org/manual/core/index-sparse/">https://docs.mongodb.org/manual/core/index-sparse/</a>
	 */
	@Contract("-> this")
	public Index sparse() {
		this.sparse = true;
		return this;
	}

	/**
	 * Build the index in background (non blocking).
	 * <p>
	 * <strong>NOTE:</strong> Since MongoDB 4.2 the background flag is ignored by the server if set.
	 *
	 * @return this.
	 * @since 1.5
	 * @deprecated since 5.0 for removal without replacement.
	 */
	@Deprecated(since = "5.0", forRemoval = true)
	@Contract("-> this")
	public Index background() {

		this.background = true;
		return this;
	}

	/**
	 * Hidden indexes are not visible to the query planner and cannot be used to support a query.
	 *
	 * @return this.
	 * @see <a href=
	 *      "https://www.mongodb.com/docs/manual/core/index-hidden/">https://www.mongodb.com/docs/manual/core/index-hidden/</a>
	 * @since 4.1
	 */
	@Contract("-> this")
	public Index hidden() {

		options.setHidden(true);
		return this;
	}

	/**
	 * Specifies TTL in seconds.
	 *
	 * @param value
	 * @return this.
	 * @since 1.5
	 */
	@Contract("_ -> this")
	public Index expire(long value) {
		return expire(value, TimeUnit.SECONDS);
	}

	/**
	 * Specifies the TTL.
	 *
	 * @param timeout must not be {@literal null}.
	 * @return this.
	 * @throws IllegalArgumentException if given {@literal timeout} is {@literal null}.
	 * @since 2.2
	 */
	@Contract("_ -> this")
	public Index expire(Duration timeout) {

		Assert.notNull(timeout, "Timeout must not be null");
		return expire(timeout.getSeconds());
	}

	/**
	 * Specifies TTL with given {@link TimeUnit}.
	 *
	 * @param value
	 * @param unit must not be {@literal null}.
	 * @return this.
	 * @since 1.5
	 */
	@Contract("_, _ -> this")
	public Index expire(long value, TimeUnit unit) {

		Assert.notNull(unit, "TimeUnit for expiration must not be null");
		options.setExpire(Duration.ofSeconds(unit.toSeconds(value)));
		return this;
	}

	/**
	 * Only index the documents in a collection that meet a specified {@link IndexFilter filter expression}.
	 *
	 * @param filter can be {@literal null}.
	 * @return this.
	 * @see <a href=
	 *      "https://docs.mongodb.com/manual/core/index-partial/">https://docs.mongodb.com/manual/core/index-partial/</a>
	 * @since 1.10
	 */
	@Contract("_ -> this")
	public Index partial(@Nullable IndexFilter filter) {

		this.filter = Optional.ofNullable(filter);
		return this;
	}

	/**
	 * Set the {@link Collation} to specify language-specific rules for string comparison, such as rules for lettercase
	 * and accent marks.<br />
	 * <strong>NOTE:</strong> Only queries using the same {@link Collation} as the {@link Index} actually make use of the
	 * index.
	 *
	 * @param collation can be {@literal null}.
	 * @return this.
	 * @since 2.0
	 */
	@Contract("_ -> this")
	public Index collation(@Nullable Collation collation) {

		this.collation = Optional.ofNullable(collation);
		return this;
	}

	public Document getIndexKeys() {

		Document document = new Document();

		for (Entry<String, Direction> entry : fieldSpec.entrySet()) {
			document.put(entry.getKey(), Direction.ASC.equals(entry.getValue()) ? 1 : -1);
		}

		return document;
	}

	public Document getIndexOptions() {

		Document document = new Document();
		if (StringUtils.hasText(name)) {
			document.put("name", name);
		}
		if (sparse) {
			document.put("sparse", true);
		}
		if (background) {
			document.put("background", true);
		}
		document.putAll(options.toDocument());

		filter.ifPresent(val -> document.put("partialFilterExpression", val.getFilterObject()));
		collation.ifPresent(val -> document.append("collation", val.toDocument()));

		return document;
	}

	@Override
	public String toString() {
		return String.format("Index: %s - Options: %s", getIndexKeys(), getIndexOptions());
	}
}