001: /**
002: * JDBM LICENSE v1.00
003: *
004: * Redistribution and use of this software and associated documentation
005: * ("Software"), with or without modification, are permitted provided
006: * that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain copyright
009: * statements and notices. Redistributions must also contain a
010: * copy of this document.
011: *
012: * 2. Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and the
014: * following disclaimer in the documentation and/or other
015: * materials provided with the distribution.
016: *
017: * 3. The name "JDBM" must not be used to endorse or promote
018: * products derived from this Software without prior written
019: * permission of Cees de Groot. For written permission,
020: * please contact cg@cdegroot.com.
021: *
022: * 4. Products derived from this Software may not be called "JDBM"
023: * nor may "JDBM" appear in their names without prior written
024: * permission of Cees de Groot.
025: *
026: * 5. Due credit should be given to the JDBM Project
027: * (http://jdbm.sourceforge.net/).
028: *
029: * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
030: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
031: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
032: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
033: * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
034: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
035: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
036: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
037: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
038: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
040: * OF THE POSSIBILITY OF SUCH DAMAGE.
041: *
042: * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
043: * Contributions are Copyright (C) 2000 by their associated contributors.
044: *
045: * $Id: RecordHeader.java,v 1.1 2000/05/06 00:00:31 boisvert Exp $
046: */package jdbm.recman;
047:
048: /**
049: * The data that comes at the start of a record of data. It stores
050: * both the current size and the avaliable size for the record - the latter
051: * can be bigger than the former, which allows the record to grow without
052: * needing to be moved and which allows the system to put small records
053: * in larger free spots.
054: */
055: class RecordHeader {
056: // offsets
057: private static final short O_CURRENTSIZE = 0; // int currentSize
058: private static final short O_AVAILABLESIZE = Magic.SZ_INT; // int availableSize
059: static final int SIZE = O_AVAILABLESIZE + Magic.SZ_INT;
060:
061: // my block and the position within the block
062: private BlockIo block;
063: private short pos;
064:
065: /**
066: * Constructs a record header from the indicated data starting at
067: * the indicated position.
068: */
069: RecordHeader(BlockIo block, short pos) {
070: this .block = block;
071: this .pos = pos;
072: if (pos > (RecordFile.BLOCK_SIZE - SIZE))
073: throw new Error("Offset too large for record header ("
074: + block.getBlockId() + ":" + pos + ")");
075: }
076:
077: /** Returns the current size */
078: int getCurrentSize() {
079: return block.readInt(pos + O_CURRENTSIZE);
080: }
081:
082: /** Sets the current size */
083: void setCurrentSize(int value) {
084: block.writeInt(pos + O_CURRENTSIZE, value);
085: }
086:
087: /** Returns the available size */
088: int getAvailableSize() {
089: return block.readInt(pos + O_AVAILABLESIZE);
090: }
091:
092: /** Sets the available size */
093: void setAvailableSize(int value) {
094: block.writeInt(pos + O_AVAILABLESIZE, value);
095: }
096:
097: // overrides java.lang.Object
098: public String toString() {
099: return "RH(" + block.getBlockId() + ":" + pos + ", avl="
100: + getAvailableSize() + ", cur=" + getCurrentSize()
101: + ")";
102: }
103: }
|