KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Bug1097240


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