Debian Codegraph @

jdbm1/src/main/java/jdbm/recman/BlockIo.java

raw
1 /**
2 * JDBM LICENSE v1.00
3 *
4 * Redistribution and use of this software and associated documentation
5 * ("Software"), with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain copyright
9 * statements and notices. Redistributions must also contain a
10 * copy of this document.
11 *
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * 3. The name "JDBM" must not be used to endorse or promote
18 * products derived from this Software without prior written
19 * permission of Cees de Groot. For written permission,
20 * please contact [email protected].
21 *
22 * 4. Products derived from this Software may not be called "JDBM"
23 * nor may "JDBM" appear in their names without prior written
24 * permission of Cees de Groot.
25 *
26 * 5. Due credit should be given to the JDBM Project
27 * (http://jdbm.sourceforge.net/).
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
43 * Contributions are Copyright (C) 2000 by their associated contributors.
44 *
45 * $Id: BlockIo.java,v 1.2 2002/08/06 05:18:36 boisvert Exp $
46 */
47 package jdbm.recman;
48
49
50 import java.io.IOException;
51 import java.io.ObjectInput;
52 import java.io.ObjectOutput;
53
54 import jdbm.I18n;
55
56
57 /**
58 * This class wraps a page-sized byte array and provides methods to read and
59 * write data to and from it. The readers and writers are just the ones that
60 * the rest of the toolkit needs, nothing else. Values written are compatible
61 * with java.io routines.
62 *
63 * This block is never accessed directly, so it does not have to be thread-safe.
64 *
65 * @see java.io.DataInput
66 * @see java.io.DataOutput
67 */
68 public final class BlockIo implements java.io.Externalizable
69 {
70 public final static long serialVersionUID = 2L;
71
72 /** The block Identifier */
73 private long blockId;
74
75 /** The row data contained in this block */
76 private transient byte[] data;
77
78 private transient BlockView view = null;
79
80 /** A flag set when this block has been modified */
81 private transient boolean dirty = false;
82
83 /** The number of pending transaction on this block */
84 private transient int transactionCount = 0;
85
86
87 /**
88 * Default constructor for serialization
89 */
90 public BlockIo()
91 {
92 // empty
93 }
94
95
96 /**
97 * Constructs a new BlockIo instance.
98 *
99 * @param blockId The identifier for this block
100 * @param data The data to store
101 */
102 BlockIo( long blockId, byte[] data )
103 {
104 // remove me for production version
105 if ( blockId < 0 )
106 {
107 throw new Error( I18n.err( I18n.ERR_539_BAD_BLOCK_ID, blockId ) );
108 }
109
110 this.blockId = blockId;
111 this.data = data;
112 }
113
114
115 /**
116 * @return the underlying array
117 */
118 byte[] getData()
119 {
120 return data;
121 }
122
123
124 /**
125 * Sets the block number. Should only be called by RecordFile.
126 *
127 * @param The block identifier
128 */
129 void setBlockId( long blockId )
130 {
131 if ( isInTransaction() )
132 {
133 throw new Error( I18n.err( I18n.ERR_540 ) );
134 }
135
136 if ( blockId < 0 )
137 {
138 throw new Error( I18n.err( I18n.ERR_539_BAD_BLOCK_ID, blockId ) );
139 }
140
141 this.blockId = blockId;
142 }
143
144
145 /**
146 * @return the block number.
147 */
148 long getBlockId()
149 {
150 return blockId;
151 }
152
153
154 /**
155 * @return the current view of the block.
156 */
157 public BlockView getView()
158 {
159 return view;
160 }
161
162
163 /**
164 * Sets the current view of the block.
165 */
166 public void setView( BlockView view )
167 {
168 this.view = view;
169 }
170
171
172 /**
173 * Sets the dirty flag
174 */
175 void setDirty()
176 {
177 dirty = true;
178 }
179
180
181 /**
182 * Clears the dirty flag
183 */
184 void setClean()
185 {
186 dirty = false;
187 }
188
189
190 /**
191 * Returns true if the dirty flag is set.
192 */
193 boolean isDirty()
194 {
195 return dirty;
196 }
197
198
199 /**
200 * Returns true if the block is still dirty with respect to the
201 * transaction log.
202 */
203 boolean isInTransaction()
204 {
205 return transactionCount != 0;
206 }
207
208
209 /**
210 * Increments transaction count for this block, to signal that this
211 * block is in the log but not yet in the data recordFile. The method also
212 * takes a snapshot so that the data may be modified in new transactions.
213 */
214 synchronized void incrementTransactionCount()
215 {
216 transactionCount++;
217
218 // @fix me ( alex )
219 setClean();
220 }
221
222
223 /**
224 * Decrements transaction count for this block, to signal that this
225 * block has been written from the log to the data recordFile.
226 */
227 synchronized void decrementTransactionCount()
228 {
229 transactionCount--;
230
231 if ( transactionCount < 0 )
232 {
233 throw new Error( I18n.err( I18n.ERR_541, getBlockId() ) );
234 }
235 }
236
237
238 /**
239 * Reads a byte from the indicated position
240 */
241 public byte readByte( int pos )
242 {
243 return data[pos];
244 }
245
246
247 /**
248 * Writes a byte to the indicated position
249 */
250 public void writeByte( int pos, byte value )
251 {
252 data[pos] = value;
253 setDirty();
254 }
255
256
257 /**
258 * Reads a short from the indicated position
259 */
260 public short readShort( int pos )
261 {
262 return ( short ) ( ( ( short ) ( data[pos + 0] & 0xff ) << 8 ) | ( ( short ) ( data[pos + 1] & 0xff ) << 0 ) );
263 }
264
265
266 /**
267 * Writes a short to the indicated position
268 */
269 public void writeShort( int pos, short value )
270 {
271 data[pos + 0] = ( byte ) ( 0xff & ( value >> 8 ) );
272 data[pos + 1] = ( byte ) ( 0xff & ( value >> 0 ) );
273 setDirty();
274 }
275
276
277 /**
278 * Reads an int from the indicated position
279 */
280 public int readInt( int pos )
281 {
282 return ( ( ( data[pos + 0] & 0xff ) << 24 ) |
283 ( ( data[pos + 1] & 0xff ) << 16 ) |
284 ( ( data[pos + 2] & 0xff ) << 8 ) | ( ( data[pos + 3] & 0xff ) << 0 ) );
285 }
286
287
288 /**
289 * Writes an int to the indicated position
290 */
291 public void writeInt( int pos, int value )
292 {
293 data[pos + 0] = ( byte ) ( 0xff & ( value >> 24 ) );
294 data[pos + 1] = ( byte ) ( 0xff & ( value >> 16 ) );
295 data[pos + 2] = ( byte ) ( 0xff & ( value >> 8 ) );
296 data[pos + 3] = ( byte ) ( 0xff & ( value >> 0 ) );
297 setDirty();
298 }
299
300
301 /**
302 * Reads a long from the indicated position
303 */
304 public long readLong( int pos )
305 {
306 // Contributed by Erwin Bolwidt <[email protected]>
307 // Gives about 15% performance improvement
308 return ( ( long ) ( ( ( data[pos + 0] & 0xff ) << 24 ) |
309 ( ( data[pos + 1] & 0xff ) << 16 ) |
310 ( ( data[pos + 2] & 0xff ) << 8 ) |
311 ( ( data[pos + 3] & 0xff ) ) ) << 32 ) |
312 ( ( long ) ( ( ( data[pos + 4] & 0xff ) << 24 ) |
313 ( ( data[pos + 5] & 0xff ) << 16 ) |
314 ( ( data[pos + 6] & 0xff ) << 8 ) |
315 ( ( data[pos + 7] & 0xff ) ) ) & 0xffffffff );
316 }
317
318
319 /**
320 * Writes a long to the indicated position
321 */
322 public void writeLong( int pos, long value )
323 {
324 data[pos + 0] = ( byte ) ( 0xff & ( value >> 56 ) );
325 data[pos + 1] = ( byte ) ( 0xff & ( value >> 48 ) );
326 data[pos + 2] = ( byte ) ( 0xff & ( value >> 40 ) );
327 data[pos + 3] = ( byte ) ( 0xff & ( value >> 32 ) );
328 data[pos + 4] = ( byte ) ( 0xff & ( value >> 24 ) );
329 data[pos + 5] = ( byte ) ( 0xff & ( value >> 16 ) );
330 data[pos + 6] = ( byte ) ( 0xff & ( value >> 8 ) );
331 data[pos + 7] = ( byte ) ( 0xff & ( value >> 0 ) );
332 setDirty();
333 }
334
335
336 // overrides java.lang.Object
337
338 public String toString()
339 {
340 return "BlockIO ( "
341 + blockId + ", "
342 + dirty + ", "
343 + view + " )";
344 }
345
346
347 // implement externalizable interface
348
349 public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
350 {
351 blockId = in.readLong();
352 int length = in.readInt();
353 data = new byte[length];
354 in.readFully( data );
355 }
356
357
358 // implement externalizable interface
359 public void writeExternal( ObjectOutput out ) throws IOException
360 {
361 out.writeLong( blockId );
362 out.writeInt( data.length );
363 out.write( data );
364 }
365 }