Search in sources :

Example 1 with ImmutableMetaDocPartIndexColumn

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

the class SnapshotMerger method merge.

private void merge(MetaDatabase db, MetaCollection col, MetaDocPart docPart, ImmutableMetaIdentifiedDocPartIndex oldStructure, ImmutableMetaIdentifiedDocPartIndex.Builder parentBuilder, ImmutableMetaDocPartIndexColumn changed) throws UnmergeableException {
    ImmutableMetaDocPartIndexColumn byIdentifier = oldStructure.getMetaDocPartIndexColumnByIdentifier(changed.getIdentifier());
    ImmutableMetaDocPartIndexColumn byPosition = oldStructure.getMetaDocPartIndexColumnByPosition(changed.getPosition());
    if (byIdentifier != byPosition) {
        throw createUnmergeableException(db, col, docPart, oldStructure, changed, byIdentifier, byPosition);
    }
    if (byIdentifier == null && byPosition == null) {
        parentBuilder.add(changed);
    }
}
Also used : ImmutableMetaDocPartIndexColumn(com.torodb.core.transaction.metainf.ImmutableMetaDocPartIndexColumn)

Example 2 with ImmutableMetaDocPartIndexColumn

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

the class SnapshotMerger method merge.

@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
private void merge(MetaDatabase oldDb, MutableMetaCollection newCol, ImmutableMetaCollection oldCol, MetaDocPart newStructure, ImmutableMetaDocPart oldStructure, ImmutableMetaDocPart.Builder parentBuilder, ImmutableMetaIdentifiedDocPartIndex changed, MetaElementState newState) throws UnmergeableException {
    ImmutableMetaIdentifiedDocPartIndex byId = oldStructure.getMetaDocPartIndexByIdentifier(changed.getIdentifier());
    ImmutableMetaIdentifiedDocPartIndex bySameColumns = oldStructure.streamIndexes().filter(oldDocPartIndex -> oldDocPartIndex.hasSameColumns(changed)).findAny().orElse(null);
    switch(newState) {
        default:
        case NOT_CHANGED:
        case NOT_EXISTENT:
            throw new AssertionError("A modification was expected, but the new state is " + newState);
        case ADDED:
        case MODIFIED:
            {
                Optional<? extends MetaIndex> anyRelatedIndex = newCol.getAnyRelatedIndex(oldCol, newStructure, changed);
                if (!anyRelatedIndex.isPresent()) {
                    throw createUnmergeableExceptionForOrphan(oldDb, oldCol, oldStructure, changed);
                }
                if (byId == null) {
                    parentBuilder.put(changed);
                    return;
                }
                assert byId != null;
                ImmutableMetaIdentifiedDocPartIndex.Builder childBuilder = new ImmutableMetaIdentifiedDocPartIndex.Builder(byId);
                Iterator<ImmutableMetaDocPartIndexColumn> indexColumnIterator = changed.iteratorColumns();
                while (indexColumnIterator.hasNext()) {
                    ImmutableMetaDocPartIndexColumn indexColumn = indexColumnIterator.next();
                    merge(oldDb, oldCol, oldStructure, byId, childBuilder, indexColumn);
                }
                parentBuilder.put(childBuilder);
                break;
            }
        case REMOVED:
            {
                Optional<? extends MetaIndex> oldMissedIndex = newCol.getAnyMissedIndex(oldCol, changed);
                if (oldMissedIndex.isPresent()) {
                    throw createUnmergeableExceptionForMissing(oldDb, oldCol, oldStructure, changed, oldMissedIndex.get());
                }
                if (byId == null || bySameColumns == null) {
                    /*
           * it has been removed on another transaction or created and removed on the current one.
           * No change must be done
           */
                    return;
                }
                assert byId != null;
                assert bySameColumns != null;
                /*
         * In this case, we can delegate on the backend transaction check. If it thinks everything
         * is fine, we can remove the element. If it thinks there is an error, then we have to
         * rollback the transaction.
         */
                parentBuilder.remove(byId);
            }
    }
}
Also used : MutableMetaIndex(com.torodb.core.transaction.metainf.MutableMetaIndex) MetaIndex(com.torodb.core.transaction.metainf.MetaIndex) ImmutableMetaIndex(com.torodb.core.transaction.metainf.ImmutableMetaIndex) ImmutableMetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.ImmutableMetaIdentifiedDocPartIndex) Optional(java.util.Optional) Iterator(java.util.Iterator) ImmutableMetaDocPartIndexColumn(com.torodb.core.transaction.metainf.ImmutableMetaDocPartIndexColumn) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ImmutableMetaDocPartIndexColumn (com.torodb.core.transaction.metainf.ImmutableMetaDocPartIndexColumn)2 ImmutableMetaIdentifiedDocPartIndex (com.torodb.core.transaction.metainf.ImmutableMetaIdentifiedDocPartIndex)1 ImmutableMetaIndex (com.torodb.core.transaction.metainf.ImmutableMetaIndex)1 MetaIndex (com.torodb.core.transaction.metainf.MetaIndex)1 MutableMetaIndex (com.torodb.core.transaction.metainf.MutableMetaIndex)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1