import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.util.jcache.CacheAccessFactory;
import javax.util.jcache.CacheAttributes;
import javax.util.jcache.CacheException;
public class Bug1097240 {
public static void main(String[] args) {
try {
CacheAttributes ca = CacheAttributes.getDefaultCacheAttributes();
ca.setDiskPath(System.getProperty("java.io.tmpdir"));
ca.setDiskCacheSize(1000);
ca.setMemoryCacheSize(10);
ca.setMaxObjects(10000);
ca.setLocal();
CacheAccessFactory factory = CacheAccessFactory.getInstance();
javax.util.jcache.Cache cache = factory.getCache();
cache.close();
cache.init(ca);
System.out.println(ca.toString());
Map cacheMap = factory.getMapAccess();
long startTime = System.currentTimeMillis();
System.out.println("writing 25,000 values");
for (int loop = 0; loop < 25000; loop++) {
String key = "Key:" + loop;
String value = "Value:" + loop;
Key keyObj = new Key(key);
Value valueObj = new Value(value);
cacheMap.put(keyObj, valueObj);
}
Set set = cacheMap.keySet();
Iterator iterator = set.iterator();
System.out.println("Enumeration elements ...");
int count = 0;
while (iterator.hasNext()) {
count++;
Key key = (Key) iterator.next();
Value val = (Value) cacheMap.get(key);
//System.out.println("" + count + "Enumeration key=" + key.val + "; value=" + val.val);
}
System.out.println("Enumeration " + count + " elements");
System.out.println("reading 25,000 values");
for (int loop = 0; loop < 25000; loop++) {
String key = "Key:" + loop;
Key keyObj = new Key(key);
Object object = cacheMap.get(keyObj);
Value valueObj=null;
try {
valueObj = (Value) object;
} catch (RuntimeException e1) {
e1.printStackTrace();
}
if (valueObj == null)
throw new InternalError("Failed");
//System.out.println("Key=" + key + "; value="+ valueObj.val);
}
Key keyObj = new Key("bob");
System.out.println("failed get=" + cacheMap.get(keyObj));
long duration = System.currentTimeMillis() - startTime;
System.out.println("Test run took " + duration);
} catch (CacheException e) {
e.printStackTrace();
}
}
public static class Value implements Externalizable {
private String val;
public Value() {
}
public Value(String val) {
this.val = val;
}
public void readExternal(ObjectInput input) throws IOException {
val = input.readUTF();
}
public void writeExternal(ObjectOutput output) throws IOException {
output.writeUTF(val);
}
}
public static class Key implements Externalizable {
private String val;
public Key() {
}
public Key(String val) {
this.val = val;
}
public void readExternal(ObjectInput input) throws IOException {
val = input.readUTF();
}
public void writeExternal(ObjectOutput output) throws IOException {
output.writeUTF(val);
}
public int hashCode() {
return val.hashCode();
}
public boolean equals(Object other) {
if (other == null)
return false;
if (getClass() != other.getClass())
return false;
Key o = (Key) other;
return val.equals(o.val);
}
}
}