From 208db9a673ad0ea2e81ed451ca68c23480f64d2b Mon Sep 17 00:00:00 2001 From: zzz Date: Tue, 8 Feb 2011 00:45:23 +0000 Subject: [PATCH] initial blockfile mods --- .../net/metanotion/io/block/BlockFile.java | 31 ++++++++++++++++--- .../metanotion/io/block/index/BSkipList.java | 16 +++++++--- .../metanotion/io/block/index/BSkipSpan.java | 24 ++++++++++++-- .../metanotion/util/skiplist/SkipLevels.java | 2 +- .../metanotion/util/skiplist/SkipList.java | 7 +++-- .../metanotion/util/skiplist/SkipSpan.java | 7 ++++- 6 files changed, 72 insertions(+), 15 deletions(-) diff --git a/core/java/src/net/metanotion/io/block/BlockFile.java b/core/java/src/net/metanotion/io/block/BlockFile.java index 205a33c92..12f46b34c 100644 --- a/core/java/src/net/metanotion/io/block/BlockFile.java +++ b/core/java/src/net/metanotion/io/block/BlockFile.java @@ -44,6 +44,7 @@ import net.metanotion.io.data.NullBytes; import net.metanotion.io.data.StringBytes; import net.metanotion.io.block.index.BSkipList; +import net.metanotion.util.skiplist.SkipList; class CorruptFileException extends IOException { } class BadFileFormatException extends IOException { } @@ -59,7 +60,7 @@ public class BlockFile { private long fileLen = PAGESIZE * 2; private int freeListStart = 0; private short mounted = 0; - public short spanSize = 127; + public short spanSize = 16; private BSkipList metaIndex = null; private HashMap openIndices = new HashMap(); @@ -246,9 +247,14 @@ public class BlockFile { } public BSkipList getIndex(String name, Serializer key, Serializer val) throws IOException { + // added I2P + BSkipList bsl = (BSkipList) openIndices.get(name); + if (bsl != null) + return bsl; + Integer page = (Integer) metaIndex.get(name); if (page == null) { return null; } - BSkipList bsl = new BSkipList(spanSize, this, page.intValue(), key, val); + bsl = new BSkipList(spanSize, this, page.intValue(), key, val, true); openIndices.put(name, bsl); return bsl; } @@ -258,7 +264,7 @@ public class BlockFile { int page = allocPage(); metaIndex.put(name, new Integer(page)); BSkipList.init(this, page, spanSize); - BSkipList bsl = new BSkipList(spanSize, this, page, key, val); + BSkipList bsl = new BSkipList(spanSize, this, page, key, val, true); openIndices.put(name, bsl); return bsl; } @@ -267,11 +273,28 @@ public class BlockFile { Integer page = (Integer) metaIndex.remove(name); if (page == null) { return; } NullBytes nb = new NullBytes(); - BSkipList bsl = new BSkipList(spanSize, this, page.intValue(), nb, nb); + BSkipList bsl = new BSkipList(spanSize, this, page.intValue(), nb, nb, true); bsl.delete(); } + /** + * Added I2P + */ + public void closeIndex(String name) { + BSkipList bsl = (BSkipList) openIndices.remove(name); + if (bsl != null) + bsl.flush(); + } + + /** + * Note (I2P) + * Does NOT close the RAF / RAI. + */ public void close() throws IOException { + // added I2P + if (metaIndex == null) + return; + metaIndex.close(); metaIndex = null; diff --git a/core/java/src/net/metanotion/io/block/index/BSkipList.java b/core/java/src/net/metanotion/io/block/index/BSkipList.java index b44003e3b..4bf4cf1c0 100644 --- a/core/java/src/net/metanotion/io/block/index/BSkipList.java +++ b/core/java/src/net/metanotion/io/block/index/BSkipList.java @@ -47,7 +47,12 @@ public class BSkipList extends SkipList { public HashMap levelHash = new HashMap(); protected BSkipList() { } + public BSkipList(int spanSize, BlockFile bf, int skipPage, Serializer key, Serializer val) throws IOException { + this(spanSize, bf, skipPage, key, val, false); + } + + public BSkipList(int spanSize, BlockFile bf, int skipPage, Serializer key, Serializer val, boolean fileOnly) throws IOException { if(spanSize < 1) { throw new Error("Span size too small"); } this.skipPage = skipPage; @@ -58,15 +63,18 @@ public class BSkipList extends SkipList { firstLevelPage = bf.file.readInt(); size = bf.file.readInt(); spans = bf.file.readInt(); - System.out.println(size + " " + spans); + //System.out.println(size + " " + spans); - first = new BSkipSpan(bf, this, firstSpanPage, key, val); + if (fileOnly) + first = new IBSkipSpan(bf, this, firstSpanPage, key, val); + else + first = new BSkipSpan(bf, this, firstSpanPage, key, val); stack = new BSkipLevels(bf, firstLevelPage, this); - rng = new Random(System.currentTimeMillis()); + //rng = new Random(System.currentTimeMillis()); } public void close() { - System.out.println("Closing index " + size + " and " + spans); + //System.out.println("Closing index " + size + " and " + spans); flush(); first = null; stack = null; 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 2c9aed3a1..9519fc83c 100644 --- a/core/java/src/net/metanotion/io/block/index/BSkipSpan.java +++ b/core/java/src/net/metanotion/io/block/index/BSkipSpan.java @@ -48,6 +48,9 @@ public class BSkipSpan extends SkipSpan { protected Serializer keySer; protected Serializer valSer; + // I2P + protected int spanSize; + public static void init(BlockFile bf, int page, int spanSize) throws IOException { BlockFile.pageSeek(bf.file, page); bf.file.writeInt(0); @@ -125,6 +128,15 @@ public class BSkipSpan extends SkipSpan { } private static void load(BSkipSpan bss, BlockFile bf, BSkipList bsl, int spanPage, Serializer key, Serializer val) throws IOException { + loadInit(bss, bf, bsl, spanPage, key, val); + loadData(bss, bf, spanPage, key, val); + } + + /** + * I2P - first half of load() + * Only read the span headers + */ + protected static void loadInit(BSkipSpan bss, BlockFile bf, BSkipList bsl, int spanPage, Serializer key, Serializer val) throws IOException { bss.bf = bf; bss.page = spanPage; bss.keySer = key; @@ -137,11 +149,17 @@ public class BSkipSpan extends SkipSpan { bss.overflowPage = bf.file.readInt(); bss.prevPage = bf.file.readInt(); bss.nextPage = bf.file.readInt(); - int sz = bf.file.readShort(); + bss.spanSize = bf.file.readShort(); bss.nKeys = bf.file.readShort(); + } - bss.keys = new Comparable[sz]; - bss.vals = new Object[sz]; + /** + * I2P - second half of load() + * Load the whole span's keys and values into memory + */ + protected static void loadData(BSkipSpan bss, BlockFile bf, int spanPage, Serializer key, Serializer val) throws IOException { + bss.keys = new Comparable[bss.spanSize]; + bss.vals = new Object[bss.spanSize]; int ksz, vsz; int curPage = spanPage; diff --git a/core/java/src/net/metanotion/util/skiplist/SkipLevels.java b/core/java/src/net/metanotion/util/skiplist/SkipLevels.java index f62b9fb51..e837127df 100644 --- a/core/java/src/net/metanotion/util/skiplist/SkipLevels.java +++ b/core/java/src/net/metanotion/util/skiplist/SkipLevels.java @@ -81,7 +81,7 @@ public class SkipLevels { return bottom.getSpan(key, search); } - public Comparable key() { return bottom.keys[0]; } + public Comparable key() { return bottom.firstKey(); } public Object get(int start, Comparable key) { for(int i=Math.min(start, levels.length - 1);i>=0;i--) { diff --git a/core/java/src/net/metanotion/util/skiplist/SkipList.java b/core/java/src/net/metanotion/util/skiplist/SkipList.java index a2d184d7c..11216ab62 100644 --- a/core/java/src/net/metanotion/util/skiplist/SkipList.java +++ b/core/java/src/net/metanotion/util/skiplist/SkipList.java @@ -30,10 +30,13 @@ package net.metanotion.util.skiplist; import java.util.Random; +import net.i2p.util.RandomSource; + public class SkipList { protected SkipSpan first; protected SkipLevels stack; - public Random rng; + // I2P mod + public static final Random rng = RandomSource.getInstance(); public int size=0; public int spans=0; @@ -46,7 +49,7 @@ public class SkipList { first = new SkipSpan(span); stack = new SkipLevels(1, first); spans = 1; - rng = new Random(System.currentTimeMillis()); + //rng = new Random(System.currentTimeMillis()); } public int size() { return size; } diff --git a/core/java/src/net/metanotion/util/skiplist/SkipSpan.java b/core/java/src/net/metanotion/util/skiplist/SkipSpan.java index 9945f0bce..894ee8632 100644 --- a/core/java/src/net/metanotion/util/skiplist/SkipSpan.java +++ b/core/java/src/net/metanotion/util/skiplist/SkipSpan.java @@ -183,7 +183,7 @@ public class SkipSpan { if(loc < 0) { loc = -1 * (loc + 1); if(next != null) { - int cmp = next.keys[0].compareTo(key); + int cmp = next.firstKey().compareTo(key); if((loc >= nKeys) && (cmp > 0)) { // It fits in between this span and the next // Try to avoid a split... @@ -264,4 +264,9 @@ public class SkipSpan { } return res; } + + /** I2P */ + public Comparable firstKey() { + return keys[0]; + } }