From e3bb73c3d62bd7fd985f339478682a011a2c84eb Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Tue, 31 Jan 2012 21:13:49 +0100 Subject: [PATCH] serialized some database access methods --- .../net/yacy/kelondro/table/SplitTable.java | 43 ++++++++++++------- .../yacy/search/index/MetadataRepository.java | 40 +++++++++-------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/source/net/yacy/kelondro/table/SplitTable.java b/source/net/yacy/kelondro/table/SplitTable.java index 465ed92e4..c86eccb43 100644 --- a/source/net/yacy/kelondro/table/SplitTable.java +++ b/source/net/yacy/kelondro/table/SplitTable.java @@ -320,7 +320,9 @@ public class SplitTable implements Index, Iterable { public Row.Entry get(final byte[] key, final boolean forcecopy) throws IOException { final Index keeper = keeperOf(key); if (keeper == null) return null; - return keeper.get(key, forcecopy); + synchronized (this) { // avoid concurrent IO from different methods + return keeper.get(key, forcecopy); + } } @Override @@ -376,8 +378,10 @@ public class SplitTable implements Index, Iterable { 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 { 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 { @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,14 +450,18 @@ public class SplitTable implements Index, Iterable { public boolean delete(final byte[] key) throws IOException { final Index table = keeperOf(key); if (table == null) return false; - return table.delete(key); + 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; - return table.remove(key); + synchronized (this) { // avoid concurrent IO from different methods + return table.remove(key); + } } @Override @@ -472,7 +479,9 @@ public class SplitTable implements Index, Iterable { if (maxtable == null) { return null; } - return maxtable.removeOne(); + synchronized (this) { // avoid concurrent IO from different methods + return maxtable.removeOne(); + } } @Override @@ -490,7 +499,9 @@ public class SplitTable implements Index, Iterable { if (maxtable == null) { return null; } - return maxtable.top(count); + synchronized (this) { // avoid concurrent IO from different methods + return maxtable.top(count); + } } @Override diff --git a/source/net/yacy/search/index/MetadataRepository.java b/source/net/yacy/search/index/MetadataRepository.java index ebc18dfd1..5f953dde4 100644 --- a/source/net/yacy/search/index/MetadataRepository.java +++ b/source/net/yacy/search/index/MetadataRepository.java @@ -599,19 +599,21 @@ public final class MetadataRepository implements Iterable { public Map domainSampleCollector() throws IOException { final Map map = new HashMap(); // first collect all domains and calculate statistics about it - final CloneableIterator i = this.urlIndexFile.keys(true, null); - String hosthash; - byte[] urlhashb; - URLHashCounter ds; - if (i != null) while (i.hasNext()) { - urlhashb = i.next(); - hosthash = ASCII.String(urlhashb, 6, 6); - ds = map.get(hosthash); - if (ds == null) { - ds = new URLHashCounter(urlhashb); - map.put(hosthash, ds); - } else { - ds.count++; + synchronized (this) { + final CloneableIterator i = this.urlIndexFile.keys(true, null); + String hosthash; + byte[] urlhashb; + URLHashCounter ds; + if (i != null) while (i.hasNext()) { + urlhashb = i.next(); + hosthash = ASCII.String(urlhashb, 6, 6); + ds = map.get(hosthash); + if (ds == null) { + ds = new URLHashCounter(urlhashb); + map.put(hosthash, ds); + } else { + ds.count++; + } } } return map; @@ -739,11 +741,13 @@ public final class MetadataRepository implements Iterable { // first collect all url hashes that belong to the domain assert hosthash.length() == 6; final ArrayList l = new ArrayList(); - final CloneableIterator 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); + synchronized (this) { + final CloneableIterator 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