From 56084e9edc7f68e6295c7d9e50868c33ba82f42e Mon Sep 17 00:00:00 2001 From: orbiter Date: Wed, 7 Jun 2006 22:28:21 +0000 Subject: [PATCH] added a intMap, the new index management structure for kelondroFlexTables git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2185 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- .../anomic/kelondro/kelondroCollection.java | 34 +++++--- .../kelondro/kelondroCollectionIntMap.java | 80 +++++++++++++++++++ .../de/anomic/kelondro/kelondroFlexTable.java | 36 +++------ 3 files changed, 116 insertions(+), 34 deletions(-) create mode 100644 source/de/anomic/kelondro/kelondroCollectionIntMap.java diff --git a/source/de/anomic/kelondro/kelondroCollection.java b/source/de/anomic/kelondro/kelondroCollection.java index 218617d1f..a75c73501 100644 --- a/source/de/anomic/kelondro/kelondroCollection.java +++ b/source/de/anomic/kelondro/kelondroCollection.java @@ -46,7 +46,7 @@ import java.util.Random; public class kelondroCollection { - private byte[] chunkcache; + protected byte[] chunkcache; private int chunkcount; private int chunksize; private int sortbound; @@ -122,6 +122,19 @@ public class kelondroCollection { return null; } + protected void set(int index, byte[] a) { + set(index, a, a.length); + } + + protected void set(int index, byte[] a, int length) { + assert (index < this.chunkcount); + int l = Math.min(this.chunksize, Math.min(length, a.length)); + synchronized (chunkcache) { + System.arraycopy(a, 0, chunkcache, chunksize * index, l); + } + this.lastTimeWrote = System.currentTimeMillis(); + } + public void add(byte[] a) { add(a, a.length); } @@ -149,22 +162,21 @@ public class kelondroCollection { } } - public void remove(byte[] a, int length) { - // the byte[] a may be shorter than the chunksize - if (chunkcount == 0) return; - synchronized(chunkcache) { - int p = find(a, length); - remove(p); - } + public byte[] remove(byte[] a) { + return remove(a, a.length); } - public void remove(byte[] a, int length, kelondroOrder ko) { + public byte[] remove(byte[] a, int length) { // the byte[] a may be shorter than the chunksize - if (chunkcount == 0) return; + if (chunkcount == 0) return null; + byte[] b = null; synchronized(chunkcache) { int p = find(a, length); + if (p < 0) return null; + b = get(p); remove(p); } + return b; } private void remove(int p) { @@ -240,7 +252,7 @@ public class kelondroCollection { } } - private int find(byte[] a, int length) { + protected int find(byte[] a, int length) { // returns the chunknumber; -1 if not found if (this.order == null) return iterativeSearch(a, length); diff --git a/source/de/anomic/kelondro/kelondroCollectionIntMap.java b/source/de/anomic/kelondro/kelondroCollectionIntMap.java new file mode 100644 index 000000000..b87919b20 --- /dev/null +++ b/source/de/anomic/kelondro/kelondroCollectionIntMap.java @@ -0,0 +1,80 @@ +// kelondroCollectionIntMap.java +// (C) 2006 by Michael Peter Christen; mc@anomic.de, Frankfurt a. M., Germany +// first published 08.06.2006 on http://www.anomic.de +// +// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $ +// $LastChangedRevision: 1986 $ +// $LastChangedBy: orbiter $ +// +// 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 +// (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 + +package de.anomic.kelondro; + +public class kelondroCollectionIntMap extends kelondroCollection { + + private kelondroRow indexrow; + + public kelondroCollectionIntMap(int keySize, int initSize) { + super(keySize + 4, initSize); + + // initialize row + this.indexrow = new kelondroRow(new int[]{keySize, 4}); + + // initialize ordering + this.setOrdering(kelondroNaturalOrder.naturalOrder); + } + + public int puti(byte[] key, int i) { + int index = -1; + synchronized (chunkcache) { + index = find(key, key.length); + } + if (index < 0) { + kelondroRow.Entry indexentry = indexrow.newEntry(); + indexentry.setCol(0, key); + indexentry.setColLongB256(1, i); + add(indexentry.bytes()); + return -1; + } else { + kelondroRow.Entry indexentry = indexrow.newEntry(get(index)); + int oldi = (int) indexentry.getColLongB256(1); + indexentry.setColLongB256(1, i); + set(index, indexentry.bytes()); + return oldi; + } + } + + public int geti(byte[] key) { + int index = -1; + synchronized (chunkcache) { + index = find(key, key.length); + } + if (index < 0) { + return -1; + } else { + kelondroRow.Entry indexentry = indexrow.newEntry(get(index)); + return (int) indexentry.getColLongB256(1); + } + } + + public int removei(byte[] key) { + byte[] b = remove(key); + if (b == null) return -1; + kelondroRow.Entry indexentry = indexrow.newEntry(b); + return (int) indexentry.getColLongB256(1); + } +} diff --git a/source/de/anomic/kelondro/kelondroFlexTable.java b/source/de/anomic/kelondro/kelondroFlexTable.java index 0278e87c5..35e98771a 100644 --- a/source/de/anomic/kelondro/kelondroFlexTable.java +++ b/source/de/anomic/kelondro/kelondroFlexTable.java @@ -30,26 +30,20 @@ import java.io.IOException; public class kelondroFlexTable extends kelondroFlexWidthArray implements kelondroIndex { - private kelondroCollection index; - private kelondroRow indexrow; + private kelondroCollectionIntMap index; public kelondroFlexTable(File path, String tablename, kelondroRow rowdef, boolean exitOnFail) throws IOException { super(path, tablename, rowdef, exitOnFail); // fill the index - this.index = new kelondroCollection(super.row().width(0) + 4); + this.index = new kelondroCollectionIntMap(super.row().width(0), 0); /* kelondroFixedWidthArray indexArray = new kelondroFixedWidthArray(new File(path, colfilename(0,0))); for (int i = 0; i < indexArray.size(); i++) index.put(indexArray.get(i).getColBytes(0), new Integer(i)); indexArray.close(); */ - this.indexrow = new kelondroRow(new int[]{super.row().width(0), 4}); - kelondroRow.Entry indexentry; for (int i = 0; i < super.col[0].size(); i++) { - indexentry = indexrow.newEntry(); - indexentry.setCol(0, super.col[0].get(i).getColBytes(0)); - indexentry.setColLongB256(1, i); - index.add(indexentry.bytes()); + index.puti(super.col[0].get(i).getColBytes(0), i); } this.index.setOrdering(kelondroNaturalOrder.naturalOrder); } @@ -70,29 +64,25 @@ public class kelondroFlexTable extends kelondroFlexWidthArray implements kelondr */ public kelondroRow.Entry get(byte[] key) throws IOException { - kelondroRow.Entry indexentry = this.indexrow.newEntry(this.index.get(key)); - if (indexentry == null) return null; - return super.get((int) indexentry.getColLongB256(1)); + int i = index.geti(key); + return super.get(i); } public kelondroRow.Entry put(kelondroRow.Entry row) throws IOException { - kelondroRow.Entry indexentry = this.indexrow.newEntry(this.index.get(row.getColBytes(0))); - if (indexentry == null) { - indexentry = indexrow.newEntry(); - indexentry.setCol(0, row.getColBytes(0)); - indexentry.setColLongB256(1, super.add(row)); - index.add(indexentry.bytes()); + int i = index.geti(row.getColBytes(0)); + if (i < 0) { + index.puti(row.getColBytes(0), super.add(row)); return null; } else { - return super.set((int) indexentry.getColLongB256(1), row); + return super.set(i, row); } } public kelondroRow.Entry remove(byte[] key) throws IOException { - kelondroRow.Entry indexentry = this.indexrow.newEntry(this.index.get(key)); - if (indexentry == null) return null; - kelondroRow.Entry r = super.get((int) indexentry.getColLongB256(1)); - super.remove((int) indexentry.getColLongB256(1)); + int i = index.removei(key); + if (i < 0) return null; + kelondroRow.Entry r = super.get(i); + super.remove(i); return r; }