Search in sources :

Example 6 with MutableMetaDatabase

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

the class SqlWriteTorodTransaction method dropIndex.

@SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", justification = "Findbugs thinks MutableMetaCollection#removeMetaIndexByName" + "has no side effect")
@Override
public boolean dropIndex(String dbName, String colName, String indexName) {
    MutableMetaDatabase db = getInternalTransaction().getMetaSnapshot().getMetaDatabaseByName(dbName);
    if (db == null) {
        return false;
    }
    MutableMetaCollection col = db.getMetaCollectionByName(colName);
    if (col == null) {
        return false;
    }
    MetaIndex index = col.getMetaIndexByName(indexName);
    if (index == null) {
        return false;
    }
    col.removeMetaIndexByName(indexName);
    getInternalTransaction().getBackendTransaction().dropIndex(db, col, index);
    return true;
}
Also used : MutableMetaIndex(com.torodb.core.transaction.metainf.MutableMetaIndex) MetaIndex(com.torodb.core.transaction.metainf.MetaIndex) MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) MutableMetaDatabase(com.torodb.core.transaction.metainf.MutableMetaDatabase) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 7 with MutableMetaDatabase

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

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

the class SqlExclusiveWriteTorodTransaction method renameCollection.

@Override
public void renameCollection(String fromDb, String fromCollection, String toDb, String toCollection) throws RollbackException, UserException {
    MutableMetaDatabase fromMetaDb = getMetaDatabaseOrThrowException(fromDb);
    MetaCollection fromMetaColl = getMetaCollectionOrThrowException(fromMetaDb, fromCollection);
    MutableMetaDatabase toMetaDb = getOrCreateMetaDatabase(toDb);
    MutableMetaCollection toMetaColl = createMetaCollection(toMetaDb, toCollection);
    getInternalTransaction().getBackendTransaction().renameCollection(fromMetaDb, fromMetaColl, toMetaDb, toMetaColl);
    fromMetaDb.removeMetaCollectionByName(fromCollection);
}
Also used : MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) MutableMetaDatabase(com.torodb.core.transaction.metainf.MutableMetaDatabase)

Example 9 with MutableMetaDatabase

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

the class SqlWriteTorodTransaction method dropDatabase.

@Override
public void dropDatabase(String db) throws RollbackException, UserException {
    MutableMetaDatabase metaDb = getMetaDatabaseOrThrowException(db);
    getInternalTransaction().getBackendTransaction().dropDatabase(metaDb);
    getInternalTransaction().getMetaSnapshot().removeMetaDatabaseByName(db);
}
Also used : MutableMetaDatabase(com.torodb.core.transaction.metainf.MutableMetaDatabase)

Example 10 with MutableMetaDatabase

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

the class SqlWriteTorodTransaction method insert.

@Override
public void insert(String db, String collection, Stream<KvDocument> documents) throws RollbackException, UserException {
    Preconditions.checkState(!isClosed());
    MutableMetaDatabase metaDb = getOrCreateMetaDatabase(db);
    MutableMetaCollection metaCol = getOrCreateMetaCollection(metaDb, collection);
    //TODO: here we can not use a pipeline
    InsertPipeline pipeline = getConnection().getServer().getInsertPipelineFactory().createInsertPipeline(getConnection().getServer().getD2RTranslatorFactory(), metaDb, metaCol, getInternalTransaction().getBackendTransaction(), concurrent);
    pipeline.insert(documents);
}
Also used : MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) MutableMetaDatabase(com.torodb.core.transaction.metainf.MutableMetaDatabase) InsertPipeline(com.torodb.torod.pipeline.InsertPipeline)

Aggregations

MutableMetaDatabase (com.torodb.core.transaction.metainf.MutableMetaDatabase)10 MutableMetaCollection (com.torodb.core.transaction.metainf.MutableMetaCollection)5 MutableMetaSnapshot (com.torodb.core.transaction.metainf.MutableMetaSnapshot)3 MutableMetaIndex (com.torodb.core.transaction.metainf.MutableMetaIndex)2 Nonnull (javax.annotation.Nonnull)2 TableRef (com.torodb.core.TableRef)1 DatabaseNotFoundException (com.torodb.core.exceptions.user.DatabaseNotFoundException)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 MetaCollection (com.torodb.core.transaction.metainf.MetaCollection)1 MetaIndex (com.torodb.core.transaction.metainf.MetaIndex)1 IndexFieldInfo (com.torodb.torod.IndexFieldInfo)1 InsertPipeline (com.torodb.torod.pipeline.InsertPipeline)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ArrayList (java.util.ArrayList)1 Tuple3 (org.jooq.lambda.tuple.Tuple3)1