StoreManager proxy that delegates to a data cache when possible.
| Method from org.apache.openjpa.datacache.DataCacheStoreManager Detail: |
public void begin() {
super.begin();
}
|
public void commit() {
try {
super.commit();
updateCaches();
} finally {
_inserts = null;
_updates = null;
_deletes = null;
}
}
|
public boolean exists(OpenJPAStateManager sm,
Object edata) {
DataCache cache = sm.getMetaData().getDataCache();
if (cache != null && !isLocking(null)
&& cache.contains(sm.getObjectId()))
return true;
return super.exists(sm, edata);
}
|
public Collection flush(Collection states) {
Collection exceps = super.flush(states);
// if there were errors evict bad instances and don't record changes
if (!exceps.isEmpty()) {
for (Iterator iter = exceps.iterator(); iter.hasNext(); ) {
Exception e = (Exception) iter.next();
if (e instanceof OptimisticException)
notifyOptimisticLockFailure((OptimisticException) e);
}
return exceps;
}
// if large transaction mode don't record individual changes
if (_ctx.isTrackChangesByType())
return exceps;
OpenJPAStateManager sm;
for (Iterator itr = states.iterator(); itr.hasNext();) {
sm = (OpenJPAStateManager) itr.next();
if (sm.getPCState() == PCState.PNEW && !sm.isFlushed()) {
if (_inserts == null)
_inserts = new ArrayList();
_inserts.add(sm);
// may have been re-persisted
if (_deletes != null)
_deletes.remove(sm);
} else if (_inserts != null
&& (sm.getPCState() == PCState.PNEWDELETED
|| sm.getPCState() == PCState.PNEWFLUSHEDDELETED))
_inserts.remove(sm);
else if (sm.getPCState() == PCState.PDIRTY) {
if (_updates == null)
_updates = new HashMap();
_updates.put(sm, sm.getDirty());
} else if (sm.getPCState() == PCState.PDELETED) {
if (_deletes == null)
_deletes = new HashSet();
_deletes.add(sm);
}
}
return Collections.EMPTY_LIST;
}
|
public boolean initialize(OpenJPAStateManager sm,
PCState state,
FetchConfiguration fetch,
Object edata) {
DataCache cache = sm.getMetaData().getDataCache();
if (cache == null || sm.isEmbedded())
return super.initialize(sm, state, fetch, edata);
DataCachePCData data = cache.get(sm.getObjectId());
if (data != null && !isLocking(fetch)) {
//### the 'data.type' access here probably needs to be
//### addressed for bug 511
sm.initialize(data.getType(), state);
data.load(sm, fetch, edata);
return true;
}
// initialize from store manager
if (!super.initialize(sm, state, fetch, edata))
return false;
if (!_ctx.getPopulateDataCache())
return true;
// make sure that we're not trying to cache an old version
cache.writeLock();
try {
data = cache.get(sm.getObjectId());
if (data != null && compareVersion(sm, sm.getVersion(),
data.getVersion()) == VERSION_EARLIER)
return true;
// cache newly loaded info. It is safe to cache data frorm
// initialize() because this method is only called upon
// initial load of the data.
if (data == null)
data = newPCData(sm);
data.store(sm);
cache.put(data);
} finally {
cache.writeUnlock();
}
return true;
}
|
public boolean load(OpenJPAStateManager sm,
BitSet fields,
FetchConfiguration fetch,
int lockLevel,
Object edata) {
DataCache cache = sm.getMetaData().getDataCache();
if (cache == null || sm.isEmbedded())
return super.load(sm, fields, fetch, lockLevel, edata);
DataCachePCData data = cache.get(sm.getObjectId());
if (lockLevel == LockLevels.LOCK_NONE && !isLocking(fetch)
&& data != null)
data.load(sm, fields, fetch, edata);
if (fields.length() == 0)
return true;
// load from store manager; clone the set of still-unloaded fields
// so that if the store manager decides to modify it it won't affect us
if (!super.load(sm, (BitSet) fields.clone(), fetch, lockLevel, edata))
return false;
if (!_ctx.getPopulateDataCache())
return true;
// Do not load changes into cache if the instance has been flushed
if (sm.isFlushed())
return true;
// make sure that we're not trying to cache an old version
cache.writeLock();
try {
data = cache.get(sm.getObjectId());
if (data != null && compareVersion(sm, sm.getVersion(),
data.getVersion()) == VERSION_EARLIER)
return true;
// cache newly loaded info
boolean isNew = data == null;
if (isNew)
data = newPCData(sm);
data.store(sm, fields);
if (isNew)
cache.put(data);
else
cache.update(data);
} finally {
cache.writeUnlock();
}
return true;
}
|
public Collection loadAll(Collection sms,
PCState state,
int load,
FetchConfiguration fetch,
Object edata) {
if (isLocking(fetch))
return super.loadAll(sms, state, load, fetch, edata);
Map unloaded = null;
List smList = null;
Map caches = new HashMap();
OpenJPAStateManager sm;
DataCache cache;
DataCachePCData data;
BitSet fields;
for (Iterator itr = sms.iterator(); itr.hasNext();) {
sm = (OpenJPAStateManager) itr.next();
cache = sm.getMetaData().getDataCache();
if (cache == null || sm.isEmbedded()) {
unloaded = addUnloaded(sm, null, unloaded);
continue;
}
if (sm.getManagedInstance() == null
|| load != FORCE_LOAD_NONE
|| sm.getPCState() == PCState.HOLLOW) {
smList = (List) caches.get(cache);
if (smList == null) {
smList = new ArrayList();
caches.put(cache, smList);
}
smList.add(sm);
} else if (!cache.contains(sm.getObjectId()))
unloaded = addUnloaded(sm, null, unloaded);
}
for (Iterator itr = caches.keySet().iterator(); itr.hasNext();) {
cache = (DataCache) itr.next();
smList = (List) caches.get(cache);
List oidList = new ArrayList(smList.size());
for (itr=smList.iterator();itr.hasNext();) {
sm = (OpenJPAStateManager) itr.next();
oidList.add((OpenJPAId) sm.getObjectId());
}
Map dataMap = cache.getAll(oidList);
for (itr=smList.iterator();itr.hasNext();) {
sm = (OpenJPAStateManager) itr.next();
data = (DataCachePCData) dataMap.get(
(OpenJPAId) sm.getObjectId());
if (sm.getManagedInstance() == null) {
if (data != null) {
//### the 'data.type' access here probably needs
//### to be addressed for bug 511
sm.initialize(data.getType(), state);
data.load(sm, fetch, edata);
} else
unloaded = addUnloaded(sm, null, unloaded);
} else if (load != FORCE_LOAD_NONE
|| sm.getPCState() == PCState.HOLLOW) {
data = cache.get(sm.getObjectId());
if (data != null) {
// load unloaded fields
fields = sm.getUnloaded(fetch);
data.load(sm, fields, fetch, edata);
if (fields.length() > 0)
unloaded = addUnloaded(sm, fields, unloaded);
} else
unloaded = addUnloaded(sm, null, unloaded);
}
}
}
if (unloaded == null)
return Collections.EMPTY_LIST;
// load with delegate
Collection failed = super.loadAll(unloaded.keySet(), state, load,
fetch, edata);
if (!_ctx.getPopulateDataCache())
return failed;
// for each loaded instance, merge loaded state into cached data
Map.Entry entry;
boolean isNew;
for (Iterator itr = unloaded.entrySet().iterator(); itr.hasNext();) {
entry = (Map.Entry) itr.next();
sm = (OpenJPAStateManager) entry.getKey();
fields = (BitSet) entry.getValue();
cache = sm.getMetaData().getDataCache();
if (cache == null || sm.isEmbedded() || (failed != null
&& failed.contains(sm.getId())))
continue;
// make sure that we're not trying to cache an old version
cache.writeLock();
try {
data = cache.get(sm.getObjectId());
if (data != null && compareVersion(sm, sm.getVersion(),
data.getVersion()) == VERSION_EARLIER)
continue;
isNew = data == null;
if (isNew)
data = newPCData(sm);
if (fields == null)
data.store(sm);
else
data.store(sm, fields);
if (isNew)
cache.put(data);
else
cache.update(data);
} finally {
cache.writeUnlock();
}
}
return failed;
}
|
public StoreQuery newQuery(String language) {
StoreQuery q = super.newQuery(language);
// if the query can't be parsed or it's using a non-parsed language
// (one for which there is no ExpressionParser), we can't cache it.
if (q == null || QueryLanguages.parserForLanguage(language) == null)
return q;
QueryCache queryCache = _ctx.getConfiguration().
getDataCacheManagerInstance().getSystemQueryCache();
if (queryCache == null)
return q;
return new QueryCacheStoreQuery(q, queryCache);
}
|
public void rollback() {
try {
super.rollback();
} finally {
_inserts = null;
_updates = null;
_deletes = null;
}
}
|
public void setContext(StoreContext ctx) {
_ctx = ctx;
_gen = ctx.getConfiguration().getDataCacheManagerInstance().
getPCDataGenerator();
super.setContext(ctx);
}
|
public boolean syncVersion(OpenJPAStateManager sm,
Object edata) {
DataCache cache = sm.getMetaData().getDataCache();
if (cache == null || sm.isEmbedded())
return super.syncVersion(sm, edata);
DataCachePCData data;
Object version = null;
data = cache.get(sm.getObjectId());
if (!isLocking(null) && data != null)
version = data.getVersion();
// if we have a cached version update from there
if (version != null) {
if (!version.equals(sm.getVersion())) {
sm.setVersion(version);
return false;
}
return true;
}
// use data store version
return super.syncVersion(sm, edata);
}
|