From 6958eff196561ba8eca6860bcf8ccbf01de95f7d Mon Sep 17 00:00:00 2001 From: orbiter Date: Thu, 12 Mar 2009 07:35:17 +0000 Subject: [PATCH] removed unnecessary exceptions, extended testing in IntegerHandleIndex git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@5701 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- .../de/anomic/kelondro/blob/HeapReader.java | 7 +- .../kelondro/index/IntegerHandleIndex.java | 71 ++++++++++++++----- .../kelondro/index/LongHandleIndex.java | 24 +++---- .../kelondro/index/ObjectIndexCache.java | 3 +- source/de/anomic/kelondro/table/EcoTable.java | 7 +- .../de/anomic/kelondro/table/FlexTable.java | 2 +- 6 files changed, 69 insertions(+), 45 deletions(-) diff --git a/source/de/anomic/kelondro/blob/HeapReader.java b/source/de/anomic/kelondro/blob/HeapReader.java index 3b564e7ba..c3de24099 100644 --- a/source/de/anomic/kelondro/blob/HeapReader.java +++ b/source/de/anomic/kelondro/blob/HeapReader.java @@ -209,12 +209,7 @@ public class HeapReader { assert index.row().primaryKeyLength == key.length : index.row().primaryKeyLength + "!=" + key.length; // check if the file index contains the key - try { - return index.get(key) >= 0; - } catch (final IOException e) { - e.printStackTrace(); - return false; - } + return index.get(key) >= 0; } public ByteOrder ordering() { diff --git a/source/de/anomic/kelondro/index/IntegerHandleIndex.java b/source/de/anomic/kelondro/index/IntegerHandleIndex.java index 39253a0df..befcf53f2 100644 --- a/source/de/anomic/kelondro/index/IntegerHandleIndex.java +++ b/source/de/anomic/kelondro/index/IntegerHandleIndex.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.Random; import java.util.concurrent.ArrayBlockingQueue; @@ -46,6 +47,7 @@ import java.util.concurrent.Future; import de.anomic.kelondro.order.Base64Order; import de.anomic.kelondro.order.ByteOrder; import de.anomic.kelondro.order.CloneableIterator; +import de.anomic.kelondro.util.MemoryControl; import de.anomic.yacy.dht.FlatWordPartitionScheme; public class IntegerHandleIndex { @@ -107,7 +109,7 @@ public class IntegerHandleIndex { return index.row(); } - public void clear() throws IOException { + public void clear() { this.index.clear(); } @@ -116,14 +118,14 @@ public class IntegerHandleIndex { return index.has(key); } - public synchronized int get(final byte[] key) throws IOException { + public synchronized int get(final byte[] key) { assert (key != null); final Row.Entry indexentry = index.get(key); if (indexentry == null) return -1; return (int) indexentry.getColLong(1); } - public synchronized int put(final byte[] key, final int i) throws IOException { + public synchronized int put(final byte[] key, final int i) { assert i >= 0 : "i = " + i; assert (key != null); final Row.Entry newentry = index.row().newEntry(); @@ -134,7 +136,7 @@ public class IntegerHandleIndex { return (int) oldentry.getColLong(1); } - public synchronized int inc(final byte[] key, int a) throws IOException { + public synchronized int inc(final byte[] key, int a) { assert key != null; assert a > 0; // it does not make sense to add 0. If this occurres, it is a performance issue @@ -164,7 +166,7 @@ public class IntegerHandleIndex { } */ - public synchronized void putUnique(final byte[] key, final int i) throws IOException { + public synchronized void putUnique(final byte[] key, final int i) { assert i >= 0 : "i = " + i; assert (key != null); final Row.Entry newentry = this.rowdef.newEntry(); @@ -173,7 +175,7 @@ public class IntegerHandleIndex { index.addUnique(newentry); } - public synchronized ArrayList removeDoubles() throws IOException { + public synchronized ArrayList removeDoubles() { final ArrayList report = new ArrayList(); Integer[] is; int c, i; @@ -191,14 +193,14 @@ public class IntegerHandleIndex { return report; } - public synchronized int remove(final byte[] key) throws IOException { + public synchronized int remove(final byte[] key) { assert (key != null); final Row.Entry indexentry = index.remove(key); if (indexentry == null) return -1; return (int) indexentry.getColLong(1); } - public synchronized int removeone() throws IOException { + public synchronized int removeone() { final Row.Entry indexentry = index.removeOne(); if (indexentry == null) return -1; return (int) indexentry.getColLong(1); @@ -208,11 +210,11 @@ public class IntegerHandleIndex { return index.size(); } - public synchronized CloneableIterator keys(final boolean up, final byte[] firstKey) throws IOException { + public synchronized CloneableIterator keys(final boolean up, final byte[] firstKey) { return index.keys(up, firstKey); } - public synchronized CloneableIterator rows(final boolean up, final byte[] firstKey) throws IOException { + public synchronized CloneableIterator rows(final boolean up, final byte[] firstKey) { return index.rows(up, firstKey); } @@ -322,17 +324,48 @@ public class IntegerHandleIndex { } public static void main(String[] args) { - int count = (args.length == 0) ? 100000 : Integer.parseInt(args[0]); - IntegerHandleIndex idx = new IntegerHandleIndex(12, Base64Order.enhancedCoder, 100000); + int count = (args.length == 0) ? 1000000 : Integer.parseInt(args[0]); + System.out.println("Starting test with " + count + " objects, minimum memory: " + (count * 16) + " bytes; " + MemoryControl.available( +) + " available"); + Random r = new Random(0); long start = System.currentTimeMillis(); - try { - for (int i = 0; i < count; i++) { - idx.inc(FlatWordPartitionScheme.positionToHash(r.nextInt(count / 32)).getBytes(), 1); - } - } catch (IOException e) { - e.printStackTrace(); + System.gc(); // for resource measurement + long a = MemoryControl.available(); + IntegerHandleIndex idx = new IntegerHandleIndex(12, Base64Order.enhancedCoder, 0); + for (int i = 0; i < count; i++) { + idx.inc(FlatWordPartitionScheme.positionToHash(r.nextInt(count)).getBytes(), 1); } - System.out.println("Result: " + (((long) count) * 1000L / (System.currentTimeMillis() - start)) + " inc per second; " + count + " loops."); + long timek = ((long) count) * 1000L / (System.currentTimeMillis() - start); + System.out.println("Result IntegerHandleIndex: " + timek + " inc per second " + count + " loops."); + System.gc(); + long memk = a - MemoryControl.available(); + System.out.println("Used Memory: " + memk + " bytes"); + System.out.println("x " + idx.get(FlatWordPartitionScheme.positionToHash(0).getBytes())); + idx = null; + + r = new Random(0); + start = System.currentTimeMillis(); + String hash; + Integer d; + System.gc(); // for resource measurement + a = MemoryControl.available(); + HashMap hm = new HashMap(0); + for (int i = 0; i < count; i++) { + hash = FlatWordPartitionScheme.positionToHash(r.nextInt(count)); + d = hm.get(hash); + if (d == null) hm.put(hash, 1); else hm.put(hash, d + 1); + } + long timej = ((long) count) * 1000L / (System.currentTimeMillis() - start); + System.out.println("Result HashMap: " +timej + " inc per second; " + count ++ " loops."); + System.gc(); + long memj = a - MemoryControl.available(); + System.out.println("Used Memory: " + memj + " bytes"); + System.out.println("x " + hm.get(FlatWordPartitionScheme.positionToHash(0))); + System.out.println("Geschwindigkeitsfaktor j/k: " + (timej / timek)); + System.out.println("Speicherfaktor j/k: " + (memj / memk)); + System.exit(0); } + } diff --git a/source/de/anomic/kelondro/index/LongHandleIndex.java b/source/de/anomic/kelondro/index/LongHandleIndex.java index ac49aca1c..6fb2b98ec 100644 --- a/source/de/anomic/kelondro/index/LongHandleIndex.java +++ b/source/de/anomic/kelondro/index/LongHandleIndex.java @@ -112,18 +112,18 @@ public class LongHandleIndex { return index.row(); } - public void clear() throws IOException { + public void clear() { index.clear(); } - public synchronized long get(final byte[] key) throws IOException { + public synchronized long get(final byte[] key) { assert (key != null); final Row.Entry indexentry = index.get(key); if (indexentry == null) return -1; return indexentry.getColLong(1); } - public synchronized long put(final byte[] key, final long l) throws IOException { + public synchronized long put(final byte[] key, final long l) { assert l >= 0 : "l = " + l; assert (key != null); final Row.Entry newentry = index.row().newEntry(); @@ -134,7 +134,7 @@ public class LongHandleIndex { return oldentry.getColLong(1); } - public synchronized void putUnique(final byte[] key, final long l) throws IOException { + public synchronized void putUnique(final byte[] key, final long l) { assert l >= 0 : "l = " + l; assert (key != null); final Row.Entry newentry = this.rowdef.newEntry(); @@ -143,7 +143,7 @@ public class LongHandleIndex { index.addUnique(newentry); } - public synchronized long add(final byte[] key, long a) throws IOException { + public synchronized long add(final byte[] key, long a) { assert key != null; assert a > 0; // it does not make sense to add 0. If this occurres, it is a performance issue @@ -162,15 +162,15 @@ public class LongHandleIndex { } } - public synchronized long inc(final byte[] key) throws IOException { + public synchronized long inc(final byte[] key) { return add(key, 1); } - public synchronized long dec(final byte[] key) throws IOException { + public synchronized long dec(final byte[] key) { return add(key, -1); } - public synchronized ArrayList removeDoubles() throws IOException { + public synchronized ArrayList removeDoubles() { final ArrayList indexreport = index.removeDoubles(); final ArrayList report = new ArrayList(); Long[] is; @@ -186,14 +186,14 @@ public class LongHandleIndex { return report; } - public synchronized long remove(final byte[] key) throws IOException { + public synchronized long remove(final byte[] key) { assert (key != null); final Row.Entry indexentry = index.remove(key); if (indexentry == null) return -1; return indexentry.getColLong(1); } - public synchronized long removeone() throws IOException { + public synchronized long removeone() { final Row.Entry indexentry = index.removeOne(); if (indexentry == null) return -1; return indexentry.getColLong(1); @@ -203,11 +203,11 @@ public class LongHandleIndex { return index.size(); } - public synchronized CloneableIterator keys(final boolean up, final byte[] firstKey) throws IOException { + public synchronized CloneableIterator keys(final boolean up, final byte[] firstKey) { return index.keys(up, firstKey); } - public synchronized CloneableIterator rows(final boolean up, final byte[] firstKey) throws IOException { + public synchronized CloneableIterator rows(final boolean up, final byte[] firstKey) { return index.rows(up, firstKey); } diff --git a/source/de/anomic/kelondro/index/ObjectIndexCache.java b/source/de/anomic/kelondro/index/ObjectIndexCache.java index df40caeab..da431467c 100644 --- a/source/de/anomic/kelondro/index/ObjectIndexCache.java +++ b/source/de/anomic/kelondro/index/ObjectIndexCache.java @@ -37,7 +37,8 @@ import de.anomic.kelondro.order.StackIterator; public class ObjectIndexCache implements ObjectIndex { private final Row rowdef; - private RowSet index0, index1; + private RowSet index0; + private RowSet index1; private final Row.EntryComparator entryComparator; public ObjectIndexCache(final Row rowdef, final int initialspace) { diff --git a/source/de/anomic/kelondro/table/EcoTable.java b/source/de/anomic/kelondro/table/EcoTable.java index 59edcfd9f..6974b49db 100644 --- a/source/de/anomic/kelondro/table/EcoTable.java +++ b/source/de/anomic/kelondro/table/EcoTable.java @@ -666,12 +666,7 @@ public class EcoTable implements ObjectIndex { final byte[] k = i.next(); assert k != null; if (k == null) return null; - try { - this.c = index.get(k); - } catch (final IOException e) { - e.printStackTrace(); - return null; - } + this.c = index.get(k); if (this.c < 0) throw new ConcurrentModificationException(); // this should only happen if the table was modified during the iteration final byte[] b = new byte[rowdef.objectsize]; if (table == null) { diff --git a/source/de/anomic/kelondro/table/FlexTable.java b/source/de/anomic/kelondro/table/FlexTable.java index fae3cbce3..f81180c86 100644 --- a/source/de/anomic/kelondro/table/FlexTable.java +++ b/source/de/anomic/kelondro/table/FlexTable.java @@ -164,7 +164,7 @@ public class FlexTable extends FlexWidthArray implements ObjectIndex { } assert (key != null) : "DEBUG: empty key in initializeRamIndex"; // should not happen; if it does, it is an error of the condentNodes iterator //System.out.println("ENTRY: " + serverLog.arrayList(indexentry.bytes(), 0, indexentry.objectsize())); - try { ri.putUnique(key, i); } catch (final IOException e) {} // no IOException can happen here + ri.putUnique(key, i); if ((i % 10000) == 0) { System.out.print('.'); System.out.flush();