001: // kelondroBufferedIOChunks.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, 2005
007: // created: 11.12.2004
008: //
009: // This program is free software; you can redistribute it and/or modify
010: // it under the terms of the GNU General Public License as published by
011: // the Free Software Foundation; either version 2 of the License, or
012: // (at your option) any later version.
013: //
014: // This program is distributed in the hope that it will be useful,
015: // but WITHOUT ANY WARRANTY; without even the implied warranty of
016: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: // GNU General Public License for more details.
018: //
019: // You should have received a copy of the GNU General Public License
020: // along with this program; if not, write to the Free Software
021: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: //
023: // Using this software in any meaning (reading, learning, copying, compiling,
024: // running) means that you agree that the Author(s) is (are) not responsible
025: // for cost, loss of data or any harm that may be caused directly or indirectly
026: // by usage of this softare or this documentation. The usage of this software
027: // is on your own risk. The installation and usage (starting/running) of this
028: // software may allow other people or application to access your computer and
029: // any attached devices and is highly dependent on the configuration of the
030: // software which must be done by the user of the software; the author(s) is
031: // (are) also not responsible for proper configuration and usage of the
032: // software, even if provoked by documentation provided together with
033: // the software.
034: //
035: // Any changes to this file according to the GPL as documented in the file
036: // gpl.txt aside this file in the shipment you received can be done to the
037: // lines that follows this copyright notice here, but changes must not be
038: // done inside the copyright notive above. A re-distribution must contain
039: // the intact and unchanged copyright notice.
040: // Contributions and changes to the program code must be marked as such.
041:
042: // this is a WRITE-buffer!
043: // the buffer MUST be flushed before closing of the underlying kelondroRA
044:
045: package de.anomic.kelondro;
046:
047: import java.io.IOException;
048: import java.util.Iterator;
049: import java.util.Map;
050: import java.util.TreeMap;
051:
052: public final class kelondroBufferedIOChunks extends
053: kelondroAbstractIOChunks implements kelondroIOChunks {
054:
055: protected kelondroRA ra;
056: private long bufferMaxSize, bufferCurrSize;
057: private long commitTimeout;
058: private TreeMap<Long, byte[]> buffer;
059: private long lastCommit = 0;
060:
061: private static final int overhead = 40;
062:
063: public kelondroBufferedIOChunks(kelondroRA ra, String name,
064: long buffer, long commitTimeout) {
065: this .name = name;
066: this .ra = ra;
067: this .bufferMaxSize = buffer;
068: this .bufferCurrSize = 0;
069: this .commitTimeout = commitTimeout;
070: this .buffer = new TreeMap<Long, byte[]>();
071: this .lastCommit = System.currentTimeMillis();
072: }
073:
074: public kelondroRA getRA() {
075: return this .ra;
076: }
077:
078: public long length() throws IOException {
079: return ra.length();
080: }
081:
082: public synchronized int read(long pos, byte[] b, int off, int len)
083: throws IOException {
084: assert (b.length >= off + len) : "read pos=" + pos
085: + ", b.length=" + b.length + ", off=" + off + ", len="
086: + len;
087:
088: // check commit time
089: if ((bufferCurrSize > bufferMaxSize)
090: || (this .lastCommit + this .commitTimeout > System
091: .currentTimeMillis())) {
092: commit();
093: this .lastCommit = System.currentTimeMillis();
094: }
095:
096: // do the read
097: synchronized (this .buffer) {
098: byte[] bb = (byte[]) buffer.get(new Long(pos));
099: if (bb == null) {
100: // entry not known, read direktly from IO
101: synchronized (this .ra) {
102: this .ra.seek(pos + off);
103: return ra.read(b, off, len);
104: }
105: }
106: // use buffered entry
107: if (bb.length >= off + len) {
108: // the bufferd entry is long enough
109: System.arraycopy(bb, off, b, off, len);
110: return len;
111: }
112: // the entry is not long enough. transmit only a part
113: System.arraycopy(bb, off, b, off, bb.length - off);
114: return bb.length - off;
115: }
116: }
117:
118: public synchronized void write(long pos, byte[] b, int off, int len)
119: throws IOException {
120: assert (b.length >= off + len) : "write pos=" + pos
121: + ", b.length=" + b.length + ", b='" + new String(b)
122: + "', off=" + off + ", len=" + len;
123:
124: //if (len > 10) System.out.println("WRITE(" + name + ", " + pos + ", " + b.length + ", " + off + ", " + len + ")");
125:
126: // do the write into buffer
127: byte[] bb = kelondroObjectSpace.alloc(len);
128: System.arraycopy(b, off, bb, 0, len);
129: synchronized (buffer) {
130: buffer.put(new Long(pos + off), bb);
131: bufferCurrSize += overhead + len;
132: }
133:
134: // check commit time
135: if ((bufferCurrSize > bufferMaxSize)
136: || (this .lastCommit + this .commitTimeout > System
137: .currentTimeMillis())) {
138: commit();
139: this .lastCommit = System.currentTimeMillis();
140: }
141: }
142:
143: public synchronized void commit() throws IOException {
144: synchronized (buffer) {
145: if (buffer.size() == 0)
146: return;
147: Iterator<Map.Entry<Long, byte[]>> i = buffer.entrySet()
148: .iterator();
149: Map.Entry<Long, byte[]> entry = i.next();
150: long lastPos = ((Long) entry.getKey()).longValue();
151: byte[] lastChunk = (byte[]) entry.getValue();
152: long nextPos;
153: byte[] nextChunk, tmpChunk;
154: synchronized (this .ra) {
155: while (i.hasNext()) {
156: entry = i.next();
157: nextPos = ((Long) entry.getKey()).longValue();
158: nextChunk = (byte[]) entry.getValue();
159: if (lastPos + lastChunk.length == nextPos) {
160: // try to combine the new chunk with the previous chunk
161: //System.out.println("combining chunks pos0=" + lastPos + ", chunk0.length=" + lastChunk.length + ", pos1=" + nextPos + ", chunk1.length=" + nextChunk.length);
162: tmpChunk = kelondroObjectSpace
163: .alloc(lastChunk.length
164: + nextChunk.length);
165: System.arraycopy(lastChunk, 0, tmpChunk, 0,
166: lastChunk.length);
167: System.arraycopy(nextChunk, 0, tmpChunk,
168: lastChunk.length, nextChunk.length);
169: kelondroObjectSpace.recycle(lastChunk);
170: lastChunk = tmpChunk;
171: tmpChunk = null;
172: kelondroObjectSpace.recycle(nextChunk);
173: } else {
174: // write the last chunk and take nextChunk next time als lastChunk
175: this .ra.seek(lastPos);
176: this .ra.write(lastChunk);
177: kelondroObjectSpace.recycle(lastChunk);
178: lastPos = nextPos;
179: lastChunk = nextChunk;
180: }
181: }
182: // at the end write just the last chunk
183: this .ra.seek(lastPos);
184: this .ra.write(lastChunk);
185: kelondroObjectSpace.recycle(lastChunk);
186: }
187: buffer.clear();
188: bufferCurrSize = 0;
189: }
190: }
191:
192: public synchronized void close() throws IOException {
193: if (this .ra != null) {
194: commit();
195: this .ra.close();
196: }
197: this .ra = null;
198: }
199:
200: protected void finalize() throws Throwable {
201: if (this.ra != null)
202: this.close();
203: super.finalize();
204: }
205:
206: }
|