001: package de.regnis.q.sequence.line;
002:
003: import java.io.*;
004:
005: import de.regnis.q.sequence.core.*;
006:
007: /**
008: * @author Marc Strapetz
009: */
010: class QSequenceLineFileSystemCacheSegment {
011:
012: // Fields =================================================================
013:
014: private final long segmentIndex;
015: private final int maximumEntryCount;
016:
017: private long[] froms;
018: private int[] lengths;
019: private int[] hashes;
020:
021: // Setup ==================================================================
022:
023: public QSequenceLineFileSystemCacheSegment(long segmentIndex,
024: int maximumEntryCount) {
025: this .segmentIndex = segmentIndex;
026: this .maximumEntryCount = maximumEntryCount;
027: this .froms = new long[maximumEntryCount];
028: this .lengths = new int[maximumEntryCount];
029: this .hashes = new int[maximumEntryCount];
030: }
031:
032: // Accessing ==============================================================
033:
034: public boolean isLoaded() {
035: return froms != null;
036: }
037:
038: public long getFrom(int index) {
039: return froms[index];
040: }
041:
042: public int getLength(int index) {
043: return lengths[index];
044: }
045:
046: public int getHash(int index) {
047: return hashes[index];
048: }
049:
050: public void setFromLengthHash(int index, long from, int length,
051: int hash) {
052: froms[index] = from;
053: lengths[index] = length;
054: hashes[index] = hash;
055: }
056:
057: public void load(RandomAccessFile file) throws IOException {
058: froms = new long[maximumEntryCount];
059: lengths = new int[maximumEntryCount];
060: hashes = new int[maximumEntryCount];
061:
062: final byte[] bytes = new byte[maximumEntryCount
063: * QSequenceLineMedia.SEGMENT_ENTRY_SIZE];
064: file.seek(segmentIndex * maximumEntryCount
065: * QSequenceLineMedia.SEGMENT_ENTRY_SIZE);
066: file.readFully(bytes);
067:
068: final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
069: final DataInputStream is = new DataInputStream(bis);
070: for (int index = 0; index < maximumEntryCount; index++) {
071: froms[index] = is.readLong();
072: lengths[index] = is.readInt();
073: hashes[index] = is.readInt();
074: }
075: }
076:
077: public void unload(RandomAccessFile file) throws IOException {
078: final ByteArrayOutputStream bos = new ByteArrayOutputStream(
079: maximumEntryCount
080: * QSequenceLineMedia.SEGMENT_ENTRY_SIZE);
081: final DataOutputStream os = new DataOutputStream(bos);
082: for (int index = 0; index < maximumEntryCount; index++) {
083: os.writeLong(froms[index]);
084: os.writeInt(lengths[index]);
085: os.writeInt(hashes[index]);
086: }
087:
088: final byte[] bytes = bos.toByteArray();
089: QSequenceAssert.assertEquals(maximumEntryCount
090: * QSequenceLineMedia.SEGMENT_ENTRY_SIZE, bytes.length);
091:
092: final long offset = segmentIndex * maximumEntryCount
093: * QSequenceLineMedia.SEGMENT_ENTRY_SIZE;
094: file.seek(offset);
095: file.write(bytes);
096:
097: froms = null;
098: lengths = null;
099: hashes = null;
100: }
101: }
|