From 1795a7325b5503c11c5e2e92addaf67deffa0264 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Tue, 15 May 2012 12:55:15 +0200 Subject: [PATCH] made HandleSet serializable --- .classpath | 2 +- source/net/yacy/cora/order/ByteOrder.java | 4 +- source/net/yacy/kelondro/index/Column.java | 72 ++++----- source/net/yacy/kelondro/index/HandleSet.java | 140 ++++++++++++------ source/net/yacy/kelondro/index/Row.java | 6 +- .../yacy/kelondro/index/RowCollection.java | 21 ++- source/net/yacy/kelondro/index/RowSet.java | 33 ++++- .../net/yacy/kelondro/order/Base64Order.java | 5 +- .../net/yacy/kelondro/order/NaturalOrder.java | 18 ++- .../net/yacy/kelondro/order/StringOrder.java | 15 +- 10 files changed, 216 insertions(+), 100 deletions(-) diff --git a/.classpath b/.classpath index 2391cf3cd8..ab179dd065 100644 --- a/.classpath +++ b/.classpath @@ -46,7 +46,7 @@ - + diff --git a/source/net/yacy/cora/order/ByteOrder.java b/source/net/yacy/cora/order/ByteOrder.java index b7fc41f129..c1bcb102f1 100644 --- a/source/net/yacy/cora/order/ByteOrder.java +++ b/source/net/yacy/cora/order/ByteOrder.java @@ -24,8 +24,10 @@ package net.yacy.cora.order; +import java.io.Serializable; -public interface ByteOrder extends Order { + +public interface ByteOrder extends Order, Serializable { @Override public boolean wellformed(byte[] a); diff --git a/source/net/yacy/kelondro/index/Column.java b/source/net/yacy/kelondro/index/Column.java index a6d806c77a..9584a3bf99 100644 --- a/source/net/yacy/kelondro/index/Column.java +++ b/source/net/yacy/kelondro/index/Column.java @@ -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 Column(String celldef) { 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 Column(String celldef) { } 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 Column(String celldef) { 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 Column(String celldef) { 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 Column(String celldef) { 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 Column(String celldef) { @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 String toString() { 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 boolean equals(final Object obj) { 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; } diff --git a/source/net/yacy/kelondro/index/HandleSet.java b/source/net/yacy/kelondro/index/HandleSet.java index a1ecbd8a27..b93175953a 100644 --- a/source/net/yacy/kelondro/index/HandleSet.java +++ b/source/net/yacy/kelondro/index/HandleSet.java @@ -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.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, Cloneable { - +public final class HandleSet implements Iterable, 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 HandleSet(Row rowdef, byte[] b) throws RowSpaceExceededException { 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 int dump(final File file) throws IOException { 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 void putAll(final HandleSet aset) throws RowSpaceExceededException /** * 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 synchronized byte[] removeOne() { 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 keys(final boolean up, final byte[] firstKey) { - return index.keys(up, firstKey); + return this.index.keys(up, firstKey); } - + + @Override public final Iterator 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 @@ private static HandleSet joinConstructiveByEnumeration(final HandleSet set1, fin 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 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 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(); + } + } diff --git a/source/net/yacy/kelondro/index/Row.java b/source/net/yacy/kelondro/index/Row.java index 4e07dee1d2..1b0def0595 100644 --- a/source/net/yacy/kelondro/index/Row.java +++ b/source/net/yacy/kelondro/index/Row.java @@ -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.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 Entry(final Entry oldrow, final int fromColumn, final boolean forceclone) 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; diff --git a/source/net/yacy/kelondro/index/RowCollection.java b/source/net/yacy/kelondro/index/RowCollection.java index df9480281c..8bb208240c 100644 --- a/source/net/yacy/kelondro/index/RowCollection.java +++ b/source/net/yacy/kelondro/index/RowCollection.java @@ -26,6 +26,7 @@ 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.kelondroException; -public class RowCollection implements Sortable, Iterable, Cloneable { +public class RowCollection implements Sortable, Iterable, 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 @@ protected RowCollection(final Row rowdef, final byte[] chunkcache, final int chu this.lastTimeWrote = lastTimeWrote; } + @Override public RowCollection clone() { return new RowCollection(this.rowdef, this.chunkcache, this.chunkcount, this.sortBound, this.lastTimeWrote); } @@ -334,6 +337,7 @@ protected synchronized final byte[] getKey(final int index) { 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 synchronized final void removeRow(final int p, final boolean keepOrder) { } + @Override public final void delete(final int p) { removeRow(p, true); } @@ -533,6 +538,7 @@ public synchronized void clear() { this.lastTimeWrote = System.currentTimeMillis(); } + @Override public int size() { return this.chunkcount; } @@ -566,14 +572,17 @@ private keyIterator(final boolean keepOrderWhenRemoving) { 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 void remove() { /** * return an iterator for the row entries in this object */ + @Override public Iterator iterator() { // iterates kelondroRow.Entry - type entries return new rowIterator(); @@ -601,14 +611,17 @@ public rowIterator() { 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 partitionthread(final RowCollection rc, final int L, final int R, final i 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 synchronized boolean isSorted() { return true; } + @Override public synchronized String toString() { final StringBuilder s = new StringBuilder(80); final Iterator i = iterator(); diff --git a/source/net/yacy/kelondro/index/RowSet.java b/source/net/yacy/kelondro/index/RowSet.java index 2ba38a0d6c..43f002b033 100644 --- a/source/net/yacy/kelondro/index/RowSet.java +++ b/source/net/yacy/kelondro/index/RowSet.java @@ -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.util.MemoryControl; -public class RowSet extends RowCollection implements Index, Iterable { +public class RowSet extends RowCollection implements Index, Iterable, Serializable { + private static final long serialVersionUID=-6036029762440788566L; private static final int collectionReSortLimit = 3000; public RowSet(final RowSet rs) { @@ -118,20 +120,24 @@ private RowSet(final Row rowdef, final byte[] chunkcache, final int chunkcount, 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 final synchronized Row.Entry get(final byte[] key, final boolean forcecop return get(index, forcecopy); } + @Override public Map get(final Collection keys, final boolean forcecopy) throws IOException, InterruptedException { final Map map = new TreeMap(row().objectOrder); Row.Entry entry; @@ -156,6 +163,7 @@ public Map get(final Collection keys, final boolean f * @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 final boolean put(final Row.Entry entry) throws RowSpaceExceededException } } + @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 final synchronized long inc(final byte[] key, final int col, final long a * 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 final synchronized void delete(final List keys) { } } + @Override public final synchronized Row.Entry remove(final byte[] a) { Row.Entry entry = null; int index; @@ -346,6 +357,7 @@ public final synchronized Iterator keys() { return super.keys(true); } + @Override public final synchronized CloneableIterator keys(final boolean up, final byte[] firstKey) { return new keyIterator(up, firstKey); } @@ -372,10 +384,12 @@ public keyIterator(final boolean up, byte[] firstKey) { } } - 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 final boolean hasNext() { } } + @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 iterator() { // iterates kelondroRow.Entry - type entries sort(); return super.iterator(); } + @Override public final synchronized CloneableIterator rows(final boolean up, final byte[] firstKey) { return new rowIterator(up, firstKey); } + @Override public final synchronized CloneableIterator rows() { return new rowIterator(true, null); } @@ -432,10 +451,12 @@ public rowIterator(final boolean up, final byte[] firstKey) { } } - 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 final boolean hasNext() { } } + @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 static byte[] randomHash(final Random r) { return randomHash(r.nextLong(), r.nextLong()); } + @Override public String filename() { return null; } + @Override public void deleteOnExit() { // do nothing, there is no file } diff --git a/source/net/yacy/kelondro/order/Base64Order.java b/source/net/yacy/kelondro/order/Base64Order.java index 2b2f689e77..ae0603de0e 100644 --- a/source/net/yacy/kelondro/order/Base64Order.java +++ b/source/net/yacy/kelondro/order/Base64Order.java @@ -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.Order; -public class Base64Order extends AbstractOrder implements ByteOrder, Comparator, Cloneable { +public class Base64Order extends AbstractOrder implements ByteOrder, Comparator, Cloneable, Serializable { + + private static final long serialVersionUID=980647587445343851L; public static final byte[] alpha_standard = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes(); public static final byte[] alpha_enhanced = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".getBytes(); diff --git a/source/net/yacy/kelondro/order/NaturalOrder.java b/source/net/yacy/kelondro/order/NaturalOrder.java index d01a5f25d4..4f18b845aa 100644 --- a/source/net/yacy/kelondro/order/NaturalOrder.java +++ b/source/net/yacy/kelondro/order/NaturalOrder.java @@ -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.Order; import net.yacy.kelondro.index.HandleSet; -public final class NaturalOrder extends AbstractOrder implements ByteOrder, Comparator, Cloneable { +public final class NaturalOrder extends AbstractOrder implements ByteOrder, Comparator, Cloneable, Serializable { + private static final long serialVersionUID=7170913936645013046L; public static final ByteOrder naturalOrder = new NaturalOrder(true); public static final Comparator naturalComparator = new StringOrder(naturalOrder); public NaturalOrder(final boolean ascending) { @@ -47,14 +49,17 @@ public HandleSet getHandleSet(final int keylength, final int space) { 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 clone() { final NaturalOrder o = new NaturalOrder(this.asc); o.rotate(this.zero); @@ -75,6 +80,7 @@ public final static ByteOrder bySignature(final String signature) { return null; } + @Override public final String signature() { if (!this.asc) return "nd"; if ( this.asc) return "nu"; @@ -92,6 +98,7 @@ private final static long cardinalI(final byte[] key, int off, final int len) { 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 long cardinal(final byte[] key) { 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 @@ private static final int sig(final int x) { // 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 int compare(final byte[] a, final byte[] b) { 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 @@ private final int compare0(final byte[] a, final int aoffset, final byte[] b, fi 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 boolean equal(final byte[] a, final byte[] b) { 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 LongIter(final Iterator b256Iterator) { 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 Long next() { return Long.valueOf(decodeLong(b)); } + @Override public void remove() { this.b256Iterator.remove(); } diff --git a/source/net/yacy/kelondro/order/StringOrder.java b/source/net/yacy/kelondro/order/StringOrder.java index 9b83ada58f..21531ed303 100644 --- a/source/net/yacy/kelondro/order/StringOrder.java +++ b/source/net/yacy/kelondro/order/StringOrder.java @@ -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 { +public class StringOrder implements Comparator, Serializable { + + private static final long serialVersionUID=-5443022063770309585L; public ByteOrder baseOrder; + public StringOrder(final ByteOrder base) { this.baseOrder = base; } - + public StringOrder(final Order 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)); } }