You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
334 lines
12 KiB
334 lines
12 KiB
// kelondroBytesLongMap.java
|
|
// (C) 2008 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
|
// first published 08.04.2008 on http://yacy.net
|
|
//
|
|
// $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.index;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
import java.util.concurrent.BlockingQueue;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.Future;
|
|
|
|
import de.anomic.kelondro.order.Base64Order;
|
|
import de.anomic.kelondro.order.ByteOrder;
|
|
import de.anomic.kelondro.order.CloneableIterator;
|
|
|
|
public class LongHandleIndex {
|
|
|
|
private final Row rowdef;
|
|
private ObjectIndexCache index;
|
|
|
|
/**
|
|
* initialize a BytesLongMap
|
|
* This may store a key and a long value for each key.
|
|
* The class is used as index for database files
|
|
* @param keylength
|
|
* @param objectOrder
|
|
* @param space
|
|
*/
|
|
public LongHandleIndex(final int keylength, final ByteOrder objectOrder, final int initialspace, final int expectedspace) {
|
|
this.rowdef = new Row(new Column[]{new Column("key", Column.celltype_binary, Column.encoder_bytes, keylength, "key"), new Column("long c-8 {b256}")}, objectOrder);
|
|
this.index = new ObjectIndexCache(rowdef, initialspace, expectedspace);
|
|
}
|
|
|
|
/**
|
|
* initialize a BytesLongMap with the content of a dumped index
|
|
* @param keylength
|
|
* @param objectOrder
|
|
* @param file
|
|
* @throws IOException
|
|
*/
|
|
public LongHandleIndex(final int keylength, final ByteOrder objectOrder, final File file, final int expectedspace) throws IOException {
|
|
this(keylength, objectOrder, (int) (file.length() / (keylength + 8)), expectedspace);
|
|
// read the index dump and fill the index
|
|
InputStream is = new BufferedInputStream(new FileInputStream(file), 1024 * 1024);
|
|
byte[] a = new byte[keylength + 8];
|
|
int c;
|
|
while (true) {
|
|
c = is.read(a);
|
|
if (c <= 0) break;
|
|
this.index.addUnique(this.rowdef.newEntry(a));
|
|
}
|
|
is.close();
|
|
is = null;
|
|
assert this.index.size() == file.length() / (keylength + 8);
|
|
}
|
|
|
|
/**
|
|
* write a dump of the index to a file. All entries are written in order
|
|
* which makes it possible to read them again in a fast way
|
|
* @param file
|
|
* @return the number of written entries
|
|
* @throws IOException
|
|
*/
|
|
public int dump(File file) throws IOException {
|
|
// we must use an iterator from the combined index, because we need the entries sorted
|
|
// otherwise we could just write the byte[] from the in kelondroRowSet which would make
|
|
// everything much faster, but this is not an option here.
|
|
File tmp = new File(file.getParentFile(), file.getName() + ".tmp");
|
|
Iterator<Row.Entry> i = this.index.rows(true, null);
|
|
OutputStream os = new BufferedOutputStream(new FileOutputStream(tmp), 4 * 1024 * 1024);
|
|
int c = 0;
|
|
while (i.hasNext()) {
|
|
os.write(i.next().bytes());
|
|
c++;
|
|
}
|
|
os.flush();
|
|
os.close();
|
|
tmp.renameTo(file);
|
|
assert file.exists() : file.toString();
|
|
assert !tmp.exists() : tmp.toString();
|
|
return c;
|
|
}
|
|
|
|
public Row row() {
|
|
return index.row();
|
|
}
|
|
|
|
public void clear() {
|
|
index.clear();
|
|
}
|
|
|
|
public synchronized long get(final byte[] key) {
|
|
assert (key != null);
|
|
final Row.Entry indexentry = index.get(key);
|
|
if (indexentry == null) return -1;
|
|
return indexentry.getColLong(1);
|
|
}
|
|
|
|
public synchronized long put(final byte[] key, final long l) {
|
|
assert l >= 0 : "l = " + l;
|
|
assert (key != null);
|
|
final Row.Entry newentry = index.row().newEntry();
|
|
newentry.setCol(0, key);
|
|
newentry.setCol(1, l);
|
|
final Row.Entry oldentry = index.replace(newentry);
|
|
if (oldentry == null) return -1;
|
|
return oldentry.getColLong(1);
|
|
}
|
|
|
|
public synchronized void putUnique(final byte[] key, final long l) {
|
|
assert l >= 0 : "l = " + l;
|
|
assert (key != null);
|
|
final Row.Entry newentry = this.rowdef.newEntry();
|
|
newentry.setCol(0, key);
|
|
newentry.setCol(1, l);
|
|
index.addUnique(newentry);
|
|
}
|
|
|
|
public synchronized long add(final byte[] key, long a) {
|
|
assert key != null;
|
|
assert a > 0; // it does not make sense to add 0. If this occurres, it is a performance issue
|
|
|
|
final Row.Entry indexentry = index.get(key);
|
|
if (indexentry == null) {
|
|
final Row.Entry newentry = this.rowdef.newEntry();
|
|
newentry.setCol(0, key);
|
|
newentry.setCol(1, a);
|
|
index.addUnique(newentry);
|
|
return 1;
|
|
} else {
|
|
long i = indexentry.getColLong(1) + a;
|
|
indexentry.setCol(1, i);
|
|
index.put(indexentry);
|
|
return i;
|
|
}
|
|
}
|
|
|
|
public synchronized long inc(final byte[] key) {
|
|
return add(key, 1);
|
|
}
|
|
|
|
public synchronized long dec(final byte[] key) {
|
|
return add(key, -1);
|
|
}
|
|
|
|
public synchronized ArrayList<Long[]> removeDoubles() {
|
|
final ArrayList<RowCollection> indexreport = index.removeDoubles();
|
|
final ArrayList<Long[]> report = new ArrayList<Long[]>();
|
|
Long[] is;
|
|
int c;
|
|
for (final RowCollection rowset: indexreport) {
|
|
is = new Long[rowset.size()];
|
|
c = 0;
|
|
for (Row.Entry e: rowset) {
|
|
is[c++] = Long.valueOf(e.getColLong(1));
|
|
}
|
|
report.add(is);
|
|
}
|
|
return report;
|
|
}
|
|
|
|
public synchronized long remove(final byte[] key) {
|
|
assert (key != null);
|
|
final Row.Entry indexentry = index.remove(key);
|
|
if (indexentry == null) return -1;
|
|
return indexentry.getColLong(1);
|
|
}
|
|
|
|
public synchronized long removeone() {
|
|
final Row.Entry indexentry = index.removeOne();
|
|
if (indexentry == null) return -1;
|
|
return indexentry.getColLong(1);
|
|
}
|
|
|
|
public synchronized int size() {
|
|
return index.size();
|
|
}
|
|
|
|
public synchronized CloneableIterator<byte[]> keys(final boolean up, final byte[] firstKey) {
|
|
return index.keys(up, firstKey);
|
|
}
|
|
|
|
public synchronized CloneableIterator<Row.Entry> rows(final boolean up, final byte[] firstKey) {
|
|
return index.rows(up, firstKey);
|
|
}
|
|
|
|
public synchronized void close() {
|
|
index.close();
|
|
index = null;
|
|
}
|
|
|
|
/**
|
|
* this method creates a concurrent thread that can take entries that are used to initialize the map
|
|
* it should be used when a bytesLongMap is initialized when a file is read. Concurrency of FileIO and
|
|
* map creation will speed up the initialization process.
|
|
* @param keylength
|
|
* @param objectOrder
|
|
* @param space
|
|
* @param bufferSize
|
|
* @return
|
|
*/
|
|
public static initDataConsumer asynchronusInitializer(final int keylength, final ByteOrder objectOrder, final int space, final int expectedspace, int bufferSize) {
|
|
initDataConsumer initializer = new initDataConsumer(new LongHandleIndex(keylength, objectOrder, space, expectedspace), bufferSize);
|
|
ExecutorService service = Executors.newSingleThreadExecutor();
|
|
initializer.setResult(service.submit(initializer));
|
|
service.shutdown();
|
|
return initializer;
|
|
}
|
|
|
|
private static class entry {
|
|
public byte[] key;
|
|
public long l;
|
|
public entry(final byte[] key, final long l) {
|
|
this.key = key;
|
|
this.l = l;
|
|
}
|
|
}
|
|
private static final entry poisonEntry = new entry(new byte[0], 0);
|
|
|
|
public static class initDataConsumer implements Callable<LongHandleIndex> {
|
|
|
|
private BlockingQueue<entry> cache;
|
|
private LongHandleIndex map;
|
|
private Future<LongHandleIndex> result;
|
|
|
|
public initDataConsumer(LongHandleIndex map, int bufferCount) {
|
|
this.map = map;
|
|
cache = new ArrayBlockingQueue<entry>(bufferCount);
|
|
}
|
|
|
|
protected void setResult(Future<LongHandleIndex> result) {
|
|
this.result = result;
|
|
}
|
|
|
|
/**
|
|
* hand over another entry that shall be inserted into the BytesLongMap with an addl method
|
|
* @param key
|
|
* @param l
|
|
*/
|
|
public void consume(final byte[] key, final long l) {
|
|
try {
|
|
cache.put(new entry(key, l));
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* to signal the initialization thread that no more entries will be sublitted with consumer()
|
|
* this method must be called. The process will not terminate if this is not called before.
|
|
*/
|
|
public void finish() {
|
|
try {
|
|
cache.put(poisonEntry);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* this must be called after a finish() was called. this method blocks until all entries
|
|
* had been processed, and the content was sorted. It returns the kelondroBytesLongMap
|
|
* that the user wanted to initialize
|
|
* @return
|
|
* @throws InterruptedException
|
|
* @throws ExecutionException
|
|
*/
|
|
public LongHandleIndex result() throws InterruptedException, ExecutionException {
|
|
return this.result.get();
|
|
}
|
|
|
|
public LongHandleIndex call() throws IOException {
|
|
try {
|
|
entry c;
|
|
while ((c = cache.take()) != poisonEntry) {
|
|
map.putUnique(c.key, c.l);
|
|
}
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
map.index.finishInitialization();
|
|
return map;
|
|
}
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
LongHandleIndex idx = new LongHandleIndex(12, Base64Order.enhancedCoder, 10000, 10000000);
|
|
byte[] s;
|
|
//long l;
|
|
for (int i = 0; i < 10000000; i = i + 8) {
|
|
s = Base64Order.enhancedCoder.uncardinal(Long.MAX_VALUE - i);
|
|
//l = Base64Order.enhancedCoder.cardinal(s);
|
|
//if (i != l) System.out.println("encoding bug for " + new String(s) + ", v = " + (Long.MAX_VALUE - i) + ", l = " + l);
|
|
//System.out.println(s);
|
|
if (idx.get(s) >= 0) System.out.println("search bug for " + new String(s) + ": " + idx.get(s));
|
|
idx.putUnique(s, 1);
|
|
}
|
|
}
|
|
}
|