synchronization needed

pull/1/head
Michael Peter Christen 13 years ago
parent fb94b47b1a
commit c6a09eab0b

@ -27,6 +27,7 @@ package net.yacy.kelondro.index;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -40,7 +41,7 @@ import net.yacy.kelondro.order.StackIterator;
public final class RAMIndex implements Index, Iterable<Row.Entry> {
private static final TreeMap<String, RAMIndex> objectTracker = new TreeMap<String, RAMIndex>();
private static final Map<String, RAMIndex> objectTracker = Collections.synchronizedSortedMap(new TreeMap<String, RAMIndex>());
private final String name;
private final Row rowdef;
@ -76,6 +77,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return new RAMIndex(this.name + ".clone", this.rowdef, this.index0.clone(), this.index1.clone(), this.entryComparator);
}
@Override
public void clear() {
reset();
}
@ -97,6 +99,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
this.index1 = null; // to show that this is the initialization phase
}
@Override
public final Row row() {
return this.index0.row();
}
@ -111,6 +114,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
}
}
@Override
public final synchronized byte[] smallestKey() {
final byte[] b0 = this.index0.smallestKey();
if (b0 == null) return null;
@ -120,6 +124,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return b1;
}
@Override
public final synchronized byte[] largestKey() {
final byte[] b0 = this.index0.largestKey();
if (b0 == null) return null;
@ -129,6 +134,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return b1;
}
@Override
public final synchronized Row.Entry get(final byte[] key, final boolean forceclone) {
assert (key != null);
finishInitialization();
@ -138,6 +144,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return this.index1.get(key, forceclone);
}
@Override
public Map<byte[], Row.Entry> get(final Collection<byte[]> keys, final boolean forcecopy) throws IOException, InterruptedException {
final Map<byte[], Row.Entry> map = new TreeMap<byte[], Row.Entry>(row().objectOrder);
Row.Entry entry;
@ -148,6 +155,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return map;
}
@Override
public final synchronized boolean has(final byte[] key) {
assert (key != null);
finishInitialization();
@ -156,7 +164,8 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return this.index1.has(key);
}
public final synchronized Row.Entry replace(final Row.Entry entry) throws RowSpaceExceededException {
@Override
public final synchronized Row.Entry replace(final Row.Entry entry) throws RowSpaceExceededException {
assert (entry != null);
finishInitialization();
// if the new entry is within the initialization part, just overwrite it
@ -177,7 +186,8 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
* @throws IOException
* @throws RowSpaceExceededException
*/
public final boolean put(final Row.Entry entry) throws RowSpaceExceededException {
@Override
public final boolean put(final Row.Entry entry) throws RowSpaceExceededException {
assert (entry != null);
if (entry == null) return true;
synchronized (this) {
@ -195,6 +205,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
}
}
@Override
public final void addUnique(final Row.Entry entry) throws RowSpaceExceededException {
assert (entry != null);
if (entry == null) return;
@ -223,6 +234,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return this.index1.inc(key, col, add, initrow);
}
@Override
public final synchronized ArrayList<RowCollection> removeDoubles() throws RowSpaceExceededException {
// finish initialization phase explicitely
this.index0.sort();
@ -235,6 +247,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return d0;
}
@Override
public final synchronized boolean delete(final byte[] key) {
finishInitialization();
// if the new entry is within the initialization part, just delete it
@ -249,6 +262,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return b;
}
@Override
public final synchronized Row.Entry remove(final byte[] key) {
finishInitialization();
// if the new entry is within the initialization part, just delete it
@ -267,6 +281,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return removed;
}
@Override
public final synchronized Row.Entry removeOne() {
if (this.index1 != null && !this.index1.isEmpty()) {
return this.index1.removeOne();
@ -277,6 +292,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return null;
}
@Override
public synchronized List<Row.Entry> top(final int count) throws IOException {
final List<Row.Entry> list = new ArrayList<Row.Entry>();
List<Row.Entry> list0 = this.index1.top(count);
@ -286,6 +302,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return list;
}
@Override
public long mem() {
if (this.index0 != null && this.index1 == null) {
return this.index0.mem();
@ -297,6 +314,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return this.index0.mem() + this.index1.mem();
}
@Override
public final synchronized int size() {
if (this.index0 != null && this.index1 == null) {
return this.index0.size();
@ -308,6 +326,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return this.index0.size() + this.index1.size();
}
@Override
public final synchronized boolean isEmpty() {
if (this.index0 != null && this.index1 == null) {
return this.index0.isEmpty();
@ -322,6 +341,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
}
@Override
public final synchronized CloneableIterator<byte[]> keys(final boolean up, final byte[] firstKey) {
// returns the key-iterator of the underlying kelondroIndex
if (this.index1 == null) {
@ -351,6 +371,7 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
true);
}
@Override
public final synchronized CloneableIterator<Row.Entry> rows(final boolean up, final byte[] firstKey) {
// returns the row-iterator of the underlying kelondroIndex
if (this.index1 == null) {
@ -381,10 +402,12 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
true);
}
@Override
public final Iterator<Entry> iterator() {
return rows();
}
@Override
public final synchronized CloneableIterator<Row.Entry> rows() {
// returns the row-iterator of the underlying kelondroIndex
if (this.index1 == null) {
@ -406,17 +429,20 @@ public final class RAMIndex implements Index, Iterable<Row.Entry> {
return new StackIterator<Row.Entry>(this.index0.rows(), this.index1.rows());
}
@Override
public final synchronized void close() {
if (this.index0 != null) this.index0.close();
if (this.index1 != null) this.index1.close();
objectTracker.remove(this.name);
}
public final String filename() {
@Override
public final String filename() {
return null; // this does not have a file name
}
public final void deleteOnExit() {
@Override
public final void deleteOnExit() {
// do nothing, there is no file
}

Loading…
Cancel
Save