Search in sources :

Example 6 with AttributeReference

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

the class IncrementUpdateActionTest method testIllegalPath.

@Test(expected = UserException.class)
public void testIllegalPath() 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.ArrayKey(2), new AttributeReference.ArrayKey(2)))), KvInteger.of(1)).apply(builder);
    ;
}
Also used : AttributeReference(com.torodb.core.language.AttributeReference) Test(org.junit.Test)

Example 7 with AttributeReference

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

the class CreateCollectionReplImpl method apply.

@Override
public Status<Empty> apply(Request req, Command<? super CreateCollectionArgument, ? super Empty> command, CreateCollectionArgument arg, SharedWriteTorodTransaction trans) {
    try {
        LOGGER.info("Creating collection {}.{}", req.getDatabase(), arg.getCollection());
        if (arg.getOptions().isCapped()) {
            LOGGER.info("Ignoring capped flag for collection {}.{}", req.getDatabase(), arg.getCollection());
        }
        if (!trans.existsCollection(req.getDatabase(), arg.getCollection())) {
            trans.createIndex(req.getDatabase(), arg.getCollection(), Constants.ID_INDEX, ImmutableList.of(new IndexFieldInfo(new AttributeReference(Arrays.asList(new Key[] { new ObjectKey(Constants.ID) })), FieldIndexOrdering.ASC.isAscending())), true);
        }
        trans.createCollection(req.getDatabase(), arg.getCollection());
    } catch (UserException ex) {
        reportErrorIgnored(LOGGER, command, ex);
    }
    return Status.ok();
}
Also used : AttributeReference(com.torodb.core.language.AttributeReference) ObjectKey(com.torodb.core.language.AttributeReference.ObjectKey) IndexFieldInfo(com.torodb.torod.IndexFieldInfo) UserException(com.torodb.core.exceptions.user.UserException)

Example 8 with AttributeReference

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

the class SqlWriteTorodTransaction method createIndex.

@Override
public boolean createIndex(String dbName, String colName, String indexName, List<IndexFieldInfo> fields, boolean unique) throws UserException {
    if (fields.size() > 1) {
        throw new UnsupportedCompoundIndexException(dbName, colName, indexName);
    }
    MutableMetaDatabase metaDb = getOrCreateMetaDatabase(dbName);
    MutableMetaCollection metaColl = getOrCreateMetaCollection(metaDb, colName);
    List<Tuple3<TableRef, String, FieldIndexOrdering>> indexFieldDefs = new ArrayList<>(fields.size());
    for (IndexFieldInfo field : fields) {
        AttributeReference attRef = field.getAttributeReference();
        FieldIndexOrdering ordering = field.isAscending() ? FieldIndexOrdering.ASC : FieldIndexOrdering.DESC;
        TableRef tableRef = extractTableRef(attRef);
        String lastKey = extractKeyName(attRef.getKeys().get(attRef.getKeys().size() - 1));
        indexFieldDefs.add(new Tuple3<>(tableRef, lastKey, ordering));
    }
    if (unique) {
        TableRef anyIndexTableRef = indexFieldDefs.stream().findAny().get().v1();
        boolean isUniqueIndexWithMutlipleTableRefs = indexFieldDefs.stream().anyMatch(t -> !t.v1().equals(anyIndexTableRef));
        if (isUniqueIndexWithMutlipleTableRefs) {
            throw new UnsupportedUniqueIndexException(dbName, colName, indexName);
        }
    }
    boolean indexExists = metaColl.streamContainedMetaIndexes().anyMatch(index -> index.getName().equals(indexName) || (index.isUnique() == unique && index.size() == indexFieldDefs.size() && Seq.seq(index.iteratorFields()).allMatch(indexField -> {
        Tuple3<TableRef, String, FieldIndexOrdering> indexFieldDef = indexFieldDefs.get(indexField.getPosition());
        return indexFieldDef != null && indexFieldDef.v1().equals(indexField.getTableRef()) && indexFieldDef.v2().equals(indexField.getName()) && indexFieldDef.v3() == indexField.getOrdering();
    })));
    if (!indexExists) {
        MutableMetaIndex metaIndex = metaColl.addMetaIndex(indexName, unique);
        for (Tuple3<TableRef, String, FieldIndexOrdering> indexFieldDef : indexFieldDefs) {
            metaIndex.addMetaIndexField(indexFieldDef.v1(), indexFieldDef.v2(), indexFieldDef.v3());
        }
        getInternalTransaction().getBackendTransaction().createIndex(metaDb, metaColl, metaIndex);
    }
    return !indexExists;
}
Also used : MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) AttributeReference(com.torodb.core.language.AttributeReference) ArrayList(java.util.ArrayList) UnsupportedUniqueIndexException(com.torodb.core.exceptions.user.UnsupportedUniqueIndexException) MutableMetaDatabase(com.torodb.core.transaction.metainf.MutableMetaDatabase) UnsupportedCompoundIndexException(com.torodb.core.exceptions.user.UnsupportedCompoundIndexException) Tuple3(org.jooq.lambda.tuple.Tuple3) MutableMetaIndex(com.torodb.core.transaction.metainf.MutableMetaIndex) IndexFieldInfo(com.torodb.torod.IndexFieldInfo) FieldIndexOrdering(com.torodb.core.transaction.metainf.FieldIndexOrdering) TableRef(com.torodb.core.TableRef)

Example 9 with AttributeReference

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

the class IndexedAttributes method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    for (AttributeReference attribute : attributes) {
        sb.append(attribute).append(' ');
        sb.append("(");
        sb.append(orderingInfo.get(attribute).name());
        sb.append(")");
        sb.append(", ");
    }
    sb.delete(sb.length() - 2, sb.length());
    return sb.toString();
}
Also used : AttributeReference(com.torodb.core.language.AttributeReference)

Example 10 with AttributeReference

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

the class ToroIndexConverter method from.

@Override
public NamedToroIndex from(String databaseObject) {
    JsonReader reader = Json.createReader(new StringReader(databaseObject));
    JsonObject object = reader.readObject();
    IndexedAttributes.Builder builder = new IndexedAttributes.Builder();
    JsonArray attsArray = object.getJsonArray(ATTS_KEY);
    Set<Integer> descendingAttPos;
    if (object.containsKey(DESCENDING)) {
        JsonArray descArray = object.getJsonArray(DESCENDING);
        descendingAttPos = Sets.newHashSetWithExpectedSize(descArray.size());
        for (int i = 0; i < descArray.size(); i++) {
            descendingAttPos.add(descArray.getInt(i));
        }
    } else {
        descendingAttPos = Collections.emptySet();
    }
    for (int i = 0; i < attsArray.size(); i++) {
        String att = attsArray.getString(i);
        AttributeReference attRef = parseAttRef(att);
        if (descendingAttPos.contains(i)) {
            builder.addAttribute(attRef, IndexType.desc);
        } else {
            builder.addAttribute(attRef, IndexType.asc);
        }
    }
    return new DefaultNamedToroIndex(object.getString(NAME_KEY), builder.build(), databaseName, collectionName, object.getBoolean(UNIQUE_KEY, false));
}
Also used : DefaultNamedToroIndex(com.torodb.core.model.DefaultNamedToroIndex) IndexedAttributes(com.torodb.core.model.IndexedAttributes) AttributeReference(com.torodb.core.language.AttributeReference) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonObject(javax.json.JsonObject) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader)

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