KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > file > SubPage


1 package com.quadcap.sql.file;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import com.quadcap.util.Debug;
44 import com.quadcap.util.Util;
45
46 /**
47  * This class implements a page allocated by a sub-page allocator.
48  *
49  * @author Stan Bailes
50  */

51 public class SubPage implements Page {
52     SubPageManager pm;
53     BlockFile file;
54     long pageNum;
55     int pageOffset;
56     Block block = null;
57
58     /**
59      * Construct a new SubPage
60      */

61     public SubPage(SubPageManager pm, long pageNum) throws IOException JavaDoc {
62     this.pm = pm;
63     this.file = pm.file;
64     this.pageNum = pageNum;
65     this.pageOffset = pm.pageOffset(pageNum);
66     this.block = file.getBlock(SubPageManager.pageBlock(pageNum));
67     }
68     
69     /**
70      * Return this page's number
71      */

72     public long getPageNum() {
73     return pageNum;
74     }
75
76     /**
77      * Read a range of bytes from the page.
78      *
79      * @param pos the offset in the page of the first byte to read
80      * @param pbuf the buffer into which the bytes are placed.
81      * @param offset the offset in <code>pbuf</code> where the first byte
82      * is placed.
83      * @param len the number of bytes to read
84      */

85     public int read(int pos, byte[] pbuf, int offset, int len) {
86         //#ifdef PARANOID
87
//- if (pos < 0 || (pos+len) > pm.getPageSize()) {
88
//- throw new ArrayIndexOutOfBoundsException("Bad read(" + pos + ", " + len + ")");
89
//- }
90
//#endif
91
return block.read(pos + pageOffset, pbuf, offset, len);
92     }
93
94     /**
95      * Write a range of bytes to the page.
96      *
97      * @param pos the offset in the page of the first byte to write
98      * @param pbuf the buffer from which the bytes are obtained
99      * @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++) buf[i] = 0;
159     p.write(0, buf, 0, pm.pageSize);
160     }
161
162     public void clear() {
163     byte[] buf = new byte[pm.pageSize];
164     for (int i = 0; i < buf.length; i++) buf[i] = 0;
165     write(0, buf, 0, pm.pageSize);
166     }
167     
168     public void decrRefCount() {
169     if (block != null) {
170         block.decrRefCount();
171         block = null;
172     }
173     }
174
175     //#ifdef DEBUG
176
public String JavaDoc toString() {
177     return "Page(" + SubPageManager.toString(pageNum) + ")";
178     }
179     //#endif
180

181 }
182
Popular Tags