001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence;
020:
021: import java.util.Arrays;
022: import java.util.BitSet;
023: import java.util.Collection;
024:
025: import org.apache.openjpa.datacache.DataCache;
026: import org.apache.openjpa.datacache.DelegatingDataCache;
027: import org.apache.openjpa.meta.ClassMetaData;
028: import org.apache.openjpa.meta.MetaDataRepository;
029:
030: /**
031: * Implements the L2 cache over the data store via delegation to DataCache.
032: *
033: * @author Abe White
034: * @since 0.4.1
035: * @nojavadoc
036: */
037: public class StoreCacheImpl implements StoreCache {
038:
039: private final MetaDataRepository _repos;
040: private final DelegatingDataCache _cache;
041:
042: /**
043: * Constructor; supply delegate.
044: */
045: public StoreCacheImpl(EntityManagerFactoryImpl emf, DataCache cache) {
046: _repos = emf.getConfiguration().getMetaDataRepositoryInstance();
047: _cache = new DelegatingDataCache(cache,
048: PersistenceExceptions.TRANSLATOR);
049: }
050:
051: /**
052: * Delegate.
053: */
054: public DataCache getDelegate() {
055: return _cache.getDelegate();
056: }
057:
058: public boolean contains(Class cls, Object oid) {
059: return _cache.getDelegate() != null
060: && _cache.contains(JPAFacadeHelper.toOpenJPAObjectId(
061: getMetaData(cls), oid));
062: }
063:
064: public boolean containsAll(Class cls, Object... oids) {
065: return containsAll(cls, Arrays.asList(oids));
066: }
067:
068: public boolean containsAll(Class cls, Collection oids) {
069: if (_cache.getDelegate() == null)
070: return oids.isEmpty();
071:
072: BitSet set = _cache.containsAll(JPAFacadeHelper
073: .toOpenJPAObjectIds(getMetaData(cls), oids));
074: for (int i = 0; i < oids.size(); i++)
075: if (!set.get(i))
076: return false;
077: return true;
078: }
079:
080: public void pin(Class cls, Object oid) {
081: if (_cache.getDelegate() != null)
082: _cache.pin(JPAFacadeHelper.toOpenJPAObjectId(
083: getMetaData(cls), oid));
084: }
085:
086: public void pinAll(Class cls, Object... oids) {
087: pinAll(cls, Arrays.asList(oids));
088: }
089:
090: public void pinAll(Class cls, Collection oids) {
091: if (_cache.getDelegate() != null)
092: _cache.pinAll(JPAFacadeHelper.toOpenJPAObjectIds(
093: getMetaData(cls), oids));
094: }
095:
096: public void unpin(Class cls, Object oid) {
097: if (_cache.getDelegate() != null)
098: _cache.unpin(JPAFacadeHelper.toOpenJPAObjectId(
099: getMetaData(cls), oid));
100: }
101:
102: public void unpinAll(Class cls, Object... oids) {
103: unpinAll(cls, Arrays.asList(oids));
104: }
105:
106: public void unpinAll(Class cls, Collection oids) {
107: if (_cache.getDelegate() != null)
108: _cache.unpinAll(JPAFacadeHelper.toOpenJPAObjectIds(
109: getMetaData(cls), oids));
110: }
111:
112: public void evict(Class cls, Object oid) {
113: if (_cache.getDelegate() != null)
114: _cache.remove(JPAFacadeHelper.toOpenJPAObjectId(
115: getMetaData(cls), oid));
116: }
117:
118: public void evictAll(Class cls, Object... oids) {
119: evictAll(cls, Arrays.asList(oids));
120: }
121:
122: public void evictAll(Class cls, Collection oids) {
123: if (_cache.getDelegate() != null)
124: _cache.removeAll(JPAFacadeHelper.toOpenJPAObjectIds(
125: getMetaData(cls), oids));
126: }
127:
128: public void evictAll() {
129: _cache.clear();
130: }
131:
132: /**
133: * Return metadata for the given class, throwing the proper exception
134: * if not persistent.
135: */
136: private ClassMetaData getMetaData(Class cls) {
137: try {
138: return _repos.getMetaData(cls, null, true);
139: } catch (RuntimeException re) {
140: throw PersistenceExceptions.toPersistenceException(re);
141: }
142: }
143:
144: public int hashCode() {
145: return _cache.hashCode();
146: }
147:
148: public boolean equals(Object other) {
149: if (other == this )
150: return true;
151: if (!(other instanceof StoreCacheImpl))
152: return false;
153: return _cache.equals(((StoreCacheImpl) other)._cache);
154: }
155: }
|