Search in sources :

Example 6 with MetaIndex

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

MetaIndex (com.torodb.core.transaction.metainf.MetaIndex)6 MutableMetaIndex (com.torodb.core.transaction.metainf.MutableMetaIndex)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 MetaCollection (com.torodb.core.transaction.metainf.MetaCollection)2 MetaDatabase (com.torodb.core.transaction.metainf.MetaDatabase)2 MetaDocPart (com.torodb.core.transaction.metainf.MetaDocPart)2 MetaIdentifiedDocPartIndex (com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex)2 Iterator (java.util.Iterator)2 Optional (java.util.Optional)2 Preconditions (com.google.common.base.Preconditions)1 Context (com.torodb.backend.ErrorHandler.Context)1 KvTable (com.torodb.backend.tables.KvTable)1 MetaDocPartTable (com.torodb.backend.tables.MetaDocPartTable)1 KvRecord (com.torodb.backend.tables.records.KvRecord)1 MetaDatabaseRecord (com.torodb.backend.tables.records.MetaDatabaseRecord)1 TableRef (com.torodb.core.TableRef)1 MetaInfoKey (com.torodb.core.backend.MetaInfoKey)1 IdentifierFactory (com.torodb.core.d2r.IdentifierFactory)1 IndexNotFoundException (com.torodb.core.exceptions.user.IndexNotFoundException)1 ImmutableMetaDocPartIndexColumn (com.torodb.core.transaction.metainf.ImmutableMetaDocPartIndexColumn)1