Search in sources :

Example 16 with AttributeReference

use of com.torodb.core.language.AttributeReference 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 17 with AttributeReference

use of com.torodb.core.language.AttributeReference in project torodb by torodb.

the class IncrementUpdateActionTest method testNullAttribute1.

@Test(expected = UserException.class)
public void testNullAttribute1() throws UpdateException {
    UpdatedToroDocumentBuilder builder = UpdatedToroDocumentBuilder.create();
    builder.newObject("f1").newArray("f2").setValue(3, KvInteger.of(3));
    new IncrementUpdateAction(Lists.<AttributeReference>newArrayList(new AttributeReference(Lists.<AttributeReference.Key<?>>newArrayList(new AttributeReference.ObjectKey("f1"), new AttributeReference.ObjectKey("f2"), new AttributeReference.ArrayKey(2)))), KvInteger.of(1)).apply(builder);
    ;
}
Also used : AttributeReference(com.torodb.core.language.AttributeReference) Test(org.junit.Test)

Example 18 with AttributeReference

use of com.torodb.core.language.AttributeReference in project torodb by torodb.

the class UpdateActionTranslator method translateSetField.

private static void translateSetField(CompositeUpdateAction.Builder builder, BsonDocument argument) {
    for (Entry<?> entry : argument) {
        Collection<AttributeReference> attRefs = parseAttributeReference(entry.getKey());
        KvValue<?> translatedValue = MongoWpConverter.translate(entry.getValue());
        builder.add(new SetFieldUpdateAction(attRefs, translatedValue), false);
    }
}
Also used : AttributeReference(com.torodb.core.language.AttributeReference) SetFieldUpdateAction(com.torodb.mongodb.language.update.SetFieldUpdateAction)

Example 19 with AttributeReference

use of com.torodb.core.language.AttributeReference in project torodb by torodb.

the class UpdateActionTranslator method translateUnsetField.

private static void translateUnsetField(CompositeUpdateAction.Builder builder, BsonDocument argument) {
    for (Entry<?> entry : argument) {
        Collection<AttributeReference> attRefs = parseAttributeReference(entry.getKey());
        builder.add(new UnsetFieldUpdateAction(attRefs), false);
    }
}
Also used : AttributeReference(com.torodb.core.language.AttributeReference) UnsetFieldUpdateAction(com.torodb.mongodb.language.update.UnsetFieldUpdateAction)

Example 20 with AttributeReference

use of com.torodb.core.language.AttributeReference in project torodb by torodb.

the class ToroIndexConverter method to.

@Override
public String to(NamedToroIndex userObject) {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    objectBuilder.add(NAME_KEY, userObject.getName());
    if (userObject.isUnique()) {
        objectBuilder.add(UNIQUE_KEY, true);
    }
    JsonArrayBuilder attsBuilder = Json.createArrayBuilder();
    JsonArrayBuilder descBuilder = Json.createArrayBuilder();
    int attPosition = 0;
    boolean hasDescending = false;
    for (Map.Entry<AttributeReference, IndexType> entry : userObject.getAttributes().entrySet()) {
        attsBuilder.add(entry.getKey().toString());
        if (IndexType.desc.equals(entry.getValue())) {
            descBuilder.add(attPosition);
            hasDescending = true;
        }
        attPosition++;
    }
    objectBuilder.add(ATTS_KEY, attsBuilder);
    if (hasDescending) {
        objectBuilder.add(DESCENDING, descBuilder);
    }
    StringWriter stringWriter = new StringWriter(200);
    JsonWriter jsonWriter = Json.createWriter(stringWriter);
    jsonWriter.writeObject(objectBuilder.build());
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) AttributeReference(com.torodb.core.language.AttributeReference) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) IndexType(com.torodb.core.model.IndexedAttributes.IndexType) Map(java.util.Map) JsonWriter(javax.json.JsonWriter)

Aggregations

AttributeReference (com.torodb.core.language.AttributeReference)21 IndexFieldInfo (com.torodb.torod.IndexFieldInfo)6 UserException (com.torodb.core.exceptions.user.UserException)5 ObjectKey (com.torodb.core.language.AttributeReference.ObjectKey)5 UpdateException (com.torodb.core.exceptions.user.UpdateException)4 FieldIndexOrdering (com.torodb.core.transaction.metainf.FieldIndexOrdering)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 CommandFailed (com.eightkdata.mongowp.exceptions.CommandFailed)2 ImmutableList (com.google.common.collect.ImmutableList)2 UnsupportedCompoundIndexException (com.torodb.core.exceptions.user.UnsupportedCompoundIndexException)2 UnsupportedUniqueIndexException (com.torodb.core.exceptions.user.UnsupportedUniqueIndexException)2 KvDocument (com.torodb.kvdocument.values.KvDocument)2 KvNumeric (com.torodb.kvdocument.values.KvNumeric)2 IndexOptions (com.torodb.mongodb.commands.pojos.index.IndexOptions)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 JsonArrayBuilder (javax.json.JsonArrayBuilder)2