Package org.jpox.cache

Source Code of org.jpox.cache.DefaultLevel2Cache

/**********************************************************************
Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
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.


Contributors:
    ...
**********************************************************************/
package org.jpox.cache;

import java.lang.ref.Reference;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import org.jpox.OMFContext;
import org.jpox.ObjectManagerHelper;
import org.jpox.ObjectManagerImpl;
import org.jpox.util.JPOXLogger;
import org.jpox.util.Localiser;
import org.jpox.util.WeakValueMap;

/**
* Default implementation of a Level 2 cache for JPOX.
* <p>
* Operates with 2 maps internally. One stores all pinned objects that have been
* selected to be retained by user's application. The other stores all other objects.
* This second map is the default location where objects are placed when being added here.
* The second (unpinned) map stores weak references meaning that they can get garbage
* collected as necessary by the JVM.
* </P>
* <P>
* Maintains collections of the classes and the identities that are to be pinned if they ever
* are put into the cache. These are defined by the pinAll(), pin() methods.
* </P>
* <P>
* All mutating methods, and the get method have been synchronized to prevent conflicts.
* </P>
*
* @version $Revision: 1.18 $
*/
public class DefaultLevel2Cache implements Level2Cache
{
    /** Localiser for messages */
    private static Localiser LOCALISER = Localiser.getInstance("org.jpox.Localisation",
        ObjectManagerImpl.class.getClassLoader());

    /** Collection of pinned classes whose objects should be pinned if they ever reach the cache. */
    protected Collection pinnedClasses;

    /** Collection of ids whose objects should be pinned if they ever reach the cache. */
    protected Collection pinnedIds;

    /** Pinned objects cache. */
    protected Map pinnedCache;

    /** Unpinned objects cache. */
    protected Map unpinnedCache;

    /**
     * Constructor.
     */
    protected DefaultLevel2Cache()
    {
        // nothing to do
    }

    /**
     * Constructor.
     * @param omfCtx OMF Context
     */
    public DefaultLevel2Cache(OMFContext omfCtx)
    {
        pinnedCache = new HashMap();
        unpinnedCache = new WeakValueMap();
    }

    /**
     * Method to evict an object from the cache.
     * @param oid The id of the object to evict
     */
    public synchronized void evict(Object oid)
    {
        if (oid == null)
        {
            return;
        }

        unpinnedCache.remove(oid);
        pinnedCache.remove(oid);
    }

    /**
     * Method to evict all objects from the L2 cache.
     */
    public synchronized void evictAll()
    {
        unpinnedCache.clear();
        pinnedCache.clear();
    }

    /**
     * Method to evict all objects of the given types from the cache.
     * @param pcClass The class to evict
     * @param subclasses Whether to also evict subclasses
     */
    public synchronized void evictAll(Class pcClass, boolean subclasses)
    {
        if (pcClass == null)
        {
            return;
        }

        Collection oidsToEvict = new HashSet();

        // Find objects to evict from pinned
        Collection pinnedObjects = pinnedCache.entrySet();
        Iterator pinnedIter = pinnedObjects.iterator();
        while (pinnedIter.hasNext())
        {
            Map.Entry entry = (Map.Entry)pinnedIter.next();
            CachedPC pc = (CachedPC)entry.getValue();
            if (pcClass.getName().equals(pc.getPCClass().getName()) ||
                (subclasses && pcClass.isAssignableFrom(pc.getPCClass())))
            {
                oidsToEvict.add(entry.getKey());
            }
        }

        // Find objects to evict from unpinned
        Collection unpinnedObjects = unpinnedCache.entrySet();
        Iterator unpinnedIter = unpinnedObjects.iterator();
        while (unpinnedIter.hasNext())
        {
            Map.Entry entry = (Map.Entry)unpinnedIter.next();
            CachedPC pc = (CachedPC)entry.getValue();
            if (pc != null && pcClass.getName().equals(pc.getPCClass().getName()) ||
                (subclasses && pcClass.isAssignableFrom(pc.getPCClass())))
            {
                oidsToEvict.add(entry.getKey());
            }
        }

        // Evict the objects
        if (!oidsToEvict.isEmpty())
        {
            evictAll(oidsToEvict);
        }
    }

    /**
     * Method to evict the objects with the specified ids.
     * @param oids The ids of the objects to evict
     */
    public synchronized void evictAll(Collection oids)
    {
        if (oids == null)
        {
            return;
        }

        Iterator iter = oids.iterator();
        while (iter.hasNext())
        {
            evict(iter.next());
        }
    }

    /**
     * Method to evict the objects with the specified ids.
     * @param oids The ids of the objects to evict
     */
    public synchronized void evictAll(Object[] oids)
    {
        if (oids == null)
        {
            return;
        }

        for (int i=0;i<oids.length;i++)
        {
            evict(oids[i]);
        }
    }

    /**
     * Method to pin an object to the cache.
     * @param oid The id of the object to pin
     */
    public synchronized void pin(Object oid)
    {
        if (oid == null)
        {
            return;
        }

        if (pinnedIds == null)
        {
            pinnedIds = new HashSet();
        }
        else if (!pinnedIds.contains(oid))
        {
            // Add this oid to the to-be-pinned collection
            pinnedIds.add(oid);
        }

        Object pc = unpinnedCache.get(oid);
        if (pc != null)
        {
            pinnedCache.put(oid, pc);
            unpinnedCache.remove(oid);
        }
    }

    /**
     * Method to pin all objects of the given types.
     * @param cls The class
     * @param subs Whether to include subclasses
     */
    public synchronized void pinAll(Class cls, boolean subs)
    {
        if (cls == null)
        {
            return;
        }

        if (pinnedClasses == null)
        {
            pinnedClasses = new HashSet();
        }

        // Check if it already exists as a pinned class
        PinnedClass pinnedCls = new PinnedClass(cls, subs);
        if (pinnedClasses.contains(pinnedCls))
        {
            return;
        }
        pinnedClasses.add(pinnedCls);

        // Update all currently unpinned objects to comply with the new class specification
        Collection unpinnedObjects = unpinnedCache.values();
        Iterator unpinnedIter = unpinnedObjects.iterator();
        while (unpinnedIter.hasNext())
        {
            CachedPC obj = (CachedPC)unpinnedIter.next();
            if ((subs && cls.isInstance(obj.getPCClass())) ||
                cls.getName().equals(obj.getPCClass().getName()))
            {
                pin(obj);
            }
        }
    }

    /**
     * Method to pin all of the supplied objects
     * @param oids The Object ids to pin
     */
    public synchronized void pinAll(Collection oids)
    {
        if (oids == null)
        {
            return;
        }

        Iterator iter = oids.iterator();
        while (iter.hasNext())
        {
            pin(iter.next());
        }
    }

    /**
     * Method to pin all of the supplied objects
     * @param oids The object ids to pin
     */
    public synchronized void pinAll(Object[] oids)
    {
        if (oids == null)
        {
            return;
        }

        for (int i=0;i<oids.length;i++)
        {
            pin(oids[i]);
        }
    }

    /**
     * Method to unpin an object
     * @param oid The object id
     */
    public synchronized void unpin(Object oid)
    {
        if (oid == null)
        {
            return;
        }

        Object pc = pinnedCache.get(oid);
        if (pc != null)
        {
            unpinnedCache.put(oid, pc);
            pinnedCache.remove(oid);
        }

        if (pinnedIds != null && pinnedIds.contains(oid))
        {
            // Remove this oid from the to-be-pinned collection
            pinnedIds.remove(oid);
        }
    }

    /**
     * Method to unpin all objects of the specified types.
     * @param cls Base class
     * @param subs Whether to include subclasses
     */
    public synchronized void unpinAll(Class cls, boolean subs)
    {
        if (cls == null)
        {
            return;
        }

        // Remove the class from the pinned collection
        if (pinnedClasses != null)
        {
            PinnedClass pinnedCls = new PinnedClass(cls, subs);
            pinnedClasses.remove(pinnedCls);
        }

        // Unpin all objects of this type currently pinned
        Collection pinnedObjects = pinnedCache.values();
        Iterator pinnedIter = pinnedObjects.iterator();
        while (pinnedIter.hasNext())
        {
            CachedPC obj = (CachedPC)pinnedIter.next();
            if ((subs && cls.isInstance(obj.getPCClass())) ||
                cls.getName().equals(obj.getPCClass().getName()))
            {
                unpin(obj);
            }
        }
    }

    /**
     * Method to unpin all of the supplied objects
     * @param oids The object ids to unpin
     */
    public synchronized void unpinAll(Collection oids)
    {
        if (oids == null)
        {
            return;
        }

        Iterator iter = oids.iterator();
        while (iter.hasNext())
        {
            unpin(iter.next());
        }
    }

    /**
     * Method to unpin all of the specified objects
     * @param oids The object ids to unpin
     */
    public synchronized void unpinAll(Object[] oids)
    {
        if (oids == null)
        {
            return;
        }

        for (int i=0;i<oids.length;i++)
        {
            unpin(oids[i]);
        }
    }

    /**
     * Method to clear the cache.
     */
    public void clear()
    {
        pinnedCache.clear();
        unpinnedCache.clear();
    }

    /**
     * Accessor for an object from the cache.
     * The returned object will not have a StateManager connected. This is
     * because data stored in the Level 2 cache is StateManager and
     * PersistenceManager independent.
     * @param oid The Object ID
     * @return The L2 cacheable object
     */
    public synchronized CachedPC get(Object oid)
    {
        if (oid == null)
        {
            return null;
        }

        CachedPC pc = (CachedPC)pinnedCache.get(oid);
        if (pc != null)
        {
            return pc;
        }
        pc = (CachedPC)unpinnedCache.get(oid);
        return pc;
    }

    /**
     * Accessor for the number of pinned objects in the cache.
     * @return Number of pinned objects
     */
    public int getNumberOfPinnedObjects()
    {
        return pinnedCache.size();
    }

    /**
     * Accessor for the number of unpinned objects in the cache.
     * @return Number of unpinned objects
     */
    public int getNumberOfUnpinnedObjects()
    {
        return unpinnedCache.size();
    }

    /**
     * Accessor for the total number of objects in the L2 cache.
     * @return Number of objects
     */
    public int getSize()
    {
        return getNumberOfPinnedObjects() + getNumberOfUnpinnedObjects();
    }

    /**
     * Method to put an object in the cache. Note that the pc object being
     * passed in must NOT have a StateManager connected. Data stored in
     * the Level 2 cache has to be independent of PersistenceManager and
     * StateManager.
     * @param oid The Object id for this object
     * @param pc The cacheable object
     * @return The value previously associated with this oid
     */
    public synchronized CachedPC put(Object oid, CachedPC pc)
    {
        if (oid == null || pc == null)
        {
            JPOXLogger.CACHE.warn(LOCALISER.msg("004011"));
            return null;
        }

        // Check if the object is disconnected from its StateManager/PM
        if (ObjectManagerHelper.getObjectManager(pc.getPersistableObject()) != null)
        {
            JPOXLogger.CACHE.error(LOCALISER.msg("004012", oid));
            return null;
        }

        // Check if we should pin this
        // a). check if the object class type is to be pinned
        boolean toBePinned = false;
        if (pinnedClasses != null)
        {
            Iterator pinnedClsIter = pinnedClasses.iterator();
            while (pinnedClsIter.hasNext())
            {
                PinnedClass pinCls = (PinnedClass)pinnedClsIter.next();
                if (pinCls.cls.getName().equals(pc.getPCClass().getName()) ||
                    (pinCls.subclasses && pinCls.cls.isAssignableFrom(pc.getPCClass())))
                {
                    toBePinned = true;
                    break;
                }
            }
        }

        // b). check if the id is to be pinned
        if (pinnedIds != null && pinnedIds.contains(oid))
        {
            toBePinned = true;
        }

        Object obj = null;
        if (pinnedCache.get(oid) != null)
        {
            // Update the pinned cache if object is already there
            obj = pinnedCache.put(oid, pc);
            if (obj != null)
            {
                return (CachedPC)obj;
            }
        }
        else
        {
            if (toBePinned)
            {
                // Update the pinned cache
                pinnedCache.put(oid, pc);
                unpinnedCache.remove(oid); // Just in case it was unpinned previously
            }
            else
            {
                // Update the unpinned cache otherwise
                obj = unpinnedCache.put(oid, pc);
                if (obj != null)
                {
                    // Values in a WeakRefCache are of type Reference
                    Reference ref = (Reference)obj;
                    return (CachedPC)ref.get();
                }
            }
        }

        return null;
    }

    /**
     * Method to check if an object with the specified id is in the cache
     * @param oid The object ID
     * @return Whether it is present
     */
    public boolean containsOid(Object oid)
    {
        return (pinnedCache.containsKey(oid) || unpinnedCache.containsKey(oid));
    }

    /**
     * Accessor for whether the cache is empty.
     * @return Whether it is empty.
     */
    public boolean isEmpty()
    {
        return (pinnedCache.isEmpty() && unpinnedCache.isEmpty());
    }
}
TOP

Related Classes of org.jpox.cache.DefaultLevel2Cache

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.