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.Serializer; 35 import net.metanotion.io.block.BlockFile; 36 import net.metanotion.io.data.NullBytes; 37 import net.metanotion.util.skiplist.SkipList; 38 import net.metanotion.util.skiplist.SkipSpan; 39 40 public class BSkipSpan extends SkipSpan { 41 42 protected BlockFile bf; 43 protected int page; 44 protected int overflowPage; 45 46 protected int prevPage; 47 protected int nextPage; 48 protected Serializer keySer; 49 protected Serializer valSer; 50 51 public static void init(BlockFile bf, int page, int spanSize) throws IOException { 52 BlockFile.pageSeek(bf.file, page); 53 bf.file.writeInt(0); 54 bf.file.writeInt(0); 55 bf.file.writeInt(0); 56 bf.file.writeShort((short) spanSize); 57 bf.file.writeShort(0); 58 } 59 60 public SkipSpan newInstance(SkipList sl) { 61 try { 62 int newPage = bf.allocPage(); 63 init(bf, newPage, bf.spanSize); 64 return new BSkipSpan(bf, (BSkipList) sl, newPage, keySer, valSer); 65 } catch (IOException ioe) { throw new Error (); } 66 } 67 68 public void killInstance() { 69 try { 70 int curPage = overflowPage; 71 int next; 72 while(curPage != 0) { 73 BlockFile.pageSeek(bf.file, curPage); 74 next = bf.file.readInt(); 75 bf.freePage(curPage); 76 curPage = next; 77 } 78 bf.freePage(page); 79 } catch(IOException ioe) { throw new Error (); } 80 } 81 82 public void flush() { 83 try { 84 BlockFile.pageSeek(bf.file, page); 85 bf.file.writeInt(overflowPage); 86 bf.file.writeInt((prev != null) ? ((BSkipSpan) prev).page : 0); 87 bf.file.writeInt((next != null) ? ((BSkipSpan) next).page : 0); 88 bf.file.writeShort((short) keys.length); 89 bf.file.writeShort((short) nKeys); 90 91 int ksz, vsz; 92 int curPage = this.page; 93 int[] curNextPage = new int[1]; 94 curNextPage[0] = this.overflowPage; 95 int[] pageCounter = new int[1]; 96 pageCounter[0] = 16; 97 byte[] keyData; 98 byte[] valData; 99 100 for(int i=0;i<nKeys;i++) { 101 if((pageCounter[0] + 4) > BlockFile.PAGESIZE) { 102 if(curNextPage[0] == 0) { 103 curNextPage[0] = bf.allocPage(); 104 BlockFile.pageSeek(bf.file, curNextPage[0]); 105 bf.file.writeInt(0); 106 BlockFile.pageSeek(bf.file, curPage); 107 bf.file.writeInt(curNextPage[0]); 108 } 109 BlockFile.pageSeek(bf.file, curNextPage[0]); 110 curPage = curNextPage[0]; 111 curNextPage[0] = bf.file.readInt(); 112 pageCounter[0] = 4; 113 } 114 keyData = this.keySer.getBytes(keys[i]); 115 valData = this.valSer.getBytes(vals[i]); 116 pageCounter[0] += 4; 117 bf.file.writeShort(keyData.length); 118 bf.file.writeShort(valData.length); 119 curPage = bf.writeMultiPageData(keyData, curPage, pageCounter, curNextPage); 120 curPage = bf.writeMultiPageData(valData, curPage, pageCounter, curNextPage); 121 } 122 BlockFile.pageSeek(bf.file, this.page); 123 this.overflowPage = bf.file.readInt(); 124 } catch(IOException ioe) { throw new Error (); } 125 } 126 127 private static void load(BSkipSpan bss, BlockFile bf, BSkipList bsl, int spanPage, Serializer key, Serializer val) throws IOException { 128 bss.bf = bf; 129 bss.page = spanPage; 130 bss.keySer = key; 131 bss.valSer = val; 132 133 bsl.spanHash.put(new Integer (spanPage), bss); 134 135 BlockFile.pageSeek(bf.file, spanPage); 136 137 bss.overflowPage = bf.file.readInt(); 138 bss.prevPage = bf.file.readInt(); 139 bss.nextPage = bf.file.readInt(); 140 int sz = bf.file.readShort(); 141 bss.nKeys = bf.file.readShort(); 142 143 bss.keys = new Comparable [sz]; 144 bss.vals = new Object [sz]; 145 146 int ksz, vsz; 147 int curPage = spanPage; 148 int[] curNextPage = new int[1]; 149 curNextPage[0] = bss.overflowPage; 150 int[] pageCounter = new int[1]; 151 pageCounter[0] = 16; 152 for(int i=0;i<bss.nKeys;i++) { 154 if((pageCounter[0] + 4) > BlockFile.PAGESIZE) { 155 BlockFile.pageSeek(bf.file, curNextPage[0]); 156 curPage = curNextPage[0]; 157 curNextPage[0] = bf.file.readInt(); 158 pageCounter[0] = 4; 159 } 160 ksz = bf.file.readShort(); 161 vsz = bf.file.readShort(); 162 pageCounter[0] +=4; 163 byte[] k = new byte[ksz]; 164 byte[] v = new byte[vsz]; 165 curPage = bf.readMultiPageData(k, curPage, pageCounter, curNextPage); 166 curPage = bf.readMultiPageData(v, curPage, pageCounter, curNextPage); 167 bss.keys[i] = (Comparable ) bss.keySer.construct(k); 169 bss.vals[i] = bss.valSer.construct(v); 170 } 171 172 } 173 174 protected BSkipSpan() { } 175 public BSkipSpan(BlockFile bf, BSkipList bsl, int spanPage, Serializer key, Serializer val) throws IOException { 176 BSkipSpan.load(this, bf, bsl, spanPage, key, val); 177 this.next = null; 178 this.prev = null; 179 180 BSkipSpan bss = this; 181 BSkipSpan temp; 182 int np = nextPage; 183 while(np != 0) { 184 temp = (BSkipSpan) bsl.spanHash.get(new Integer (np)); 185 if(temp != null) { 186 bss.next = temp; 187 break; 188 } 189 bss.next = new BSkipSpan(); 190 bss.next.next = null; 191 bss.next.prev = bss; 192 bss = (BSkipSpan) bss.next; 193 194 BSkipSpan.load(bss, bf, bsl, np, key, val); 195 np = bss.nextPage; 196 } 197 198 bss = this; 199 np = prevPage; 200 while(np != 0) { 201 temp = (BSkipSpan) bsl.spanHash.get(new Integer (np)); 202 if(temp != null) { 203 bss.next = temp; 204 break; 205 } 206 bss.prev = new BSkipSpan(); 207 bss.prev.next = bss; 208 bss.prev.prev = null; 209 bss = (BSkipSpan) bss.prev; 210 211 BSkipSpan.load(bss, bf, bsl, np, key, val); 212 np = bss.prevPage; 213 } 214 } 215 } 216 | Popular Tags |