2006-06-20 23:41:12 +02:00
|
|
|
// kelondroBytesIntMap.java
|
2006-06-08 00:28:21 +02:00
|
|
|
// (C) 2006 by Michael Peter Christen; mc@anomic.de, Frankfurt a. M., Germany
|
2006-06-20 23:41:12 +02:00
|
|
|
// first published 18.06.2006 on http://www.anomic.de
|
2006-06-08 00:28:21 +02:00
|
|
|
//
|
|
|
|
// $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;
|
|
|
|
|
2006-06-30 14:54:19 +02:00
|
|
|
import java.io.IOException;
|
2008-01-20 02:22:46 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
2006-06-30 14:54:19 +02:00
|
|
|
|
|
|
|
public class kelondroBytesIntMap {
|
|
|
|
|
2007-05-16 16:36:56 +02:00
|
|
|
private kelondroRow rowdef;
|
2008-01-07 23:36:48 +01:00
|
|
|
private kelondroIndex index;
|
2006-06-08 00:28:21 +02:00
|
|
|
|
2007-06-26 16:37:10 +02:00
|
|
|
public kelondroBytesIntMap(kelondroIndex ki) {
|
2006-06-30 14:54:19 +02:00
|
|
|
assert (ki.row().columns() == 2); // must be a key/index relation
|
|
|
|
assert (ki.row().width(1) == 4); // the value must be a b256-encoded int, 4 bytes long
|
2008-01-07 23:36:48 +01:00
|
|
|
this.index = ki;
|
2007-05-16 16:36:56 +02:00
|
|
|
this.rowdef = ki.row();
|
2006-06-08 00:28:21 +02:00
|
|
|
}
|
2006-06-30 14:54:19 +02:00
|
|
|
|
2008-01-11 01:12:01 +01:00
|
|
|
public kelondroBytesIntMap(int keylength, kelondroByteOrder objectOrder, int space) {
|
2007-05-16 16:36:56 +02:00
|
|
|
this.rowdef = new kelondroRow(new kelondroColumn[]{new kelondroColumn("key", kelondroColumn.celltype_binary, kelondroColumn.encoder_bytes, keylength, "key"), new kelondroColumn("int c-4 {b256}")}, objectOrder, 0);
|
2008-01-07 23:36:48 +01:00
|
|
|
this.index = new kelondroRAMIndex(rowdef, space);
|
2007-05-16 12:48:26 +02:00
|
|
|
}
|
|
|
|
|
2007-06-26 16:37:10 +02:00
|
|
|
public kelondroRow row() {
|
2008-01-07 23:36:48 +01:00
|
|
|
return index.row();
|
2007-03-06 23:43:32 +01:00
|
|
|
}
|
|
|
|
|
2008-06-18 01:56:39 +02:00
|
|
|
public synchronized boolean has(byte[] key) {
|
|
|
|
assert (key != null);
|
|
|
|
return index.has(key);
|
|
|
|
}
|
|
|
|
|
2006-10-11 02:46:45 +02:00
|
|
|
public synchronized int geti(byte[] key) throws IOException {
|
2006-10-24 15:48:16 +02:00
|
|
|
assert (key != null);
|
2008-01-07 23:36:48 +01:00
|
|
|
kelondroRow.Entry indexentry = index.get(key);
|
2007-05-16 17:20:15 +02:00
|
|
|
if (indexentry == null) return -1;
|
|
|
|
return (int) indexentry.getColLong(1);
|
2006-06-21 00:13:17 +02:00
|
|
|
}
|
|
|
|
|
2006-10-11 02:46:45 +02:00
|
|
|
public synchronized int puti(byte[] key, int i) throws IOException {
|
2008-01-17 13:12:52 +01:00
|
|
|
assert i >= 0 : "i = " + i;
|
2006-10-24 15:48:16 +02:00
|
|
|
assert (key != null);
|
2008-01-07 23:36:48 +01:00
|
|
|
kelondroRow.Entry newentry = index.row().newEntry();
|
2006-06-21 00:13:17 +02:00
|
|
|
newentry.setCol(0, key);
|
2006-08-11 05:20:44 +02:00
|
|
|
newentry.setCol(1, i);
|
2008-01-07 23:36:48 +01:00
|
|
|
kelondroRow.Entry oldentry = index.put(newentry);
|
2006-06-21 00:13:17 +02:00
|
|
|
if (oldentry == null) return -1;
|
2006-08-11 05:20:44 +02:00
|
|
|
return (int) oldentry.getColLong(1);
|
2006-06-21 00:13:17 +02:00
|
|
|
}
|
2006-06-08 01:00:26 +02:00
|
|
|
|
2008-04-08 13:55:59 +02:00
|
|
|
public synchronized boolean addi(byte[] key, int i) throws IOException {
|
2008-01-17 13:12:52 +01:00
|
|
|
assert i >= 0 : "i = " + i;
|
2006-10-24 15:48:16 +02:00
|
|
|
assert (key != null);
|
2007-05-16 16:36:56 +02:00
|
|
|
kelondroRow.Entry newentry = this.rowdef.newEntry();
|
2006-10-19 23:14:37 +02:00
|
|
|
newentry.setCol(0, key);
|
|
|
|
newentry.setCol(1, i);
|
2008-04-08 13:55:59 +02:00
|
|
|
return index.addUnique(newentry);
|
2006-10-19 23:14:37 +02:00
|
|
|
}
|
|
|
|
|
2008-01-20 02:22:46 +01:00
|
|
|
public synchronized ArrayList<Integer[]> removeDoubles() throws IOException {
|
|
|
|
ArrayList<Integer[]> report = new ArrayList<Integer[]>();
|
|
|
|
Integer[] is;
|
|
|
|
Iterator<kelondroRow.Entry> ei;
|
|
|
|
int c;
|
2008-05-02 00:40:42 +02:00
|
|
|
for (kelondroRowCollection delset: index.removeDoubles()) {
|
|
|
|
is = new Integer[delset.size()];
|
|
|
|
ei = delset.rows();
|
2008-01-20 02:22:46 +01:00
|
|
|
c = 0;
|
|
|
|
while (ei.hasNext()) {
|
|
|
|
is[c++] = new Integer((int) ei.next().getColLong(1));
|
|
|
|
}
|
|
|
|
report.add(is);
|
|
|
|
}
|
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
2006-10-11 02:46:45 +02:00
|
|
|
public synchronized int removei(byte[] key) throws IOException {
|
2006-10-24 15:48:16 +02:00
|
|
|
assert (key != null);
|
2008-01-19 13:23:56 +01:00
|
|
|
kelondroRow.Entry indexentry = index.remove(key, true); // keeping the order will prevent multiple re-sorts
|
2006-06-21 00:13:17 +02:00
|
|
|
if (indexentry == null) return -1;
|
2006-08-11 05:20:44 +02:00
|
|
|
return (int) indexentry.getColLong(1);
|
2006-06-08 00:28:21 +02:00
|
|
|
}
|
2006-06-20 23:41:12 +02:00
|
|
|
|
2006-10-16 17:04:16 +02:00
|
|
|
public synchronized int removeonei() throws IOException {
|
2008-01-17 13:12:52 +01:00
|
|
|
kelondroRow.Entry indexentry = index.removeOne();
|
|
|
|
if (indexentry == null) return -1;
|
2008-01-07 23:36:48 +01:00
|
|
|
return (int) indexentry.getColLong(1);
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
2007-04-19 15:37:02 +02:00
|
|
|
public synchronized int size() {
|
2008-01-07 23:36:48 +01:00
|
|
|
return index.size();
|
2006-06-30 14:54:19 +02:00
|
|
|
}
|
|
|
|
|
2008-01-09 10:58:56 +01:00
|
|
|
public synchronized kelondroCloneableIterator<byte[]> keys(boolean up, byte[] firstKey) throws IOException {
|
2008-01-07 23:36:48 +01:00
|
|
|
return index.keys(up, firstKey);
|
2007-12-27 18:56:59 +01:00
|
|
|
}
|
|
|
|
|
2008-01-09 10:58:56 +01:00
|
|
|
public synchronized kelondroCloneableIterator<kelondroRow.Entry> rows(boolean up, byte[] firstKey) throws IOException {
|
2008-01-07 23:36:48 +01:00
|
|
|
return index.rows(up, firstKey);
|
2006-08-05 21:18:33 +02:00
|
|
|
}
|
|
|
|
|
2006-10-16 17:04:16 +02:00
|
|
|
public kelondroProfile profile() {
|
2008-01-07 23:36:48 +01:00
|
|
|
return index.profile();
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
2007-03-09 09:48:47 +01:00
|
|
|
public synchronized void close() {
|
2008-01-07 23:36:48 +01:00
|
|
|
index.close();
|
|
|
|
index = null;
|
2007-05-16 12:48:26 +02:00
|
|
|
}
|
|
|
|
|
2006-06-08 00:28:21 +02:00
|
|
|
}
|