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

com.gs.fw.common.mithra.cache.AbstractNonDatedTransactionalCache 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.cache;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.fw.common.mithra.*;
import com.gs.fw.common.mithra.attribute.Attribute;
import com.gs.fw.common.mithra.attribute.update.AttributeUpdateWrapper;
import com.gs.fw.common.mithra.behavior.AbstractTransactionalBehavior;



public abstract class AbstractNonDatedTransactionalCache extends AbstractNonDatedCache
{
    private static final NonNullMutableBoolean IRRELEVANT_DIRTY = new NonNullMutableBoolean();

    protected AbstractNonDatedTransactionalCache(Attribute[] pkAttributes, MithraObjectFactory factory, long timeToLive, long relationshipTimeToLive)
    {
        super(pkAttributes, factory, timeToLive, relationshipTimeToLive);
    }

    protected AbstractNonDatedTransactionalCache(Attribute[] pkAttributes, MithraObjectFactory factory, Attribute[] immutableAttributes, long timeToLive, long relationshipTimeToLive)
    {
        super(pkAttributes, factory, immutableAttributes, timeToLive, relationshipTimeToLive);
    }

    @Override
    public IndexReference getIndexRef(Attribute attribute)
    {
        IndexReference indexRef = this.getIndexRefForSingleAttribute(attribute);
        if (indexRef.isValid())
        {
            Index index = this.getIndices()[indexRef.indexReference-1];
            if (MithraManagerProvider.getMithraManager().isInTransaction() && !index.isUnique())
            {
                return this.noIndexReference;
            }
            return indexRef;
        }
        return this.noIndexReference;
    }

    @Override
    public IndexReference getBestIndexReference(List attributes)
    {
        IndexReference bestIndex = this.getBestIndex(attributes);
        if (bestIndex.isValid())
        {
            Index index = this.getIndices()[bestIndex.indexReference-1];
            if (MithraManagerProvider.getMithraManager().isInTransaction() && !index.isUnique())
            {
                return this.noIndexReference;
            }
            return bestIndex;
        }
        return this.noIndexReference;
    }

    @Override
    protected void reindexAffectedIndicesAndSetData(MithraObject object, MithraDataObject newData,
            UnifiedSet affectedIndicies, Object optionalBehavior)
    {
        MithraManager mithraManager = MithraManagerProvider.getMithraManager();
        MithraTransaction tx = mithraManager.zGetCurrentTransactionWithNoCheck();
        if (tx == null)
        {
            super.reindexAffectedIndicesAndSetData(object, newData, affectedIndicies, optionalBehavior);
            return;
        }
        List prepared = FastList.newList(affectedIndicies.size());
        for (Iterator it = affectedIndicies.iterator(); it.hasNext();)
        {
            TransactionalIndex index = (TransactionalIndex) it.next();
            if (index.prepareForReindex(object, tx))
            {
                prepared.add(index);
                it.remove();
            }
            else
            {
                index.removeIgnoringTransaction(object);
            }
        }
        object.zSetData(newData, optionalBehavior);
        for (Iterator it = affectedIndicies.iterator(); it.hasNext();)
        {
            TransactionalIndex index = (TransactionalIndex) it.next();
            index.putIgnoringTransaction(object, newData, false);
        }
        for(int i=0;i 0)
                {
                    this.readWriteLock.acquireWriteLock();
                    lock = Boolean.TRUE;
                    for (Iterator it = affectedIndicies.iterator(); it.hasNext();)
                    {
                        Index index = (Index) it.next();
                        index.remove(object);
                    }
                }
            }
/*
             the indicies have now been updated with the new value, but the object still holds onto the old
             value. If the index is queried in this state, it can return the wrong results
             because of equality checking inside the index.
             Therefore, we must perform the object update under the cache's write lock
*/
            updateWrapper.updateData();
            if (affectedIndicies.size() > 0)
            {
                for (Iterator it = affectedIndicies.iterator(); it.hasNext();)
                {
                    Index index = (Index) it.next();
                    index.put(object);
                }
            }
        }
        finally
        {
            if (lock != null)
            {
                this.readWriteLock.release();
            }
        }
    }

    @Override
    public void reindexForTransaction(MithraObject object, AttributeUpdateWrapper updateWrapper)
    {
        Attribute attribute = updateWrapper.getAttribute();
        if (this.getMonitoredAttributes().contains(attribute))
        {
            List affectedIndicies = (List)this.getAttributeToIndexMap().get(attribute);
            if (affectedIndicies.size() > 0)
            {
                MithraManager mithraManager = MithraManagerProvider.getMithraManager();
                MithraTransaction tx = mithraManager.zGetCurrentTransactionWithNoCheck();
                try
                {
                    this.getCacheLock().acquireWriteLock();
                    for(Iterator it = affectedIndicies.iterator();it.hasNext();)
                    {
                        TransactionalIndex index = (TransactionalIndex)it.next();
                        index.prepareForReindexInTransaction(object, tx);
                    }
                    updateWrapper.updateData();
                    for(Iterator it = affectedIndicies.iterator();it.hasNext();)
                    {
                        TransactionalIndex index = (TransactionalIndex)it.next();
                        index.finishForReindex(object, tx);
                    }
                }
                finally
                {
                    this.getCacheLock().release();
                }
                return;
            }
        }
        updateWrapper.updateData();
    }

    @Override
    public void removeIgnoringTransaction(MithraObject object)
    {
        try
        {
            this.getCacheLock().acquireWriteLock();
            Index[] indices = this.getIndices();
            for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy