package org.fjank.tests;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.HashMap;
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 DiskBackedHashtable {
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(25000);
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 250,000 values");
Map map = new HashMap();
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int loop = 0; loop < 250000; loop++) {
String key = "Key:" + loop;
String value = "Value:" + loop;
Key keyObj = new Key(key);
Value valueObj = new Value(value);
cacheMap.put(keyObj, valueObj);
if (loop % 10000 == 0) {
System.out.println("" + loop + "Insert=" + key + "; value=" + value);
}
}
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);
if (count % 10000 == 0) {
System.out.println("" + count + "Enumeration key=" + key.val + "; value=" + val.val);
}
}
System.out.println("Enumeration " + count + " elements");
System.out.println("reading 250,000 values");
for (int loop = 0; loop < 250000; loop++) {
String key = "Key:" + loop;
Key keyObj = new Key(key);
Value valueObj = (Value) cacheMap.get(keyObj);
if (valueObj == null) {
throw new InternalError("Failed");
}
if (count % 10000 == 0) {
System.out.println("Key=" + key + "; value=" + valueObj.val);
}
}
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);
}
}
}