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

com.coditory.sherlock.mongo.MongoCollectionInitializer Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package com.coditory.sherlock.mongo;

import com.mongodb.client.MongoCollection;
import org.bson.Document;

import java.util.concurrent.atomic.AtomicBoolean;

import static com.coditory.sherlock.Preconditions.expectNonNull;
import static com.coditory.sherlock.mongo.MongoDistributedLock.INDEX;
import static com.coditory.sherlock.mongo.MongoDistributedLock.INDEX_OPTIONS;

class MongoCollectionInitializer {
    private final MongoCollection collection;
    private final AtomicBoolean indexesCreated = new AtomicBoolean(false);

    MongoCollectionInitializer(MongoCollection collection) {
        expectNonNull(collection, "collection");
        this.collection = collection;
    }

    MongoCollection getInitializedCollection() {
        boolean shouldCreateIndexes = indexesCreated.compareAndSet(false, true);
        if (!shouldCreateIndexes) {
            return collection;
        }
        collection.createIndex(INDEX, INDEX_OPTIONS);
        return collection;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy