From f4905d2742e1270617fb0db77540a0a3aaa28a4b Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sun, 27 Mar 2011 22:36:45 +0000
Subject: [PATCH] - Use new UTF8StringBytes - Track number of SkipLevels in a
 SkipList - More double-checks - Caching cleanups - Cleanups, logging,
 generics

---
 .../metanotion/io/block/index/BSkipLevels.java  |  6 ++++++
 .../metanotion/io/block/index/BSkipSpan.java    | 17 ++++++++++++-----
 .../metanotion/io/block/index/IBSkipSpan.java   |  9 ++++++---
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/core/java/src/net/metanotion/io/block/index/BSkipLevels.java b/core/java/src/net/metanotion/io/block/index/BSkipLevels.java
index 7a9dce53a1..0288fc45ff 100644
--- a/core/java/src/net/metanotion/io/block/index/BSkipLevels.java
+++ b/core/java/src/net/metanotion/io/block/index/BSkipLevels.java
@@ -54,11 +54,13 @@ public class BSkipLevels extends SkipLevels {
 	public final int levelPage;
 	public final int spanPage;
 	public final BlockFile bf;
+	private final BSkipList bsl;
 	private boolean isKilled;
 
 	public BSkipLevels(BlockFile bf, int levelPage, BSkipList bsl) throws IOException {
 		this.levelPage = levelPage;
 		this.bf = bf;
+		this.bsl = bsl;
 
 		BlockFile.pageSeek(bf.file, levelPage);
 		long magic = bf.file.readLong();
@@ -73,6 +75,9 @@ public class BSkipLevels extends SkipLevels {
 			throw new IOException("Invalid Level Skip size " + nonNull + " / " + maxLen);
 		spanPage = bf.file.readUnsignedInt();
 		bottom = bsl.spanHash.get(Integer.valueOf(spanPage));
+		if (bottom == null)
+			// FIXME recover better?
+			BlockFile.log.error("No span found in cache???");
 
 		this.levels = new BSkipLevels[maxLen];
 		if (BlockFile.log.shouldLog(Log.DEBUG))
@@ -138,6 +143,7 @@ public class BSkipLevels extends SkipLevels {
 		if (BlockFile.log.shouldLog(Log.INFO))
 			BlockFile.log.info("Killing " + this);
 		isKilled = true;
+		bsl.levelHash.remove(levelPage);
 		bf.freePage(levelPage);
 	}
 
diff --git a/core/java/src/net/metanotion/io/block/index/BSkipSpan.java b/core/java/src/net/metanotion/io/block/index/BSkipSpan.java
index 277b9e80ac..49153399ff 100644
--- a/core/java/src/net/metanotion/io/block/index/BSkipSpan.java
+++ b/core/java/src/net/metanotion/io/block/index/BSkipSpan.java
@@ -60,7 +60,8 @@ public class BSkipSpan extends SkipSpan {
 	protected static final int MAGIC = 0x5370616e;  // "Span"
 	protected static final int HEADER_LEN = 20;
 	protected static final int CONT_HEADER_LEN = 8;
-	protected BlockFile bf;
+	protected final BlockFile bf;
+	private final BSkipList bsl;
 	protected int page;
 	protected int overflowPage;
 
@@ -113,6 +114,7 @@ public class BSkipSpan extends SkipSpan {
 		} catch (IOException ioe) {
 			BlockFile.log.error("Error freeing " + this, ioe);
 		}
+		bsl.spanHash.remove(this.page);
 	}
 
 	public void flush() {
@@ -213,7 +215,6 @@ public class BSkipSpan extends SkipSpan {
 	protected static void loadInit(BSkipSpan bss, BlockFile bf, BSkipList bsl, int spanPage, Serializer key, Serializer val) throws IOException {
 		if (bss.isKilled)
 			BlockFile.log.error("Already killed!! " + bss, new Exception());
-		bss.bf = bf;
 		bss.page = spanPage;
 		bss.keySer = key;
 		bss.valSer = val;
@@ -301,8 +302,14 @@ public class BSkipSpan extends SkipSpan {
 		}
 	}
 
-	protected BSkipSpan() { }
+	protected BSkipSpan(BlockFile bf, BSkipList bsl) {
+		this.bf = bf;
+		this.bsl = bsl;
+	}
+
 	public BSkipSpan(BlockFile bf, BSkipList bsl, int spanPage, Serializer key, Serializer val) throws IOException {
+		this.bf = bf;
+		this.bsl = bsl;
 		BSkipSpan.load(this, bf, bsl, spanPage, key, val);
 		this.next = null;
 		this.prev = null;
@@ -315,7 +322,7 @@ public class BSkipSpan extends SkipSpan {
 				bss.next = temp;
 				break;
 			}
-			bss.next = new BSkipSpan();
+			bss.next = new BSkipSpan(bf, bsl);
 			bss.next.next = null;
 			bss.next.prev = bss;
 			bss = (BSkipSpan) bss.next;
@@ -332,7 +339,7 @@ public class BSkipSpan extends SkipSpan {
 				bss.next = temp;
 				break;
 			}
-			bss.prev = new BSkipSpan();
+			bss.prev = new BSkipSpan(bf, bsl);
 			bss.prev.next = bss;
 			bss.prev.prev = null;
 			bss = (BSkipSpan) bss.prev;
diff --git a/core/java/src/net/metanotion/io/block/index/IBSkipSpan.java b/core/java/src/net/metanotion/io/block/index/IBSkipSpan.java
index 8b65cb4c97..08ac4fe9f3 100644
--- a/core/java/src/net/metanotion/io/block/index/IBSkipSpan.java
+++ b/core/java/src/net/metanotion/io/block/index/IBSkipSpan.java
@@ -232,9 +232,12 @@ public class IBSkipSpan extends BSkipSpan {
 		}
 	}
 
-	protected IBSkipSpan() { }
+	private IBSkipSpan(BlockFile bf, BSkipList bsl) {
+		super(bf, bsl);
+	}
 
 	public IBSkipSpan(BlockFile bf, BSkipList bsl, int spanPage, Serializer key, Serializer val) throws IOException {
+		super(bf, bsl);
 		if (BlockFile.log.shouldLog(Log.DEBUG))
 			BlockFile.log.debug("New ibss page " + spanPage);
 		BSkipSpan.loadInit(this, bf, bsl, spanPage, key, val);
@@ -251,7 +254,7 @@ public class IBSkipSpan extends BSkipSpan {
 				bss.next = temp;
 				break;
 			}
-			bss.next = new IBSkipSpan();
+			bss.next = new IBSkipSpan(bf, bsl);
 			bss.next.next = null;
 			bss.next.prev = bss;
 			bss = (IBSkipSpan) bss.next;
@@ -269,7 +272,7 @@ public class IBSkipSpan extends BSkipSpan {
 				bss.next = temp;
 				break;
 			}
-			bss.prev = new IBSkipSpan();
+			bss.prev = new IBSkipSpan(bf, bsl);
 			bss.prev.next = bss;
 			bss.prev.prev = null;
 			bss = (IBSkipSpan) bss.prev;
-- 
GitLab