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: FreeLogicalRowIdPage.java,v 1.1 2000/05/06 00:00:31 boisvert Exp $
046: */package jdbm.recman;
047:
048: /**
049: * Class describing a page that holds logical rowids that were freed. Note
050: * that the methods have *physical* rowids in their signatures - this is
051: * because logical and physical rowids are internally the same, only their
052: * external representation (i.e. in the client API) differs.
053: */
054: class FreeLogicalRowIdPage extends PageHeader {
055: // offsets
056: private static final short O_COUNT = PageHeader.SIZE; // short count
057: static final short O_FREE = (short) (O_COUNT + Magic.SZ_SHORT);
058: static final short ELEMS_PER_PAGE = (short) ((RecordFile.BLOCK_SIZE - O_FREE) / PhysicalRowId.SIZE);
059:
060: // slots we returned.
061: final PhysicalRowId[] slots = new PhysicalRowId[ELEMS_PER_PAGE];
062:
063: /**
064: * Constructs a data page view from the indicated block.
065: */
066: FreeLogicalRowIdPage(BlockIo block) {
067: super (block);
068: }
069:
070: /**
071: * Factory method to create or return a data page for the
072: * indicated block.
073: */
074: static FreeLogicalRowIdPage getFreeLogicalRowIdPageView(
075: BlockIo block) {
076:
077: BlockView view = block.getView();
078: if (view != null && view instanceof FreeLogicalRowIdPage)
079: return (FreeLogicalRowIdPage) view;
080: else
081: return new FreeLogicalRowIdPage(block);
082: }
083:
084: /** Returns the number of free rowids */
085: short getCount() {
086: return block.readShort(O_COUNT);
087: }
088:
089: /** Sets the number of free rowids */
090: private void setCount(short i) {
091: block.writeShort(O_COUNT, i);
092: }
093:
094: /** Frees a slot */
095: void free(int slot) {
096: get(slot).setBlock(0);
097: setCount((short) (getCount() - 1));
098: }
099:
100: /** Allocates a slot */
101: PhysicalRowId alloc(int slot) {
102: setCount((short) (getCount() + 1));
103: get(slot).setBlock(-1);
104: return get(slot);
105: }
106:
107: /** Returns true if a slot is allocated */
108: boolean isAllocated(int slot) {
109: return get(slot).getBlock() > 0;
110: }
111:
112: /** Returns true if a slot is free */
113: boolean isFree(int slot) {
114: return !isAllocated(slot);
115: }
116:
117: /** Returns the value of the indicated slot */
118: PhysicalRowId get(int slot) {
119: if (slots[slot] == null)
120: slots[slot] = new PhysicalRowId(block, slotToOffset(slot));
121: ;
122: return slots[slot];
123: }
124:
125: /** Converts slot to offset */
126: private short slotToOffset(int slot) {
127: return (short) (O_FREE + (slot * PhysicalRowId.SIZE));
128: }
129:
130: /**
131: * Returns first free slot, -1 if no slots are available
132: */
133: int getFirstFree() {
134: for (int i = 0; i < ELEMS_PER_PAGE; i++) {
135: if (isFree(i))
136: return i;
137: }
138: return -1;
139: }
140:
141: /**
142: * Returns first allocated slot, -1 if no slots are available.
143: */
144: int getFirstAllocated() {
145: for (int i = 0; i < ELEMS_PER_PAGE; i++) {
146: if (isAllocated(i))
147: return i;
148: }
149: return -1;
150: }
151: }
|