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.ConfigNumber;
00044
import com.quadcap.util.Debug;
00045
import com.quadcap.util.Util;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 public class Block extends Cacheable implements
Page {
00056 private byte[]
buf;
00057
00058 public long getPageNum() {
return key; }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 public void init(Object store,
long key)
throws IOException {
00071
00072
if (
Trace.bit(0)) {
00073
Debug.println(
"Block.init(" + store +
", " + key +
")");
00074 }
00075
00076 super.init(store, key);
00077
BlockStore bs = (
BlockStore)store;
00078
if (
buf == null)
buf =
new byte[bs.
blockSize()];
00079 bs.
read(
getPageNum(),
buf);
00080 setDirty(
false);
00081 }
00082
00083
00084
00085
00086 public final void flush() throws IOException {
00087
00088
if (
Trace.bit(1)) {
00089
Debug.println(0,
"Block[" + key +
"].flush()");
00090 }
00091
00092
if (dirty) {
00093
00094
if (
Trace.bit(0)) {
00095
Debug.println(
"flush [" +
getPageNum() +
"]");
00096 }
00097
00098
BlockStore bs = (
BlockStore)store;
00099 bs.
write(
getPageNum(),
buf);
00100 setDirty(
false);
00101 }
00102 }
00103
00104
00105
00106
00107
00108
00109 public final Object
getData() {
return buf; }
00110
00111
00112
00113
00114
00115
00116
00117
00118 public byte[]
getDataAndReset() {
00119
00120
if (
Trace.bit(0)) {
00121
Debug.println(
"Block[" + key +
"].getDataAndReset()");
00122 }
00123
00124 byte[] r =
buf;
00125 buf =
new byte[r.length];
00126 setDirty(
true);
00127
return r;
00128 }
00129
00130
00131
00132
00133
00134
00135 public void setData(Object obj) {
00136
00137
if (
Trace.bit(0)) {
00138
Debug.println(
"Block[" + key +
"].setData(" + obj +
")");
00139 }
00140
00141 byte[] r = (byte[])obj;
00142
if (r.length !=
buf.length) {
00143
throw new RuntimeException(
"Bad buffer length!");
00144 }
00145
buf = r;
00146 setDirty(
true);
00147 }
00148
00149
00150
00151
00152 public String
toString() {
00153 StringBuffer sb =
new StringBuffer();
00154 sb.append(
"[key(" +
getKey() +
"), dirty(" + dirty +
"), refcount(" +
00155 refCount +
")]");
00156
return sb.toString();
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 public final int read(
int pos, byte[] pbuf,
int offset,
int len) {
00169
00170
if (
Trace.bit(1)) {
00171
Debug.println(
"Block[" + key +
"].read(" + pos +
", " + len +
")" +
00172
", offset = " + offset +
", buf.len = " +
buf.length +
00173
", pbuf.len = " + pbuf.length);
00174 }
00175
00176
00177 System.arraycopy(
buf, pos, pbuf, offset, len);
00178
00179
if (
Trace.bit(0)) {
00180
Debug.println(
"Block[" + key +
"].read(" + pos +
", " + len +
00181
"): " +
Util.strBytes(pbuf, offset, len));
00182 }
00183
00184
return len;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 public final int write(
int pos, byte[] pbuf,
int offset,
int len) {
00197
00198
if (
Trace.bit(0)) {
00199
Debug.println(
"Block[" + key +
"].write(" + pos +
", " +
00200
Util.strBytes(pbuf, offset, len) +
")");
00201 }
00202
00203 System.arraycopy(pbuf, offset,
buf, pos, len);
00204 setDirty(
true);
00205
return len;
00206 }
00207
00208 public final byte
readByte(
int pos) {
00209
00210
if (
Trace.bit(1)) {
00211
Debug.println(
"Block[" + key +
"].read(" + (pos) +
"): " +
buf[pos]);
00212 }
00213
00214
return buf[pos];
00215 }
00216
00217 public final void writeByte(
int pos, byte val) {
00218
00219
if (
Trace.bit(0)) {
00220
Debug.println(
"Block[" + key +
"].write(" + pos +
"): " + val);
00221 }
00222
00223
buf[pos] = val;
00224 setDirty(
true);
00225 }
00226
00227
00228
00229
00230
00231
00232 public final short readShort(
int pos) {
00233
short ret =
ByteUtil.getShort(
buf, pos);
00234
00235
if (
Trace.bit(1)) {
00236
Debug.println(
"Block[" + key +
"].readShort(" + pos +
") = " + ret);
00237 }
00238
00239
return ret;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 public final void writeShort(
int pos,
short val) {
00249
00250
if (
Trace.bit(0)) {
00251
Debug.println(
"Block[" + key +
"].writeShort(" + pos +
", " +
00252 val +
")");
00253 }
00254
00255
ByteUtil.putShort(
buf, pos, val);
00256 setDirty(
true);
00257 }
00258
00259
00260
00261
00262
00263
00264 public final int readInt(
int pos) {
00265
int ret =
ByteUtil.getInt(
buf, pos);
00266
00267
if (
Trace.bit(1)) {
00268
Debug.println(
"Block[" + key +
"].readInt(" + pos +
") = " + ret);
00269 }
00270
00271
return ret;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 public final void writeInt(
int pos,
int val) {
00281
00282
if (
Trace.bit(0)) {
00283
Debug.println(
"Block[" + key +
"].writeInt(" + pos +
", " +
00284 val +
")");
00285 }
00286
00287
ByteUtil.putInt(
buf, pos, val);
00288 setDirty(
true);
00289 }
00290
00291
00292
00293
00294
00295
00296 public final long readLong(
int pos) {
00297
long ret =
ByteUtil.getLong(
buf, pos);
00298
00299
if (
Trace.bit(1)) {
00300
Debug.println(
"Block[" + key +
"].readLong(" + pos +
") = " + ret);
00301 }
00302
00303
return ret;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312 public final void writeLong(
int pos,
long val) {
00313
00314
if (
Trace.bit(0)) {
00315
Debug.println(
"Block[" + key +
"].writeLong(" + pos +
", " +
00316 val +
")");
00317 }
00318
00319
ByteUtil.putLong(
buf, pos, val);
00320 setDirty(
true);
00321 }
00322
00323 public final void takeData(
Page p) {
00324 setData(((
Block)p).
getDataAndReset());
00325 }
00326
00327 public void clear() {
00328
for (
int i = 0; i <
buf.length; i++)
buf[i] = 0;
00329 setDirty(
true);
00330 }
00331
00332
00333
00334 void init() {
00335
buf =
new byte[32];
00336 }
00337
00338
00339 public String
signature() {
00340
return signature(
buf);
00341 }
00342
00343 public static String
signature(byte[] buf) {
00344
return signature(buf, 0, buf.length);
00345 }
00346 public static String
signature(byte[] buf,
int off,
int cnt) {
00347 byte[] b = com.quadcap.io.Base64OutputStream.base64;
00348
int h = 31415;
00349
long tot = 0;
00350
for (
int i = 0; i < cnt; i++) {
00351 tot += buf[off+i];
00352 h ^= (h << 7) ^ (h >> 3) ^ buf[off + i];
00353 }
00354
return "" + (
char)b[(h >> 1) & 63] + (
char)b[(h >> 8) & 63] + (
char)b[(h >> 16) & 63] +
00355 ((tot == 0) ?
"*" :
"");
00356 }
00357
00358
00359 public static void main(String args[]) {
00360
Block b =
new Block();
00361 b.
init();
00362 b.
writeInt(20, 128);
00363 byte[] bufx =
new byte[32];
00364 b.
read(20, bufx, 0, 4);
00365
int v = b.
readInt(20);
00366 }
00367
00368 }