Example usage for com.mongodb.client.model IndexOptions isBackground

List of usage examples for com.mongodb.client.model IndexOptions isBackground

Introduction

In this page you can find the example usage for com.mongodb.client.model IndexOptions isBackground.

Prototype

public boolean isBackground() 

Source Link

Document

Create the index in the background

Usage

From source file:org.wso2.extension.siddhi.store.mongodb.util.MongoTableUtils.java

License:Open Source License

/**
 * Utility method which can be used to check if the existing indices contain the expected indices
 * defined by the annotation 'PrimaryKey' and 'IndexBy' and log a warning when indices differs.
 *
 * @param existingIndices List of indices that the collection contains.
 * @param expectedIndices List of indices that are defined by the annotations.
 *//*  www  .  j  a  va2 s  . c  om*/
public static void checkExistingIndices(List<IndexModel> expectedIndices,
        MongoCursor<Document> existingIndices) {
    Map<String, Object> indexOptionsMap = new HashMap<>();
    List<Document> expectedIndexDocuments = expectedIndices.stream().map(expectedIndex -> {
        IndexOptions expectedIndexOptions = expectedIndex.getOptions();
        indexOptionsMap.put("key", expectedIndex.getKeys());
        // Default value for name of the index
        if (expectedIndexOptions.getName() == null) {
            StringBuilder indexName = new StringBuilder();
            ((Document) expectedIndex.getKeys())
                    .forEach((key, value) -> indexName.append("_").append(key).append("_").append(value));
            indexName.deleteCharAt(0);
            indexOptionsMap.put("name", indexName.toString());
        } else {
            indexOptionsMap.put("name", expectedIndexOptions.getName());
        }
        // Default value for the version
        if (expectedIndexOptions.getVersion() == null) {
            indexOptionsMap.put("v", 2);
        } else {
            indexOptionsMap.put("v", expectedIndexOptions.getVersion());
        }
        indexOptionsMap.put("unique", expectedIndexOptions.isUnique());
        indexOptionsMap.put("background", expectedIndexOptions.isBackground());
        indexOptionsMap.put("sparse", expectedIndexOptions.isSparse());
        indexOptionsMap.put("expireAfterSeconds", expectedIndexOptions.getExpireAfter(TimeUnit.SECONDS));
        indexOptionsMap.put("weights", expectedIndexOptions.getWeights());
        indexOptionsMap.put("languageOverride", expectedIndexOptions.getLanguageOverride());
        indexOptionsMap.put("defaultLanguage", expectedIndexOptions.getDefaultLanguage());
        indexOptionsMap.put("textVersion", expectedIndexOptions.getTextVersion());
        indexOptionsMap.put("sphereVersion", expectedIndexOptions.getSphereVersion());
        indexOptionsMap.put("textVersion", expectedIndexOptions.getTextVersion());
        indexOptionsMap.put("bits", expectedIndexOptions.getBits());
        indexOptionsMap.put("min", expectedIndexOptions.getMin());
        indexOptionsMap.put("max", expectedIndexOptions.getMax());
        indexOptionsMap.put("bucketSize", expectedIndexOptions.getBucketSize());
        indexOptionsMap.put("partialFilterExpression", expectedIndexOptions.getPartialFilterExpression());
        indexOptionsMap.put("collation", expectedIndexOptions.getCollation());
        indexOptionsMap.put("storageEngine", expectedIndexOptions.getStorageEngine());

        //Remove if Default Values - these would not be in the existingIndexDocument.
        indexOptionsMap.values().removeIf(Objects::isNull);
        indexOptionsMap.remove("unique", false);
        indexOptionsMap.remove("background", false);
        indexOptionsMap.remove("sparse", false);

        return new Document(indexOptionsMap);
    }).collect(Collectors.toList());

    List<Document> existingIndexDocuments = new ArrayList<>();
    existingIndices.forEachRemaining(existingIndex -> {
        existingIndex.remove("ns");
        existingIndexDocuments.add(existingIndex);
    });

    if (!existingIndexDocuments.containsAll(expectedIndexDocuments)) {
        log.warn("Existing indices differs from the expected indices defined by the Annotations 'PrimaryKey' "
                + "and 'IndexBy'.\nExisting Indices '" + existingIndexDocuments.toString() + "'.\n"
                + "Expected Indices '" + expectedIndexDocuments.toString() + "'");
    }
}