1 package org.fjank.tests; 2 3 import java.io.Externalizable ; 4 import java.io.IOException ; 5 import java.io.ObjectInput ; 6 import java.io.ObjectOutput ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 import java.util.Set ; 11 import javax.util.jcache.CacheAccessFactory; 12 import javax.util.jcache.CacheAttributes; 13 import javax.util.jcache.CacheException; 14 15 public class DiskBackedHashtable { 16 public static void main(String [] args) { 17 try { 18 CacheAttributes ca = CacheAttributes.getDefaultCacheAttributes(); 19 ca.setDiskPath(System.getProperty("java.io.tmpdir")); 20 ca.setDiskCacheSize(1000); 21 ca.setMemoryCacheSize(10); 22 ca.setMaxObjects(25000); 23 ca.setLocal(); 24 CacheAccessFactory factory = CacheAccessFactory.getInstance(); 25 javax.util.jcache.Cache cache = factory.getCache(); 26 cache.close(); 27 cache.init(ca); 28 System.out.println(ca.toString()); 29 Map cacheMap = factory.getMapAccess(); 30 long startTime = System.currentTimeMillis(); 31 System.out.println("writing 250,000 values"); 32 Map map = new HashMap (); 33 try { 34 Thread.sleep(5000); 35 } catch (InterruptedException e1) { 36 e1.printStackTrace(); 38 } 39 for (int loop = 0; loop < 250000; loop++) { 40 String key = "Key:" + loop; 41 String value = "Value:" + loop; 42 Key keyObj = new Key(key); 43 Value valueObj = new Value(value); 44 cacheMap.put(keyObj, valueObj); 45 if (loop % 10000 == 0) { 46 System.out.println("" + loop + "Insert=" + key + "; value=" + value); 47 } 48 } 49 Set set = cacheMap.keySet(); 50 Iterator iterator = set.iterator(); 51 System.out.println("Enumeration elements ..."); 52 int count = 0; 53 while (iterator.hasNext()) { 54 count++; 55 Key key = (Key) iterator.next(); 56 Value val = (Value) cacheMap.get(key); 57 if (count % 10000 == 0) { 58 System.out.println("" + count + "Enumeration key=" + key.val + "; value=" + val.val); 59 } 60 } 61 62 System.out.println("Enumeration " + count + " elements"); 63 System.out.println("reading 250,000 values"); 64 for (int loop = 0; loop < 250000; loop++) { 65 String key = "Key:" + loop; 66 Key keyObj = new Key(key); 67 Value valueObj = (Value) cacheMap.get(keyObj); 68 if (valueObj == null) { 69 throw new InternalError ("Failed"); 70 } 71 if (count % 10000 == 0) { 72 System.out.println("Key=" + key + "; value=" + valueObj.val); 73 } 74 } 75 long duration = System.currentTimeMillis() - startTime; 76 System.out.println("Test run took " + duration); 77 } catch (CacheException e) { 78 e.printStackTrace(); 79 } 80 } 81 82 public static class Value implements Externalizable { 83 private String val; 84 85 public Value() { 86 } 87 88 public Value(String val) { 89 this.val = val; 90 } 91 92 public void readExternal(ObjectInput input) throws IOException { 93 val = input.readUTF(); 94 } 95 96 public void writeExternal(ObjectOutput output) throws IOException { 97 output.writeUTF(val); 98 } 99 } 100 public static class Key implements Externalizable { 101 private String val; 102 103 public Key() { 104 } 105 106 public Key(String val) { 107 this.val = val; 108 } 109 110 public void readExternal(ObjectInput input) throws IOException { 111 val = input.readUTF(); 112 } 113 114 public void writeExternal(ObjectOutput output) throws IOException { 115 output.writeUTF(val); 116 } 117 118 public int hashCode() { 119 return val.hashCode(); 120 } 121 122 public boolean equals(Object other) { 123 if (other == null) 124 return false; 125 if (getClass() != other.getClass()) 126 return false; 127 Key o = (Key) other; 128 return val.equals(o.val); 129 } 130 } 131 } | Popular Tags |