Search in sources :

Example 1 with MutableMetaCollection

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

the class SqlWriteTorodTransaction method dropCollection.

@Override
public void dropCollection(String db, String collection) throws RollbackException, UserException {
    MutableMetaDatabase metaDb = getMetaDatabaseOrThrowException(db);
    MutableMetaCollection metaColl = getMetaCollectionOrThrowException(metaDb, collection);
    getInternalTransaction().getBackendTransaction().dropCollection(metaDb, metaColl);
    metaDb.removeMetaCollectionByName(collection);
}
Also used : MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) MutableMetaDatabase(com.torodb.core.transaction.metainf.MutableMetaDatabase)

Example 2 with MutableMetaCollection

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

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

the class SqlWriteTorodTransaction method createMetaCollection.

protected MutableMetaCollection createMetaCollection(MutableMetaDatabase metaDb, String colName) {
    MutableMetaCollection metaCol;
    Preconditions.checkState(!isClosed());
    metaCol = metaDb.addMetaCollection(colName, getConnection().getServer().getIdentifierFactory().toCollectionIdentifier(getInternalTransaction().getMetaSnapshot(), metaDb.getName(), colName));
    getInternalTransaction().getBackendTransaction().addCollection(metaDb, metaCol);
    return metaCol;
}
Also used : MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection)

Example 4 with MutableMetaCollection

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

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

the class SameThreadInsertPipeline method insert.

@Override
public void insert(Stream<KvDocument> docs) throws RollbackException, UserException {
    D2RTranslationBatchFunction d2rFun = new D2RTranslationBatchFunction(translatorFactory, metaDb, mutableMetaCollection);
    DefaultToBackendFunction r2BackendFun = new DefaultToBackendFunction(jobFactory, metaDb, mutableMetaCollection);
    try {
        Iterators.partition(docs.iterator(), docBatchSize).forEachRemaining(list -> {
            CollectionData collData = d2rFun.apply(list);
            Iterable<BackendTransactionJob> jobs = r2BackendFun.apply(collData);
            jobs.forEach(Unchecked.consumer(job -> job.execute(backendConnection)));
        });
    } catch (UncheckedException ex) {
        Throwable cause = ex.getCause();
        if (cause != null && cause instanceof UserException) {
            throw (UserException) cause;
        }
        throw ex;
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) Unchecked(org.jooq.lambda.Unchecked) KvDocument(com.torodb.kvdocument.values.KvDocument) UserException(com.torodb.core.exceptions.user.UserException) BackendTransactionJobFactory(com.torodb.core.dsl.backend.BackendTransactionJobFactory) Iterators(com.google.common.collect.Iterators) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) RollbackException(com.torodb.core.transaction.RollbackException) D2RTranslatorFactory(com.torodb.core.d2r.D2RTranslatorFactory) Assisted(com.google.inject.assistedinject.Assisted) Inject(javax.inject.Inject) MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) UncheckedException(org.jooq.lambda.UncheckedException) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) Stream(java.util.stream.Stream) DefaultToBackendFunction(com.torodb.torod.pipeline.DefaultToBackendFunction) WriteBackendTransaction(com.torodb.core.backend.WriteBackendTransaction) Preconditions(com.google.common.base.Preconditions) CollectionData(com.torodb.core.d2r.CollectionData) D2RTranslationBatchFunction(com.torodb.torod.pipeline.D2RTranslationBatchFunction) InsertPipeline(com.torodb.torod.pipeline.InsertPipeline) UncheckedException(org.jooq.lambda.UncheckedException) D2RTranslationBatchFunction(com.torodb.torod.pipeline.D2RTranslationBatchFunction) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) UserException(com.torodb.core.exceptions.user.UserException) DefaultToBackendFunction(com.torodb.torod.pipeline.DefaultToBackendFunction)

Aggregations

MutableMetaCollection (com.torodb.core.transaction.metainf.MutableMetaCollection)8 MutableMetaDatabase (com.torodb.core.transaction.metainf.MutableMetaDatabase)5 CollectionData (com.torodb.core.d2r.CollectionData)2 D2RTranslatorFactory (com.torodb.core.d2r.D2RTranslatorFactory)2 MutableMetaIndex (com.torodb.core.transaction.metainf.MutableMetaIndex)2 KvDocument (com.torodb.kvdocument.values.KvDocument)2 InsertPipeline (com.torodb.torod.pipeline.InsertPipeline)2 Preconditions (com.google.common.base.Preconditions)1 Iterators (com.google.common.collect.Iterators)1 Lists (com.google.common.collect.Lists)1 Assisted (com.google.inject.assistedinject.Assisted)1 TableRef (com.torodb.core.TableRef)1 WriteBackendTransaction (com.torodb.core.backend.WriteBackendTransaction)1 D2RTranslator (com.torodb.core.d2r.D2RTranslator)1 BackendTransactionJob (com.torodb.core.dsl.backend.BackendTransactionJob)1 BackendTransactionJobFactory (com.torodb.core.dsl.backend.BackendTransactionJobFactory)1 UnsupportedCompoundIndexException (com.torodb.core.exceptions.user.UnsupportedCompoundIndexException)1 UnsupportedUniqueIndexException (com.torodb.core.exceptions.user.UnsupportedUniqueIndexException)1 UserException (com.torodb.core.exceptions.user.UserException)1 AttributeReference (com.torodb.core.language.AttributeReference)1