Search in sources :

Example 1 with ImmutableMetaIdentifiedDocPartIndex

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

the class SnapshotMerger method merge.

@SuppressWarnings("checkstyle:LineLength")
private void merge(MetaDatabase newDb, MetaDatabase oldDb, MutableMetaCollection newStructure, ImmutableMetaCollection oldStructure, ImmutableMetaCollection.Builder parentBuilder, MutableMetaDocPart changed) throws UnmergeableException {
    ImmutableMetaDocPart byRef = oldStructure.getMetaDocPartByTableRef(changed.getTableRef());
    ImmutableMetaDocPart byId = oldStructure.getMetaDocPartByIdentifier(changed.getIdentifier());
    if (byRef != byId) {
        throw createUnmergeableException(oldDb, oldStructure, changed, byRef, byId);
    }
    if (byRef == null && byId == null) {
        parentBuilder.put(changed.immutableCopy());
        return;
    }
    assert byRef != null;
    assert byId != null;
    ImmutableMetaDocPart.Builder childBuilder = new ImmutableMetaDocPart.Builder(byId);
    for (ImmutableMetaField addedMetaField : changed.getAddedMetaFields()) {
        merge(oldDb, newStructure, oldStructure, changed, byId, childBuilder, addedMetaField);
    }
    for (ImmutableMetaScalar addedMetaScalar : changed.getAddedMetaScalars()) {
        merge(oldDb, oldStructure, byId, childBuilder, addedMetaScalar);
    }
    for (Tuple2<ImmutableMetaIdentifiedDocPartIndex, MetaElementState> addedMetaDocPartIndex : changed.getModifiedMetaDocPartIndexes()) {
        merge(oldDb, newStructure, oldStructure, changed, byId, childBuilder, addedMetaDocPartIndex.v1(), addedMetaDocPartIndex.v2());
    }
    parentBuilder.put(childBuilder);
}
Also used : ImmutableMetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.ImmutableMetaIdentifiedDocPartIndex) ImmutableMetaDocPart(com.torodb.core.transaction.metainf.ImmutableMetaDocPart) MetaElementState(com.torodb.core.transaction.metainf.MetaElementState) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) ImmutableMetaScalar(com.torodb.core.transaction.metainf.ImmutableMetaScalar)

Example 2 with ImmutableMetaIdentifiedDocPartIndex

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

ImmutableMetaIdentifiedDocPartIndex (com.torodb.core.transaction.metainf.ImmutableMetaIdentifiedDocPartIndex)2 ImmutableMetaDocPart (com.torodb.core.transaction.metainf.ImmutableMetaDocPart)1 ImmutableMetaDocPartIndexColumn (com.torodb.core.transaction.metainf.ImmutableMetaDocPartIndexColumn)1 ImmutableMetaField (com.torodb.core.transaction.metainf.ImmutableMetaField)1 ImmutableMetaIndex (com.torodb.core.transaction.metainf.ImmutableMetaIndex)1 ImmutableMetaScalar (com.torodb.core.transaction.metainf.ImmutableMetaScalar)1 MetaElementState (com.torodb.core.transaction.metainf.MetaElementState)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