Search in sources :

Example 6 with IndexFieldInfo

use of com.torodb.torod.IndexFieldInfo in project torodb by torodb.

the class DropIndexesImplementation method indexFieldsMatchKeys.

private boolean indexFieldsMatchKeys(IndexInfo index, List<IndexOptions.Key> keys) {
    if (index.getFields().size() != keys.size()) {
        return false;
    }
    Iterator<IndexFieldInfo> fieldsIterator = index.getFields().iterator();
    Iterator<IndexOptions.Key> keysIterator = keys.iterator();
    while (fieldsIterator.hasNext()) {
        IndexFieldInfo field = fieldsIterator.next();
        IndexOptions.Key key = keysIterator.next();
        if ((field.isAscending() && key.getType() != KnownType.asc.getIndexType()) || (!field.isAscending() && key.getType() != KnownType.desc.getIndexType()) || (field.getAttributeReference().getKeys().size() != key.getKeys().size())) {
            return false;
        }
        Iterator<AttributeReference.Key<?>> fieldKeysIterator = field.getAttributeReference().getKeys().iterator();
        Iterator<String> keyKeysIterator = key.getKeys().iterator();
        while (fieldKeysIterator.hasNext()) {
            AttributeReference.Key<?> fieldKey = fieldKeysIterator.next();
            String keyKey = keyKeysIterator.next();
            if (!fieldKey.toString().equals(keyKey)) {
                return false;
            }
        }
    }
    return true;
}
Also used : IndexOptions(com.torodb.mongodb.commands.pojos.index.IndexOptions) AttributeReference(com.torodb.core.language.AttributeReference) IndexFieldInfo(com.torodb.torod.IndexFieldInfo)

Example 7 with IndexFieldInfo

use of com.torodb.torod.IndexFieldInfo in project torodb by torodb.

the class CreateIndexesImplementation method apply.

@Override
public Status<CreateIndexesResult> apply(Request req, Command<? super CreateIndexesArgument, ? super CreateIndexesResult> command, CreateIndexesArgument arg, WriteMongodTransaction context) {
    int indexesBefore = (int) context.getTorodTransaction().getIndexesInfo(req.getDatabase(), arg.getCollection()).count();
    int indexesAfter = indexesBefore;
    try {
        boolean existsCollection = context.getTorodTransaction().existsCollection(req.getDatabase(), arg.getCollection());
        if (!existsCollection) {
            context.getTorodTransaction().createIndex(req.getDatabase(), arg.getCollection(), Constants.ID_INDEX, ImmutableList.<IndexFieldInfo>of(new IndexFieldInfo(new AttributeReference(Arrays.asList(new Key[] { new ObjectKey(Constants.ID) })), FieldIndexOrdering.ASC.isAscending())), true);
        }
        boolean createdCollectionAutomatically = !existsCollection;
        for (IndexOptions indexOptions : arg.getIndexesToCreate()) {
            if (indexOptions.getKeys().size() < 1) {
                return Status.from(ErrorCode.CANNOT_CREATE_INDEX, "Index keys cannot be empty.");
            }
            if (indexOptions.isBackground()) {
                throw new CommandFailed("createIndexes", "Building index in background is not supported right now");
            }
            if (indexOptions.isSparse()) {
                throw new CommandFailed("createIndexes", "Sparse index are not supported right now");
            }
            List<IndexFieldInfo> fields = new ArrayList<>(indexOptions.getKeys().size());
            for (IndexOptions.Key indexKey : indexOptions.getKeys()) {
                AttributeReference.Builder attRefBuilder = new AttributeReference.Builder();
                for (String key : indexKey.getKeys()) {
                    attRefBuilder.addObjectKey(key);
                }
                IndexType indexType = indexKey.getType();
                if (!KnownType.contains(indexType)) {
                    return Status.from(ErrorCode.CANNOT_CREATE_INDEX, "bad index key pattern: Unknown index plugin '" + indexKey.getType().getName() + "'");
                }
                Optional<FieldIndexOrdering> ordering = indexType.accept(filedIndexOrderingConverterVisitor, null);
                if (!ordering.isPresent()) {
                    throw new CommandFailed("createIndexes", "Index of type " + indexType.getName() + " is not supported right now");
                }
                fields.add(new IndexFieldInfo(attRefBuilder.build(), ordering.get().isAscending()));
            }
            if (context.getTorodTransaction().createIndex(req.getDatabase(), arg.getCollection(), indexOptions.getName(), fields, indexOptions.isUnique())) {
                indexesAfter++;
            }
        }
        String note = null;
        if (indexesAfter == indexesBefore) {
            note = "all indexes already exist";
        }
        return Status.ok(new CreateIndexesResult(indexesBefore, indexesAfter, note, createdCollectionAutomatically));
    } catch (UserException ex) {
        return Status.from(ErrorCode.COMMAND_FAILED, ex.getLocalizedMessage());
    } catch (CommandFailed ex) {
        return Status.from(ex);
    }
}
Also used : IndexOptions(com.torodb.mongodb.commands.pojos.index.IndexOptions) AttributeReference(com.torodb.core.language.AttributeReference) ObjectKey(com.torodb.core.language.AttributeReference.ObjectKey) ArrayList(java.util.ArrayList) CreateIndexesResult(com.torodb.mongodb.commands.signatures.admin.CreateIndexesCommand.CreateIndexesResult) CommandFailed(com.eightkdata.mongowp.exceptions.CommandFailed) IndexFieldInfo(com.torodb.torod.IndexFieldInfo) UserException(com.torodb.core.exceptions.user.UserException) AscIndexType(com.torodb.mongodb.commands.pojos.index.type.AscIndexType) DescIndexType(com.torodb.mongodb.commands.pojos.index.type.DescIndexType) IndexType(com.torodb.mongodb.commands.pojos.index.type.IndexType) FieldIndexOrdering(com.torodb.core.transaction.metainf.FieldIndexOrdering)

Example 8 with IndexFieldInfo

use of com.torodb.torod.IndexFieldInfo in project torodb by torodb.

the class DropIndexesReplImpl method indexFieldsMatchKeys.

private boolean indexFieldsMatchKeys(IndexInfo index, List<IndexOptions.Key> keys) {
    if (index.getFields().size() != keys.size()) {
        return false;
    }
    Iterator<IndexFieldInfo> fieldsIterator = index.getFields().iterator();
    Iterator<IndexOptions.Key> keysIterator = keys.iterator();
    while (fieldsIterator.hasNext()) {
        IndexFieldInfo field = fieldsIterator.next();
        IndexOptions.Key key = keysIterator.next();
        if ((field.isAscending() && key.getType() != KnownType.asc.getIndexType()) || (!field.isAscending() && key.getType() != KnownType.desc.getIndexType()) || (field.getAttributeReference().getKeys().size() != key.getKeys().size())) {
            return false;
        }
        Iterator<AttributeReference.Key<?>> fieldKeysIterator = field.getAttributeReference().getKeys().iterator();
        Iterator<String> keyKeysIterator = key.getKeys().iterator();
        while (fieldKeysIterator.hasNext()) {
            AttributeReference.Key<?> fieldKey = fieldKeysIterator.next();
            String keyKey = keyKeysIterator.next();
            if (!fieldKey.toString().equals(keyKey)) {
                return false;
            }
        }
    }
    return true;
}
Also used : IndexOptions(com.torodb.mongodb.commands.pojos.index.IndexOptions) AttributeReference(com.torodb.core.language.AttributeReference) IndexFieldInfo(com.torodb.torod.IndexFieldInfo)

Aggregations

AttributeReference (com.torodb.core.language.AttributeReference)8 IndexFieldInfo (com.torodb.torod.IndexFieldInfo)8 UserException (com.torodb.core.exceptions.user.UserException)5 ObjectKey (com.torodb.core.language.AttributeReference.ObjectKey)5 FieldIndexOrdering (com.torodb.core.transaction.metainf.FieldIndexOrdering)4 IndexOptions (com.torodb.mongodb.commands.pojos.index.IndexOptions)4 ArrayList (java.util.ArrayList)4 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)2 UnsupportedCompoundIndexException (com.torodb.core.exceptions.user.UnsupportedCompoundIndexException)2 UnsupportedUniqueIndexException (com.torodb.core.exceptions.user.UnsupportedUniqueIndexException)2 KvDocument (com.torodb.kvdocument.values.KvDocument)2 AscIndexType (com.torodb.mongodb.commands.pojos.index.type.AscIndexType)2 DescIndexType (com.torodb.mongodb.commands.pojos.index.type.DescIndexType)2 IndexType (com.torodb.mongodb.commands.pojos.index.type.IndexType)2 CreateIndexesResult (com.torodb.mongodb.commands.signatures.admin.CreateIndexesCommand.CreateIndexesResult)2 ErrorCode (com.eightkdata.mongowp.ErrorCode)1 Status (com.eightkdata.mongowp.Status)1 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)1 Command (com.eightkdata.mongowp.server.api.Command)1 Request (com.eightkdata.mongowp.server.api.Request)1