KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > fjank > tests > DiskBackedHashtable


1 package org.fjank.tests;
2
3 import java.io.Externalizable JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.ObjectInput JavaDoc;
6 import java.io.ObjectOutput JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
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 JavaDoc[] 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 JavaDoc cacheMap = factory.getMapAccess();
30             long startTime = System.currentTimeMillis();
31             System.out.println("writing 250,000 values");
32             Map JavaDoc map = new HashMap JavaDoc();
33             try {
34                 Thread.sleep(5000);
35             } catch (InterruptedException JavaDoc e1) {
36                 // TODO Auto-generated catch block
37
e1.printStackTrace();
38             }
39             for (int loop = 0; loop < 250000; loop++) {
40                 String JavaDoc key = "Key:" + loop;
41                 String JavaDoc 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 JavaDoc set = cacheMap.keySet();
50             Iterator JavaDoc 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 JavaDoc key = "Key:" + loop;
66                 Key keyObj = new Key(key);
67                 Value valueObj = (Value) cacheMap.get(keyObj);
68                 if (valueObj == null) {
69                     throw new InternalError JavaDoc("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 JavaDoc {
83         private String JavaDoc val;
84
85         public Value() {
86         }
87
88         public Value(String JavaDoc val) {
89             this.val = val;
90         }
91
92         public void readExternal(ObjectInput JavaDoc input) throws IOException JavaDoc {
93             val = input.readUTF();
94         }
95
96         public void writeExternal(ObjectOutput JavaDoc output) throws IOException JavaDoc {
97             output.writeUTF(val);
98         }
99     }
100     public static class Key implements Externalizable JavaDoc {
101         private String JavaDoc val;
102
103         public Key() {
104         }
105
106         public Key(String JavaDoc val) {
107             this.val = val;
108         }
109
110         public void readExternal(ObjectInput JavaDoc input) throws IOException JavaDoc {
111             val = input.readUTF();
112         }
113
114         public void writeExternal(ObjectOutput JavaDoc output) throws IOException JavaDoc {
115             output.writeUTF(val);
116         }
117
118         public int hashCode() {
119             return val.hashCode();
120         }
121
122         public boolean equals(Object JavaDoc 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