001: // kelondroCachedRA.java
002: // -----------------------
003: // part of The Kelondro Database
004: // (C) by Michael Peter Christen; mc@anomic.de
005: // first published on http://www.anomic.de
006: // Frankfurt, Germany, 2004, 2005
007: // last major change: 06.10.2004
008: // this file was previously named kelondroBufferedRA
009: //
010: // This program is free software; you can redistribute it and/or modify
011: // it under the terms of the GNU General Public License as published by
012: // the Free Software Foundation; either version 2 of the License, or
013: // (at your option) any later version.
014: //
015: // This program is distributed in the hope that it will be useful,
016: // but WITHOUT ANY WARRANTY; without even the implied warranty of
017: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: // GNU General Public License for more details.
019: //
020: // You should have received a copy of the GNU General Public License
021: // along with this program; if not, write to the Free Software
022: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023: //
024: // Using this software in any meaning (reading, learning, copying, compiling,
025: // running) means that you agree that the Author(s) is (are) not responsible
026: // for cost, loss of data or any harm that may be caused directly or indirectly
027: // by usage of this softare or this documentation. The usage of this software
028: // is on your own risk. The installation and usage (starting/running) of this
029: // software may allow other people or application to access your computer and
030: // any attached devices and is highly dependent on the configuration of the
031: // software which must be done by the user of the software; the author(s) is
032: // (are) also not responsible for proper configuration and usage of the
033: // software, even if provoked by documentation provided together with
034: // the software.
035: //
036: // Any changes to this file according to the GPL as documented in the file
037: // gpl.txt aside this file in the shipment you received can be done to the
038: // lines that follows this copyright notice here, but changes must not be
039: // done inside the copyright notive above. A re-distribution must contain
040: // the intact and unchanged copyright notice.
041: // Contributions and changes to the program code must be marked as such.
042:
043: package de.anomic.kelondro;
044:
045: import java.io.IOException;
046: import java.util.HashMap;
047: import java.util.Iterator;
048:
049: public class kelondroCachedRA extends kelondroAbstractRA implements
050: kelondroRA {
051:
052: protected kelondroRA ra;
053: protected kelondroMScoreCluster<Integer> cacheScore;
054: protected HashMap<Integer, byte[]> cacheMemory;
055: private int cacheMaxElements;
056: private int cacheElementSize;
057: private long seekpos;
058:
059: public kelondroCachedRA(kelondroRA ra, int cachesize,
060: int elementsize) {
061: this .ra = ra;
062: this .name = ra.name();
063: this .cacheMemory = new HashMap<Integer, byte[]>();
064: this .cacheScore = new kelondroMScoreCluster<Integer>();
065: this .cacheElementSize = elementsize;
066: this .cacheMaxElements = cachesize / cacheElementSize;
067: this .seekpos = 0;
068: }
069:
070: public long length() throws IOException {
071: return ra.available();
072: }
073:
074: public long available() throws IOException {
075: synchronized (ra) {
076: ra.seek(seekpos);
077: return ra.available();
078: }
079: }
080:
081: private int cacheElementNumber(long address) {
082: return (int) address / cacheElementSize;
083: }
084:
085: private int cacheElementOffset(long address) {
086: return (int) address % cacheElementSize;
087: }
088:
089: private byte[] readCache(int cacheNr) throws IOException {
090: Integer cacheNrI = new Integer(cacheNr);
091: byte[] cache = (byte[]) cacheMemory.get(cacheNrI);
092: if (cache == null) {
093: if (cacheMemory.size() >= cacheMaxElements) {
094: // delete elements in buffer if buffer too big
095: Iterator<Integer> it = cacheScore.scores(true);
096: Integer element = (Integer) it.next();
097: writeCache((byte[]) cacheMemory.get(element), element
098: .intValue());
099: cacheMemory.remove(element);
100: int age = cacheScore.deleteScore(element);
101: de.anomic.server.logging.serverLog.logFine("CACHE: "
102: + name, "GC; age="
103: + ((((int) (0xFFFFFFFFL & System
104: .currentTimeMillis())) - age) / 1000));
105: }
106: // add new element
107: cache = new byte[cacheElementSize];
108: //System.out.println("buffernr=" + bufferNr + ", elSize=" + bufferElementSize);
109: ra.seek(cacheNr * cacheElementSize);
110: ra.read(cache, 0, cacheElementSize);
111: cacheMemory.put(cacheNrI, cache);
112: }
113: cacheScore.setScore(cacheNrI, (int) (0xFFFFFFFFL & System
114: .currentTimeMillis()));
115: return cache;
116: }
117:
118: private void writeCache(byte[] cache, int cacheNr)
119: throws IOException {
120: if (cache == null)
121: return;
122: Integer cacheNrI = new Integer(cacheNr);
123: ra.seek(cacheNr * cacheElementSize);
124: ra.write(cache, 0, cacheElementSize);
125: cacheScore.setScore(cacheNrI, (int) (0xFFFFFFFFL & System
126: .currentTimeMillis()));
127: }
128:
129: // pseudo-native method read
130: public int read() throws IOException {
131: int bn = cacheElementNumber(seekpos);
132: int offset = cacheElementOffset(seekpos);
133: seekpos++;
134: return 0xFF & readCache(bn)[offset];
135: }
136:
137: // pseudo-native method write
138: public void write(int b) throws IOException {
139: int bn = cacheElementNumber(seekpos);
140: int offset = cacheElementOffset(seekpos);
141: byte[] cache = readCache(bn);
142: seekpos++;
143: cache[offset] = (byte) b;
144: //writeBuffer(buffer, bn);
145: }
146:
147: public int read(byte[] b, int off, int len) throws IOException {
148: int bn1 = cacheElementNumber(seekpos);
149: int bn2 = cacheElementNumber(seekpos + len - 1);
150: int offset = cacheElementOffset(seekpos);
151: byte[] buffer = readCache(bn1);
152: if (bn1 == bn2) {
153: // simple case
154: //System.out.println("C1: bn1=" + bn1 + ", offset=" + offset + ", off=" + off + ", len=" + len);
155: System.arraycopy(buffer, offset, b, off, len);
156: seekpos += len;
157: return len;
158: }
159:
160: // do recursively
161: int this len = cacheElementSize - offset;
162: //System.out.println("C2: bn1=" + bn1 + ", bn2=" + bn2 +", offset=" + offset + ", off=" + off + ", len=" + len + ", thislen=" + thislen);
163: System.arraycopy(buffer, offset, b, off, this len);
164: seekpos += this len;
165: return this len + read(b, off + this len, len - this len);
166: }
167:
168: public void write(byte[] b, int off, int len) throws IOException {
169: int bn1 = cacheElementNumber(seekpos);
170: int bn2 = cacheElementNumber(seekpos + len - 1);
171: int offset = cacheElementOffset(seekpos);
172: byte[] cache = readCache(bn1);
173: if (bn1 == bn2) {
174: // simple case
175: System.arraycopy(b, off, cache, offset, len);
176: seekpos += len;
177: //writeBuffer(buffer, bn1);
178: } else {
179: // do recursively
180: int this len = cacheElementSize - offset;
181: System.arraycopy(b, off, cache, offset, this len);
182: seekpos += this len;
183: //writeBuffer(buffer, bn1);
184: write(b, off + this len, len - this len);
185: }
186: }
187:
188: public void seek(long pos) throws IOException {
189: seekpos = pos;
190: }
191:
192: public void close() throws IOException {
193: // write all unwritten buffers
194: if (cacheMemory == null)
195: return;
196: Iterator<Integer> it = cacheScore.scores(true);
197: while (it.hasNext()) {
198: Integer element = it.next();
199: writeCache((byte[]) cacheMemory.get(element), element
200: .intValue());
201: cacheMemory.remove(element);
202: }
203: ra.close();
204: cacheScore = null;
205: cacheMemory = null;
206: }
207:
208: public void finalize() {
209: try {
210: close();
211: } catch (IOException e) {
212: }
213: }
214: }
|