Search in sources :

Example 1 with TableRef

use of com.torodb.core.TableRef in project torodb by torodb.

the class BatchMetaCollectionTest method testAddMetaDocPart.

@Test
public void testAddMetaDocPart() {
    TableRef tableRef = tableRefFactory.createChild(tableRefFactory.createRoot(), "aPath");
    String tableId = "aTableId";
    BatchMetaDocPart newDocPart = collection.addMetaDocPart(tableRef, tableId);
    assertEquals(newDocPart, collection.getMetaDocPartByTableRef(tableRef));
    assertNotNull(newDocPart);
    verify(delegate).addMetaDocPart(tableRef, tableId);
}
Also used : TableRef(com.torodb.core.TableRef) Test(org.junit.Test)

Example 2 with TableRef

use of com.torodb.core.TableRef in project torodb by torodb.

the class SqlTorodTransaction method findByAttRef.

@Override
public TorodCursor findByAttRef(String dbName, String colName, AttributeReference attRef, KvValue<?> value) {
    MetaDatabase db = getInternalTransaction().getMetaSnapshot().getMetaDatabaseByName(dbName);
    if (db == null) {
        LOGGER.trace("Db with name " + dbName + " does not exist. An empty cursor is returned");
        return new EmptyTorodCursor();
    }
    MetaCollection col = db.getMetaCollectionByName(colName);
    if (col == null) {
        LOGGER.trace("Collection " + dbName + '.' + colName + " does not exist. An empty cursor is returned");
        return new EmptyTorodCursor();
    }
    TableRef ref = extractTableRef(attRef);
    String lastKey = extractKeyName(attRef.getKeys().get(attRef.getKeys().size() - 1));
    MetaDocPart docPart = col.getMetaDocPartByTableRef(ref);
    if (docPart == null) {
        LOGGER.trace("DocPart " + dbName + '.' + colName + '.' + ref + " does not exist. An empty cursor is returned");
        return new EmptyTorodCursor();
    }
    MetaField field = docPart.getMetaFieldByNameAndType(lastKey, FieldType.from(value.getType()));
    if (field == null) {
        LOGGER.trace("Field " + dbName + '.' + colName + '.' + ref + '.' + lastKey + " does not exist. An empty cursor is returned");
        return new EmptyTorodCursor();
    }
    return toToroCursor(getInternalTransaction().getBackendTransaction().findByField(db, col, docPart, field, value));
}
Also used : EmptyTorodCursor(com.torodb.torod.cursors.EmptyTorodCursor) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) MetaField(com.torodb.core.transaction.metainf.MetaField) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) TableRef(com.torodb.core.TableRef)

Example 3 with TableRef

use of com.torodb.core.TableRef 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 4 with TableRef

use of com.torodb.core.TableRef in project torodb by torodb.

the class SharedWriteBackendTransactionImpl method dropIndex.

@Override
public void dropIndex(MetaDatabase db, MutableMetaCollection col, MetaIndex index) {
    Preconditions.checkState(!isClosed(), "This transaction is closed");
    getSqlInterface().getMetaDataWriteInterface().deleteMetaIndex(getDsl(), db, col, index);
    Iterator<TableRef> tableRefIterator = index.streamTableRefs().iterator();
    while (tableRefIterator.hasNext()) {
        TableRef tableRef = tableRefIterator.next();
        MutableMetaDocPart docPart = col.getMetaDocPartByTableRef(tableRef);
        if (docPart != null) {
            Iterator<? extends MetaIdentifiedDocPartIndex> docPartIndexesIterator = docPart.streamIndexes().iterator();
            while (docPartIndexesIterator.hasNext()) {
                MetaIdentifiedDocPartIndex docPartIndex = docPartIndexesIterator.next();
                if (index.isCompatible(docPart, docPartIndex)) {
                    boolean existsAnyOtherCompatibleIndex = col.streamContainedMetaIndexes().anyMatch(otherIndex -> otherIndex != index && otherIndex.isCompatible(docPart, docPartIndex));
                    if (!existsAnyOtherCompatibleIndex) {
                        dropIndex(db, col, docPart, docPartIndex);
                        LOGGER.info("Dropped index {} for table {}", docPartIndex.getIdentifier(), docPart.getIdentifier());
                    }
                }
            }
        }
    }
}
Also used : MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) MutableMetaDocPart(com.torodb.core.transaction.metainf.MutableMetaDocPart) TableRef(com.torodb.core.TableRef)

Example 5 with TableRef

use of com.torodb.core.TableRef in project torodb by torodb.

the class SharedWriteBackendTransactionImpl method addDocPart.

@Override
public void addDocPart(MetaDatabase db, MetaCollection col, MetaDocPart newDocPart) {
    Preconditions.checkState(!isClosed(), "This transaction is closed");
    getSqlInterface().getMetaDataWriteInterface().addMetaDocPart(getDsl(), db, col, newDocPart);
    TableRef tableRef = newDocPart.getTableRef();
    if (tableRef.isRoot()) {
        getSqlInterface().getStructureInterface().createRootDocPartTable(getDsl(), db.getIdentifier(), newDocPart.getIdentifier(), tableRef);
        getSqlInterface().getStructureInterface().streamRootDocPartTableIndexesCreation(db.getIdentifier(), newDocPart.getIdentifier(), tableRef).forEach(consumer -> {
            String index = consumer.apply(getDsl());
            LOGGER.info("Created internal index {} for table {}", index, newDocPart.getIdentifier());
        });
    } else {
        getSqlInterface().getStructureInterface().createDocPartTable(getDsl(), db.getIdentifier(), newDocPart.getIdentifier(), tableRef, col.getMetaDocPartByTableRef(tableRef.getParent().get()).getIdentifier());
        getSqlInterface().getStructureInterface().streamDocPartTableIndexesCreation(db.getIdentifier(), newDocPart.getIdentifier(), tableRef, col.getMetaDocPartByTableRef(tableRef.getParent().get()).getIdentifier()).forEach(consumer -> {
            String index = consumer.apply(getDsl());
            LOGGER.info("Created internal index {} for table {}", index, newDocPart.getIdentifier());
        });
    }
}
Also used : TableRef(com.torodb.core.TableRef)

Aggregations

TableRef (com.torodb.core.TableRef)32 Test (org.junit.Test)13 MetaDocPart (com.torodb.core.transaction.metainf.MetaDocPart)6 DocPartResult (com.torodb.core.d2r.DocPartResult)5 ToroDocument (com.torodb.core.document.ToroDocument)5 MetaCollection (com.torodb.core.transaction.metainf.MetaCollection)5 MetaDatabase (com.torodb.core.transaction.metainf.MetaDatabase)5 R2DTranslator (com.torodb.core.d2r.R2DTranslator)4 MetaField (com.torodb.core.transaction.metainf.MetaField)4 KvDocument (com.torodb.kvdocument.values.KvDocument)4 KvArray (com.torodb.kvdocument.values.KvArray)3 KvValue (com.torodb.kvdocument.values.KvValue)3 MetaIdentifiedDocPartIndex (com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex)2 MutableMetaCollection (com.torodb.core.transaction.metainf.MutableMetaCollection)2 MutableMetaDatabase (com.torodb.core.transaction.metainf.MutableMetaDatabase)2 MutableMetaDocPart (com.torodb.core.transaction.metainf.MutableMetaDocPart)2 EmptyTorodCursor (com.torodb.torod.cursors.EmptyTorodCursor)2 ArrayList (java.util.ArrayList)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1