Search in sources :

Example 1 with MutableMetaIndex

use of com.torodb.core.transaction.metainf.MutableMetaIndex 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 2 with MutableMetaIndex

use of com.torodb.core.transaction.metainf.MutableMetaIndex in project torodb by torodb.

the class ExclusiveWriteBackendTransactionImpl method copyMetaCollection.

private void copyMetaCollection(MetaDatabase fromDb, MetaCollection fromColl, MutableMetaDatabase toDb, MutableMetaCollection toColl) {
    IdentifierFactory identifierFactory = getIdentifierFactory();
    Iterator<? extends MetaIndex> fromMetaIndexIterator = fromColl.streamContainedMetaIndexes().iterator();
    while (fromMetaIndexIterator.hasNext()) {
        MetaIndex fromMetaIndex = fromMetaIndexIterator.next();
        MutableMetaIndex toMetaIndex = toColl.addMetaIndex(fromMetaIndex.getName(), fromMetaIndex.isUnique());
        getSqlInterface().getMetaDataWriteInterface().addMetaIndex(getDsl(), toDb, toColl, toMetaIndex);
        copyIndexFields(fromMetaIndex, toDb, toColl, toMetaIndex);
    }
    Iterator<? extends MetaDocPart> fromMetaDocPartIterator = fromColl.streamContainedMetaDocParts().iterator();
    while (fromMetaDocPartIterator.hasNext()) {
        MetaDocPart fromMetaDocPart = fromMetaDocPartIterator.next();
        MutableMetaDocPart toMetaDocPart = toColl.addMetaDocPart(fromMetaDocPart.getTableRef(), identifierFactory.toDocPartIdentifier(toDb, toColl.getName(), fromMetaDocPart.getTableRef()));
        getSqlInterface().getMetaDataWriteInterface().addMetaDocPart(getDsl(), toDb, toColl, toMetaDocPart);
        copyScalar(identifierFactory, fromMetaDocPart, toDb, toColl, toMetaDocPart);
        copyFields(identifierFactory, fromMetaDocPart, toDb, toColl, toMetaDocPart);
        copyIndexes(identifierFactory, fromMetaDocPart, toDb, toColl, toMetaDocPart);
        int nextRid = ridGenerator.getDocPartRidGenerator(fromDb.getName(), fromColl.getName()).nextRid(fromMetaDocPart.getTableRef());
        ridGenerator.getDocPartRidGenerator(toDb.getName(), toColl.getName()).setNextRid(toMetaDocPart.getTableRef(), nextRid - 1);
    }
}
Also used : MutableMetaIndex(com.torodb.core.transaction.metainf.MutableMetaIndex) MetaIndex(com.torodb.core.transaction.metainf.MetaIndex) MutableMetaDocPart(com.torodb.core.transaction.metainf.MutableMetaDocPart) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) MutableMetaDocPart(com.torodb.core.transaction.metainf.MutableMetaDocPart) MutableMetaIndex(com.torodb.core.transaction.metainf.MutableMetaIndex) IdentifierFactory(com.torodb.core.d2r.IdentifierFactory)

Aggregations

MutableMetaIndex (com.torodb.core.transaction.metainf.MutableMetaIndex)2 TableRef (com.torodb.core.TableRef)1 IdentifierFactory (com.torodb.core.d2r.IdentifierFactory)1 UnsupportedCompoundIndexException (com.torodb.core.exceptions.user.UnsupportedCompoundIndexException)1 UnsupportedUniqueIndexException (com.torodb.core.exceptions.user.UnsupportedUniqueIndexException)1 AttributeReference (com.torodb.core.language.AttributeReference)1 FieldIndexOrdering (com.torodb.core.transaction.metainf.FieldIndexOrdering)1 MetaDocPart (com.torodb.core.transaction.metainf.MetaDocPart)1 MetaIndex (com.torodb.core.transaction.metainf.MetaIndex)1 MutableMetaCollection (com.torodb.core.transaction.metainf.MutableMetaCollection)1 MutableMetaDatabase (com.torodb.core.transaction.metainf.MutableMetaDatabase)1 MutableMetaDocPart (com.torodb.core.transaction.metainf.MutableMetaDocPart)1 IndexFieldInfo (com.torodb.torod.IndexFieldInfo)1 ArrayList (java.util.ArrayList)1 Tuple3 (org.jooq.lambda.tuple.Tuple3)1