1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.core.cache; |
21 |
| |
22 |
| import org.apache.commons.logging.Log; |
23 |
| import org.apache.commons.logging.LogFactory; |
24 |
| import org.apache.xindice.core.Collection; |
25 |
| import org.apache.xindice.core.data.Entry; |
26 |
| import org.apache.xindice.core.data.Key; |
27 |
| import org.apache.xindice.core.data.Value; |
28 |
| import org.apache.xindice.xml.NodeSource; |
29 |
| import org.apache.xindice.xml.SymbolTable; |
30 |
| import org.apache.xindice.xml.dom.DBDocument; |
31 |
| import org.apache.xindice.xml.dom.DOMParser; |
32 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
33 |
| |
34 |
| import org.w3c.dom.Document; |
35 |
| |
36 |
| import java.util.Map; |
37 |
| import java.util.WeakHashMap; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| public class DocumentCacheImpl implements DocumentCache { |
51 |
| |
52 |
| private static final Log log = LogFactory.getLog(DocumentCacheImpl.class); |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| private final Map table = new WeakHashMap(); |
58 |
| |
59 |
| |
60 |
62340
| public Entry getEntry(Collection col, Key key) {
|
61 |
62340
| final CacheKey ckey = new CacheKey(col, key);
|
62 |
62340
| Entry e;
|
63 |
62340
| synchronized (table) {
|
64 |
62340
| e = (Entry) table.get(ckey);
|
65 |
| } |
66 |
62340
| if (log.isDebugEnabled()) {
|
67 |
0
| log.debug("Get " + ckey + ": " + (e == null ? "MISS" : "HIT"));
|
68 |
| } |
69 |
62340
| if (e == null) {
|
70 |
44231
| return null;
|
71 |
| } |
72 |
| |
73 |
18109
| switch (e.getEntryType()) {
|
74 |
18087
| case Entry.DOCUMENT:
|
75 |
18087
| Document doc;
|
76 |
18087
| if (col.isCompressed()) {
|
77 |
14944
| SymbolTable s = col.getSymbols();
|
78 |
14944
| NodeSource ns = new NodeSource(col, key);
|
79 |
14944
| doc = new DocumentImpl(((Value) e.getValue()).getData(), s, ns);
|
80 |
| } else { |
81 |
3143
| try {
|
82 |
3143
| doc = DOMParser.toDocument((Value) e.getValue());
|
83 |
3143
| ((DBDocument) doc).setSource(new NodeSource(col, key));
|
84 |
| } catch (Exception ex) { |
85 |
0
| if (log.isWarnEnabled()) {
|
86 |
0
| log.warn("ignored exception", ex);
|
87 |
| } |
88 |
0
| return null;
|
89 |
| } |
90 |
| } |
91 |
18087
| return new Entry(key, doc, e.getMeta());
|
92 |
| |
93 |
22
| case Entry.BINARY:
|
94 |
22
| return new Entry(key, ((Value) e.getValue()).getData(), e.getMeta());
|
95 |
| |
96 |
0
| default:
|
97 |
0
| throw new IllegalStateException("Invalid cache entry type: <" + e.getEntryType() + ">");
|
98 |
| } |
99 |
| } |
100 |
| |
101 |
49
| public Entry getEntryMeta(Collection col, Key key) {
|
102 |
49
| Entry e;
|
103 |
49
| synchronized (table) {
|
104 |
49
| e = (Entry) table.get(new CacheKey(col, key));
|
105 |
| } |
106 |
49
| if (e == null) {
|
107 |
0
| return null;
|
108 |
| } |
109 |
| |
110 |
49
| return new Entry(key, e.getMeta());
|
111 |
| } |
112 |
| |
113 |
45449
| public void putEntry(Collection col, Key key, byte type, Value value, Map meta) {
|
114 |
45449
| CacheKey ckey = new CacheKey(col, key);
|
115 |
45449
| synchronized (table) {
|
116 |
45449
| table.put(ckey, new Entry(type, key, value, meta));
|
117 |
| } |
118 |
| } |
119 |
| |
120 |
0
| public void putEntryMeta(Collection col, Key key, byte type, Map meta) {
|
121 |
0
| CacheKey ckey = new CacheKey(col, key);
|
122 |
0
| synchronized (table) {
|
123 |
0
| Entry e = (Entry) table.get(ckey);
|
124 |
0
| if (e == null) {
|
125 |
0
| e = new Entry(type, key, null, meta);
|
126 |
| } else { |
127 |
0
| e = new Entry(type, key, e.getValue(), meta);
|
128 |
| } |
129 |
| |
130 |
0
| table.put(ckey, e);
|
131 |
| } |
132 |
| } |
133 |
| |
134 |
1959
| public void removeEntry(Collection col, Key key) {
|
135 |
1959
| synchronized (table) {
|
136 |
1959
| table.remove(new CacheKey(col, key));
|
137 |
| } |
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| } |