00001
package com.quadcap.sql.file;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.IOException;
00042
00043
import com.quadcap.util.Debug;
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 class BlockPath {
00057
00058 BlockAccess a;
00059
00060 int depth;
00061
00062 long pos;
00063
00064 int[]
p;
00065
00066 long[]
r;
00067
00068 int[]
tp;
00069
00070
00071
00072
00073
00074
00075
00076
00077 public BlockPath(
BlockAccess a,
long pos)
throws IOException {
00078
this.a =
a;
00079
this.pos =
pos;
00080
00081
depth =
a.
depth;
00082
00083
p =
new int[
depth + 1];
00084
r =
new long[
depth + 1];
00085
tp =
new int[
depth + 1];
00086
00087
getPath(
pos,
p);
00088
getRefs();
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098 public final Page getLeafBlock() throws IOException {
00099
return a.
file.
getPage(
r[
r.length-1]);
00100 }
00101
00102
00103
00104
00105
00106 public final int getBufPos() {
00107
return p[
p.length-1];
00108 }
00109
00110
00111
00112
00113 public final int getDepth() {
return depth; }
00114
00115
00116
00117
00118 public final long getPos() {
return pos; }
00119
00120
00121
00122
00123
00124 public final void setPos(
long newpos)
throws IOException {
00125
if (newpos !=
pos) {
00126
getPath(newpos,
tp);
00127
updatePath(
tp);
00128
pos = newpos;
00129 }
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 public final void incr(
long offset)
throws IOException {
00139 setPos(
pos + offset);
00140 }
00141
00142
00143
00144
00145 private final void getPath(
long pos,
int[] path) {
00146
00147
int len =
a.
radices.length;
00148
for (
int i = 0; i < len; i++) {
00149 path[i] = (
int)(pos /
a.
radices[i]);
00150 pos %=
a.
radices[i];
00151
00152
00153 }
00154 path[len] = (
int)pos;
00155
00156 }
00157
00158
00159
00160
00161 private final void getRefs() throws IOException {
00162
r[0] =
a.
rootBlock;
00163
for (
int i = 1; i <
r.length; i++) {
00164
try {
00165
Page b =
a.
file.
getPage(
r[i-1]);
00166
try {
00167
r[i] =
a.
makeBlockRef(b,
p[i-1], i==1);
00168 } finally {
00169 b.
decrRefCount();
00170 }
00171 }
catch (IOException ex) {
00172
Debug.println(
"getRefs, i = " + i +
00173
", r[" + i +
"]=" +
00174
SubPageManager.toString(
r[i-1]));
00175
Debug.println(
"this = " +
this);
00176
throw ex;
00177 }
catch (
RuntimeException ex) {
00178
Debug.println(
"getRefs, i = " + i +
00179
", r[" + i +
"]=" +
00180
SubPageManager.toString(
r[i-1]));
00181
Debug.println(
"this = " +
this);
00182
throw ex;
00183 }
00184 }
00185 }
00186
00187
00188
00189
00190
00191 private final void updatePath(
int[] path)
throws IOException {
00192
if (
true) {
00193
this.p = path;
00194
getRefs();
00195 }
else {
00196
boolean update =
false;
00197
for (
int i = 1; i <
r.length; i++) {
00198 update |= (
p[i-1] != path[i-1]);
00199
p[i-1] = path[i-1];
00200
if (update) {
00201
Page b =
a.
file.
getPage(
r[i-1]);
00202
try {
00203
r[i] =
a.
makeBlockRef(b, path[i-1], i==1);
00204 } finally {
00205 b.
decrRefCount();
00206 }
00207 }
00208 }
00209
p[
r.length-1] = path[
r.length-1];
00210 }
00211 }
00212
00213
00214
00215
00216
00217 public String
toString() {
00218 StringBuffer sb =
new StringBuffer();
00219 sb.append(
"Path[" +
pos +
"] p(");
00220
for (
int i = 0; i <
p.length; i++) {
00221
if (i > 0) sb.append(
", ");
00222 sb.append(
"" +
p[i]);
00223 }
00224 sb.append(
") r(");
00225
for (
int i = 0; i <
r.length; i++) {
00226
if (i > 0) sb.append(
", ");
00227 sb.append(
"" +
SubPageManager.toString(
r[i]));
00228 }
00229 sb.append(
")");
00230
return sb.toString();
00231 }
00232
00233
00234 }