File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\PageStreamData.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 org.h2.engine.Session;
10
11 /**
12 * A data page of a stream. The format is:
13 * <ul>
14 * <li>page type: byte (0)</li>
15 * <li>checksum: short (1-2)</li>
16 * <li>the trunk page id: int (3-6)</li>
17 * <li>log key: int (7-10)</li>
18 * <li>data (11-)</li>
19 * </ul>
20 */
21 public class PageStreamData extends Page {
22
23 private static final int DATA_START = 11;
24
25 private final PageStore store;
26 private int trunk;
27 private int logKey;
28 private Data data;
29 private int remaining;
30
31 private PageStreamData(PageStore store, int pageId, int trunk, int logKey) {
32 setPos(pageId);
33 this.store = store;
34 this.trunk = trunk;
35 this.logKey = logKey;
36 }
37
38 /**
39 * Read a stream data page.
40 *
41 * @param store the page store
42 * @param data the data
43 * @param pageId the page id
44 * @return the page
45 */
46 static PageStreamData read(PageStore store, Data data, int pageId) {
47 PageStreamData p = new PageStreamData(store, pageId, 0, 0);
48 p.data = data;
49 p.read();
50 return p;
51 }
52
53 /**
54 * Create a new stream trunk page.
55 *
56 * @param store the page store
57 * @param pageId the page id
58 * @param trunk the trunk page
59 * @param logKey the log key
60 * @return the page
61 */
62 static PageStreamData create(PageStore store, int pageId, int trunk, int logKey) {
63 return new PageStreamData(store, pageId, trunk, logKey);
64 }
65
66 /**
67 * Read the page from the disk.
68 */
69 private void read() {
70 data.reset();
71 data.readByte();
72 data.readShortInt();
73 trunk = data.readInt();
74 logKey = data.readInt();
75 }
76
77 /**
78 * Write the header data.
79 */
80 void initWrite() {
81 data = store.createData();
82 data.writeByte((byte) Page.TYPE_STREAM_DATA);
83 data.writeShortInt(0);
84 data.writeInt(trunk);
85 data.writeInt(logKey);
86 remaining = store.getPageSize() - data.length();
87 }
88
89 /**
90 * Write the data to the buffer.
91 *
92 * @param buff the source data
93 * @param offset the offset in the source buffer
94 * @param len the number of bytes to write
95 * @return the number of bytes written
96 */
97 int write(byte[] buff, int offset, int len) {
98 int max = Math.min(remaining, len);
99 data.write(buff, offset, max);
100 remaining -= max;
101 return max;
102 }
103
104 public void write() {
105 store.writePage(getPos(), data);
106 }
107
108 /**
109 * Get the number of bytes that fit in a page.
110 *
111 * @param pageSize the page size
112 * @return the number of bytes
113 */
114 static int getCapacity(int pageSize) {
115 return pageSize - DATA_START;
116 }
117
118 /**
119 * Read the next bytes from the buffer.
120 *
121 * @param startPos the position in the data page
122 * @param buff the target buffer
123 * @param off the offset in the target buffer
124 * @param len the number of bytes to read
125 */
126 void read(int startPos, byte[] buff, int off, int len) {
127 System.arraycopy(data.getBytes(), startPos, buff, off, len);
128 }
129
130 /**
131 * Get the number of remaining data bytes of this page.
132 *
133 * @return the remaining byte count
134 */
135 int getRemaining() {
136 return remaining;
137 }
138
139 /**
140 * Get the estimated memory size.
141 *
142 * @return number of double words (4 bytes)
143 */
144 public int getMemory() {
145 return store.getPageSize() >> 2;
146 }
147
148 public void moveTo(Session session, int newPos) {
149 // not required
150 }
151
152 int getLogKey() {
153 return logKey;
154 }
155
156 public String toString() {
157 return "[" + getPos() + "] stream data key:" + logKey + " pos:" + data.length() + " remaining:" + remaining;
158 }
159
160 public boolean canRemove() {
161 return true;
162 }
163
164 public static int getReadStart() {
165 return DATA_START;
166 }
167
168 public boolean canMove() {
169 return false;
170 }
171
172 }