2009-11-03 23:47:53 +01:00
|
|
|
// MergeIterator.java
|
2005-05-08 16:42:09 +02:00
|
|
|
// --------------------------
|
|
|
|
// part of The Kelondro Database
|
2008-07-20 19:14:51 +02:00
|
|
|
// (C) by Michael Peter Christen; mc@yacy.net
|
2005-05-08 16:42:09 +02:00
|
|
|
// first published on http://www.anomic.de
|
|
|
|
// Frankfurt, Germany, 2005
|
|
|
|
// last major change: 08.05.2005
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2012-09-21 16:46:57 +02:00
|
|
|
package net.yacy.kelondro.util;
|
2005-05-08 16:42:09 +02:00
|
|
|
|
2006-07-27 00:40:11 +02:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2008-08-19 16:10:40 +02:00
|
|
|
import java.util.Collection;
|
2005-05-08 16:42:09 +02:00
|
|
|
import java.util.Comparator;
|
2006-09-30 00:27:20 +02:00
|
|
|
import java.util.ConcurrentModificationException;
|
2005-07-06 16:48:41 +02:00
|
|
|
import java.util.Iterator;
|
2005-05-08 16:42:09 +02:00
|
|
|
|
2011-12-16 23:59:29 +01:00
|
|
|
import net.yacy.cora.order.CloneableIterator;
|
|
|
|
import net.yacy.cora.order.Order;
|
2013-07-09 14:28:25 +02:00
|
|
|
import net.yacy.cora.util.ConcurrentLog;
|
2009-11-05 21:28:37 +01:00
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
|
2009-01-30 23:44:20 +01:00
|
|
|
public class MergeIterator<E> implements CloneableIterator<E> {
|
2012-05-30 15:27:45 +02:00
|
|
|
|
2010-01-11 00:09:48 +01:00
|
|
|
private final Comparator<E> comp;
|
|
|
|
private final CloneableIterator<E> a;
|
|
|
|
private final CloneableIterator<E> b;
|
2008-05-08 18:51:01 +02:00
|
|
|
private E na, nb;
|
|
|
|
private final Method merger;
|
|
|
|
private final boolean up;
|
2012-05-30 15:27:45 +02:00
|
|
|
|
2009-01-30 23:44:20 +01:00
|
|
|
public MergeIterator(
|
2009-01-30 23:08:08 +01:00
|
|
|
final CloneableIterator<E> a,
|
|
|
|
final CloneableIterator<E> b,
|
2009-01-21 19:23:37 +01:00
|
|
|
final Comparator<E> c,
|
|
|
|
final Method m,
|
|
|
|
final boolean up) {
|
2005-05-08 16:42:09 +02:00
|
|
|
// this works currently only for String-type key iterations
|
2009-03-13 00:05:18 +01:00
|
|
|
assert a != null;
|
|
|
|
assert b != null;
|
2005-05-08 16:42:09 +02:00
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
this.up = up;
|
2006-01-11 01:32:44 +01:00
|
|
|
this.comp = c;
|
2006-07-27 00:40:11 +02:00
|
|
|
this.merger = m;
|
2005-05-08 16:42:09 +02:00
|
|
|
nexta();
|
|
|
|
nextb();
|
|
|
|
}
|
2009-01-21 19:23:37 +01:00
|
|
|
|
2012-07-02 15:40:40 +02:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
this.a.close();
|
|
|
|
this.b.close();
|
|
|
|
}
|
|
|
|
|
2012-05-30 15:27:45 +02:00
|
|
|
@Override
|
2009-01-30 23:44:20 +01:00
|
|
|
public MergeIterator<E> clone(final Object modifier) {
|
2012-05-30 15:27:45 +02:00
|
|
|
assert this.a != null;
|
|
|
|
assert this.b != null;
|
|
|
|
assert this.merger != null;
|
|
|
|
return new MergeIterator<E>(this.a.clone(modifier), this.b.clone(modifier), this.comp, this.merger, this.up);
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
|
2005-05-08 16:42:09 +02:00
|
|
|
private void nexta() {
|
2005-07-27 14:51:00 +02:00
|
|
|
try {
|
2012-05-30 15:27:45 +02:00
|
|
|
if (this.a != null && this.a.hasNext()) this.na = this.a.next(); else this.na = null;
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final ConcurrentModificationException e) {
|
2012-05-30 15:27:45 +02:00
|
|
|
this.na = null;
|
2005-07-27 14:51:00 +02:00
|
|
|
}
|
2005-05-08 16:42:09 +02:00
|
|
|
}
|
|
|
|
private void nextb() {
|
2005-07-27 14:51:00 +02:00
|
|
|
try {
|
2012-05-30 15:27:45 +02:00
|
|
|
if (this.b != null && this.b.hasNext()) this.nb = this.b.next(); else this.nb = null;
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final ConcurrentModificationException e) {
|
2012-05-30 15:27:45 +02:00
|
|
|
this.nb = null;
|
2005-07-27 14:51:00 +02:00
|
|
|
}
|
2005-05-08 16:42:09 +02:00
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
|
|
|
|
@Override
|
2005-05-08 16:42:09 +02:00
|
|
|
public boolean hasNext() {
|
2012-05-30 15:27:45 +02:00
|
|
|
return (this.na != null) || (this.nb != null);
|
2005-05-08 16:42:09 +02:00
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
2008-01-19 01:40:19 +01:00
|
|
|
public E next() {
|
2007-12-27 18:56:59 +01:00
|
|
|
E s;
|
2012-05-30 15:27:45 +02:00
|
|
|
if (this.na == null) {
|
|
|
|
s = this.nb;
|
2005-05-08 16:42:09 +02:00
|
|
|
nextb();
|
|
|
|
return s;
|
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
if (this.nb == null) {
|
|
|
|
s = this.na;
|
2005-05-08 16:42:09 +02:00
|
|
|
nexta();
|
|
|
|
return s;
|
|
|
|
}
|
2006-07-26 13:21:51 +02:00
|
|
|
// compare the Objects
|
2012-05-30 15:27:45 +02:00
|
|
|
final int c = this.comp.compare(this.na, this.nb);
|
2005-05-08 16:42:09 +02:00
|
|
|
if (c == 0) {
|
2006-07-27 00:40:11 +02:00
|
|
|
try {
|
|
|
|
//System.out.print("MERGE OF " + na.toString() + " AND " + nb.toString() + ": ");
|
2012-05-30 15:27:45 +02:00
|
|
|
s = (E) this.merger.invoke(null, new Object[]{this.na, this.nb});
|
2006-07-27 00:40:11 +02:00
|
|
|
//System.out.println("RESULT IS " + s.toString());
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final IllegalArgumentException e) {
|
2013-07-09 14:28:25 +02:00
|
|
|
ConcurrentLog.logException(e);
|
2006-07-27 00:40:11 +02:00
|
|
|
s = null;
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final IllegalAccessException e) {
|
2013-07-09 14:28:25 +02:00
|
|
|
ConcurrentLog.logException(e);
|
2006-07-27 00:40:11 +02:00
|
|
|
s = null;
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final InvocationTargetException e) {
|
2013-07-09 14:28:25 +02:00
|
|
|
ConcurrentLog.logException(e);
|
|
|
|
ConcurrentLog.logException(e.getCause());
|
2006-07-27 00:40:11 +02:00
|
|
|
s = null;
|
|
|
|
}
|
2005-05-08 16:42:09 +02:00
|
|
|
nexta();
|
|
|
|
nextb();
|
|
|
|
return s;
|
2012-05-30 15:27:45 +02:00
|
|
|
} else if ((this.up && c < 0) || (!this.up && c > 0)) {
|
|
|
|
s = this.na;
|
2005-05-08 16:42:09 +02:00
|
|
|
nexta();
|
|
|
|
return s;
|
|
|
|
} else {
|
2012-05-30 15:27:45 +02:00
|
|
|
s = this.nb;
|
2005-05-08 16:42:09 +02:00
|
|
|
nextb();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
|
|
|
|
@Override
|
2005-05-08 16:42:09 +02:00
|
|
|
public void remove() {
|
|
|
|
throw new java.lang.UnsupportedOperationException("merge does not support remove");
|
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
|
2009-01-30 23:44:20 +01:00
|
|
|
public static <A> CloneableIterator<A> cascade(final Collection<CloneableIterator<A>> iterators, final Order<A> c, final Method merger, final boolean up) {
|
2005-05-22 15:27:54 +02:00
|
|
|
// this extends the ability to combine two iterators
|
2008-01-29 23:13:30 +01:00
|
|
|
// to the ability of combining a set of iterators
|
2012-06-08 10:20:43 +02:00
|
|
|
if (iterators == null || iterators.isEmpty()) return new CloneableIterator<A>(){
|
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public A next() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public CloneableIterator<A> clone(Object modifier) {
|
|
|
|
return this;
|
|
|
|
}
|
2012-07-02 15:40:40 +02:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
}
|
2012-06-08 10:20:43 +02:00
|
|
|
};
|
2008-01-29 23:13:30 +01:00
|
|
|
return cascade(iterators.iterator(), c, merger, up);
|
2005-05-22 15:27:54 +02:00
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
|
2009-01-30 23:44:20 +01:00
|
|
|
private static <A> CloneableIterator<A> cascade(final Iterator<CloneableIterator<A>> iiterators, final Order<A> c, final Method merger, final boolean up) {
|
2012-06-08 10:20:43 +02:00
|
|
|
if (iiterators == null || !(iiterators.hasNext())) return new CloneableIterator<A>(){
|
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public A next() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public CloneableIterator<A> clone(Object modifier) {
|
|
|
|
return this;
|
|
|
|
}
|
2012-07-02 15:40:40 +02:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
}
|
2012-06-08 10:20:43 +02:00
|
|
|
};
|
2009-01-30 23:08:08 +01:00
|
|
|
final CloneableIterator<A> one = iiterators.next();
|
2005-05-22 15:27:54 +02:00
|
|
|
if (!(iiterators.hasNext())) return one;
|
2009-10-18 02:53:43 +02:00
|
|
|
assert merger != null;
|
2009-01-30 23:44:20 +01:00
|
|
|
return new MergeIterator<A>(one, cascade(iiterators, c, merger, up), c, merger, up);
|
2006-07-27 00:40:11 +02:00
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
|
2008-08-02 15:57:00 +02:00
|
|
|
public static final Method simpleMerge;
|
2006-07-27 00:40:11 +02:00
|
|
|
static {
|
2008-08-02 15:57:00 +02:00
|
|
|
Method meth = null;
|
2006-07-27 00:40:11 +02:00
|
|
|
try {
|
2012-09-21 16:46:57 +02:00
|
|
|
final Class<?> c = net.yacy.kelondro.util.MergeIterator.class;
|
2008-08-02 15:57:00 +02:00
|
|
|
meth = c.getMethod("mergeEqualByReplace", new Class[]{Object.class, Object.class});
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final SecurityException e) {
|
2008-09-18 16:12:15 +02:00
|
|
|
System.out.println("Error while initializing simpleMerge (1): " + e.getMessage());
|
2008-08-02 15:57:00 +02:00
|
|
|
meth = null;
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final NoSuchMethodException e) {
|
2008-09-18 16:12:15 +02:00
|
|
|
System.out.println("Error while initializing simpleMerge (3): " + e.getMessage());
|
2008-08-02 15:57:00 +02:00
|
|
|
meth = null;
|
2006-07-27 00:40:11 +02:00
|
|
|
}
|
2009-10-18 02:53:43 +02:00
|
|
|
assert meth != null;
|
2012-05-30 15:27:45 +02:00
|
|
|
simpleMerge = meth;
|
2005-05-22 15:27:54 +02:00
|
|
|
}
|
2012-05-30 15:27:45 +02:00
|
|
|
|
2008-09-18 16:12:15 +02:00
|
|
|
// do not remove the following method, it is not reference anywhere directly but indirectly using reflection
|
|
|
|
// please see initialization of simpleMerge above
|
2012-07-05 10:23:07 +02:00
|
|
|
public static Object mergeEqualByReplace(final Object a, @SuppressWarnings("unused") final Object b) {
|
2008-09-18 16:12:15 +02:00
|
|
|
return a;
|
|
|
|
}
|
2005-05-08 16:42:09 +02:00
|
|
|
}
|