KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > metanotion > io > block > index > BSkipSpan


1 /*
2 Copyright (c) 2006, Matthew Estes
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13     * Neither the name of Metanotion Software nor the names of its
14 contributors may be used to endorse or promote products derived from this
15 software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */

29 package net.metanotion.io.block.index;
30
31 import java.io.IOException JavaDoc;
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 JavaDoc {
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 JavaDoc ioe) { throw new Error JavaDoc(); }
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 JavaDoc ioe) { throw new Error JavaDoc(); }
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 JavaDoc ioe) { throw new Error JavaDoc(); }
125     }
126
127     private static void load(BSkipSpan bss, BlockFile bf, BSkipList bsl, int spanPage, Serializer key, Serializer val) throws IOException JavaDoc {
128         bss.bf = bf;
129         bss.page = spanPage;
130         bss.keySer = key;
131         bss.valSer = val;
132
133         bsl.spanHash.put(new Integer JavaDoc(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 JavaDoc[sz];
144         bss.vals = new Object JavaDoc[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 // System.out.println("Span Load " + sz + " nKeys " + nKeys + " page " + curPage);
153
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 // System.out.println("i=" + i + ", Page " + curPage + ", offset " + pageCounter[0] + " ksz " + ksz + " vsz " + vsz);
168
bss.keys[i] = (Comparable JavaDoc) 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 JavaDoc {
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 JavaDoc(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 JavaDoc(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