2008-01-17 22:48:08 +01:00
|
|
|
// kelondroSplitTable.java
|
2008-07-20 19:14:51 +02:00
|
|
|
// (C) 2006 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
2006-10-13 01:17:16 +02:00
|
|
|
// first published 12.10.2006 on http://www.anomic.de
|
|
|
|
//
|
|
|
|
// This is a part of YaCy, a peer-to-peer based web search engine
|
|
|
|
//
|
|
|
|
// $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
|
|
|
|
|
2009-01-30 23:44:20 +01:00
|
|
|
package de.anomic.kelondro.table;
|
2006-10-13 01:17:16 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2009-04-03 00:15:33 +02:00
|
|
|
import java.text.ParseException;
|
2008-01-20 02:22:46 +01:00
|
|
|
import java.util.ArrayList;
|
2006-10-13 01:17:16 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2007-02-25 22:06:26 +01:00
|
|
|
import java.util.List;
|
2006-11-05 22:30:53 +01:00
|
|
|
import java.util.Map;
|
2009-04-03 00:15:33 +02:00
|
|
|
import java.util.Random;
|
2008-03-31 17:41:19 +02:00
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.CompletionService;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ExecutorCompletionService;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Future;
|
2008-03-31 20:45:27 +02:00
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
2008-03-31 17:41:19 +02:00
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2006-10-13 01:17:16 +02:00
|
|
|
|
2009-06-17 11:58:15 +02:00
|
|
|
import de.anomic.kelondro.blob.ArrayStack;
|
|
|
|
import de.anomic.kelondro.index.Cache;
|
2009-01-30 16:33:00 +01:00
|
|
|
import de.anomic.kelondro.index.Column;
|
2009-03-02 11:00:32 +01:00
|
|
|
import de.anomic.kelondro.index.ObjectIndexCache;
|
2009-01-30 16:33:00 +01:00
|
|
|
import de.anomic.kelondro.index.Row;
|
|
|
|
import de.anomic.kelondro.index.RowCollection;
|
|
|
|
import de.anomic.kelondro.index.ObjectIndex;
|
2009-01-30 23:08:08 +01:00
|
|
|
import de.anomic.kelondro.order.CloneableIterator;
|
|
|
|
import de.anomic.kelondro.order.NaturalOrder;
|
2009-01-30 23:44:20 +01:00
|
|
|
import de.anomic.kelondro.order.MergeIterator;
|
|
|
|
import de.anomic.kelondro.order.Order;
|
2009-02-24 11:40:20 +01:00
|
|
|
import de.anomic.kelondro.order.StackIterator;
|
2009-04-03 00:15:33 +02:00
|
|
|
import de.anomic.kelondro.util.DateFormatter;
|
2009-03-30 17:31:25 +02:00
|
|
|
import de.anomic.kelondro.util.FileUtils;
|
2009-01-31 02:06:56 +01:00
|
|
|
import de.anomic.kelondro.util.NamePrefixThreadFactory;
|
2009-06-15 23:19:54 +02:00
|
|
|
import de.anomic.yacy.logging.Log;
|
2006-10-13 01:17:16 +02:00
|
|
|
|
2009-01-30 23:44:20 +01:00
|
|
|
public class SplitTable implements ObjectIndex {
|
2008-01-17 22:48:08 +01:00
|
|
|
|
|
|
|
// this is a set of kelondro tables
|
|
|
|
// the set is divided into tables with different entry date
|
|
|
|
// the table type can be either kelondroFlex or kelondroEco
|
|
|
|
|
|
|
|
private static final int EcoFSBufferSize = 20;
|
2009-03-13 17:52:31 +01:00
|
|
|
static final ObjectIndex dummyIndex = new ObjectIndexCache(new Row(new Column[]{new Column("key", Column.celltype_binary, Column.encoder_bytes, 2, "key")}, NaturalOrder.naturalOrder), 0, 0);
|
2008-03-31 17:41:19 +02:00
|
|
|
|
|
|
|
// the thread pool for the keeperOf executor service
|
|
|
|
private ExecutorService executor;
|
2006-10-13 01:17:16 +02:00
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
private HashMap<String, ObjectIndex> tables; // a map from a date string to a kelondroIndex object
|
|
|
|
private final Row rowdef;
|
2008-08-02 14:12:04 +02:00
|
|
|
private final File path;
|
2009-04-03 00:15:33 +02:00
|
|
|
private final String prefix;
|
2009-01-30 23:44:20 +01:00
|
|
|
private final Order<Row.Entry> entryOrder;
|
2009-04-03 00:15:33 +02:00
|
|
|
private String current;
|
|
|
|
private long fileAgeLimit;
|
|
|
|
private long fileSizeLimit;
|
2006-10-13 01:17:16 +02:00
|
|
|
|
2009-04-03 00:15:33 +02:00
|
|
|
public SplitTable(
|
|
|
|
final File path,
|
|
|
|
final String tablename,
|
|
|
|
final Row rowdef,
|
|
|
|
final boolean resetOnFail) {
|
2009-06-17 11:58:15 +02:00
|
|
|
this(path, tablename, rowdef, ArrayStack.oneMonth, (long) Integer.MAX_VALUE, resetOnFail);
|
2009-04-03 00:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public SplitTable(
|
|
|
|
final File path,
|
|
|
|
final String tablename,
|
|
|
|
final Row rowdef,
|
|
|
|
final long fileAgeLimit,
|
|
|
|
final long fileSizeLimit,
|
|
|
|
final boolean resetOnFail) {
|
2006-10-13 01:17:16 +02:00
|
|
|
this.path = path;
|
2009-04-03 00:15:33 +02:00
|
|
|
this.prefix = tablename;
|
2006-10-13 01:17:16 +02:00
|
|
|
this.rowdef = rowdef;
|
2009-04-03 00:15:33 +02:00
|
|
|
this.fileAgeLimit = fileAgeLimit;
|
|
|
|
this.fileSizeLimit = fileSizeLimit;
|
2009-01-30 16:33:00 +01:00
|
|
|
this.entryOrder = new Row.EntryComparator(rowdef.objectOrder);
|
2008-02-19 10:14:07 +01:00
|
|
|
init(resetOnFail);
|
2007-04-05 12:14:48 +02:00
|
|
|
}
|
|
|
|
|
2009-04-03 00:15:33 +02:00
|
|
|
String newFilename() {
|
|
|
|
return prefix + "." + DateFormatter.formatShortMilliSecond(new Date()) + ".table";
|
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public void init(final boolean resetOnFail) {
|
2009-04-03 00:15:33 +02:00
|
|
|
current = null;
|
2008-03-31 17:41:19 +02:00
|
|
|
|
|
|
|
// init the thread pool for the keeperOf executor service
|
2009-01-31 02:06:56 +01:00
|
|
|
this.executor = new ThreadPoolExecutor(
|
|
|
|
Runtime.getRuntime().availableProcessors() + 1,
|
|
|
|
Runtime.getRuntime().availableProcessors() + 1, 10,
|
|
|
|
TimeUnit.SECONDS,
|
|
|
|
new LinkedBlockingQueue<Runnable>(),
|
2009-04-03 00:15:33 +02:00
|
|
|
new NamePrefixThreadFactory(prefix));
|
2006-10-13 01:17:16 +02:00
|
|
|
|
|
|
|
// initialized tables map
|
2009-01-30 16:33:00 +01:00
|
|
|
this.tables = new HashMap<String, ObjectIndex>();
|
2006-10-19 23:14:37 +02:00
|
|
|
if (!(path.exists())) path.mkdirs();
|
2009-04-03 00:15:33 +02:00
|
|
|
String[] tablefile = path.list();
|
|
|
|
|
|
|
|
// zero pass: migrate old table names
|
|
|
|
File f;
|
|
|
|
Random r = new Random(System.currentTimeMillis());
|
|
|
|
for (int i = 0; i < tablefile.length; i++) {
|
|
|
|
if ((tablefile[i].startsWith(prefix)) &&
|
|
|
|
(tablefile[i].charAt(prefix.length()) == '.') &&
|
|
|
|
(tablefile[i].length() == prefix.length() + 7)) {
|
|
|
|
f = new File(path, tablefile[i]);
|
|
|
|
String newname = tablefile[i] + "0100000" + (Long.toString(r.nextLong())+"00000").substring(1,5) + ".table";
|
|
|
|
f.renameTo(new File(path, newname));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tablefile = path.list();
|
2006-10-13 01:17:16 +02:00
|
|
|
|
2006-11-05 22:30:53 +01:00
|
|
|
// first pass: find tables
|
2008-08-02 14:12:04 +02:00
|
|
|
final HashMap<String, Long> t = new HashMap<String, Long>();
|
2009-04-03 00:15:33 +02:00
|
|
|
long ram, sum = 0, time, maxtime = 0;
|
|
|
|
Date d;
|
2008-01-17 22:48:08 +01:00
|
|
|
for (int i = 0; i < tablefile.length; i++) {
|
2009-04-03 00:15:33 +02:00
|
|
|
if ((tablefile[i].startsWith(prefix)) &&
|
|
|
|
(tablefile[i].charAt(prefix.length()) == '.') &&
|
|
|
|
(tablefile[i].length() == prefix.length() + 24)) {
|
2008-01-17 22:48:08 +01:00
|
|
|
f = new File(path, tablefile[i]);
|
2009-04-03 00:15:33 +02:00
|
|
|
try {
|
|
|
|
d = DateFormatter.parseShortMilliSecond(tablefile[i].substring(prefix.length() + 1, prefix.length() + 18));
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
time = d.getTime();
|
|
|
|
if (time > maxtime) {
|
|
|
|
current = tablefile[i];
|
|
|
|
maxtime = time;
|
|
|
|
}
|
|
|
|
|
2009-06-07 23:48:01 +02:00
|
|
|
ram = EcoTable.staticRAMIndexNeed(f, rowdef);
|
2006-11-06 03:05:39 +01:00
|
|
|
if (ram > 0) {
|
2008-08-06 21:43:12 +02:00
|
|
|
t.put(tablefile[i], Long.valueOf(ram));
|
2006-11-06 03:05:39 +01:00
|
|
|
sum += ram;
|
2006-11-05 22:30:53 +01:00
|
|
|
}
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
|
|
|
}
|
2006-11-05 22:30:53 +01:00
|
|
|
|
|
|
|
// second pass: open tables
|
2008-01-06 20:23:38 +01:00
|
|
|
Iterator<Map.Entry<String, Long>> i;
|
|
|
|
Map.Entry<String, Long> entry;
|
|
|
|
String maxf;
|
2006-11-06 03:05:39 +01:00
|
|
|
long maxram;
|
2009-01-30 16:33:00 +01:00
|
|
|
ObjectIndex table;
|
2006-11-06 03:05:39 +01:00
|
|
|
while (t.size() > 0) {
|
|
|
|
// find maximum table
|
|
|
|
maxram = 0;
|
|
|
|
maxf = null;
|
|
|
|
i = t.entrySet().iterator();
|
|
|
|
while (i.hasNext()) {
|
2008-01-06 20:23:38 +01:00
|
|
|
entry = i.next();
|
|
|
|
ram = entry.getValue().longValue();
|
2006-11-06 03:05:39 +01:00
|
|
|
if (ram > maxram) {
|
2008-01-06 20:23:38 +01:00
|
|
|
maxf = entry.getKey();
|
2006-11-06 03:05:39 +01:00
|
|
|
maxram = ram;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// open next biggest table
|
|
|
|
t.remove(maxf);
|
2009-04-03 00:15:33 +02:00
|
|
|
if (maxf != null) {
|
2008-06-06 18:01:27 +02:00
|
|
|
f = new File(path, maxf);
|
2009-01-31 00:33:47 +01:00
|
|
|
Log.logInfo("kelondroSplitTable", "opening partial eco table " + f);
|
2009-01-30 23:08:08 +01:00
|
|
|
table = new EcoTable(f, rowdef, EcoTable.tailCacheUsageAuto, EcoFSBufferSize, 0);
|
2009-04-03 00:15:33 +02:00
|
|
|
tables.put(maxf, table);
|
2008-01-17 22:48:08 +01:00
|
|
|
}
|
2006-11-05 22:30:53 +01:00
|
|
|
}
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
2007-03-06 23:43:32 +01:00
|
|
|
|
2008-05-24 14:30:50 +02:00
|
|
|
public void clear() throws IOException {
|
2007-04-05 12:14:48 +02:00
|
|
|
this.close();
|
2008-08-02 14:12:04 +02:00
|
|
|
final String[] l = path.list();
|
2007-04-05 12:14:48 +02:00
|
|
|
for (int i = 0; i < l.length; i++) {
|
2009-04-03 00:15:33 +02:00
|
|
|
if (l[i].startsWith(prefix)) {
|
2008-08-02 14:12:04 +02:00
|
|
|
final File f = new File(path, l[i]);
|
2009-06-28 23:02:56 +02:00
|
|
|
if (f.isDirectory()) delete(path, l[i]); else FileUtils.deletedelete(f);
|
2007-04-05 12:14:48 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-19 10:14:07 +01:00
|
|
|
init(true);
|
2007-04-05 12:14:48 +02:00
|
|
|
}
|
|
|
|
|
2009-06-28 23:02:56 +02:00
|
|
|
public static void delete(final File path, final String tablename) {
|
|
|
|
final File tabledir = new File(path, tablename);
|
|
|
|
if (!(tabledir.exists())) return;
|
|
|
|
if ((!(tabledir.isDirectory()))) {
|
|
|
|
FileUtils.deletedelete(tabledir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String[] files = tabledir.list();
|
|
|
|
for (int i = 0; i < files.length; i++) {
|
|
|
|
FileUtils.deletedelete(new File(tabledir, files[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
FileUtils.deletedelete(tabledir);
|
|
|
|
}
|
|
|
|
|
2007-03-06 23:43:32 +01:00
|
|
|
public String filename() {
|
2009-04-03 00:15:33 +02:00
|
|
|
return new File(path, prefix).toString();
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
|
|
|
|
2007-04-19 15:37:02 +02:00
|
|
|
public int size() {
|
2009-01-30 16:33:00 +01:00
|
|
|
final Iterator<ObjectIndex> i = tables.values().iterator();
|
2006-10-13 01:17:16 +02:00
|
|
|
int s = 0;
|
2008-01-06 20:23:38 +01:00
|
|
|
while (i.hasNext()) s += i.next().size();
|
2006-10-13 01:17:16 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2006-10-26 15:50:50 +02:00
|
|
|
public int writeBufferSize() {
|
|
|
|
int s = 0;
|
2009-01-30 16:33:00 +01:00
|
|
|
for (final ObjectIndex index : tables.values()) {
|
2009-01-30 23:44:20 +01:00
|
|
|
if (index instanceof Cache) s += ((Cache) index).writeBufferSize();
|
2006-10-26 15:50:50 +02:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
public Row row() {
|
2006-10-13 01:17:16 +02:00
|
|
|
return this.rowdef;
|
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public boolean has(final byte[] key) {
|
2008-03-31 17:41:19 +02:00
|
|
|
return keeperOf(key) != null;
|
2007-01-08 14:13:30 +01:00
|
|
|
}
|
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
public synchronized Row.Entry get(final byte[] key) throws IOException {
|
|
|
|
final ObjectIndex keeper = keeperOf(key);
|
2006-10-21 02:19:00 +02:00
|
|
|
if (keeper == null) return null;
|
2008-03-31 17:41:19 +02:00
|
|
|
return keeper.get(key);
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
|
|
|
|
2009-04-03 00:15:33 +02:00
|
|
|
private ObjectIndex newTable() {
|
|
|
|
this.current = newFilename();
|
|
|
|
final File f = new File(path, this.current);
|
|
|
|
EcoTable table = new EcoTable(f, rowdef, EcoTable.tailCacheDenyUsage, EcoFSBufferSize, 0);
|
|
|
|
tables.put(this.current, table);
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ObjectIndex checkTable(ObjectIndex table) {
|
|
|
|
// check size and age of given table; in case it is too large or too old
|
|
|
|
// create a new table
|
|
|
|
String name = new File(table.filename()).getName();
|
|
|
|
long d;
|
|
|
|
try {
|
|
|
|
d = DateFormatter.parseShortMilliSecond(name.substring(prefix.length() + 1, prefix.length() + 18)).getTime();
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
d = 0;
|
|
|
|
}
|
|
|
|
if (d + this.fileAgeLimit < System.currentTimeMillis() || new File(this.path, name).length() >= this.fileSizeLimit) {
|
|
|
|
return newTable();
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2009-03-11 21:23:19 +01:00
|
|
|
public synchronized Row.Entry replace(final Row.Entry row) throws IOException {
|
2007-04-03 14:10:12 +02:00
|
|
|
assert row.objectsize() <= this.rowdef.objectsize;
|
2009-04-03 00:15:33 +02:00
|
|
|
ObjectIndex keeper = keeperOf(row.getColBytes(0));
|
2009-03-11 21:23:19 +01:00
|
|
|
if (keeper != null) return keeper.replace(row);
|
2009-04-03 00:15:33 +02:00
|
|
|
keeper = (this.current == null) ? newTable() : checkTable(this.tables.get(this.current));
|
|
|
|
keeper.put(row);
|
2006-10-21 02:19:00 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2009-03-11 21:23:19 +01:00
|
|
|
public synchronized void put(final Row.Entry row) throws IOException {
|
|
|
|
assert row.objectsize() <= this.rowdef.objectsize;
|
2009-04-03 00:15:33 +02:00
|
|
|
ObjectIndex keeper = keeperOf(row.getColBytes(0));
|
2009-03-11 21:23:19 +01:00
|
|
|
if (keeper != null) {keeper.put(row); return;}
|
2009-04-03 00:15:33 +02:00
|
|
|
keeper = (this.current == null) ? newTable() : checkTable(this.tables.get(this.current));
|
|
|
|
keeper.put(row);
|
2009-03-11 21:23:19 +01:00
|
|
|
}
|
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
public synchronized ObjectIndex keeperOf(final byte[] key) {
|
2008-03-31 17:41:19 +02:00
|
|
|
// because the index is stored only in one table,
|
|
|
|
// and the index is completely in RAM, a concurrency will create
|
|
|
|
// not concurrent File accesses
|
|
|
|
//long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
// start a concurrent query to database tables
|
2009-01-30 16:33:00 +01:00
|
|
|
final CompletionService<ObjectIndex> cs = new ExecutorCompletionService<ObjectIndex>(executor);
|
2009-06-19 01:24:23 +02:00
|
|
|
int accepted = 0;
|
2009-01-30 16:33:00 +01:00
|
|
|
for (final ObjectIndex table : tables.values()) {
|
2008-03-31 20:45:27 +02:00
|
|
|
try {
|
2009-01-30 16:33:00 +01:00
|
|
|
cs.submit(new Callable<ObjectIndex>() {
|
|
|
|
public ObjectIndex call() {
|
2008-08-02 15:57:00 +02:00
|
|
|
if (table.has(key)) return table;
|
|
|
|
return dummyIndex;
|
2008-03-31 20:45:27 +02:00
|
|
|
}
|
|
|
|
});
|
2009-06-19 01:24:23 +02:00
|
|
|
accepted++;
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final RejectedExecutionException e) {
|
2008-03-31 20:45:27 +02:00
|
|
|
// the executor is either shutting down or the blocking queue is full
|
|
|
|
// execute the search direct here without concurrency
|
2008-06-18 01:56:39 +02:00
|
|
|
if (table.has(key)) return table;
|
2008-03-31 20:45:27 +02:00
|
|
|
}
|
2006-10-21 02:19:00 +02:00
|
|
|
}
|
2008-03-31 17:41:19 +02:00
|
|
|
|
|
|
|
// read the result
|
|
|
|
try {
|
2009-06-19 01:24:23 +02:00
|
|
|
for (int i = 0; i < accepted; i++) {
|
2009-01-30 16:33:00 +01:00
|
|
|
final Future<ObjectIndex> f = cs.take();
|
2009-06-19 01:24:23 +02:00
|
|
|
//hash(System.out.println("**********accepted = " + accepted + ", i =" + i);
|
|
|
|
if (f == null) continue;
|
2009-01-30 16:33:00 +01:00
|
|
|
final ObjectIndex index = f.get();
|
2008-03-31 17:41:19 +02:00
|
|
|
if (index != dummyIndex) {
|
|
|
|
//System.out.println("*DEBUG SplitTable success.time = " + (System.currentTimeMillis() - start) + " ms");
|
2009-06-19 01:24:23 +02:00
|
|
|
return index;
|
2008-03-31 17:41:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//System.out.println("*DEBUG SplitTable fail.time = " + (System.currentTimeMillis() - start) + " ms");
|
|
|
|
return null;
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final InterruptedException e) {
|
2008-03-31 17:41:19 +02:00
|
|
|
Thread.currentThread().interrupt();
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final ExecutionException e) {
|
2009-05-05 22:24:20 +02:00
|
|
|
e.printStackTrace();
|
2008-03-31 17:41:19 +02:00
|
|
|
throw new RuntimeException(e.getCause());
|
|
|
|
}
|
|
|
|
//System.out.println("*DEBUG SplitTable fail.time = " + (System.currentTimeMillis() - start) + " ms");
|
2006-10-21 02:19:00 +02:00
|
|
|
return null;
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
public synchronized void addUnique(final Row.Entry row) throws IOException {
|
2007-04-03 14:10:12 +02:00
|
|
|
assert row.objectsize() <= this.rowdef.objectsize;
|
2009-04-03 00:15:33 +02:00
|
|
|
ObjectIndex table = (this.current == null) ? null : tables.get(this.current);
|
|
|
|
if (table == null) table = newTable(); else table = checkTable(table);
|
2008-11-24 00:55:08 +01:00
|
|
|
table.addUnique(row);
|
2007-05-16 12:48:26 +02:00
|
|
|
}
|
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
public ArrayList<RowCollection> removeDoubles() throws IOException {
|
|
|
|
final Iterator<ObjectIndex> i = tables.values().iterator();
|
|
|
|
final ArrayList<RowCollection> report = new ArrayList<RowCollection>();
|
2008-01-20 02:22:46 +01:00
|
|
|
while (i.hasNext()) {
|
|
|
|
report.addAll(i.next().removeDoubles());
|
|
|
|
}
|
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
public synchronized Row.Entry remove(final byte[] key) throws IOException {
|
|
|
|
final ObjectIndex table = keeperOf(key);
|
2008-03-31 17:41:19 +02:00
|
|
|
if (table == null) return null;
|
2008-07-05 02:35:20 +02:00
|
|
|
return table.remove(key);
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
|
|
|
|
2009-01-30 16:33:00 +01:00
|
|
|
public synchronized Row.Entry removeOne() throws IOException {
|
|
|
|
final Iterator<ObjectIndex> i = tables.values().iterator();
|
|
|
|
ObjectIndex table, maxtable = null;
|
2006-10-16 17:04:16 +02:00
|
|
|
int maxcount = -1;
|
|
|
|
while (i.hasNext()) {
|
2008-06-06 18:01:27 +02:00
|
|
|
table = i.next();
|
2006-10-16 17:04:16 +02:00
|
|
|
if (table.size() > maxcount) {
|
|
|
|
maxtable = table;
|
|
|
|
maxcount = table.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (maxtable == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2008-08-02 15:57:00 +02:00
|
|
|
return maxtable.removeOne();
|
2006-10-16 17:04:16 +02:00
|
|
|
}
|
|
|
|
|
2009-01-30 23:08:08 +01:00
|
|
|
public synchronized CloneableIterator<byte[]> keys(final boolean up, final byte[] firstKey) throws IOException {
|
|
|
|
final List<CloneableIterator<byte[]>> c = new ArrayList<CloneableIterator<byte[]>>(tables.size());
|
2009-01-30 16:33:00 +01:00
|
|
|
final Iterator<ObjectIndex> i = tables.values().iterator();
|
2009-03-13 00:05:18 +01:00
|
|
|
CloneableIterator<byte[]> k;
|
2007-12-27 18:56:59 +01:00
|
|
|
while (i.hasNext()) {
|
2009-03-13 00:05:18 +01:00
|
|
|
k = i.next().keys(up, firstKey);
|
|
|
|
if (k != null) c.add(k);
|
2007-12-27 18:56:59 +01:00
|
|
|
}
|
2009-01-30 23:44:20 +01:00
|
|
|
return MergeIterator.cascade(c, rowdef.objectOrder, MergeIterator.simpleMerge, up);
|
2007-12-27 18:56:59 +01:00
|
|
|
}
|
|
|
|
|
2009-01-30 23:08:08 +01:00
|
|
|
public synchronized CloneableIterator<Row.Entry> rows(final boolean up, final byte[] firstKey) throws IOException {
|
|
|
|
final List<CloneableIterator<Row.Entry>> c = new ArrayList<CloneableIterator<Row.Entry>>(tables.size());
|
2009-01-30 16:33:00 +01:00
|
|
|
final Iterator<ObjectIndex> i = tables.values().iterator();
|
2007-03-09 14:55:26 +01:00
|
|
|
while (i.hasNext()) {
|
2008-08-19 16:10:40 +02:00
|
|
|
c.add(i.next().rows(up, firstKey));
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
2009-01-30 23:44:20 +01:00
|
|
|
return MergeIterator.cascade(c, entryOrder, MergeIterator.simpleMerge, up);
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
2009-02-24 11:40:20 +01:00
|
|
|
|
|
|
|
public synchronized CloneableIterator<Row.Entry> rows() throws IOException {
|
|
|
|
final List<CloneableIterator<Row.Entry>> c = new ArrayList<CloneableIterator<Row.Entry>>(tables.size());
|
|
|
|
final Iterator<ObjectIndex> i = tables.values().iterator();
|
|
|
|
while (i.hasNext()) {
|
|
|
|
c.add(i.next().rows());
|
|
|
|
}
|
|
|
|
return StackIterator.stack(c);
|
|
|
|
}
|
2006-10-24 15:48:16 +02:00
|
|
|
|
2007-03-09 09:48:47 +01:00
|
|
|
public synchronized void close() {
|
|
|
|
if (tables == null) return;
|
2008-03-31 17:41:19 +02:00
|
|
|
this.executor.shutdown();
|
|
|
|
try {
|
|
|
|
this.executor.awaitTermination(3, TimeUnit.SECONDS);
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final InterruptedException e) {
|
2008-03-31 17:41:19 +02:00
|
|
|
}
|
|
|
|
this.executor = null;
|
2009-01-30 16:33:00 +01:00
|
|
|
final Iterator<ObjectIndex> i = tables.values().iterator();
|
2007-03-09 09:48:47 +01:00
|
|
|
while (i.hasNext()) {
|
2008-01-06 20:23:38 +01:00
|
|
|
i.next().close();
|
2007-03-09 09:48:47 +01:00
|
|
|
}
|
2008-03-31 17:41:19 +02:00
|
|
|
this.tables = null;
|
2006-10-13 01:17:16 +02:00
|
|
|
}
|
2008-09-12 13:51:48 +02:00
|
|
|
|
|
|
|
public void deleteOnExit() {
|
2009-01-30 16:33:00 +01:00
|
|
|
for (ObjectIndex i: this.tables.values()) i.deleteOnExit();
|
2008-09-12 13:51:48 +02:00
|
|
|
}
|
2006-10-13 01:17:16 +02:00
|
|
|
|
|
|
|
}
|