All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.spring.data.KiwiMongoIndexes Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.spring.data;

import lombok.experimental.UtilityClass;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.IndexDefinition;

import java.util.stream.Stream;

/**
 * Utilities related to Mongo indexes.
 */
@UtilityClass
public class KiwiMongoIndexes {

    /**
     * Ensure indexes exist for the given collection (defined by {@code clazz}), sort direction, and properties.
     *
     * @param mongoTemplate the MongoTemplate instance
     * @param clazz         the entity/document class representing the mapped MongoDB collection
     * @param direction     the sort direction for all specified properties
     * @param properties    the properties for which to add an index
     * @see org.springframework.data.mongodb.core.index.IndexOperations#ensureIndex(IndexDefinition)
     */
    public static void ensureIndices(MongoTemplate mongoTemplate,
                                     Class clazz,
                                     Sort.Direction direction,
                                     String... properties) {

        Stream.of(properties).forEach(prop ->
                mongoTemplate.indexOps(clazz).ensureIndex(new Index(prop, direction)));
    }

    /**
     * Ensure indexes exist for the given collection, sort direction, and properties.
     *
     * @param mongoTemplate  the MongoTemplate instance
     * @param collectionName the MongoDB collection name
     * @param direction      the sort direction for all specified properties
     * @param properties     the properties for which to add an index
     * @see org.springframework.data.mongodb.core.index.IndexOperations#ensureIndex(IndexDefinition)
     */
    public static void ensureIndices(MongoTemplate mongoTemplate,
                                     String collectionName,
                                     Sort.Direction direction,
                                     String... properties) {

        Stream.of(properties).forEach(prop ->
                mongoTemplate.indexOps(collectionName).ensureIndex(new Index(prop, direction)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy