com.maxifier.mxcache.impl.caches.abs.AbstractIntByteCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mxcache-runtime Show documentation
Show all versions of mxcache-runtime Show documentation
Constains all classes necessary for launching a MxCache-instrumentated application
/*
* Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
*/
package com.maxifier.mxcache.impl.caches.abs;
import com.maxifier.mxcache.CacheFactory;
import com.maxifier.mxcache.caches.*;
import com.maxifier.mxcache.impl.MutableStatistics;
import com.maxifier.mxcache.impl.CacheId;
import com.maxifier.mxcache.impl.CalculatableHelper;
import com.maxifier.mxcache.impl.resource.*;
import com.maxifier.mxcache.provider.CacheDescriptor;
import com.maxifier.mxcache.storage.*;
/**
* THIS IS GENERATED CLASS! DON'T EDIT IT MANUALLY!
*
* GENERATED FROM P2PCache.template
*
* @author Andrey Yakoushin ([email protected])
* @author Alexander Kochurov ([email protected])
*/
public abstract class AbstractIntByteCache extends AbstractCache implements IntByteCache, IntByteStorage {
private final IntByteCalculatable calculatable;
public AbstractIntByteCache(Object owner, IntByteCalculatable calculatable, MutableStatistics statistics) {
super(owner, statistics);
this.calculatable = calculatable;
}
@Override
public byte getOrCreate(int o) {
if (DependencyTracker.isBypassCaches()) {
return calculatable.calculate(owner, o);
} else {
lock();
try {
if (isCalculated(o)) {
DependencyTracker.mark(getDependencyNode());
hit();
return load(o);
}
DependencyNode callerNode = DependencyTracker.track(getDependencyNode());
try {
while(true) {
try {
return create(o);
} catch (ResourceOccupied e) {
if (callerNode != null) {
throw e;
} else {
unlock();
try {
e.getResource().waitForEndOfModification();
} finally {
lock();
}
if (isCalculated(o)) {
hit();
return load(o);
}
}
}
}
} finally {
DependencyTracker.exit(callerNode);
}
} finally {
unlock();
}
}
}
protected byte create(int o) {
long start = System.nanoTime();
byte t = calculatable.calculate(owner, o);
long end = System.nanoTime();
miss(end - start);
save(o, t);
return t;
}
@Override
public CacheDescriptor getDescriptor() {
CacheId id = CalculatableHelper.getId(calculatable.getClass());
return CacheFactory.getProvider().getDescriptor(id);
}
@Override
public String toString() {
return getDescriptor() + ": " + owner;
}
}