Skip to content

Commit

Permalink
* activated write buffer for databases.
Browse files Browse the repository at this point in the history
  This should increase IO performance and reduce HD activity
* bugfixes for new exception-on-failure policy
* bugfixes for new IOChunks
* new Object pool for database write-buffer


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1204 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Dec 12, 2005
1 parent c59d1b2 commit f27f9ec
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 174 deletions.
11 changes: 7 additions & 4 deletions source/de/anomic/kelondro/kelondroAbstractIOChunks.java
Expand Up @@ -59,11 +59,14 @@ public String name() {

// derived methods:
public void readFully(long pos, byte[] b, int off, int len) throws IOException {
final int r = read(pos, b, off, len);
if (r < 0) return; // read exceeded EOF
if (r < len) {
if (len < 0) throw new IndexOutOfBoundsException("length is negative:" + len);
if (b.length < off + len) throw new IndexOutOfBoundsException("bounds do not fit: b.length=" + b.length + ", off=" + off + ", len=" + len);
while (len > 0) {
int r = read(pos, b, off, len);
if (r < 0) throw new IOException("EOF"); // read exceeded EOF
pos += r;
readFully(pos, b, off + r, len - r);
off += r;
len -= r;
}
}

Expand Down
11 changes: 8 additions & 3 deletions source/de/anomic/kelondro/kelondroAbstractRA.java
Expand Up @@ -71,9 +71,14 @@ public String name() {

// derived methods:
public void readFully(byte[] b, int off, int len) throws IOException {
final int r = read(b, off, len);
if (r < 0) return; // read exceeded EOF
if (r < len) { readFully(b, off + r, len - r); }
if (len < 0) throw new IndexOutOfBoundsException("length is negative:" + len);
if (b.length < off + len) throw new IndexOutOfBoundsException("bounds do not fit: b.length=" + b.length + ", off=" + off + ", len=" + len);
while (len > 0) {
int r = read(b, off, len);
if (r < 0) throw new IOException("EOF"); // read exceeded EOF
off += r;
len -= r;
}
}

public byte readByte() throws IOException {
Expand Down
61 changes: 33 additions & 28 deletions source/de/anomic/kelondro/kelondroBufferedIOChunks.java
Expand Up @@ -52,16 +52,19 @@
public final class kelondroBufferedIOChunks extends kelondroAbstractIOChunks implements kelondroIOChunks {

protected kelondroRA ra;
private int bufferkb;
private int bufferMaxSize, bufferCurrSize;
private long commitTimeout;
private HashMap buffer;
private long lastCommit = 0;

private static final int overhead = 40;


public kelondroBufferedIOChunks(kelondroRA ra, String name, int bufferkb, long commitTimeout) {
this.name = name;
this.ra = ra;
this.bufferkb = bufferkb;
this.bufferMaxSize = 1024 * bufferkb;
this.bufferCurrSize = 0;
this.commitTimeout = commitTimeout;
this.buffer = new HashMap();
this.lastCommit = System.currentTimeMillis();
Expand All @@ -71,50 +74,50 @@ public int read(long pos, byte[] b, int off, int len) throws IOException {
assert (b.length >= off + len): "read pos=" + pos + ", b.length=" + b.length + ", off=" + off + ", len=" + len;

// check commit time
if (this.lastCommit + this.commitTimeout > System.currentTimeMillis()) {
if ((bufferCurrSize > bufferMaxSize) ||
(this.lastCommit + this.commitTimeout < System.currentTimeMillis())) {
commit();
this.lastCommit = System.currentTimeMillis();
}

// do the read
if ((off == 0) && (b.length == len)) {
synchronized (this.buffer) {
byte[] bb = (byte[]) buffer.get(new Long(pos));
if (bb == null) {
synchronized (this.ra) {
this.ra.seek(pos);
return ra.read(b, off, len);
}
synchronized (this.buffer) {
byte[] bb = (byte[]) buffer.get(new Long(pos));
if (bb == null) {
// entry not known, read direktly from IO
synchronized (this.ra) {
this.ra.seek(pos + off);
return ra.read(b, off, len);
}
} else {
// use buffered entry
if (bb.length >= off + len) {
// the bufferd entry is long enough
System.arraycopy(bb, off, b, off, len);
return len;
} else {
if (bb.length >= len) {
System.arraycopy(bb, 0, b, off, len);
return len;
} else {
System.arraycopy(bb, 0, b, off, bb.length);
return bb.length;
}
// the entry is not long enough. transmit only a part
System.arraycopy(bb, off, b, off, bb.length - off);
return bb.length - off;
}
}
} else {
byte[] bb = new byte[len];
int r = read(pos + off, bb, 0, len);
System.arraycopy(bb, 0, b, off, r);
return r;
}
}

public void write(long pos, byte[] b, int off, int len) throws IOException {
assert (b.length >= off + len): "write pos=" + pos + ", b.length=" + b.length + ", b='" + new String(b) + "', off=" + off + ", len=" + len;

// do the write into buffer
byte[] bb = new byte[len];
byte[] bb = kelondroObjectSpace.alloc(len);
System.arraycopy(b, off, bb, 0, len);
synchronized (buffer) {
buffer.put(new Long(pos), bb);
buffer.put(new Long(pos + off), bb);
bufferCurrSize += overhead + pos + off;
}

// check commit time
if (this.lastCommit + this.commitTimeout > System.currentTimeMillis()) {
if ((bufferCurrSize > bufferMaxSize) ||
(this.lastCommit + this.commitTimeout < System.currentTimeMillis())) {
commit();
this.lastCommit = System.currentTimeMillis();
}
Expand All @@ -134,9 +137,11 @@ public void commit() throws IOException {
b = (byte[]) entry.getValue();
this.ra.seek(pos);
this.ra.write(b);
kelondroObjectSpace.recycle(b);
}
}
buffer.clear();
bufferCurrSize = 0;
}
}

Expand All @@ -152,5 +157,5 @@ protected void finalize() throws Throwable {
if (this.ra != null) this.close();
super.finalize();
}

}
80 changes: 80 additions & 0 deletions source/de/anomic/kelondro/kelondroObjectSpace.java
@@ -0,0 +1,80 @@
// kelondroObjectSpace.java
// ------------------------
// part of The Kelondro Database
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2005
// created: 12.12.2004
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Using this software in any meaning (reading, learning, copying, compiling,
// running) means that you agree that the Author(s) is (are) not responsible
// for cost, loss of data or any harm that may be caused directly or indirectly
// by usage of this softare or this documentation. The usage of this software
// is on your own risk. The installation and usage (starting/running) of this
// software may allow other people or application to access your computer and
// any attached devices and is highly dependent on the configuration of the
// software which must be done by the user of the software; the author(s) is
// (are) also not responsible for proper configuration and usage of the
// software, even if provoked by documentation provided together with
// the software.
//
// Any changes to this file according to the GPL as documented in the file
// gpl.txt aside this file in the shipment you received can be done to the
// lines that follows this copyright notice here, but changes must not be
// done inside the copyright notive above. A re-distribution must contain
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.

package de.anomic.kelondro;

import java.util.ArrayList;
import java.util.HashMap;

public class kelondroObjectSpace {

private static final int minSize = 10;
private static HashMap objects = new HashMap();

public static byte[] alloc(int len) {
if (len < minSize) return new byte[len];
synchronized (objects) {
ArrayList buf = (ArrayList) objects.get(new Integer(len));
if ((buf == null) || (buf.size() == 0)) return new byte[len];
return (byte[]) buf.remove(buf.size() - 1);
}
}

public static void recycle(byte[] b) {
if (b.length < minSize) {
b = null;
return;
}
synchronized (objects) {
final Integer i = new Integer(b.length);
ArrayList buf = (ArrayList) objects.get(i);
if (buf == null) {
buf = new ArrayList();
buf.add(b);
objects.put(i, buf);
} else {
buf.add(b);
}
}
b = null;
}

}
14 changes: 10 additions & 4 deletions source/de/anomic/kelondro/kelondroRecords.java
Expand Up @@ -72,6 +72,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.Map;
import java.util.Iterator;
Expand Down Expand Up @@ -151,6 +152,9 @@ public class kelondroRecords {

// optional logger
protected Logger theLogger = null;

// Random. This is used to shift flush-times of write-buffers to differrent time
private static Random random = new Random(System.currentTimeMillis());

private class usageControl {
private int USEDC; // counter of used elements
Expand Down Expand Up @@ -219,8 +223,8 @@ private void init(kelondroRA ra, short ohbytec, short ohhandlec,
int[] columns, int FHandles, int txtProps, int txtPropWidth) throws IOException {

// create new Chunked IO
//this.entryFile = new kelondroBufferedIOChunks(ra, ra.name(), 1024, 5000);
this.entryFile = new kelondroRAIOChunks(ra, ra.name());
this.entryFile = new kelondroBufferedIOChunks(ra, ra.name(), 1024, 8000 + random.nextLong() % 2000);
//this.entryFile = new kelondroRAIOChunks(ra, ra.name());

// store dynamic run-time data
this.overhead = ohbytec + 4 * ohhandlec;
Expand Down Expand Up @@ -276,6 +280,8 @@ private void init(kelondroRA ra, short ohbytec, short ohhandlec,
for (int i = 0; i < this.TXTPROPS.length; i++) {
entryFile.write(POS_TXTPROPS + TXTPROPW * i, ea);
}

this.entryFile.commit();
}

public void setLogger(Logger newLogger) {
Expand Down Expand Up @@ -320,8 +326,8 @@ public kelondroRecords(kelondroRA ra, long buffersize) throws IOException{

private void init(kelondroRA ra) throws IOException {
// read from Chunked IO
//this.entryFile = new kelondroBufferedIOChunks(ra, ra.name(), 1024, 5000);
this.entryFile = new kelondroRAIOChunks(ra, ra.name());
this.entryFile = new kelondroBufferedIOChunks(ra, ra.name(), 1024, 8000 + random.nextLong() % 2000);
//this.entryFile = new kelondroRAIOChunks(ra, ra.name());

// read dynamic variables that are back-ups of stored values in file;
// read/defined on instantiation
Expand Down
6 changes: 5 additions & 1 deletion source/de/anomic/plasma/plasmaCrawlBalancer.java
Expand Up @@ -58,7 +58,11 @@ public class plasmaCrawlBalancer {

public plasmaCrawlBalancer(File stackFile, long buffersize) throws IOException {
if (stackFile.exists())
stack = new kelondroStack(stackFile, buffersize);
try {
stack = new kelondroStack(stackFile, buffersize);
} catch (IOException e) {
stack = new kelondroStack(stackFile, buffersize, new int[] {plasmaURL.urlHashLength});
}
else
stack = new kelondroStack(stackFile, buffersize, new int[] {plasmaURL.urlHashLength});
domainStacks = new HashMap();
Expand Down

0 comments on commit f27f9ec

Please sign in to comment.