All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.fw.common.mithra.transaction.TransactionOperation Maven / Gradle / Ivy

There is a newer version: 18.1.0
Show newest version
/*
 Copyright 2016 Goldman Sachs.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */

package com.gs.fw.common.mithra.transaction;

import com.gs.fw.common.mithra.MithraDatabaseException;
import com.gs.fw.common.mithra.MithraObjectPortal;
import com.gs.fw.common.mithra.MithraTransactionalObject;
import com.gs.fw.common.mithra.cache.FullUniqueIndex;
import com.gs.fw.common.mithra.cache.TransactionalUnderlyingObjectGetter;
import com.gs.fw.common.mithra.cache.UnderlyingObjectGetter;
import com.gs.fw.common.mithra.extractor.Extractor;
import com.gs.fw.common.mithra.extractor.IdentityExtractor;
import com.gs.fw.common.mithra.finder.RelatedFinder;
import com.gs.fw.common.mithra.util.ListFactory;
import com.gs.reladomo.metadata.ReladomoClassMetaData;

import java.util.List;



public abstract class TransactionOperation
{

    private MithraTransactionalObject mithraObject;
    private MithraObjectPortal portal;

    public static final int COMBINE_DIRECTION_NONE = 0;
    public static final int COMBINE_DIRECTION_FORWARD = 1;
    public static final int COMBINE_DIRECTION_BACKWARD = 2;

    public TransactionOperation(MithraTransactionalObject mithraObject, MithraObjectPortal portal)
    {
        this.mithraObject = mithraObject;
        this.portal = portal;
    }

    public MithraTransactionalObject getMithraObject()
    {
        return mithraObject;
    }

    public abstract void execute() throws MithraDatabaseException;

    public TransactionOperation combinePurge(MithraTransactionalObject obj, MithraObjectPortal incomingPortal)
    {
        return null;
    }

    public TransactionOperation combineInsert(MithraTransactionalObject obj, MithraObjectPortal incomingPortal)
    {
        return null;
    }

    public TransactionOperation combineDelete(MithraTransactionalObject obj, MithraObjectPortal incomingPortal)
    {
        return null;
    }

    public TransactionOperation combineUpdate(TransactionOperation op)
    {
        return null;
    }

    public boolean isInsert()
    {
        return false;
    }

    public boolean isDelete()
    {
        return false;
    }

    public boolean isUpdate()
    {
        return false;
    }

    public boolean isMultiUpdate()
    {
        return false;
    }

    public boolean isBatchUpdate()
    {
        return false;
    }

    public TransactionOperation combine(TransactionOperation op)
    {
        if (op.isInsert())
        {
            return combineInsertOperation(op);
        }
        else if (op.isDelete())
        {
            return combineDeleteOperation(op);
        }
        else if (op.isUpdate())
        {
            return combineUpdate(op);
        }
        else if (op.isMultiUpdate())
        {
            return combineMultiUpdate(op);
        }
        else if (op.isBatchUpdate())
        {
            return combineBatchUpdate(op);
        }
        return null;
    }

    public TransactionOperation combineBatchUpdate(TransactionOperation op)
    {
        return null;
    }

    public TransactionOperation combineMultiUpdate(TransactionOperation op)
    {
        return null;
    }

    public TransactionOperation combineInsertOperation(TransactionOperation op)
    {
        return null;
    }

    public TransactionOperation combineDeleteOperation(TransactionOperation op)
    {
        return null;
    }

    public boolean isCombinableWithInsertOperation()
    {
        return true;
    }

    public List getAllObjects()
    {
        return ListFactory.create(this.mithraObject);
    }

    protected MithraObjectPortal getPortal()
    {
        return this.portal;
    }

    public int getPassThroughDirection(TransactionOperation next)
    {
        if (next == DoNothingTransactionOperation.getInstance())
        {
            return COMBINE_DIRECTION_BACKWARD | COMBINE_DIRECTION_FORWARD;
        }
        if (!next.isCombinableWithInsertOperation())
        {
            return COMBINE_DIRECTION_NONE;
        }
        if (this.getPortal() == next.getPortal())
        {
            if (this.touchesSameObject(next))
            {
                if (this.getPortal().getFinder().getAsOfAttributes() != null)
                {
                    return getPassThroughForSameDated(next);
                }
                return COMBINE_DIRECTION_NONE;
            }
            else return getCombineDirectionWithDependencyCheck(next);
        }
        else
        {
            return getCombineDirectionWithDependencyCheck(next);
        }
    }

    protected int getPassThroughForSameDated(TransactionOperation next)
    {
        if (this.isInsert() && next.isAnyUpdate() && next.isAsOfAttributeToOnlyUpdate())
        {
            return COMBINE_DIRECTION_FORWARD;
        }
        if (next.isInsert() && this.isAnyUpdate() && this.isAsOfAttributeToOnlyUpdate())
        {
            return COMBINE_DIRECTION_BACKWARD;
        }
        return COMBINE_DIRECTION_NONE;
    }

    protected boolean isAsOfAttributeToOnlyUpdate()
    {
        return false;
    }

    protected boolean isAnyUpdate()
    {
        return this.isUpdate() || this.isMultiUpdate() || this.isBatchUpdate();
    }

    private int getCombineDirectionWithDependencyCheck(TransactionOperation next)
    {
        MithraObjectPortal thisPortal = this.getPortal();
        MithraObjectPortal nextPortal = next.getPortal();
        if (thisPortal.isParentFinder(nextPortal.getFinder()))
        {
            return getCombineDirectionForParent();
        }
        else if (nextPortal.isParentFinder(thisPortal.getFinder()))
        {
            return getCombineDirectionForChild();
        }
        return COMBINE_DIRECTION_FORWARD | COMBINE_DIRECTION_BACKWARD;
    }

    protected FullUniqueIndex getIndexedObjects()
    {
        return null;
    }

    protected abstract int getCombineDirectionForParent();
    protected abstract int getCombineDirectionForChild();

    protected boolean touchesSameObject(TransactionOperation otherOp)
    {
        int otherSize = otherOp.getTotalOperationsSize();
        int localSize = this.getTotalOperationsSize();
        if (otherSize == 1 && localSize == 1)
        {
            return this.getMithraObject().zIsSameObjectWithoutAsOfAttributes(otherOp.getMithraObject());
        }
        if (otherSize > 100 && localSize <= otherSize)
        {
            return indexCompare(otherOp.getIndexedObjects(), this.getAllObjects());
        }
        if (localSize > 100 && otherSize <= localSize)
        {
            return indexCompare(this.getIndexedObjects(), otherOp.getAllObjects());
        }
        return loopCompare(this.getAllObjects(), otherOp.getAllObjects());
    }

    public int getTotalOperationsSize()
    {
        return 1;
    }

    protected FullUniqueIndex createFullUniqueIndex(List objects)
    {
        RelatedFinder finder = this.getPortal().getFinder();
        Extractor[] extractors = IdentityExtractor.getArrayInstance();
        UnderlyingObjectGetter getter = null;
        if (finder.getAsOfAttributes() != null)
        {
            extractors = ReladomoClassMetaData.fromFinder(finder).getUniqueExtractors();
            getter = new TransactionalUnderlyingObjectGetter();
        }
        FullUniqueIndex index = new FullUniqueIndex(extractors, objects.size());
        if (getter != null)
        {
            index.setUnderlyingObjectGetter(getter);
        }
        for(int i=0;i localObjects.size())
        {
            small = localObjects;
            large = allObjects;
        }
        for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy