added comparator and more fixes for SortStack/SortStore

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6494 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 16 years ago
parent d2938c44a1
commit dbdf2570ba

@ -25,6 +25,7 @@
package de.anomic.search; package de.anomic.search;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.TreeSet; import java.util.TreeSet;
@ -37,7 +38,7 @@ import net.yacy.kelondro.order.Base64Order;
import net.yacy.repository.LoaderDispatcher; import net.yacy.repository.LoaderDispatcher;
public class MediaSnippet implements Comparable<MediaSnippet> { public class MediaSnippet implements Comparable<MediaSnippet>, Comparator<MediaSnippet> {
public ContentDomain type; public ContentDomain type;
public DigestURI href, source; public DigestURI href, source;
public String name, attr; public String name, attr;
@ -67,6 +68,10 @@ public class MediaSnippet implements Comparable<MediaSnippet> {
return Base64Order.enhancedCoder.compare(this.href.hash().getBytes(), o.href.hash().getBytes()); return Base64Order.enhancedCoder.compare(this.href.hash().getBytes(), o.href.hash().getBytes());
} }
public int compare(MediaSnippet o1, MediaSnippet o2) {
return o1.compareTo(o2);
}
public static ArrayList<MediaSnippet> retrieveMediaSnippets(final DigestURI url, final TreeSet<byte[]> queryhashes, final ContentDomain mediatype, final boolean fetchOnline, final int timeout, final boolean reindexing) { public static ArrayList<MediaSnippet> retrieveMediaSnippets(final DigestURI url, final TreeSet<byte[]> queryhashes, final ContentDomain mediatype, final boolean fetchOnline, final int timeout, final boolean reindexing) {
if (queryhashes.size() == 0) { if (queryhashes.size() == 0) {
Log.logFine("snippet fetch", "no query hashes given for url " + url); Log.logFine("snippet fetch", "no query hashes given for url " + url);

@ -331,7 +331,7 @@ public final class RankingProcess extends Thread {
return rwi; return rwi;
} }
// second appearances of dom // second appearances of dom
m.push(rwi); m.push(rwi.element, rwi.weight);
} }
// no more entries in sorted RWI entries. Now take Elements from the doubleDomCache // no more entries in sorted RWI entries. Now take Elements from the doubleDomCache
// find best entry from all caches // find best entry from all caches

@ -28,6 +28,7 @@ package de.anomic.search;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import net.yacy.document.Condenser; import net.yacy.document.Condenser;
@ -43,7 +44,7 @@ import net.yacy.kelondro.rwi.Reference;
import de.anomic.yacy.yacySeed; import de.anomic.yacy.yacySeed;
import de.anomic.yacy.yacySeedDB; import de.anomic.yacy.yacySeedDB;
public class ResultEntry implements Comparable<ResultEntry> { public class ResultEntry implements Comparable<ResultEntry>, Comparator<ResultEntry> {
// payload objects // payload objects
private final URIMetadataRow urlentry; private final URIMetadataRow urlentry;
@ -166,6 +167,9 @@ public class ResultEntry implements Comparable<ResultEntry> {
return urlentry.toString(textSnippet.getLineRaw()); return urlentry.toString(textSnippet.getLineRaw());
} }
public int compareTo(ResultEntry o) { public int compareTo(ResultEntry o) {
return Base64Order.enhancedCoder.compare(this.hash().getBytes(), o.hash().getBytes()); return Base64Order.enhancedCoder.compare(this.urlentry.hash().getBytes(), o.urlentry.hash().getBytes());
}
public int compare(ResultEntry o1, ResultEntry o2) {
return Base64Order.enhancedCoder.compare(o1.urlentry.hash().getBytes(), o2.urlentry.hash().getBytes());
} }
} }

@ -179,7 +179,7 @@ public class ResultFetcher {
// apply post-ranking // apply post-ranking
long ranking = Long.valueOf(rankedCache.getOrder().cardinal(resultEntry.word())); long ranking = Long.valueOf(rankedCache.getOrder().cardinal(resultEntry.word()));
ranking += postRanking(resultEntry, rankedCache.getTopics()); ranking += postRanking(resultEntry, rankedCache.getTopics());
//System.out.println("*** resultEntry.hash = " + resultEntry.hash());
result.push(resultEntry, ranking); result.push(resultEntry, ranking);
if (nav_topics) rankedCache.addTopics(resultEntry); if (nav_topics) rankedCache.addTopics(resultEntry);
//System.out.println("DEBUG SNIPPET_LOADING: thread " + id + " got " + resultEntry.url()); //System.out.println("DEBUG SNIPPET_LOADING: thread " + id + " got " + resultEntry.url());
@ -339,7 +339,9 @@ public class ResultFetcher {
if (imagemedia != null) { if (imagemedia != null) {
for (int j = 0; j < imagemedia.size(); j++) { for (int j = 0; j < imagemedia.size(); j++) {
ms = imagemedia.get(j); ms = imagemedia.get(j);
int b = images.size();
images.push(ms, Long.valueOf(ms.ranking)); images.push(ms, Long.valueOf(ms.ranking));
System.out.println("*** image " + ms.href.hash() + " images.size = " + b + "/" + images.size());
} }
} }
} }

@ -26,7 +26,9 @@
package net.yacy.document.geolocalization; package net.yacy.document.geolocalization;
public class Coordinates implements Comparable<Coordinates> { import java.util.Comparator;
public class Coordinates implements Comparable<Coordinates>, Comparator<Coordinates> {
private static final double tenmeter = 90.0 / 1.0e6; private static final double tenmeter = 90.0 / 1.0e6;
@ -76,6 +78,10 @@ public class Coordinates implements Comparable<Coordinates> {
return 0; return 0;
} }
public int compare(Coordinates o1, Coordinates o2) {
return o1.compareTo(o2);
}
/** /**
* equality test that is needed to use the class inside HashMap/HashSet * equality test that is needed to use the class inside HashMap/HashSet
*/ */

@ -24,9 +24,11 @@
package net.yacy.document.parser.html; package net.yacy.document.parser.html;
import java.util.Comparator;
import net.yacy.kelondro.data.meta.DigestURI; import net.yacy.kelondro.data.meta.DigestURI;
public class ImageEntry implements Comparable<ImageEntry> { public class ImageEntry implements Comparable<ImageEntry>, Comparator<ImageEntry> {
private final DigestURI url; private final DigestURI url;
private final String alt; private final String alt;
@ -84,6 +86,10 @@ public class ImageEntry implements Comparable<ImageEntry> {
if (thc > ohc) return 1; if (thc > ohc) return 1;
return this.url.toString().compareTo((h).url.toString()); return this.url.toString().compareTo((h).url.toString());
} }
public int compare(ImageEntry o1, ImageEntry o2) {
return o1.compareTo(o2);
}
@Override @Override
public boolean equals(final Object o) { public boolean equals(final Object o) {

@ -226,7 +226,7 @@ public final class Row {
} }
public class Entry implements Comparable<Entry> { public class Entry implements Comparable<Entry>, Comparator<Entry> {
private byte[] rowinstance; private byte[] rowinstance;
private int offset; // the offset where the row starts within rowinstance private int offset; // the offset where the row starts within rowinstance
@ -332,6 +332,10 @@ public final class Row {
return objectOrder.compare(this.bytes(), 0, this.getPrimaryKeyLength(), o.bytes(), 0, o.getPrimaryKeyLength()); return objectOrder.compare(this.bytes(), 0, this.getPrimaryKeyLength(), o.bytes(), 0, o.getPrimaryKeyLength());
} }
public int compare(Entry o1, Entry o2) {
return o1.compareTo(o2);
}
public final boolean equals(final Entry otherEntry) { public final boolean equals(final Entry otherEntry) {
// compares the content of the complete entry // compares the content of the complete entry
final byte[] t = this.bytes(); final byte[] t = this.bytes();

@ -30,6 +30,7 @@ package net.yacy.kelondro.table;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
@ -1219,7 +1220,7 @@ public class Records {
} }
public class Handle implements Comparable<Handle> { public class Handle implements Comparable<Handle>, Comparator<Handle> {
public final static int NUL = Integer.MIN_VALUE; // the meta value for the kelondroTray' NUL abstraction public final static int NUL = Integer.MIN_VALUE; // the meta value for the kelondroTray' NUL abstraction

@ -40,7 +40,7 @@ public class SortStack<E extends Comparable<E>> {
// for a double-check. // for a double-check.
private TreeMap<Long, List<E>> onstack; // object within the stack private TreeMap<Long, List<E>> onstack; // object within the stack
private HashSet<E> instack; // keeps track which element has been on the stack or is now in the offstack private HashSet<E> instack; // keeps track which element has been on the stack
protected int maxsize; protected int maxsize;
public SortStack(final int maxsize) { public SortStack(final int maxsize) {
@ -52,11 +52,7 @@ public class SortStack<E extends Comparable<E>> {
} }
public int size() { public int size() {
return this.onstack.size(); return this.instack.size();
}
public void push(final stackElement se) {
push(se.element, se.weight);
} }
/** /**
@ -113,6 +109,7 @@ public class SortStack<E extends Comparable<E>> {
final List<E> l = this.onstack.get(w); final List<E> l = this.onstack.get(w);
final E element = l.remove(0); final E element = l.remove(0);
if (l.size() == 0) this.onstack.remove(w); if (l.size() == 0) this.onstack.remove(w);
this.instack.remove(element);
return new stackElement(element, w); return new stackElement(element, w);
} }

@ -27,6 +27,7 @@
package net.yacy.kelondro.util; package net.yacy.kelondro.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
/** /**
@ -38,10 +39,18 @@ import java.util.Iterator;
public class SortStore<E extends Comparable<E>> extends SortStack<E> { public class SortStore<E extends Comparable<E>> extends SortStack<E> {
private final ArrayList<stackElement> offstack; // objects that had been on the stack but had been removed private final ArrayList<stackElement> offstack; // objects that had been on the stack but had been removed
private HashSet<E> offset; // keeps track which element has been on the stack or is now in the offstack
private long largest;
public SortStore() {
this(-1);
}
public SortStore(final int maxsize) { public SortStore(final int maxsize) {
super(maxsize); super(maxsize);
this.largest = Long.MIN_VALUE;
this.offstack = new ArrayList<stackElement>(); this.offstack = new ArrayList<stackElement>();
this.offset = new HashSet<E>();
} }
public int size() { public int size() {
@ -53,10 +62,12 @@ public class SortStore<E extends Comparable<E>> extends SortStack<E> {
} }
public synchronized void push(final E element, final Long weight) { public synchronized void push(final E element, final Long weight) {
if (this.offset.contains(element)) return;
super.push(element, weight); super.push(element, weight);
this.largest = Math.max(this.largest, weight.longValue());
if (this.maxsize <= 0) return; if (this.maxsize <= 0) return;
while ((super.size() > 0) && (super.size() + this.offstack.size() > this.maxsize)) { while ((super.size() > 0) && (this.size() > this.maxsize)) {
super.pop(); this.pop();
} }
} }
@ -69,9 +80,19 @@ public class SortStore<E extends Comparable<E>> extends SortStack<E> {
final stackElement se = super.pop(); final stackElement se = super.pop();
if (se == null) return null; if (se == null) return null;
this.offstack.add(se); this.offstack.add(se);
this.offset.add(se.element);
return se; return se;
} }
public synchronized stackElement top() {
return super.top();
}
public synchronized boolean exists(final E element) {
if (super.exists(element)) return true;
return this.offset.contains(element);
}
/** /**
* return an element from a specific position. It is either taken from the offstack, * return an element from a specific position. It is either taken from the offstack,
* or removed from the onstack. * or removed from the onstack.
@ -84,7 +105,7 @@ public class SortStore<E extends Comparable<E>> extends SortStack<E> {
return this.offstack.get(position); return this.offstack.get(position);
} }
if (position >= size()) return null; // we don't have that element if (position >= size()) return null; // we don't have that element
while (position >= this.offstack.size()) this.offstack.add(super.pop()); while (position >= this.offstack.size()) this.pop();
return this.offstack.get(position); return this.offstack.get(position);
} }
@ -95,18 +116,18 @@ public class SortStore<E extends Comparable<E>> extends SortStack<E> {
* @param count * @param count
* @return * @return
*/ */
public ArrayList<stackElement> list(final int count) { public synchronized ArrayList<stackElement> list(final int count) {
if (count < 0) { if (count < 0) {
// shift all elements // shift all elements
while (super.size() > 0) this.offstack.add(super.pop()); while (super.size() > 0) this.offstack.add(this.pop());
return this.offstack; return this.offstack;
} }
if (size() < count) throw new RuntimeException("list(" + count + ") exceeded avaiable number of elements (" + size() + ")"); if (size() < count) throw new RuntimeException("list(" + count + ") exceeded avaiable number of elements (" + size() + ")");
while (this.offstack.size() < count) this.offstack.add(super.pop()); while (this.offstack.size() < count) this.offstack.add(this.pop());
return this.offstack; return this.offstack;
} }
public void remove(final E element) { public synchronized void remove(final E element) {
super.remove(element); super.remove(element);
Iterator<stackElement> i = this.offstack.iterator(); Iterator<stackElement> i = this.offstack.iterator();
while (i.hasNext()) { while (i.hasNext()) {
@ -116,4 +137,26 @@ public class SortStore<E extends Comparable<E>> extends SortStack<E> {
} }
} }
} }
public synchronized boolean bottom(final long weight) {
if (super.bottom(weight)) return true;
return weight >= this.largest;
}
public static void main(String[] args) {
SortStore<String> a = new SortStore<String>();
a.push("abc", 1L);
a.pop();
a.push("abc", 2L);
a.push("6s_7dfZk4xvc", 1L);
a.push("6s_7dfZk4xvc", 1L);
a.push("6s_7dfZk4xvc", 1L);
a.push("6s_7dfZk4xvc", 1L);
a.push("6s_7dfZk4xvc", 1L);
a.push("6s_7dfZk4xvc", 1L);
a.push("6s_7dfZk4xvc", 1L);
a.push("6s_7dfZk4xvc", 1L);
a.pop();
System.out.println("size = " + a.size());
}
} }

Loading…
Cancel
Save