initial blockfile mods

This commit is contained in:
zzz
2011-02-08 00:45:23 +00:00
parent 7f10a67804
commit 208db9a673
6 changed files with 72 additions and 15 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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--) {

View File

@ -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; }

View File

@ -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];
}
}