made HandleSet serializable

pull/1/head
Michael Peter Christen 13 years ago
parent e7e381d110
commit 1795a7325b

@ -46,7 +46,7 @@
<classpathentry kind="lib" path="lib/httpclient-4.1.3.jar"/>
<classpathentry kind="lib" path="lib/httpmime-4.1.3.jar"/>
<classpathentry kind="lib" path="lib/commons-io-2.1.jar"/>
<classpathentry kind="lib" path="lib/apache-solr-solrj-3.6.0.jar"/>
<classpathentry kind="lib" path="lib/apache-solr-solrj-3.6.0.jar" sourcepath="/solrj/src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/icu4j-core.jar"/>
<classpathentry kind="output" path="gen"/>

@ -24,8 +24,10 @@
package net.yacy.cora.order;
import java.io.Serializable;
public interface ByteOrder extends Order<byte[]> {
public interface ByteOrder extends Order<byte[]>, Serializable {
@Override
public boolean wellformed(byte[] a);

@ -10,7 +10,7 @@
// $LastChangedBy$
//
// LICENSE
//
//
// 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
@ -27,9 +27,13 @@
package net.yacy.kelondro.index;
import java.io.Serializable;
import net.yacy.kelondro.util.kelondroException;
public final class Column {
public final class Column implements Serializable {
private static final long serialVersionUID=6558500565023465301L;
public static final int celltype_undefined = 0;
public static final int celltype_boolean = 1;
@ -37,12 +41,12 @@ public final class Column {
public static final int celltype_string = 3;
public static final int celltype_cardinal = 4;
public static final int celltype_bitfield = 5;
public static final int encoder_none = 0;
public static final int encoder_b64e = 1;
public static final int encoder_b256 = 2;
public static final int encoder_bytes = 3;
public int cellwidth;
public final String nickname;
protected final int celltype;
@ -65,7 +69,7 @@ public final class Column {
celldef = celldef.trim();
if (celldef.length() > 0 && celldef.charAt(0) == '<') celldef = celldef.substring(1);
if (celldef.endsWith(">")) celldef = celldef.substring(0, celldef.length() - 1);
// parse type definition
int p = celldef.indexOf(' ');
String typename = "";
@ -76,7 +80,7 @@ public final class Column {
} else {
typename = celldef.substring(0, p);
celldef = celldef.substring(p + 1).trim();
if (typename.equals("boolean")) {
this.celltype = celltype_boolean;
this.cellwidth = 1;
@ -109,9 +113,9 @@ public final class Column {
this.cellwidth = -1; // yet undefined
} else {
throw new kelondroException("kelondroColumn - undefined type def '" + typename + "'");
}
}
}
// parse length
p = celldef.indexOf('-');
if (p < 0) {
@ -144,7 +148,7 @@ public final class Column {
celldef = celldef.substring(q + 1);
}
}
// check length constraints
if (this.cellwidth < 0) throw new kelondroException("kelondroColumn - no cell width given for " + this.nickname);
if (((typename.equals("boolean")) && (this.cellwidth > 1)) ||
@ -179,9 +183,9 @@ public final class Column {
if (this.celltype == celltype_cardinal) throw new kelondroException("kelondroColumn - encoder missing for cell " + this.nickname);
this.encoder = encoder_bytes;
}
assert (this.celltype != celltype_cardinal) || (this.encoder == encoder_b64e) || (this.encoder == encoder_b256);
// parse/check description
if (celldef.length() > 0 && celldef.charAt(0) == '"') {
p = celldef.indexOf('"', 1);
@ -195,43 +199,43 @@ public final class Column {
@Override
public final String toString() {
final StringBuilder s = new StringBuilder(20);
switch (celltype) {
switch (this.celltype) {
case celltype_undefined:
s.append(nickname);
s.append(this.nickname);
s.append('-');
s.append(cellwidth);
s.append(this.cellwidth);
break;
case celltype_boolean:
s.append("boolean ");
s.append(nickname);
s.append(this.nickname);
break;
case celltype_binary:
s.append("byte[] ");
s.append(nickname);
s.append(this.nickname);
s.append('-');
s.append(cellwidth);
s.append(this.cellwidth);
break;
case celltype_string:
s.append("String ");
s.append(nickname);
s.append(this.nickname);
s.append('-');
s.append(cellwidth);
s.append(this.cellwidth);
break;
case celltype_cardinal:
s.append("Cardinal ");
s.append(nickname);
s.append(this.nickname);
s.append('-');
s.append(cellwidth);
s.append(this.cellwidth);
break;
case celltype_bitfield:
s.append("Bitfield ");
s.append(nickname);
s.append(this.nickname);
s.append('-');
s.append(cellwidth);
s.append(this.cellwidth);
break;
}
switch (encoder) {
switch (this.encoder) {
case encoder_b64e:
s.append(" {b64e}");
break;
@ -249,11 +253,11 @@ public final class Column {
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + celltype;
result = prime * result + cellwidth;
result = prime * result + encoder;
result = prime * result + this.celltype;
result = prime * result + this.cellwidth;
result = prime * result + this.encoder;
result = prime * result
+ ((nickname == null) ? 0 : nickname.hashCode());
+ ((this.nickname == null) ? 0 : this.nickname.hashCode());
return result;
}
@ -266,12 +270,12 @@ public final class Column {
if (obj == null) return false;
if (!(obj instanceof Column)) return false;
final Column other = (Column) obj;
if (celltype != other.celltype) return false;
if (cellwidth != other.cellwidth) return false;
if (encoder != other.encoder) return false;
if (nickname == null) {
if (this.celltype != other.celltype) return false;
if (this.cellwidth != other.cellwidth) return false;
if (this.encoder != other.encoder) return false;
if (this.nickname == null) {
if (other.nickname != null) return false;
} else if (!nickname.equals(other.nickname)) return false;
} else if (!this.nickname.equals(other.nickname)) return false;
return true;
}

@ -7,7 +7,7 @@
// $LastChangedBy$
//
// LICENSE
//
//
// 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
@ -31,27 +31,34 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Iterator;
import net.yacy.cora.document.UTF8;
import net.yacy.cora.order.ByteOrder;
import net.yacy.cora.order.CloneableIterator;
import net.yacy.kelondro.logging.Log;
import net.yacy.kelondro.order.NaturalOrder;
import net.yacy.kelondro.util.SetTools;
public final class HandleSet implements Iterable<byte[]>, Cloneable {
public final class HandleSet implements Iterable<byte[]>, Cloneable, Serializable {
private static final long serialVersionUID=444204785291174968L;
private final Row rowdef;
private RowSet index;
public HandleSet(final int keylength, final ByteOrder objectOrder, final int expectedspace) {
this.rowdef = new Row(new Column[]{new Column("key", Column.celltype_binary, Column.encoder_bytes, keylength, "key")}, objectOrder);
try {
this.index = new RowSet(rowdef, expectedspace);
this.index = new RowSet(this.rowdef, expectedspace);
} catch (RowSpaceExceededException e) {
try {
this.index = new RowSet(rowdef, 0);
this.index = new RowSet(this.rowdef, 0);
} catch (RowSpaceExceededException ee) {
Log.logException(ee);
this.index = null;
@ -73,18 +80,18 @@ public final class HandleSet implements Iterable<byte[]>, Cloneable {
public HandleSet clone() {
return new HandleSet(this.rowdef, this.index.clone());
}
public byte[] export() {
return index.exportCollection();
return this.index.exportCollection();
}
/**
* initialize a HandleSet with the content of a dump
* @param keylength
* @param objectOrder
* @param file
* @throws IOException
* @throws RowSpaceExceededException
* @throws IOException
* @throws RowSpaceExceededException
*/
public HandleSet(final int keylength, final ByteOrder objectOrder, final File file) throws IOException, RowSpaceExceededException {
this(keylength, objectOrder, (int) (file.length() / (keylength + 8)));
@ -128,32 +135,32 @@ public final class HandleSet implements Iterable<byte[]>, Cloneable {
os.close();
return c;
}
public final synchronized byte[] smallestKey() {
return this.index.smallestKey();
}
public final synchronized byte[] largestKey() {
return this.index.largestKey();
}
public ByteOrder comparator() {
return this.rowdef.objectOrder;
}
public final Row row() {
return index.row();
return this.index.row();
}
public final void clear() {
this.index.clear();
}
public final synchronized boolean has(final byte[] key) {
assert (key != null);
return index.has(key);
return this.index.has(key);
}
public final void putAll(final HandleSet aset) throws RowSpaceExceededException {
for (byte[] b: aset) put(b);
}
@ -161,36 +168,36 @@ public final class HandleSet implements Iterable<byte[]>, Cloneable {
/**
* Adds the key to the set
* @param key
* @return true if this set did _not_ already contain the given key.
* @return true if this set did _not_ already contain the given key.
* @throws IOException
* @throws RowSpaceExceededException
*/
public final boolean put(final byte[] key) throws RowSpaceExceededException {
assert (key != null);
final Row.Entry newentry = index.row().newEntry(key);
return index.put(newentry);
final Row.Entry newentry = this.index.row().newEntry(key);
return this.index.put(newentry);
}
public final void putUnique(final byte[] key) throws RowSpaceExceededException {
assert (key != null);
final Row.Entry newentry = index.row().newEntry(key);
index.addUnique(newentry);
final Row.Entry newentry = this.index.row().newEntry(key);
this.index.addUnique(newentry);
}
public final boolean remove(final byte[] key) {
assert (key != null);
Row.Entry indexentry;
indexentry = index.remove(key);
indexentry = this.index.remove(key);
return indexentry != null;
}
public final synchronized byte[] removeOne() {
Row.Entry indexentry;
indexentry = index.removeOne();
indexentry = this.index.removeOne();
if (indexentry == null) return null;
return indexentry.getPrimaryKeyBytes();
}
/**
* get one entry; objects are taken from the end of the list
* a getOne(0) would return the same object as removeOne() would remove
@ -200,39 +207,40 @@ public final class HandleSet implements Iterable<byte[]>, Cloneable {
public final synchronized byte[] getOne(int idx) {
if (idx >= this.size()) return null;
Row.Entry indexentry;
indexentry = index.get(this.size() - 1 - idx, true);
indexentry = this.index.get(this.size() - 1 - idx, true);
if (indexentry == null) return null;
return indexentry.getPrimaryKeyBytes();
}
public final synchronized boolean isEmpty() {
return index.isEmpty();
return this.index.isEmpty();
}
public final synchronized int size() {
return index.size();
return this.index.size();
}
public final synchronized CloneableIterator<byte[]> keys(final boolean up, final byte[] firstKey) {
return index.keys(up, firstKey);
return this.index.keys(up, firstKey);
}
@Override
public final Iterator<byte[]> iterator() {
return keys(true, null);
}
public final synchronized void close() {
index.close();
index = null;
this.index.close();
this.index = null;
}
@Override
public final String toString() {
return this.index.toString();
}
// set tools
public HandleSet joinConstructive(final HandleSet other) throws RowSpaceExceededException {
return joinConstructive(this, other);
}
@ -299,26 +307,62 @@ public final class HandleSet implements Iterable<byte[]>, Cloneable {
public void excludeDestructive(final HandleSet other) {
excludeDestructive(this, other);
}
private static void excludeDestructive(final HandleSet set1, final HandleSet set2) {
if (set1 == null) return;
if (set2 == null) return;
assert set1.comparator() == set2.comparator();
if (set1.isEmpty() || set2.isEmpty()) return;
if (set1.size() < set2.size())
excludeDestructiveByTestSmallInLarge(set1, set2);
else
excludeDestructiveByTestLargeInSmall(set1, set2);
}
private static void excludeDestructiveByTestSmallInLarge(final HandleSet small, final HandleSet large) {
final Iterator<byte[]> mi = small.iterator();
while (mi.hasNext()) if (large.has(mi.next())) mi.remove();
}
private static void excludeDestructiveByTestLargeInSmall(final HandleSet large, final HandleSet small) {
final Iterator<byte[]> si = small.iterator();
while (si.hasNext()) large.remove(si.next());
}
public static void main(String[] args) {
HandleSet s = new HandleSet(8, NaturalOrder.naturalOrder, 100);
try {
s.put(UTF8.getBytes("Hello"));
s.put(UTF8.getBytes("World"));
// test Serializable
try {
// write to file
File f = File.createTempFile("HandleSet", "stream");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(s);
out.close();
// read from file
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
HandleSet s1 = (HandleSet) in.readObject();
in.close();
for (byte[] b: s1) {
System.out.println(UTF8.String(b));
}
s1.close();
} catch(IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (RowSpaceExceededException e) {
e.printStackTrace();
}
s.close();
Log.shutdown();
}
}

@ -27,6 +27,7 @@
package net.yacy.kelondro.index;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
@ -48,9 +49,10 @@ import net.yacy.kelondro.util.ByteBuffer;
import net.yacy.kelondro.util.kelondroException;
public final class Row {
public final class Row implements Serializable {
//private final static Pattern commaPattern = Pattern.compile(",");
private static final long serialVersionUID=-148412365988669116L;
protected final Column[] row;
public final int[] colstart;
@ -257,7 +259,7 @@ public final class Row {
public Entry(final byte[] newrow, final int start, final boolean forceclone) {
if (forceclone || newrow.length - start < Row.this.objectsize) {
this.rowinstance = new byte[Row.this.objectsize];
System.arraycopy(newrow, start, this.rowinstance, 0, Row.this.objectsize);
System.arraycopy(newrow, start, this.rowinstance, 0, Math.min(newrow.length, Row.this.objectsize));
this.offset = 0;
} else {
this.rowinstance = newrow;

@ -26,6 +26,7 @@ package net.yacy.kelondro.index;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -46,10 +47,11 @@ import net.yacy.kelondro.util.MemoryControl;
import net.yacy.kelondro.util.kelondroException;
public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>, Cloneable {
public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>, Cloneable, Serializable {
private static final long serialVersionUID=-4670634138825982705L;
private static final byte[] EMPTY_CACHE = new byte[0];
private static final byte[] EMPTY_CACHE = new byte[0];
public static final long growfactorLarge100 = 140L;
public static final long growfactorSmall100 = 120L;
private static final int isortlimit = 20;
@ -134,6 +136,7 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
this.lastTimeWrote = lastTimeWrote;
}
@Override
public RowCollection clone() {
return new RowCollection(this.rowdef, this.chunkcache, this.chunkcount, this.sortBound, this.lastTimeWrote);
}
@ -334,6 +337,7 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
return b;
}
@Override
public synchronized final Row.Entry get(final int index, final boolean clone) {
assert (index >= 0) : "get: access with index " + index + " is below zero";
assert (index < this.chunkcount) : "get: access with index " + index + " is above chunkcount " + this.chunkcount + "; sortBound = " + this.sortBound;
@ -478,6 +482,7 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
}
@Override
public final void delete(final int p) {
removeRow(p, true);
}
@ -533,6 +538,7 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
this.lastTimeWrote = System.currentTimeMillis();
}
@Override
public int size() {
return this.chunkcount;
}
@ -566,14 +572,17 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
this.keepOrderWhenRemoving = keepOrderWhenRemoving;
}
@Override
public boolean hasNext() {
return this.p < RowCollection.this.chunkcount;
}
@Override
public byte[] next() {
return getKey(this.p++);
}
@Override
public void remove() {
this.p--;
removeRow(this.p, this.keepOrderWhenRemoving);
@ -583,6 +592,7 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
/**
* return an iterator for the row entries in this object
*/
@Override
public Iterator<Row.Entry> iterator() {
// iterates kelondroRow.Entry - type entries
return new rowIterator();
@ -601,14 +611,17 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
this.p = 0;
}
@Override
public boolean hasNext() {
return this.p < RowCollection.this.chunkcount;
}
@Override
public Row.Entry next() {
return get(this.p++, true);
}
@Override
public void remove() {
this.p--;
removeRow(this.p, true);
@ -637,6 +650,7 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
this.S = S;
}
@Override
public Integer call() throws Exception {
return Integer.valueOf(this.rc.partition(this.L, this.R, this.S, new byte[this.rc.rowdef.objectsize]));
}
@ -817,6 +831,7 @@ public class RowCollection implements Sortable<Row.Entry>, Iterable<Row.Entry>,
return true;
}
@Override
public synchronized String toString() {
final StringBuilder s = new StringBuilder(80);
final Iterator<Row.Entry> i = iterator();

@ -25,6 +25,7 @@
package net.yacy.kelondro.index;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
@ -42,8 +43,9 @@ import net.yacy.kelondro.order.NaturalOrder;
import net.yacy.kelondro.util.MemoryControl;
public class RowSet extends RowCollection implements Index, Iterable<Row.Entry> {
public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>, Serializable {
private static final long serialVersionUID=-6036029762440788566L;
private static final int collectionReSortLimit = 3000;
public RowSet(final RowSet rs) {
@ -118,20 +120,24 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
super(rowdef, chunkcache, chunkcount, sortBound, lastTimeWrote);
}
@Override
public RowSet clone() {
return new RowSet(super.rowdef, super.chunkcache, super.chunkcount, super.sortBound, super.lastTimeWrote);
}
public void reset() {
@Override
public void reset() {
super.reset();
}
@Override
public final synchronized boolean has(final byte[] key) {
assert key.length == this.rowdef.primaryKeyLength;
final int index = find(key, 0);
return index >= 0;
}
@Override
public final synchronized Row.Entry get(final byte[] key, final boolean forcecopy) {
assert key.length == this.rowdef.primaryKeyLength;
final int index = find(key, 0);
@ -139,6 +145,7 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
return get(index, forcecopy);
}
@Override
public Map<byte[], Row.Entry> get(final Collection<byte[]> keys, final boolean forcecopy) throws IOException, InterruptedException {
final Map<byte[], Row.Entry> map = new TreeMap<byte[], Row.Entry>(row().objectOrder);
Row.Entry entry;
@ -156,6 +163,7 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
* @throws IOException
* @throws RowSpaceExceededException
*/
@Override
public final boolean put(final Row.Entry entry) throws RowSpaceExceededException {
assert (entry != null);
final byte[] key = entry.getPrimaryKeyBytes();
@ -176,6 +184,7 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
}
}
@Override
public final Row.Entry replace(final Row.Entry entry) throws RowSpaceExceededException {
assert (entry != null);
final byte[] key = entry.getPrimaryKeyBytes();
@ -227,6 +236,7 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
* if the entry was found, return the entry, but delete the entry from the set
* if the entry was not found, return null.
*/
@Override
public final synchronized boolean delete(final byte[] a) {
boolean exists = false;
int index;
@ -258,6 +268,7 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
}
}
@Override
public final synchronized Row.Entry remove(final byte[] a) {
Row.Entry entry = null;
int index;
@ -346,6 +357,7 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
return super.keys(true);
}
@Override
public final synchronized CloneableIterator<byte[]> keys(final boolean up, final byte[] firstKey) {
return new keyIterator(up, firstKey);
}
@ -372,10 +384,12 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
}
}
public final keyIterator clone(final Object second) {
@Override
public final keyIterator clone(final Object second) {
return new keyIterator(this.up, (byte[]) second);
}
@Override
public final boolean hasNext() {
if (this.p < 0) return false;
if (this.p >= size()) return false;
@ -386,27 +400,32 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
}
}
@Override
public final byte[] next() {
final byte[] key = getKey(this.p);
if (this.up) this.p++; else this.p--;
return key;
}
@Override
public final void remove() {
throw new UnsupportedOperationException();
}
}
@Override
public final synchronized Iterator<Row.Entry> iterator() {
// iterates kelondroRow.Entry - type entries
sort();
return super.iterator();
}
@Override
public final synchronized CloneableIterator<Row.Entry> rows(final boolean up, final byte[] firstKey) {
return new rowIterator(up, firstKey);
}
@Override
public final synchronized CloneableIterator<Row.Entry> rows() {
return new rowIterator(true, null);
}
@ -432,10 +451,12 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
}
}
public final rowIterator clone(final Object second) {
@Override
public final rowIterator clone(final Object second) {
return new rowIterator(this.up, (byte[]) second);
}
@Override
public final boolean hasNext() {
if (this.p < 0) return false;
if (this.p >= size()) return false;
@ -446,12 +467,14 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
}
}
@Override
public final Row.Entry next() {
final Row.Entry entry = get(this.p, true);
if (this.up) this.p++; else this.p--;
return entry;
}
@Override
public final void remove() {
throw new UnsupportedOperationException();
}
@ -677,10 +700,12 @@ public class RowSet extends RowCollection implements Index, Iterable<Row.Entry>
return randomHash(r.nextLong(), r.nextLong());
}
@Override
public String filename() {
return null;
}
@Override
public void deleteOnExit() {
// do nothing, there is no file
}

@ -27,6 +27,7 @@
package net.yacy.kelondro.order;
import java.io.Serializable;
import java.util.Comparator;
import net.yacy.cora.document.UTF8;
@ -35,7 +36,9 @@ import net.yacy.cora.order.ByteOrder;
import net.yacy.cora.order.Order;
public class Base64Order extends AbstractOrder<byte[]> implements ByteOrder, Comparator<byte[]>, Cloneable {
public class Base64Order extends AbstractOrder<byte[]> implements ByteOrder, Comparator<byte[]>, Cloneable, Serializable {
private static final long serialVersionUID=980647587445343851L;
public static final byte[] alpha_standard = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes();
public static final byte[] alpha_enhanced = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".getBytes();

@ -26,6 +26,7 @@
package net.yacy.kelondro.order;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Iterator;
@ -34,8 +35,9 @@ import net.yacy.cora.order.ByteOrder;
import net.yacy.cora.order.Order;
import net.yacy.kelondro.index.HandleSet;
public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrder, Comparator<byte[]>, Cloneable {
public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrder, Comparator<byte[]>, Cloneable, Serializable {
private static final long serialVersionUID=7170913936645013046L;
public static final ByteOrder naturalOrder = new NaturalOrder(true);
public static final Comparator<String> naturalComparator = new StringOrder(naturalOrder);
public NaturalOrder(final boolean ascending) {
@ -47,14 +49,17 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
return new HandleSet(keylength, this, space);
}
@Override
public boolean wellformed(final byte[] a) {
return true;
}
@Override
public boolean wellformed(final byte[] a, final int astart, final int alength) {
return true;
}
@Override
public final Order<byte[]> clone() {
final NaturalOrder o = new NaturalOrder(this.asc);
o.rotate(this.zero);
@ -75,6 +80,7 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
return null;
}
@Override
public final String signature() {
if (!this.asc) return "nd";
if ( this.asc) return "nu";
@ -92,6 +98,7 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
return c;
}
@Override
public final long cardinal(final byte[] key) {
if (this.zero == null) return cardinalI(key, 0, key.length);
final long zeroCardinal = cardinalI(this.zero, 0, this.zero.length);
@ -100,6 +107,7 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
return Long.MAX_VALUE - keyCardinal + zeroCardinal;
}
@Override
public long cardinal(final byte[] key, final int off, final int len) {
if (this.zero == null) return cardinalI(key, off, len);
final long zeroCardinal = cardinalI(this.zero, 0, this.zero.length);
@ -150,6 +158,7 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
// is less than, equal to, or greater than the second.
// two arrays are also equal if one array is a subset of the other's array
// with filled-up char(0)-values
@Override
public final int compare(final byte[] a, final byte[] b) {
if (a.length == b.length) {
return (this.asc) ? compare0(a, b, a.length) : compare0(b, 0, a, 0, a.length);
@ -165,10 +174,12 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
return (a.length > b.length) ? -1 : 1;
}
@Override
public final int compare(final byte[] a, final byte[] b, final int length) {
return (this.asc) ? compare0(a, b, length) : compare0(b, a, length);
}
@Override
public final int compare(final byte[] a, final int aoffset, final byte[] b, final int boffset, final int length) {
return (this.asc) ? compare0(a, aoffset, b, boffset, length) : compare0(b, boffset, a, aoffset, length);
}
@ -193,6 +204,7 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
return sig(az - bz);
}
@Override
public final boolean equal(final byte[] a, final byte[] b) {
if ((a == null) && (b == null)) return true;
if ((a == null) || (b == null)) return false;
@ -206,6 +218,7 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
return true;
}
@Override
public final boolean equal(final byte[] a, int astart, final byte[] b, int bstart, int length) {
if ((a == null) && (b == null)) return true;
if ((a == null) || (b == null)) return false;
@ -284,10 +297,12 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
this.b256Iterator = b256Iterator;
}
@Override
public boolean hasNext() {
return this.b256Iterator.hasNext();
}
@Override
public Long next() {
final byte[] b = this.b256Iterator.next();
assert (b != null);
@ -295,6 +310,7 @@ public final class NaturalOrder extends AbstractOrder<byte[]> implements ByteOrd
return Long.valueOf(decodeLong(b));
}
@Override
public void remove() {
this.b256Iterator.remove();
}

@ -10,7 +10,7 @@
// $LastChangedBy$
//
// LICENSE
//
//
// 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
@ -27,24 +27,29 @@
package net.yacy.kelondro.order;
import java.io.Serializable;
import java.util.Comparator;
import net.yacy.cora.document.UTF8;
import net.yacy.cora.order.ByteOrder;
import net.yacy.cora.order.Order;
public class StringOrder implements Comparator<String> {
public class StringOrder implements Comparator<String>, Serializable {
private static final long serialVersionUID=-5443022063770309585L;
public ByteOrder baseOrder;
public StringOrder(final ByteOrder base) {
this.baseOrder = base;
}
public StringOrder(final Order<byte[]> base) {
this.baseOrder = (ByteOrder) base;
}
@Override
public final int compare(final String s1, final String s2) {
return baseOrder.compare(UTF8.getBytes(s1), UTF8.getBytes(s2));
return this.baseOrder.compare(UTF8.getBytes(s1), UTF8.getBytes(s2));
}
}

Loading…
Cancel
Save