1 package com.quadcap.sql.file; 2 3 40 41 import java.io.IOException ; 42 43 import com.quadcap.util.ConfigNumber; 44 import com.quadcap.util.Debug; 45 import com.quadcap.util.Util; 46 47 55 public class Block extends Cacheable implements Page { 56 private byte[] buf; 57 58 public long getPageNum() { return key; } 59 60 70 public void init(Object store, long key) throws IOException { 71 if (Trace.bit(0)) { 73 Debug.println("Block.init(" + store + ", " + key + ")"); 74 } 75 super.init(store, key); 77 BlockStore bs = (BlockStore)store; 78 if (buf == null) buf = new byte[bs.blockSize()]; 79 bs.read(getPageNum(), buf); 80 setDirty(false); 81 } 82 83 86 public final void flush() throws IOException { 87 if (Trace.bit(1)) { 89 Debug.println(0, "Block[" + key + "].flush()"); 90 } 91 if (dirty) { 93 if (Trace.bit(0)) { 95 Debug.println("flush [" + getPageNum() + "]"); 96 } 97 BlockStore bs = (BlockStore)store; 99 bs.write(getPageNum(), buf); 100 setDirty(false); 101 } 102 } 103 104 109 public final Object getData() { return buf; } 110 111 118 public byte[] getDataAndReset() { 119 if (Trace.bit(0)) { 121 Debug.println("Block[" + key + "].getDataAndReset()"); 122 } 123 byte[] r = buf; 125 buf = new byte[r.length]; 126 setDirty(true); 127 return r; 128 } 129 130 135 public void setData(Object obj) { 136 if (Trace.bit(0)) { 138 Debug.println("Block[" + key + "].setData(" + obj + ")"); 139 } 140 byte[] r = (byte[])obj; 142 if (r.length != buf.length) { 143 throw new RuntimeException ("Bad buffer length!"); 144 } 145 buf = r; 146 setDirty(true); 147 } 148 149 152 public String toString() { 153 StringBuffer sb = new StringBuffer (); 154 sb.append("[key(" + getKey() + "), dirty(" + dirty + "), refcount(" + 155 refCount + ")]"); 156 return sb.toString(); 157 } 158 159 168 public final int read(int pos, byte[] pbuf, int offset, int len) { 169 if (Trace.bit(1)) { 171 Debug.println("Block[" + key + "].read(" + pos + ", " + len + ")" + 172 ", offset = " + offset + ", buf.len = " + buf.length + 173 ", pbuf.len = " + pbuf.length); 174 } 175 177 System.arraycopy(buf, pos, pbuf, offset, len); 178 if (Trace.bit(0)) { 180 Debug.println("Block[" + key + "].read(" + pos + ", " + len + 181 "): " + Util.strBytes(pbuf, offset, len)); 182 } 183 return len; 185 } 186 187 196 public final int write(int pos, byte[] pbuf, int offset, int len) { 197 if (Trace.bit(0)) { 199 Debug.println("Block[" + key + "].write(" + pos + ", " + 200 Util.strBytes(pbuf, offset, len) + ")"); 201 } 202 System.arraycopy(pbuf, offset, buf, pos, len); 204 setDirty(true); 205 return len; 206 } 207 208 public final byte readByte(int pos) { 209 if (Trace.bit(1)) { 211 Debug.println("Block[" + key + "].read(" + (pos) + "): " + buf[pos]); 212 } 213 return buf[pos]; 215 } 216 217 public final void writeByte(int pos, byte val) { 218 if (Trace.bit(0)) { 220 Debug.println("Block[" + key + "].write(" + pos + "): " + val); 221 } 222 buf[pos] = val; 224 setDirty(true); 225 } 226 227 232 public final short readShort(int pos) { 233 short ret = ByteUtil.getShort(buf, pos); 234 if (Trace.bit(1)) { 236 Debug.println("Block[" + key + "].readShort(" + pos + ") = " + ret); 237 } 238 return ret; 240 } 241 242 248 public final void writeShort(int pos, short val) { 249 if (Trace.bit(0)) { 251 Debug.println("Block[" + key + "].writeShort(" + pos + ", " + 252 val + ")"); 253 } 254 ByteUtil.putShort(buf, pos, val); 256 setDirty(true); 257 } 258 259 264 public final int readInt(int pos) { 265 int ret = ByteUtil.getInt(buf, pos); 266 if (Trace.bit(1)) { 268 Debug.println("Block[" + key + "].readInt(" + pos + ") = " + ret); 269 } 270 return ret; 272 } 273 274 280 public final void writeInt(int pos, int val) { 281 if (Trace.bit(0)) { 283 Debug.println("Block[" + key + "].writeInt(" + pos + ", " + 284 val + ")"); 285 } 286 ByteUtil.putInt(buf, pos, val); 288 setDirty(true); 289 } 290 291 296 public final long readLong(int pos) { 297 long ret = ByteUtil.getLong(buf, pos); 298 if (Trace.bit(1)) { 300 Debug.println("Block[" + key + "].readLong(" + pos + ") = " + ret); 301 } 302 return ret; 304 } 305 306 312 public final void writeLong(int pos, long val) { 313 if (Trace.bit(0)) { 315 Debug.println("Block[" + key + "].writeLong(" + pos + ", " + 316 val + ")"); 317 } 318 ByteUtil.putLong(buf, pos, val); 320 setDirty(true); 321 } 322 323 public final void takeData(Page p) { 324 setData(((Block)p).getDataAndReset()); 325 } 326 327 public void clear() { 328 for (int i = 0; i < buf.length; i++) buf[i] = 0; 329 setDirty(true); 330 } 331 332 void init() { 335 buf = new byte[32]; 336 } 337 338 public String signature() { 340 return signature(buf); 341 } 342 343 public static String signature(byte[] buf) { 344 return signature(buf, 0, buf.length); 345 } 346 public static String signature(byte[] buf, int off, int cnt) { 347 byte[] b = com.quadcap.io.Base64OutputStream.base64; 348 int h = 31415; 349 long tot = 0; 350 for (int i = 0; i < cnt; i++) { 351 tot += buf[off+i]; 352 h ^= (h << 7) ^ (h >> 3) ^ buf[off + i]; 353 } 354 return "" + (char)b[(h >> 1) & 63] + (char)b[(h >> 8) & 63] + (char)b[(h >> 16) & 63] + 355 ((tot == 0) ? "*" : ""); 356 } 357 358 359 public static void main(String args[]) { 360 Block b = new Block(); 361 b.init(); 362 b.writeInt(20, 128); 363 byte[] bufx = new byte[32]; 364 b.read(20, bufx, 0, 4); 365 int v = b.readInt(20); 366 } 367 } 369 | Popular Tags |