From 421ee64f33200fa2238a487a4c69782921143337 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Tue, 11 Nov 2014 13:57:04 +0100 Subject: [PATCH] another fix to ordering of table indexes; fixes also network stats graphics --- source/net/yacy/kelondro/index/RAMIndex.java | 2 +- .../yacy/kelondro/index/RAMIndexCluster.java | 20 +++++++--- .../net/yacy/kelondro/table/SplitTable.java | 2 +- .../net/yacy/kelondro/util/StackIterator.java | 37 ++++++++++++------- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/source/net/yacy/kelondro/index/RAMIndex.java b/source/net/yacy/kelondro/index/RAMIndex.java index fea44fad5..7b1eea0ac 100644 --- a/source/net/yacy/kelondro/index/RAMIndex.java +++ b/source/net/yacy/kelondro/index/RAMIndex.java @@ -437,7 +437,7 @@ public final class RAMIndex implements Index, Iterable { // sort index1 to enable working of the merge iterator //index1.sort(); //assert consistencyAnalysis0() : "consistency problem: " + consistencyAnalysis(); - return new StackIterator(this.index0.rows(), this.index1.rows()); + return new StackIterator(this.index0.rows(), this.index1.rows(), null, true); } @Override diff --git a/source/net/yacy/kelondro/index/RAMIndexCluster.java b/source/net/yacy/kelondro/index/RAMIndexCluster.java index 9fe7a6032..8d952af28 100644 --- a/source/net/yacy/kelondro/index/RAMIndexCluster.java +++ b/source/net/yacy/kelondro/index/RAMIndexCluster.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -53,7 +54,7 @@ public final class RAMIndexCluster implements Index, Iterable, Clonea this.name = name; this.cluster = new RAMIndex[clusterSize]; this.rowdef = rowdef; - for (int i = 0; i < clusterSize; i++) { + for (int i = 0; i < this.cluster.length; i++) { this.cluster[i] = null; // lazy initialization, the actual initialization is at accessArray() } } @@ -319,20 +320,29 @@ public final class RAMIndexCluster implements Index, Iterable, Clonea @Override @SuppressWarnings("unchecked") - public final CloneableIterator rows(final boolean up, final byte[] firstKey) { + public final CloneableIterator rows(final boolean up, final byte[] firstKey) { synchronized (this.cluster) { - final List> col = new ArrayList>(this.cluster.length); + final List> col = new ArrayList>(this.cluster.length); for (RAMIndex element : this.cluster) { if (element != null) { col.add(element.rows(up, firstKey)); } } - return StackIterator.stack(col.toArray((CloneableIterator[]) Array.newInstance(CloneableIterator.class, col.size()))); + return StackIterator.stack(col.toArray((CloneableIterator[]) Array.newInstance(CloneableIterator.class, col.size())), getEntryComparator(), up); } } + + private Comparator getEntryComparator() { + return new Comparator() { + @Override + public int compare(Row.Entry o1, Row.Entry o2) { + return RAMIndexCluster.this.rowdef.objectOrder.compare(o1.getPrimaryKeyBytes(), o2.getPrimaryKeyBytes()); + } + }; + } @Override - public final CloneableIterator rows() { + public final CloneableIterator rows() { return rows(true, null); } diff --git a/source/net/yacy/kelondro/table/SplitTable.java b/source/net/yacy/kelondro/table/SplitTable.java index 21f6d9042..a70c0ff1f 100644 --- a/source/net/yacy/kelondro/table/SplitTable.java +++ b/source/net/yacy/kelondro/table/SplitTable.java @@ -573,7 +573,7 @@ public class SplitTable implements Index, Iterable { while (i.hasNext()) { c[d++] = i.next().rows(); } - return StackIterator.stack(c); + return StackIterator.stack(c, null, true); } @Override diff --git a/source/net/yacy/kelondro/util/StackIterator.java b/source/net/yacy/kelondro/util/StackIterator.java index 3f862f106..7f4a285f7 100644 --- a/source/net/yacy/kelondro/util/StackIterator.java +++ b/source/net/yacy/kelondro/util/StackIterator.java @@ -23,6 +23,7 @@ package net.yacy.kelondro.util; import java.lang.reflect.Array; +import java.util.Comparator; import java.util.ConcurrentModificationException; import net.yacy.cora.order.CloneableIterator; @@ -32,32 +33,37 @@ public class StackIterator implements CloneableIterator { private final CloneableIterator a, b; private E na, nb; - + private final Comparator c; + public StackIterator( final CloneableIterator a, - final CloneableIterator b) { + final CloneableIterator b, + final Comparator c, final boolean up) { // this works currently only for String-type key iterations this.a = a; this.b = b; + this.c = up ? c : new Comparator() { + @Override public int compare(E o1, E o2) {return -c.compare(o1, o2);} + }; nexta(); nextb(); } @Override public StackIterator clone(final Object modifier) { - return new StackIterator(this.a.clone(modifier), this.b.clone(modifier)); + return new StackIterator(this.a.clone(modifier), this.b.clone(modifier), this.c, true); } private void nexta() { try { - if ((this.a != null) && (this.a.hasNext())) this.na = this.a.next(); else this.na = null; + if (this.a != null && this.a.hasNext()) this.na = this.a.next(); else this.na = null; } catch (final ConcurrentModificationException e) { this.na = null; } } private void nextb() { try { - if ((this.b != null) && (this.b.hasNext())) this.nb = this.b.next(); else this.nb = null; + if (this.b != null && this.b.hasNext()) this.nb = this.b.next(); else this.nb = null; } catch (final ConcurrentModificationException e) { this.nb = null; } @@ -65,7 +71,7 @@ public class StackIterator implements CloneableIterator { @Override public boolean hasNext() { - return (this.na != null) || (this.nb != null); + return this.na != null || this.nb != null; } @Override @@ -81,9 +87,14 @@ public class StackIterator implements CloneableIterator { nexta(); return s; } - // just stack the Objects - s = this.na; - nexta(); + // return the object in right order + if (this.c == null || this.c.compare(this.na, this.nb) <= 0) { + s = this.na; + nexta(); + return s; + } + s = this.nb; + nextb(); return s; } @@ -93,7 +104,7 @@ public class StackIterator implements CloneableIterator { } @SuppressWarnings("unchecked") - public static CloneableIterator stack(final CloneableIterator[] iterators) { + public static CloneableIterator stack(final CloneableIterator[] iterators, final Comparator c, final boolean up) { // this extends the ability to combine two iterators // to the ability of combining a set of iterators if (iterators == null || iterators.length == 0) return new CloneableIterator() { @@ -122,13 +133,13 @@ public class StackIterator implements CloneableIterator { if (iterators.length == 2) { if (iterators[0] == null) return iterators[1]; if (iterators[1] == null) return iterators[0]; - return new StackIterator(iterators[0], iterators[1]); + return new StackIterator(iterators[0], iterators[1], c, up); } CloneableIterator a = iterators[0]; final CloneableIterator[] iterators0 = (CloneableIterator[]) Array.newInstance(CloneableIterator.class, iterators.length - 1); System.arraycopy(iterators, 1, iterators0, 0, iterators.length - 1); - if (a == null) return stack(iterators0); - return new StackIterator(a, stack(iterators0)); + if (a == null) return stack(iterators0, c, up); + return new StackIterator(a, stack(iterators0, c, up), c, up); } @Override