Search in sources :

Example 1 with ImmutableMetaCollection

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

the class BatchMetaCollectionTest method setUp.

@Before
public void setUp() {
    ImmutableMetaCollection immutableCollection = new ImmutableMetaCollection.Builder("colName", "colId").put(new ImmutableMetaDocPart(tableRefFactory.createRoot(), "docPartName")).build();
    delegate = Mockito.spy(new WrapperMutableMetaCollection(immutableCollection, (o) -> {
    }));
    collection = new BatchMetaCollection(delegate);
}
Also used : WrapperMutableMetaCollection(com.torodb.core.transaction.metainf.WrapperMutableMetaCollection) ImmutableMetaDocPart(com.torodb.core.transaction.metainf.ImmutableMetaDocPart) ImmutableMetaCollection(com.torodb.core.transaction.metainf.ImmutableMetaCollection) Before(org.junit.Before)

Example 2 with ImmutableMetaCollection

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

the class SnapshotMerger method merge.

private void merge(MetaDatabase newStructure, ImmutableMetaDatabase oldStructure, ImmutableMetaDatabase.Builder parentBuilder, MutableMetaCollection newCol, MetaElementState newState) throws UnmergeableException {
    ImmutableMetaCollection byName = oldStructure.getMetaCollectionByName(newCol.getName());
    ImmutableMetaCollection byId = oldStructure.getMetaCollectionByIdentifier(newCol.getIdentifier());
    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:
            {
                if (byName != byId) {
                    throw createUnmergeableException(oldStructure, newCol, byName, byId);
                }
                if (byName == null && byId == null) {
                    parentBuilder.put(newCol.immutableCopy());
                    return;
                }
                assert byName != null;
                assert byId != null;
                ImmutableMetaCollection.Builder childBuilder = new ImmutableMetaCollection.Builder(byId);
                for (MutableMetaDocPart modifiedDocPart : newCol.getModifiedMetaDocParts()) {
                    merge(newStructure, oldStructure, newCol, byId, childBuilder, modifiedDocPart);
                }
                for (Tuple2<MutableMetaIndex, MetaElementState> modifiedIndex : newCol.getModifiedMetaIndexes()) {
                    merge(oldStructure, newCol, byId, childBuilder, modifiedIndex.v1(), modifiedIndex.v2());
                }
                parentBuilder.put(childBuilder);
                break;
            }
        case REMOVED:
            {
                if (byName != byId) {
                    /*
           * The backend transaction will remove by id, but it is referencing another name on the
           * current snapshot, so the final state will be inconsistent. It is better to fail.
           */
                    throw createUnmergeableException(oldStructure, newCol, byName, byId);
                }
                if (byName == null && byId == null) {
                    /*
           * it has been removed on another transaction or created and removed on the current one.
           * No change must be done
           */
                    return;
                }
                assert byName != null;
                assert byId != 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(byName);
            }
    }
}
Also used : MutableMetaDocPart(com.torodb.core.transaction.metainf.MutableMetaDocPart) Tuple2(org.jooq.lambda.tuple.Tuple2) ImmutableMetaCollection(com.torodb.core.transaction.metainf.ImmutableMetaCollection)

Aggregations

ImmutableMetaCollection (com.torodb.core.transaction.metainf.ImmutableMetaCollection)2 ImmutableMetaDocPart (com.torodb.core.transaction.metainf.ImmutableMetaDocPart)1 MutableMetaDocPart (com.torodb.core.transaction.metainf.MutableMetaDocPart)1 WrapperMutableMetaCollection (com.torodb.core.transaction.metainf.WrapperMutableMetaCollection)1 Tuple2 (org.jooq.lambda.tuple.Tuple2)1 Before (org.junit.Before)1