|
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.filer; |
|
21 |
| |
|
22 |
| import org.apache.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| import org.apache.xindice.core.DBException; |
|
25 |
| import org.apache.xindice.core.DBObject; |
|
26 |
| import org.apache.xindice.core.FaultCodes; |
|
27 |
| import org.apache.xindice.core.data.Key; |
|
28 |
| import org.apache.xindice.core.data.Value; |
|
29 |
| import org.apache.xindice.util.Configurable; |
|
30 |
| import org.apache.xindice.util.Configuration; |
|
31 |
| |
|
32 |
| import java.io.ByteArrayInputStream; |
|
33 |
| import java.io.ByteArrayOutputStream; |
|
34 |
| import java.io.DataInput; |
|
35 |
| import java.io.DataInputStream; |
|
36 |
| import java.io.DataOutput; |
|
37 |
| import java.io.DataOutputStream; |
|
38 |
| import java.io.File; |
|
39 |
| import java.io.IOException; |
|
40 |
| import java.io.InputStream; |
|
41 |
| import java.io.OutputStream; |
|
42 |
| import java.io.RandomAccessFile; |
|
43 |
| import java.lang.ref.WeakReference; |
|
44 |
| import java.util.Collection; |
|
45 |
| import java.util.EmptyStackException; |
|
46 |
| import java.util.HashMap; |
|
47 |
| import java.util.Iterator; |
|
48 |
| import java.util.Map; |
|
49 |
| import java.util.Stack; |
|
50 |
| import java.util.WeakHashMap; |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| public abstract class Paged implements DBObject, Configurable { |
|
82 |
| |
|
83 |
| private static final Log log = LogFactory.getLog(Paged.class); |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| private static final int MAX_DIRTY_SIZE = 128; |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| protected static final String CONFIG_PAGESIZE = "pagesize"; |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| protected static final String CONFIG_PAGECOUNT = "pagecount"; |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| protected static final String CONFIG_KEYSIZE_MAX = "maxkeysize"; |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| protected static final String CONFIG_DESCRIPTORS_MAX = "max-descriptors"; |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| private static final int DEFAULT_PAGESIZE = 4096; |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| private static final int DEFAULT_PAGECOUNT = 1024; |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| private static final int FILE_HEADER_SIZE = 4096; |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| private static final int DEFAULT_KEYSIZE_MAX = 256; |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| private static final int DEFAULT_DESCRIPTORS_MAX = 16; |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| protected static final byte UNUSED = 0; |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| protected static final byte OVERFLOW = 126; |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| protected static final byte DELETED = 127; |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| protected static final int NO_PAGE = -1; |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| private Configuration config; |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| private final Map pages = new WeakHashMap(); |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| private final Object pagesLock = new Object(); |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| private Map dirty = new HashMap(); |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| private final Object dirtyLock = new Object(); |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| private final Stack descriptors = new Stack(); |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| private int descriptorsCount; |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| private int descriptorsMax; |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| private boolean opened; |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| private File file; |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| private final FileHeader fileHeader; |
|
223 |
| |
|
224 |
| |
|
225 |
1134
| public Paged() {
|
|
226 |
1134
| descriptorsMax = DEFAULT_DESCRIPTORS_MAX;
|
|
227 |
1134
| fileHeader = createFileHeader();
|
|
228 |
| } |
|
229 |
| |
|
230 |
0
| public Paged(File file) {
|
|
231 |
0
| this();
|
|
232 |
0
| setFile(file);
|
|
233 |
| } |
|
234 |
| |
|
235 |
1134
| public void setConfig(Configuration config) {
|
|
236 |
1134
| this.config = config;
|
|
237 |
| |
|
238 |
1134
| fileHeader.setPageSize(config.getIntAttribute(CONFIG_PAGESIZE,
|
|
239 |
| fileHeader.getPageSize())); |
|
240 |
1134
| fileHeader.setPageCount(config.getLongAttribute(CONFIG_PAGECOUNT,
|
|
241 |
| fileHeader.getPageCount())); |
|
242 |
1134
| fileHeader.setMaxKeySize(config.getShortAttribute(CONFIG_KEYSIZE_MAX,
|
|
243 |
| fileHeader.getMaxKeySize())); |
|
244 |
1134
| descriptorsMax = getConfig().getIntAttribute(CONFIG_DESCRIPTORS_MAX,
|
|
245 |
| descriptorsMax); |
|
246 |
| } |
|
247 |
| |
|
248 |
1134
| public Configuration getConfig() {
|
|
249 |
1134
| return config;
|
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
1134
| protected final void setFile(final File file) {
|
|
258 |
1134
| this.file = file;
|
|
259 |
| } |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
849
| protected final File getFile() {
|
|
267 |
849
| return file;
|
|
268 |
| } |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
72826
| protected final RandomAccessFile getDescriptor() throws IOException {
|
|
276 |
72826
| synchronized (descriptors) {
|
|
277 |
| |
|
278 |
72826
| if (!descriptors.empty()) {
|
|
279 |
68803
| return (RandomAccessFile) descriptors.pop();
|
|
280 |
| } |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
4023
| if (descriptorsCount < descriptorsMax) {
|
|
285 |
4023
| descriptorsCount++;
|
|
286 |
4023
| return new RandomAccessFile(file, "rw");
|
|
287 |
| } |
|
288 |
| |
|
289 |
| |
|
290 |
0
| while (true) {
|
|
291 |
0
| try {
|
|
292 |
0
| descriptors.wait();
|
|
293 |
0
| return (RandomAccessFile) descriptors.pop();
|
|
294 |
| } catch (InterruptedException e) { |
|
295 |
| |
|
296 |
| } catch (EmptyStackException e) { |
|
297 |
| |
|
298 |
| } |
|
299 |
| } |
|
300 |
| } |
|
301 |
| } |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
72826
| protected final void putDescriptor(RandomAccessFile raf) {
|
|
307 |
72825
| if (raf != null) {
|
|
308 |
72825
| synchronized (descriptors) {
|
|
309 |
72826
| descriptors.push(raf);
|
|
310 |
72826
| descriptors.notify();
|
|
311 |
| } |
|
312 |
| } |
|
313 |
| } |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
4019
| protected final void closeDescriptor(RandomAccessFile raf) {
|
|
319 |
4023
| if (raf != null) {
|
|
320 |
4023
| try {
|
|
321 |
4023
| raf.close();
|
|
322 |
| } catch (IOException e) { |
|
323 |
| |
|
324 |
| } |
|
325 |
| |
|
326 |
| |
|
327 |
4023
| synchronized (descriptors) {
|
|
328 |
4023
| descriptorsCount --;
|
|
329 |
| } |
|
330 |
| } |
|
331 |
| } |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
87637
| protected final Page getPage(long pageNum) throws IOException {
|
|
341 |
87637
| Page p = updateIdentityMap(pageNum);
|
|
342 |
| |
|
343 |
| |
|
344 |
87637
| p.read();
|
|
345 |
87637
| return p;
|
|
346 |
| } |
|
347 |
| |
|
348 |
98300
| private Page updateIdentityMap(long pageNum) {
|
|
349 |
98299
| final PageKey k = new PageKey(pageNum);
|
|
350 |
98301
| Page p = null;
|
|
351 |
98304
| synchronized (pagesLock) {
|
|
352 |
| |
|
353 |
98307
| WeakReference ref = (WeakReference) pages.get(k);
|
|
354 |
98307
| if (ref != null) {
|
|
355 |
68956
| p = (Page) ref.get();
|
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| } |
|
360 |
| |
|
361 |
| |
|
362 |
98307
| if (p == null) {
|
|
363 |
29351
| p = new Page(pageNum);
|
|
364 |
29351
| pages.put(p, new WeakReference(p));
|
|
365 |
| } |
|
366 |
| } |
|
367 |
| |
|
368 |
98303
| return p;
|
|
369 |
| } |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
13301
| protected final Value readValue(Page page) throws IOException {
|
|
380 |
13301
| final PageHeader sph = page.getPageHeader();
|
|
381 |
13301
| ByteArrayOutputStream bos = new ByteArrayOutputStream(sph.getRecordLen());
|
|
382 |
| |
|
383 |
| |
|
384 |
13301
| Page p = page;
|
|
385 |
13301
| while (true) {
|
|
386 |
13302
| PageHeader ph = p.getPageHeader();
|
|
387 |
| |
|
388 |
| |
|
389 |
13302
| p.streamTo(bos);
|
|
390 |
| |
|
391 |
| |
|
392 |
13302
| long nextPage = ph.getNextPage();
|
|
393 |
13302
| if (nextPage == NO_PAGE) {
|
|
394 |
13301
| break;
|
|
395 |
| } |
|
396 |
1
| p = getPage(nextPage);
|
|
397 |
| } |
|
398 |
| |
|
399 |
| |
|
400 |
13301
| return new Value(bos.toByteArray());
|
|
401 |
| } |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
0
| protected final Value readValue(long page) throws IOException {
|
|
412 |
0
| return readValue(getPage(page));
|
|
413 |
| } |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
28162
| protected final void writeValue(Page page, Value value) throws IOException {
|
|
424 |
28161
| if (value == null) {
|
|
425 |
0
| throw new IOException("Can't write a null value");
|
|
426 |
| } |
|
427 |
| |
|
428 |
28162
| InputStream is = value.getInputStream();
|
|
429 |
| |
|
430 |
| |
|
431 |
28160
| PageHeader hdr = page.getPageHeader();
|
|
432 |
28161
| hdr.setRecordLen(value.getLength());
|
|
433 |
28162
| page.streamFrom(is);
|
|
434 |
| |
|
435 |
| |
|
436 |
28162
| while (is.available() > 0) {
|
|
437 |
3
| Page lpage = page;
|
|
438 |
3
| PageHeader lhdr = hdr;
|
|
439 |
| |
|
440 |
| |
|
441 |
3
| long np = lhdr.getNextPage();
|
|
442 |
3
| if (np != NO_PAGE) {
|
|
443 |
| |
|
444 |
0
| page = getPage(np);
|
|
445 |
| } else { |
|
446 |
| |
|
447 |
3
| page = getFreePage();
|
|
448 |
3
| lhdr.setNextPage(page.getPageNum());
|
|
449 |
| } |
|
450 |
| |
|
451 |
| |
|
452 |
3
| hdr = page.getPageHeader();
|
|
453 |
3
| hdr.setStatus(OVERFLOW);
|
|
454 |
| |
|
455 |
| |
|
456 |
3
| page.streamFrom(is);
|
|
457 |
3
| lpage.write();
|
|
458 |
| } |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
28162
| long np = hdr.getNextPage();
|
|
463 |
28160
| if (np != NO_PAGE) {
|
|
464 |
0
| unlinkPages(np);
|
|
465 |
| } |
|
466 |
| |
|
467 |
28160
| hdr.setNextPage(NO_PAGE);
|
|
468 |
28162
| page.write();
|
|
469 |
| } |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
| |
|
479 |
0
| protected final void writeValue(long page, Value value) throws IOException {
|
|
480 |
0
| writeValue(getPage(page), value);
|
|
481 |
| } |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
3117
| protected void unlinkPages(Page page) throws IOException {
|
|
490 |
| |
|
491 |
| |
|
492 |
3117
| long firstPage = page.pageNum;
|
|
493 |
3117
| while (page.header.nextPage != NO_PAGE) {
|
|
494 |
0
| page = getPage(page.header.nextPage);
|
|
495 |
| } |
|
496 |
3117
| long lastPage = page.pageNum;
|
|
497 |
| |
|
498 |
| |
|
499 |
3117
| synchronized (fileHeader) {
|
|
500 |
| |
|
501 |
| |
|
502 |
|