1 29 package net.metanotion.io.block.index; 30 31 import java.io.IOException ; 32 import java.util.HashMap ; 33 import java.util.Random ; 34 35 import net.metanotion.io.RandomAccessInterface; 36 import net.metanotion.io.Serializer; 37 import net.metanotion.io.block.BlockFile; 38 import net.metanotion.util.skiplist.*; 39 40 public class BSkipList extends SkipList { 41 public int firstSpanPage = 0; 42 public int firstLevelPage = 0; 43 public int skipPage = 0; 44 public BlockFile bf; 45 46 public HashMap spanHash = new HashMap (); 47 public HashMap levelHash = new HashMap (); 48 49 protected BSkipList() { } 50 public BSkipList(int spanSize, BlockFile bf, int skipPage, Serializer key, Serializer val) throws IOException { 51 if(spanSize < 1) { throw new Error ("Span size too small"); } 52 53 this.skipPage = skipPage; 54 this.bf = bf; 55 56 BlockFile.pageSeek(bf.file, skipPage); 57 firstSpanPage = bf.file.readInt(); 58 firstLevelPage = bf.file.readInt(); 59 size = bf.file.readInt(); 60 spans = bf.file.readInt(); 61 System.out.println(size + " " + spans); 62 63 first = new BSkipSpan(bf, this, firstSpanPage, key, val); 64 stack = new BSkipLevels(bf, firstLevelPage, this); 65 rng = new Random (System.currentTimeMillis()); 66 } 67 68 public void close() { 69 System.out.println("Closing index " + size + " and " + spans); 70 flush(); 71 first = null; 72 stack = null; 73 } 74 75 public void flush() { 76 try { 77 BlockFile.pageSeek(bf.file, skipPage); 78 bf.file.writeInt(firstSpanPage); 79 bf.file.writeInt(firstLevelPage); 80 bf.file.writeInt(size); 81 bf.file.writeInt(spans); 82 83 } catch (IOException ioe) { throw new Error (); } 84 } 85 86 public void delete() throws IOException { 87 SkipLevels curLevel = stack, nextLevel; 88 while(curLevel != null) { 89 nextLevel = curLevel.levels[0]; 90 curLevel.killInstance(); 91 curLevel = nextLevel; 92 } 93 94 SkipSpan curSpan = first, nextSpan; 95 while(curSpan != null) { 96 nextSpan = curSpan.next; 97 curSpan.killInstance(); 98 curSpan = nextSpan; 99 } 100 101 bf.freePage(skipPage); 102 } 103 104 public static void init(BlockFile bf, int page, int spanSize) throws IOException { 105 int firstSpan = bf.allocPage(); 106 int firstLevel = bf.allocPage(); 107 BlockFile.pageSeek(bf.file, page); 108 bf.file.writeInt(firstSpan); 109 bf.file.writeInt(firstLevel); 110 bf.file.writeInt(0); 111 bf.file.writeInt(1); 112 BSkipSpan.init(bf, firstSpan, spanSize); 113 BSkipLevels.init(bf, firstLevel, firstSpan, 4); 114 } 115 116 public int maxLevels() { 117 int max = super.maxLevels(); 118 int cells = (int) ((BlockFile.PAGESIZE - 8) / 4); 119 return (max > cells) ? cells : max; 120 } 121 122 } 123 | Popular Tags |