Example usage for com.mongodb.client.model IndexModel IndexModel

List of usage examples for com.mongodb.client.model IndexModel IndexModel

Introduction

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

Prototype

public IndexModel(final Bson keys) 

Source Link

Document

Construct an instance with the given keys.

Usage

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

License:Open Source License

/**
 * Utility method which can be used to create an IndexModel.
 *
 * @param fieldName   the attribute on which the index is to be created.
 * @param sortOrder   the sort order of the index to be created.
 * @param indexOption json string of the options of the index to be created.
 * @return IndexModel.// w w  w.  j a  v  a 2s . c om
 */
private static IndexModel createIndexModel(String fieldName, Integer sortOrder, String indexOption) {
    Document indexDocument = new Document(fieldName, sortOrder);
    if (indexOption == null) {
        return new IndexModel(indexDocument);
    } else {
        IndexOptions indexOptions = new IndexOptions();
        Document indexOptionDocument;
        try {
            indexOptionDocument = Document.parse(indexOption);
            for (Map.Entry<String, Object> indexEntry : indexOptionDocument.entrySet()) {
                Object value = indexEntry.getValue();
                switch (indexEntry.getKey()) {
                case "unique":
                    indexOptions.unique(Boolean.parseBoolean(value.toString()));
                    break;
                case "background":
                    indexOptions.background(Boolean.parseBoolean(value.toString()));
                    break;
                case "name":
                    indexOptions.name(value.toString());
                    break;
                case "sparse":
                    indexOptions.sparse(Boolean.parseBoolean(value.toString()));
                    break;
                case "expireAfterSeconds":
                    indexOptions.expireAfter(Long.parseLong(value.toString()), TimeUnit.SECONDS);
                    break;
                case "version":
                    indexOptions.version(Integer.parseInt(value.toString()));
                    break;
                case "weights":
                    indexOptions.weights((Bson) value);
                    break;
                case "languageOverride":
                    indexOptions.languageOverride(value.toString());
                    break;
                case "defaultLanguage":
                    indexOptions.defaultLanguage(value.toString());
                    break;
                case "textVersion":
                    indexOptions.textVersion(Integer.parseInt(value.toString()));
                    break;
                case "sphereVersion":
                    indexOptions.sphereVersion(Integer.parseInt(value.toString()));
                    break;
                case "bits":
                    indexOptions.bits(Integer.parseInt(value.toString()));
                    break;
                case "min":
                    indexOptions.min(Double.parseDouble(value.toString()));
                    break;
                case "max":
                    indexOptions.max(Double.parseDouble(value.toString()));
                    break;
                case "bucketSize":
                    indexOptions.bucketSize(Double.parseDouble(value.toString()));
                    break;
                case "partialFilterExpression":
                    indexOptions.partialFilterExpression((Bson) value);
                    break;
                case "collation":
                    DBObject collationOptions = (DBObject) value;
                    Collation.Builder builder = Collation.builder();
                    for (String collationKey : collationOptions.keySet()) {
                        String collationObj = value.toString();
                        switch (collationKey) {
                        case "locale":
                            builder.locale(collationObj);
                            break;
                        case "caseLevel":
                            builder.caseLevel(Boolean.parseBoolean(collationObj));
                            break;
                        case "caseFirst":
                            builder.collationCaseFirst(CollationCaseFirst.fromString(collationObj));
                            break;
                        case "strength":
                            builder.collationStrength(CollationStrength.valueOf(collationObj));
                            break;
                        case "numericOrdering":
                            builder.numericOrdering(Boolean.parseBoolean(collationObj));
                            break;
                        case "normalization":
                            builder.normalization(Boolean.parseBoolean(collationObj));
                            break;
                        case "backwards":
                            builder.backwards(Boolean.parseBoolean(collationObj));
                            break;
                        case "alternate":
                            builder.collationAlternate(CollationAlternate.fromString(collationObj));
                            break;
                        case "maxVariable":
                            builder.collationMaxVariable(CollationMaxVariable.fromString(collationObj));
                            break;
                        default:
                            log.warn("Annotation 'IndexBy' for the field '" + fieldName + "' contains "
                                    + "unknown 'Collation' Option key : '" + collationKey + "'. Please "
                                    + "check your query and try again.");
                            break;
                        }
                    }
                    if (builder.build().getLocale() != null) {
                        indexOptions.collation(builder.build());
                    } else {
                        throw new MongoTableException("Annotation 'IndexBy' for the field '" + fieldName + "'"
                                + " do not contain option for locale. Please check your query and try again.");
                    }
                    break;
                case "storageEngine":
                    indexOptions.storageEngine((Bson) value);
                    break;
                default:
                    log.warn("Annotation 'IndexBy' for the field '" + fieldName + "' contains unknown option "
                            + "key : '" + indexEntry.getKey() + "'. Please check your query and try again.");
                    break;
                }
            }
        } catch (JsonParseException | NumberFormatException e) {
            throw new MongoTableException("Annotation 'IndexBy' for the field '" + fieldName + "' contains "
                    + "illegal value(s) for index option. Please check your query and try again.", e);
        }
        return new IndexModel(indexDocument, indexOptions);
    }
}