File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\Page.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.store;
8
9 import java.lang.reflect.Array;
10 import org.h2.engine.Session;
11 import org.h2.util.CacheObject;
12
13 /**
14 * A page. Format:
15 * <ul><li>0-3: parent page id (0 for root)
16 * </li><li>4-4: page type
17 * </li><li>page-type specific data
18 * </li></ul>
19 */
20 public abstract class Page extends CacheObject {
21
22 /**
23 * This is the last page of a chain.
24 */
25 public static final int FLAG_LAST = 16;
26
27 /**
28 * An empty page.
29 */
30 public static final int TYPE_EMPTY = 0;
31
32 /**
33 * A data leaf page (without overflow: + FLAG_LAST).
34 */
35 public static final int TYPE_DATA_LEAF = 1;
36
37 /**
38 * A data node page (never has overflow pages).
39 */
40 public static final int TYPE_DATA_NODE = 2;
41
42 /**
43 * A data overflow page (the last page: + FLAG_LAST).
44 */
45 public static final int TYPE_DATA_OVERFLOW = 3;
46
47 /**
48 * A b-tree leaf page (without overflow: + FLAG_LAST).
49 */
50 public static final int TYPE_BTREE_LEAF = 4;
51
52 /**
53 * A b-tree node page (never has overflow pages).
54 */
55 public static final int TYPE_BTREE_NODE = 5;
56
57 /**
58 * A page containing a list of free pages (the last page: + FLAG_LAST).
59 */
60 public static final int TYPE_FREE_LIST = 6;
61
62 /**
63 * A stream trunk page.
64 */
65 public static final int TYPE_STREAM_TRUNK = 7;
66
67 /**
68 * A stream data page.
69 */
70 public static final int TYPE_STREAM_DATA = 8;
71
72 private static final int COPY_THRESHOLD = 4;
73
74 /**
75 * When this page was changed the last time.
76 */
77 protected int changeCount;
78
79 /**
80 * Copy the data to a new location, change the parent to point to the new
81 * location, and free up the current page.
82 *
83 * @param session the session
84 * @param newPos the new position
85 */
86 public abstract void moveTo(Session session, int newPos);
87
88 /**
89 * Write the page.
90 */
91 public abstract void write();
92
93 /**
94 * Insert a value in an array. A new array is created if required.
95 *
96 * @param old the old array
97 * @param oldSize the old size
98 * @param pos the position
99 * @param x the value to insert
100 * @return the (new) array
101 */
102 @SuppressWarnings("unchecked")
103 public static <T> T[] insert(T[] old, int oldSize, int pos, T x) {
104 T[] result;
105 if (old.length > oldSize) {
106 result = old;
107 } else {
108 // according to a test, this is as fast as "new Row[..]"
109 result = (T[]) Array.newInstance(old.getClass().getComponentType(), oldSize + 1 + COPY_THRESHOLD);
110 if (pos > 0) {
111 System.arraycopy(old, 0, result, 0, pos);
112 }
113 }
114 if (oldSize - pos > 0) {
115 System.arraycopy(old, pos, result, pos + 1, oldSize - pos);
116 }
117 result[pos] = x;
118 return result;
119 }
120
121 /**
122 * Delete a value in an array. A new array is created if required.
123 *
124 * @param old the old array
125 * @param oldSize the old size
126 * @param pos the position
127 * @return the (new) array
128 */
129 @SuppressWarnings("unchecked")
130 public
131 static <T> T[] remove(T[] old, int oldSize, int pos) {
132 T[] result;
133 if (old.length - oldSize < COPY_THRESHOLD) {
134 result = old;
135 } else {
136 // according to a test, this is as fast as "new Row[..]"
137 result = (T[]) Array.newInstance(old.getClass().getComponentType(), oldSize - 1);
138 System.arraycopy(old, 0, result, 0, Math.min(oldSize - 1, pos));
139 }
140 if (pos < oldSize) {
141 System.arraycopy(old, pos + 1, result, pos, oldSize - pos - 1);
142 }
143 return result;
144 }
145
146 /**
147 * Insert a value in an array. A new array is created if required.
148 *
149 * @param old the old array
150 * @param oldSize the old size
151 * @param pos the position
152 * @param x the value to insert
153 * @return the (new) array
154 */
155 protected static long[] insert(long[] old, int oldSize, int pos, long x) {
156 long[] result;
157 if (old != null && old.length > oldSize) {
158 result = old;
159 } else {
160 result = new long[oldSize + 1 + COPY_THRESHOLD];
161 if (pos > 0) {
162 System.arraycopy(old, 0, result, 0, pos);
163 }
164 }
165 if (old != null && oldSize - pos > 0) {
166 System.arraycopy(old, pos, result, pos + 1, oldSize - pos);
167 }
168 result[pos] = x;
169 return result;
170 }
171
172 /**
173 * Delete a value in an array. A new array is created if required.
174 *
175 * @param old the old array
176 * @param oldSize the old size
177 * @param pos the position
178 * @return the (new) array
179 */
180 protected static long[] remove(long[] old, int oldSize, int pos) {
181 long[] result;
182 if (old.length - oldSize < COPY_THRESHOLD) {
183 result = old;
184 } else {
185 result = new long[oldSize - 1];
186 System.arraycopy(old, 0, result, 0, pos);
187 }
188 System.arraycopy(old, pos + 1, result, pos, oldSize - pos - 1);
189 return result;
190 }
191
192 /**
193 * Insert a value in an array. A new array is created if required.
194 *
195 * @param old the old array
196 * @param oldSize the old size
197 * @param pos the position
198 * @param x the value to insert
199 * @return the (new) array
200 */
201 protected static int[] insert(int[] old, int oldSize, int pos, int x) {
202 int[] result;
203 if (old != null && old.length > oldSize) {
204 result = old;
205 } else {
206 result = new int[oldSize + 1 + COPY_THRESHOLD];
207 if (pos > 0 && old != null) {
208 System.arraycopy(old, 0, result, 0, pos);
209 }
210 }
211 if (old != null && oldSize - pos > 0) {
212 System.arraycopy(old, pos, result, pos + 1, oldSize - pos);
213 }
214 result[pos] = x;
215 return result;
216 }
217
218 /**
219 * Delete a value in an array. A new array is created if required.
220 *
221 * @param old the old array
222 * @param oldSize the old size
223 * @param pos the position
224 * @return the (new) array
225 */
226 protected static int[] remove(int[] old, int oldSize, int pos) {
227 int[] result;
228 if (old.length - oldSize < COPY_THRESHOLD) {
229 result = old;
230 } else {
231 result = new int[oldSize - 1];
232 System.arraycopy(old, 0, result, 0, Math.min(oldSize - 1, pos));
233 }
234 if (pos < oldSize) {
235 System.arraycopy(old, pos + 1, result, pos, oldSize - pos - 1);
236 }
237 return result;
238 }
239
240 /**
241 * Add a value to a subset of the array.
242 *
243 * @param array the array
244 * @param from the index of the first element (including)
245 * @param to the index of the last element (excluding)
246 * @param x the value to add
247 */
248 protected static void add(int[] array, int from, int to, int x) {
249 for (int i = from; i < to; i++) {
250 array[i] += x;
251 }
252 }
253
254 /**
255 * If this page can be moved. Transaction log and free-list pages can not.
256 *
257 * @return true if moving is allowed
258 */
259 public boolean canMove() {
260 return true;
261 }
262
263 }
264