Quadcap Embeddable Database

com/quadcap/sql/file/SubPage.java

Go to the documentation of this file.
00001 package com.quadcap.sql.file; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import com.quadcap.util.Debug; 00044 import com.quadcap.util.Util; 00045 00046 /** 00047 * This class implements a page allocated by a sub-page allocator. 00048 * 00049 * @author Stan Bailes 00050 */ 00051 public class SubPage implements Page { 00052 SubPageManager pm; 00053 BlockFile file; 00054 long pageNum; 00055 int pageOffset; 00056 Block block = null; 00057 00058 /** 00059 * Construct a new SubPage 00060 */ 00061 public SubPage(SubPageManager pm, long pageNum) throws IOException { 00062 this.pm = pm; 00063 this.file = pm.file; 00064 this.pageNum = pageNum; 00065 this.pageOffset = pm.pageOffset(pageNum); 00066 this.block = file.getBlock(SubPageManager.pageBlock(pageNum)); 00067 } 00068 00069 /** 00070 * Return this page's number 00071 */ 00072 public long getPageNum() { 00073 return pageNum; 00074 } 00075 00076 /** 00077 * Read a range of bytes from the page. 00078 * 00079 * @param pos the offset in the page of the first byte to read 00080 * @param pbuf the buffer into which the bytes are placed. 00081 * @param offset the offset in <code>pbuf</code> where the first byte 00082 * is placed. 00083 * @param len the number of bytes to read 00084 */ 00085 public int read(int pos, byte[] pbuf, int offset, int len) { 00086 //#ifdef PARANOID 00087 //- if (pos < 0 || (pos+len) > pm.getPageSize()) { 00088 //- throw new ArrayIndexOutOfBoundsException("Bad read(" + pos + ", " + len + ")"); 00089 //- } 00090 //#endif 00091 return block.read(pos + pageOffset, pbuf, offset, len); 00092 } 00093 00094 /** 00095 * Write a range of bytes to the page. 00096 * 00097 * @param pos the offset in the page of the first byte to write 00098 * @param pbuf the buffer from which the bytes are obtained 00099 * @param offset the offset in <code>pbuf</code> of the first byte 00100 * to write 00101 * @param len the number of bytes to write 00102 */ 00103 public int write(int pos, byte[] pbuf, int offset, int len) { 00104 //#ifdef PARANOID 00105 //- if (pos < 0 || pos >= pm.getPageSize()) { 00106 //- throw new ArrayIndexOutOfBoundsException("Bad write(" + pos + ", " + len + ")"); 00107 //- } 00108 //#endif 00109 return block.write(pos + pageOffset, pbuf, offset, len); 00110 } 00111 00112 /** 00113 * Read an integer (4-byte) value from the page. 00114 * 00115 * @param pos the offset in the page of the integer. 00116 */ 00117 public int readInt(int pos) { 00118 return block.readInt(pos + pageOffset); 00119 } 00120 00121 /** 00122 * Write an integer (4-byte) value to the page. 00123 * 00124 * @param pos the offset in the page of the integer. 00125 * @param val the integer value to write. 00126 */ 00127 public void writeInt(int pos, int val) { 00128 block.writeInt(pos + pageOffset, val); 00129 } 00130 00131 /** 00132 * Read a long (8-byte) value from the page. 00133 * 00134 * @param pos the offset in the page of the long. 00135 */ 00136 public long readLong(int pos) { 00137 return block.readLong(pos + pageOffset); 00138 } 00139 00140 /** 00141 * Write a long (8-byte) value to the page. 00142 * 00143 * @param pos the offset in the page of the long. 00144 * @param val the long value to write. 00145 */ 00146 public void writeLong(int pos, long val) { 00147 block.writeLong(pos + pageOffset, val); 00148 } 00149 00150 /** 00151 * Move the contents of the other page to this page, and zero out 00152 * the other page. 00153 */ 00154 public void takeData(Page p) { 00155 byte[] buf = new byte[pm.pageSize]; 00156 p.read(0, buf, 0, pm.pageSize); 00157 write(0, buf, 0, pm.pageSize); 00158 for (int i = 0; i < buf.length; i++) buf[i] = 0; 00159 p.write(0, buf, 0, pm.pageSize); 00160 } 00161 00162 public void clear() { 00163 byte[] buf = new byte[pm.pageSize]; 00164 for (int i = 0; i < buf.length; i++) buf[i] = 0; 00165 write(0, buf, 0, pm.pageSize); 00166 } 00167 00168 public void decrRefCount() { 00169 if (block != null) { 00170 block.decrRefCount(); 00171 block = null; 00172 } 00173 } 00174 00175 //#ifdef DEBUG 00176 public String toString() { 00177 return "Page(" + SubPageManager.toString(pageNum) + ")"; 00178 } 00179 //#endif 00180 00181 }