serialized some database access methods

pull/1/head
Michael Peter Christen 13 years ago
parent 9727015213
commit e3bb73c3d6

@ -320,8 +320,10 @@ public class SplitTable implements Index, Iterable<Row.Entry> {
public Row.Entry get(final byte[] key, final boolean forcecopy) throws IOException {
final Index keeper = keeperOf(key);
if (keeper == null) return null;
synchronized (this) { // avoid concurrent IO from different methods
return keeper.get(key, forcecopy);
}
}
@Override
public Map<byte[], Row.Entry> get(final Collection<byte[]> keys, final boolean forcecopy) throws IOException, InterruptedException {
@ -376,8 +378,10 @@ public class SplitTable implements Index, Iterable<Row.Entry> {
public Row.Entry replace(final Row.Entry row) throws IOException, RowSpaceExceededException {
assert row.objectsize() <= this.rowdef.objectsize;
Index keeper = keeperOf(row.getPrimaryKeyBytes());
if (keeper != null) return keeper.replace(row);
synchronized (this.tables) {
if (keeper != null) synchronized (this) { // avoid concurrent IO from different methods
return keeper.replace(row);
}
synchronized (this) {
assert this.current == null || this.tables.get(this.current) != null : "this.current = " + this.current;
keeper = (this.current == null) ? newTable() : checkTable(this.tables.get(this.current));
}
@ -397,12 +401,11 @@ public class SplitTable implements Index, Iterable<Row.Entry> {
assert row.objectsize() <= this.rowdef.objectsize;
final byte[] key = row.getPrimaryKeyBytes();
if (this.tables == null) return true;
Index keeper = null;
synchronized (this.tables) {
keeper = keeperOf(key);
Index keeper = keeperOf(key);
if (keeper != null) synchronized (this) { // avoid concurrent IO from different methods
return keeper.put(row);
}
if (keeper != null) return keeper.put(row);
synchronized (this.tables) {
synchronized (this) {
keeper = keeperOf(key); // we must check that again because it could have changed in between
if (keeper != null) return keeper.put(row);
assert this.current == null || this.tables.get(this.current) != null : "this.current = " + this.current;
@ -425,12 +428,12 @@ public class SplitTable implements Index, Iterable<Row.Entry> {
@Override
public void addUnique(final Row.Entry row) throws IOException, RowSpaceExceededException {
assert row.objectsize() <= this.rowdef.objectsize;
Index table = (this.current == null) ? null : this.tables.get(this.current);
synchronized (this.tables) {
Index keeper = (this.current == null) ? null : this.tables.get(this.current);
synchronized (this) {
assert this.current == null || this.tables.get(this.current) != null : "this.current = " + this.current;
if (table == null) table = newTable(); else table = checkTable(table);
if (keeper == null) keeper = newTable(); else keeper = checkTable(keeper);
}
table.addUnique(row);
keeper.addUnique(row);
}
@Override
@ -447,15 +450,19 @@ public class SplitTable implements Index, Iterable<Row.Entry> {
public boolean delete(final byte[] key) throws IOException {
final Index table = keeperOf(key);
if (table == null) return false;
synchronized (this) { // avoid concurrent IO from different methods
return table.delete(key);
}
}
@Override
public Row.Entry remove(final byte[] key) throws IOException {
final Index table = keeperOf(key);
if (table == null) return null;
synchronized (this) { // avoid concurrent IO from different methods
return table.remove(key);
}
}
@Override
public Row.Entry removeOne() throws IOException {
@ -472,8 +479,10 @@ public class SplitTable implements Index, Iterable<Row.Entry> {
if (maxtable == null) {
return null;
}
synchronized (this) { // avoid concurrent IO from different methods
return maxtable.removeOne();
}
}
@Override
public List<Row.Entry> top(final int count) throws IOException {
@ -490,8 +499,10 @@ public class SplitTable implements Index, Iterable<Row.Entry> {
if (maxtable == null) {
return null;
}
synchronized (this) { // avoid concurrent IO from different methods
return maxtable.top(count);
}
}
@Override
public CloneableIterator<byte[]> keys(final boolean up, final byte[] firstKey) throws IOException {

@ -599,6 +599,7 @@ public final class MetadataRepository implements Iterable<byte[]> {
public Map<String, URLHashCounter> domainSampleCollector() throws IOException {
final Map<String, URLHashCounter> map = new HashMap<String, URLHashCounter>();
// first collect all domains and calculate statistics about it
synchronized (this) {
final CloneableIterator<byte[]> i = this.urlIndexFile.keys(true, null);
String hosthash;
byte[] urlhashb;
@ -614,6 +615,7 @@ public final class MetadataRepository implements Iterable<byte[]> {
ds.count++;
}
}
}
return map;
}
@ -739,12 +741,14 @@ public final class MetadataRepository implements Iterable<byte[]> {
// first collect all url hashes that belong to the domain
assert hosthash.length() == 6;
final ArrayList<String> l = new ArrayList<String>();
synchronized (this) {
final CloneableIterator<byte[]> i = this.urlIndexFile.keys(true, null);
String hash;
while (i != null && i.hasNext()) {
hash = ASCII.String(i.next());
if (hosthash.equals(hash.substring(6))) l.add(hash);
}
}
// then delete the urls using this list
int cnt = 0;

Loading…
Cancel
Save