1 29 package net.metanotion.io.block.index; 30 31 import java.io.IOException ; 32 33 import net.metanotion.io.RandomAccessInterface; 34 import net.metanotion.io.block.BlockFile; 35 import net.metanotion.util.skiplist.SkipList; 36 import net.metanotion.util.skiplist.SkipLevels; 37 import net.metanotion.util.skiplist.SkipSpan; 38 39 public class BSkipLevels extends SkipLevels { 40 public int levelPage; 41 public int spanPage; 42 public BlockFile bf; 43 44 protected BSkipLevels() { } 45 public BSkipLevels(BlockFile bf, int levelPage, BSkipList bsl) throws IOException { 46 this.levelPage = levelPage; 47 this.bf = bf; 48 49 BlockFile.pageSeek(bf.file, levelPage); 50 51 bsl.levelHash.put(new Integer (this.levelPage), this); 52 53 int maxLen = bf.file.readShort(); 54 int nonNull = bf.file.readShort(); 55 spanPage = bf.file.readInt(); 56 bottom = (BSkipSpan) bsl.spanHash.get(new Integer (spanPage)); 57 58 this.levels = new BSkipLevels[maxLen]; 59 int lp; 60 for(int i=0;i<nonNull;i++) { 61 lp = bf.file.readInt(); 62 if(lp != 0) { 63 levels[i] = (BSkipLevels) bsl.levelHash.get(new Integer (lp)); 64 if(levels[i] == null) { 65 levels[i] = new BSkipLevels(bf, lp, bsl); 66 bsl.levelHash.put(new Integer (lp), levels[i]); 67 } 68 } else { 69 levels[i] = null; 70 } 71 } 72 73 } 74 75 public static void init(BlockFile bf, int page, int spanPage, int maxHeight) throws IOException { 76 BlockFile.pageSeek(bf.file, page); 77 bf.file.writeShort((short) maxHeight); 78 bf.file.writeShort(0); 79 bf.file.writeInt(spanPage); 80 } 81 82 public void flush() { 83 try { 84 BlockFile.pageSeek(bf.file, levelPage); 85 bf.file.writeShort((short) levels.length); 86 int i=0; 87 for(i=0;i<levels.length;i++) { if(levels[i] == null) { break; } } 88 bf.file.writeShort(i); 89 bf.file.writeInt(((BSkipSpan) bottom).page); 90 for(i=0;i<levels.length;i++) { 91 if(levels[i]==null) { break; } 92 bf.file.writeInt(((BSkipLevels) levels[i]).levelPage); 93 } 94 } catch (IOException ioe) { throw new Error (); } 95 } 96 97 public void killInstance() { 98 try { 99 bf.freePage(levelPage); 100 } catch (IOException ioe) { throw new Error (); } 101 } 102 103 public SkipLevels newInstance(int levels, SkipSpan ss, SkipList sl) { 104 try { 105 BSkipSpan bss = (BSkipSpan) ss; 106 BSkipList bsl = (BSkipList) sl; 107 int page = bf.allocPage(); 108 BSkipLevels.init(bf, page, bss.page, levels); 109 return new BSkipLevels(bf, page, bsl); 110 } catch (IOException ioe) { throw new Error (); } 111 } 112 } 113 | Popular Tags |