Mercurial > hg > blitz_condensed
comparison test/org/dancres/blitz/arc/ArcTest.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.arc; | |
2 | |
3 import java.io.IOException; | |
4 | |
5 import java.util.HashMap; | |
6 import java.util.Random; | |
7 | |
8 import org.dancres.blitz.cache.Identifiable; | |
9 import org.dancres.blitz.cache.Identifier; | |
10 | |
11 public class ArcTest { | |
12 private static Random theRNG = new Random(); | |
13 private static int theMaxEntries; | |
14 private static ArcCache theCache; | |
15 | |
16 public static void main(String args[]) { | |
17 BackingStore myStore = new BackingStoreImpl(); | |
18 | |
19 System.out.println("ArcTest with CacheSize: " + args[0] + | |
20 " Max Entry:" + args[1]); | |
21 | |
22 theCache = new ArcCache(myStore, Integer.parseInt(args[0])); | |
23 | |
24 theMaxEntries = Integer.parseInt(args[1]); | |
25 | |
26 try { | |
27 // Write the entries in | |
28 for (int i = 0; i < theMaxEntries; i++) { | |
29 IdentifierImpl myId = new IdentifierImpl(i); | |
30 Element myElement = new Element(myId, i); | |
31 theCache.insert(myElement); | |
32 } | |
33 | |
34 while (true) { | |
35 int myChoice = theRNG.nextInt(2); | |
36 | |
37 switch(myChoice) { | |
38 case 0 : { | |
39 randomSelect(); | |
40 break; | |
41 } | |
42 | |
43 case 1 : { | |
44 randomRange(); | |
45 break; | |
46 } | |
47 } | |
48 } | |
49 } catch (Exception anE) { | |
50 System.err.println("Exceptioned"); | |
51 anE.printStackTrace(System.err); | |
52 } | |
53 } | |
54 | |
55 private static final void randomSelect() throws Exception { | |
56 // Random select | |
57 int myInt = theRNG.nextInt(theMaxEntries); | |
58 | |
59 System.out.println("Random: " + myInt); | |
60 CacheBlockDescriptor myCBD = | |
61 theCache.find(new IdentifierImpl(myInt)); | |
62 | |
63 myCBD.release(); | |
64 } | |
65 | |
66 private static final void randomRange() throws Exception { | |
67 // Random start point | |
68 int myStart = theRNG.nextInt(theMaxEntries); | |
69 | |
70 int myLength = theRNG.nextInt((theMaxEntries - myStart)); | |
71 | |
72 System.out.println("Linear: " + myStart + ", " + myLength); | |
73 | |
74 for (int i = myStart; i < (myStart + myLength); i++) { | |
75 CacheBlockDescriptor myCBD = | |
76 theCache.find(new IdentifierImpl(i)); | |
77 | |
78 myCBD.release(); | |
79 } | |
80 } | |
81 | |
82 private static final class IdentifierImpl implements Identifier { | |
83 private int theInt; | |
84 | |
85 IdentifierImpl(int anInt) { | |
86 theInt = anInt; | |
87 } | |
88 | |
89 public boolean equals(Object anObject) { | |
90 if (anObject instanceof IdentifierImpl) { | |
91 IdentifierImpl myOther = (IdentifierImpl) anObject; | |
92 | |
93 return (myOther.theInt == theInt); | |
94 } | |
95 | |
96 return false; | |
97 } | |
98 | |
99 public int hashCode() { | |
100 return theInt; | |
101 } | |
102 | |
103 public int compareTo(Object anObject) { | |
104 IdentifierImpl myOther = (IdentifierImpl) anObject; | |
105 | |
106 return theInt - myOther.theInt; | |
107 } | |
108 | |
109 public String toString() { | |
110 return "Id: " + theInt; | |
111 } | |
112 } | |
113 | |
114 private static final class Element implements Identifiable { | |
115 private IdentifierImpl theId; | |
116 private int theVal; | |
117 | |
118 Element(IdentifierImpl anId, int aVal) { | |
119 theId = anId; | |
120 theVal = aVal; | |
121 } | |
122 | |
123 public Identifier getId() { | |
124 return theId; | |
125 } | |
126 | |
127 public String toString() { | |
128 return "Element: " + theVal; | |
129 } | |
130 } | |
131 | |
132 private static final class BackingStoreImpl implements BackingStore { | |
133 private HashMap thePrepared = new HashMap(); | |
134 private HashMap theStorage = new HashMap(); | |
135 | |
136 public String getName() { | |
137 return "ArcTest::BackingStoreImpl"; | |
138 } | |
139 | |
140 public void prepareForCaching(Identifiable anIdentifiable) { | |
141 synchronized(thePrepared) { | |
142 thePrepared.put(anIdentifiable.getId(), anIdentifiable); | |
143 } | |
144 } | |
145 | |
146 /** | |
147 @return Identifiable associated with Identifier or <code>null</code> | |
148 if it cannot be found. | |
149 */ | |
150 public Identifiable load(Identifier anId) throws IOException { | |
151 Identifiable myIdent; | |
152 | |
153 // First check prepcache | |
154 synchronized(thePrepared) { | |
155 myIdent = (Identifiable) thePrepared.remove(anId); | |
156 | |
157 if (myIdent != null) { | |
158 synchronized(theStorage) { | |
159 theStorage.put(anId, myIdent); | |
160 } | |
161 | |
162 return myIdent; | |
163 } | |
164 } | |
165 | |
166 synchronized(theStorage) { | |
167 myIdent = (Identifiable) theStorage.get(anId); | |
168 | |
169 if (myIdent == null) { | |
170 System.err.println("Panic: storage lost entry"); | |
171 throw new RuntimeException(); | |
172 } else | |
173 return myIdent; | |
174 } | |
175 } | |
176 | |
177 /** | |
178 Must deal with handling of delete, update and write. | |
179 */ | |
180 public void save(Identifiable anIdentifiable) throws IOException { | |
181 // Nothing for us to do in this case | |
182 } | |
183 | |
184 public void force() throws IOException { | |
185 // Nothing for us to do in this case | |
186 } | |
187 } | |
188 } |