1 package com.quadcap.sql.file; 2 3 40 41 import java.io.IOException ; 42 43 import com.quadcap.util.Debug; 44 45 55 56 class BlockPath { 57 58 BlockAccess a; 59 60 int depth; 61 62 long pos; 63 64 int[] p; 65 66 long[] r; 67 68 int[] tp; 69 70 77 public BlockPath(BlockAccess a, long pos) throws IOException { 78 this.a = a; 79 this.pos = pos; 80 81 depth = a.depth; 82 83 p = new int[depth + 1]; 84 r = new long[depth + 1]; 85 tp = new int[depth + 1]; 86 87 getPath(pos, p); 88 getRefs(); 89 } 90 91 98 public final Page getLeafBlock() throws IOException { 99 return a.file.getPage(r[r.length-1]); 100 } 101 102 106 public final int getBufPos() { 107 return p[p.length-1]; 108 } 109 110 113 public final int getDepth() { return depth; } 114 115 118 public final long getPos() { return pos; } 119 120 121 124 public final void setPos(long newpos) throws IOException { 125 if (newpos != pos) { 126 getPath(newpos, tp); 127 updatePath(tp); 128 pos = newpos; 129 } 130 } 131 132 138 public final void incr(long offset) throws IOException { 139 setPos(pos + offset); 140 } 141 142 145 private final void getPath(long pos, int[] path) { 146 int len = a.radices.length; 148 for (int i = 0; i < len; i++) { 149 path[i] = (int)(pos / a.radices[i]); 150 pos %= a.radices[i]; 151 } 154 path[len] = (int)pos; 155 } 157 158 161 private final void getRefs() throws IOException { 162 r[0] = a.rootBlock; 163 for (int i = 1; i < r.length; i++) { 164 try { 165 Page b = a.file.getPage(r[i-1]); 166 try { 167 r[i] = a.makeBlockRef(b, p[i-1], i==1); 168 } finally { 169 b.decrRefCount(); 170 } 171 } catch (IOException ex) { 172 Debug.println("getRefs, i = " + i + 173 ", r[" + i + "]=" + 174 SubPageManager.toString(r[i-1])); 175 Debug.println("this = " + this); 176 throw ex; 177 } catch (RuntimeException ex) { 178 Debug.println("getRefs, i = " + i + 179 ", r[" + i + "]=" + 180 SubPageManager.toString(r[i-1])); 181 Debug.println("this = " + this); 182 throw ex; 183 } 184 } 185 } 186 187 191 private final void updatePath(int[] path) throws IOException { 192 if (true) { 193 this.p = path; 194 getRefs(); 195 } else { 196 boolean update = false; 197 for (int i = 1; i < r.length; i++) { 198 update |= (p[i-1] != path[i-1]); 199 p[i-1] = path[i-1]; 200 if (update) { 201 Page b = a.file.getPage(r[i-1]); 202 try { 203 r[i] = a.makeBlockRef(b, path[i-1], i==1); 204 } finally { 205 b.decrRefCount(); 206 } 207 } 208 } 209 p[r.length-1] = path[r.length-1]; 210 } 211 } 212 213 217 public String toString() { 218 StringBuffer sb = new StringBuffer (); 219 sb.append("Path[" + pos + "] p("); 220 for (int i = 0; i < p.length; i++) { 221 if (i > 0) sb.append(", "); 222 sb.append("" + p[i]); 223 } 224 sb.append(") r("); 225 for (int i = 0; i < r.length; i++) { 226 if (i > 0) sb.append(", "); 227 sb.append("" + SubPageManager.toString(r[i])); 228 } 229 sb.append(")"); 230 return sb.toString(); 231 } 232 234 } 235 | Popular Tags |