01: package de.regnis.q.sequence.line;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05:
06: import de.regnis.q.sequence.line.simplifier.*;
07:
08: /**
09: * @author Marc Strapetz
10: */
11: final class QSequenceLineFileSystemCache implements QSequenceLineCache {
12:
13: // Static =================================================================
14:
15: public static QSequenceLineFileSystemCache create(
16: QSequenceLineRAData data,
17: QSequenceLineTempDirectoryFactory tempDirectoryFactory,
18: int maximumBytesInMemory, int maximumSegmentSize,
19: QSequenceLineSimplifier simplifier) throws IOException {
20: final QSequenceLineFileSystemCache cache = new QSequenceLineFileSystemCache(
21: data, tempDirectoryFactory, maximumBytesInMemory,
22: maximumSegmentSize, simplifier);
23: final QSequenceLineReader reader = new QSequenceLineReader();
24: final InputStream stream = data.read(0, data.length());
25: reader.read(stream, cache, simplifier);
26: stream.close();
27: return cache;
28: }
29:
30: // Fields =================================================================
31:
32: private final QSequenceLineRAData data;
33: private final QSequenceLineSimplifier simplifier;
34: private final QSequenceLineFileSystemCacheSegments segments;
35:
36: private int lineCount;
37:
38: // Setup ==================================================================
39:
40: private QSequenceLineFileSystemCache(QSequenceLineRAData data,
41: QSequenceLineTempDirectoryFactory tempDirectoryFactory,
42: int maximumBytesInMemory, int maximumSegmentSize,
43: QSequenceLineSimplifier simplifier) {
44: this .data = data;
45: this .simplifier = simplifier;
46: this .segments = new QSequenceLineFileSystemCacheSegments(
47: tempDirectoryFactory, maximumBytesInMemory,
48: maximumSegmentSize);
49: }
50:
51: // Implemented ============================================================
52:
53: public void close() throws IOException {
54: segments.close();
55: }
56:
57: public void addLine(QSequenceLine line) throws IOException {
58: if (lineCount >= Integer.MAX_VALUE) {
59: throw new IOException("Too many lines.");
60: }
61:
62: segments.setFromLengthHash(lineCount, line.getFrom(), line
63: .getContentLength(), line.getCompareHash());
64: lineCount++;
65: }
66:
67: public int getLineCount() {
68: return lineCount;
69: }
70:
71: public QSequenceLine getLine(int index) throws IOException {
72: final long from = segments.getFrom(index);
73: final int length = segments.getLength(index);
74: final byte[] bytes = new byte[length];
75: data.get(bytes, from, length);
76: return new QSequenceLine(from, bytes, simplifier);
77: }
78:
79: public int getLineHash(int index) throws IOException {
80: return segments.getHash(index);
81: }
82: }
|