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