Search in sources :

Example 11 with MetaField

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

the class SqlWriteTorodTransaction method deleteByAttRef.

@Override
public long deleteByAttRef(String dbName, String colName, AttributeReference attRef, KvValue<?> value) {
    MetaDatabase db = getInternalTransaction().getMetaSnapshot().getMetaDatabaseByName(dbName);
    if (db == null) {
        return 0;
    }
    MetaCollection col = db.getMetaCollectionByName(colName);
    if (col == null) {
        return 0;
    }
    TableRef tableRef = extractTableRef(attRef);
    String lastKey = extractKeyName(attRef.getKeys().get(attRef.getKeys().size() - 1));
    MetaDocPart docPart = col.getMetaDocPartByTableRef(tableRef);
    if (docPart == null) {
        return 0;
    }
    MetaField field = docPart.getMetaFieldByNameAndType(lastKey, FieldType.from(value.getType()));
    if (field == null) {
        return 0;
    }
    Collection<Integer> dids = getInternalTransaction().getBackendTransaction().findByField(db, col, docPart, field, value).asDidCursor().getRemaining();
    getInternalTransaction().getBackendTransaction().deleteDids(db, col, dids);
    return dids.size();
}
Also used : MutableMetaDatabase(com.torodb.core.transaction.metainf.MutableMetaDatabase) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) MetaField(com.torodb.core.transaction.metainf.MetaField) MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) TableRef(com.torodb.core.TableRef)

Example 12 with MetaField

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

the class AbstractReadInterface method getCollectionDidsWithFieldsInBatch.

@SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION", "ODR_OPEN_DATABASE_RESOURCE" }, justification = "ResultSet is wrapped in a Cursor<Integer>. It's iterated and closed in caller code")
private Cursor<Integer> getCollectionDidsWithFieldsInBatch(DSLContext dsl, MetaDatabase metaDatabase, MetaCollection metaCol, MetaDocPart metaDocPart, Multimap<MetaField, KvValue<?>> valuesMultimap) throws SQLException {
    @SuppressWarnings("checkstyle:LineLength") Provider<Stream<Map.Entry<MetaField, Collection<KvValue<?>>>>> valuesMultimapSortedStreamProvider = () -> valuesMultimap.asMap().entrySet().stream().sorted((e1, e2) -> e1.getKey().getIdentifier().compareTo(e2.getKey().getIdentifier()));
    String statement = getReadCollectionDidsWithFieldInStatement(metaDatabase.getIdentifier(), metaDocPart.getIdentifier(), valuesMultimapSortedStreamProvider.get().map(e -> new Tuple2<String, Integer>(e.getKey().getIdentifier(), e.getValue().size())));
    Connection connection = dsl.configuration().connectionProvider().acquire();
    try {
        PreparedStatement preparedStatement = connection.prepareStatement(statement);
        int parameterIndex = 1;
        Iterator<Map.Entry<MetaField, Collection<KvValue<?>>>> valuesMultimapSortedIterator = valuesMultimapSortedStreamProvider.get().iterator();
        while (valuesMultimapSortedIterator.hasNext()) {
            Map.Entry<MetaField, Collection<KvValue<?>>> valuesMultimapEntry = valuesMultimapSortedIterator.next();
            for (KvValue<?> value : valuesMultimapEntry.getValue()) {
                sqlHelper.setPreparedStatementValue(preparedStatement, parameterIndex, valuesMultimapEntry.getKey().getType(), value);
                parameterIndex++;
            }
        }
        return new DefaultDidCursor(errorHandler, preparedStatement.executeQuery());
    } finally {
        dsl.configuration().connectionProvider().release(connection);
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Connection(java.sql.Connection) Provider(javax.inject.Provider) IteratorCursor(com.torodb.core.cursors.IteratorCursor) Multimap(com.google.common.collect.Multimap) Seq(org.jooq.lambda.Seq) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) TableRefFactory(com.torodb.core.TableRefFactory) KvValue(com.torodb.kvdocument.values.KvValue) Tuple2(org.jooq.lambda.tuple.Tuple2) SQLException(java.sql.SQLException) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) ResultSetDocPartResult(com.torodb.backend.d2r.ResultSetDocPartResult) ResultSet(java.sql.ResultSet) Map(java.util.Map) DSLContext(org.jooq.DSLContext) Context(com.torodb.backend.ErrorHandler.Context) Nonnull(javax.annotation.Nonnull) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) Unchecked(org.jooq.lambda.Unchecked) Iterator(java.util.Iterator) Cursor(com.torodb.core.cursors.Cursor) Collection(java.util.Collection) EmptyCursor(com.torodb.core.cursors.EmptyCursor) DocPartTableFields(com.torodb.backend.tables.MetaDocPartTable.DocPartTableFields) PreparedStatement(java.sql.PreparedStatement) Collectors(java.util.stream.Collectors) TableRef(com.torodb.core.TableRef) List(java.util.List) MetaField(com.torodb.core.transaction.metainf.MetaField) Stream(java.util.stream.Stream) DocPartResult(com.torodb.core.d2r.DocPartResult) Entry(java.util.Map.Entry) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) MetaField(com.torodb.core.transaction.metainf.MetaField) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Entry(java.util.Map.Entry) Tuple2(org.jooq.lambda.tuple.Tuple2) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) Collection(java.util.Collection) Stream(java.util.stream.Stream) Map(java.util.Map) KvValue(com.torodb.kvdocument.values.KvValue) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

MetaField (com.torodb.core.transaction.metainf.MetaField)12 MetaDocPart (com.torodb.core.transaction.metainf.MetaDocPart)6 TableRef (com.torodb.core.TableRef)5 MetaCollection (com.torodb.core.transaction.metainf.MetaCollection)5 MetaDatabase (com.torodb.core.transaction.metainf.MetaDatabase)5 MetaScalar (com.torodb.core.transaction.metainf.MetaScalar)5 KvValue (com.torodb.kvdocument.values.KvValue)3 EmptyCursor (com.torodb.core.cursors.EmptyCursor)2 EmptyTorodCursor (com.torodb.torod.cursors.EmptyTorodCursor)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ImmutableList (com.google.common.collect.ImmutableList)1 Multimap (com.google.common.collect.Multimap)1 Context (com.torodb.backend.ErrorHandler.Context)1 ResultSetDocPartResult (com.torodb.backend.d2r.ResultSetDocPartResult)1 DocPartTableFields (com.torodb.backend.tables.MetaDocPartTable.DocPartTableFields)1 TableRefFactory (com.torodb.core.TableRefFactory)1 Cursor (com.torodb.core.cursors.Cursor)1 IteratorCursor (com.torodb.core.cursors.IteratorCursor)1