001: package com.quadcap.sql.file;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import com.quadcap.util.Debug;
044: import com.quadcap.util.Util;
045:
046: /**
047: * This class implements a page allocated by a sub-page allocator.
048: *
049: * @author Stan Bailes
050: */
051: public class SubPage implements Page {
052: SubPageManager pm;
053: BlockFile file;
054: long pageNum;
055: int pageOffset;
056: Block block = null;
057:
058: /**
059: * Construct a new SubPage
060: */
061: public SubPage(SubPageManager pm, long pageNum) throws IOException {
062: this .pm = pm;
063: this .file = pm.file;
064: this .pageNum = pageNum;
065: this .pageOffset = pm.pageOffset(pageNum);
066: this .block = file.getBlock(SubPageManager.pageBlock(pageNum));
067: }
068:
069: /**
070: * Return this page's number
071: */
072: public long getPageNum() {
073: return pageNum;
074: }
075:
076: /**
077: * Read a range of bytes from the page.
078: *
079: * @param pos the offset in the page of the first byte to read
080: * @param pbuf the buffer into which the bytes are placed.
081: * @param offset the offset in <code>pbuf</code> where the first byte
082: * is placed.
083: * @param len the number of bytes to read
084: */
085: public int read(int pos, byte[] pbuf, int offset, int len) {
086: //#ifdef PARANOID
087: //- if (pos < 0 || (pos+len) > pm.getPageSize()) {
088: //- throw new ArrayIndexOutOfBoundsException("Bad read(" + pos + ", " + len + ")");
089: //- }
090: //#endif
091: return block.read(pos + pageOffset, pbuf, offset, len);
092: }
093:
094: /**
095: * Write a range of bytes to the page.
096: *
097: * @param pos the offset in the page of the first byte to write
098: * @param pbuf the buffer from which the bytes are obtained
099: * @param offset the offset in <code>pbuf</code> of the first byte
100: * to write
101: * @param len the number of bytes to write
102: */
103: public int write(int pos, byte[] pbuf, int offset, int len) {
104: //#ifdef PARANOID
105: //- if (pos < 0 || pos >= pm.getPageSize()) {
106: //- throw new ArrayIndexOutOfBoundsException("Bad write(" + pos + ", " + len + ")");
107: //- }
108: //#endif
109: return block.write(pos + pageOffset, pbuf, offset, len);
110: }
111:
112: /**
113: * Read an integer (4-byte) value from the page.
114: *
115: * @param pos the offset in the page of the integer.
116: */
117: public int readInt(int pos) {
118: return block.readInt(pos + pageOffset);
119: }
120:
121: /**
122: * Write an integer (4-byte) value to the page.
123: *
124: * @param pos the offset in the page of the integer.
125: * @param val the integer value to write.
126: */
127: public void writeInt(int pos, int val) {
128: block.writeInt(pos + pageOffset, val);
129: }
130:
131: /**
132: * Read a long (8-byte) value from the page.
133: *
134: * @param pos the offset in the page of the long.
135: */
136: public long readLong(int pos) {
137: return block.readLong(pos + pageOffset);
138: }
139:
140: /**
141: * Write a long (8-byte) value to the page.
142: *
143: * @param pos the offset in the page of the long.
144: * @param val the long value to write.
145: */
146: public void writeLong(int pos, long val) {
147: block.writeLong(pos + pageOffset, val);
148: }
149:
150: /**
151: * Move the contents of the other page to this page, and zero out
152: * the other page.
153: */
154: public void takeData(Page p) {
155: byte[] buf = new byte[pm.pageSize];
156: p.read(0, buf, 0, pm.pageSize);
157: write(0, buf, 0, pm.pageSize);
158: for (int i = 0; i < buf.length; i++)
159: buf[i] = 0;
160: p.write(0, buf, 0, pm.pageSize);
161: }
162:
163: public void clear() {
164: byte[] buf = new byte[pm.pageSize];
165: for (int i = 0; i < buf.length; i++)
166: buf[i] = 0;
167: write(0, buf, 0, pm.pageSize);
168: }
169:
170: public void decrRefCount() {
171: if (block != null) {
172: block.decrRefCount();
173: block = null;
174: }
175: }
176:
177: //#ifdef DEBUG
178: public String toString() {
179: return "Page(" + SubPageManager.toString(pageNum) + ")";
180: }
181: //#endif
182:
183: }
|