Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/entry/ci/BitCacheLines.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.entry.ci; | |
2 | |
3 import com.go.trove.util.IntHashMap; | |
4 | |
5 import org.dancres.blitz.entry.EntrySleeve; | |
6 | |
7 import org.dancres.struct.BitIndex; | |
8 | |
9 /** | |
10 Maintains a set of cache lines (key'd by hashcode) for a particular | |
11 field of one type of Entry. | |
12 */ | |
13 class BitCacheLines { | |
14 private IntHashMap theHashCodes = new IntHashMap(); | |
15 private int theOffset; | |
16 private String theName; | |
17 | |
18 private int theSize; | |
19 | |
20 BitCacheLines(int anOffset, String aName, int aSize) { | |
21 theOffset = anOffset; | |
22 theName = aName; | |
23 theSize = aSize; | |
24 } | |
25 | |
26 String getName() { | |
27 return theName; | |
28 } | |
29 | |
30 BitIndex getHits(int aHashcode) { | |
31 BitCacheLine myEntries = getLine(aHashcode, false); | |
32 | |
33 if (myEntries == null) | |
34 return null; | |
35 | |
36 return myEntries.getSlots(); | |
37 } | |
38 | |
39 /** | |
40 @return the number of entries under a particular hashcode | |
41 */ | |
42 int getSize(int aHashcode) { | |
43 BitCacheLine myEntries = getLine(aHashcode, false); | |
44 | |
45 if (myEntries == null) | |
46 return 0; | |
47 | |
48 return myEntries.getSize(); | |
49 } | |
50 | |
51 /** | |
52 @return the number of different hashcodes we know about | |
53 */ | |
54 int getSize() { | |
55 synchronized(theHashCodes) { | |
56 return theHashCodes.size(); | |
57 } | |
58 } | |
59 | |
60 void insert(EntrySleeve aSleeve, int aSlot) { | |
61 int myKey = getKey(aSleeve); | |
62 | |
63 synchronized(theHashCodes) { | |
64 BitCacheLine myEntries = getLine(myKey, true); | |
65 myEntries.insert(aSleeve, aSlot); | |
66 } | |
67 } | |
68 | |
69 void remove(EntrySleeve aSleeve, int aSlot) { | |
70 int myKey = getKey(aSleeve); | |
71 | |
72 synchronized (theHashCodes) { | |
73 BitCacheLine myEntries = getLine(myKey, false); | |
74 | |
75 myEntries.remove(aSleeve, aSlot); | |
76 | |
77 if (myEntries.getSize() == 0) | |
78 theHashCodes.remove(myKey); | |
79 } | |
80 } | |
81 | |
82 private int getKey(EntrySleeve aSleeve) { | |
83 int myHash = aSleeve.getHashCodeForField(theOffset); | |
84 | |
85 return myHash; | |
86 } | |
87 | |
88 private synchronized BitCacheLine getLine(int aKey, boolean doCreate) { | |
89 BitCacheLine myEntries; | |
90 | |
91 myEntries = (BitCacheLine) theHashCodes.get(aKey); | |
92 | |
93 if ((myEntries == null) && doCreate) { | |
94 myEntries = new BitCacheLine(theSize); | |
95 theHashCodes.put(aKey, myEntries); | |
96 } | |
97 | |
98 return myEntries; | |
99 } | |
100 } |