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: FreePhysicalRowIdPageManager.java,v 1.2 2001/11/17 16:14:25 boisvert Exp $
046: */package jdbm.recman;
047:
048: import java.io.IOException;
049:
050: /**
051: * This class manages free physical rowid pages and provides methods
052: * to free and allocate physical rowids on a high level.
053: */
054: final class FreePhysicalRowIdPageManager {
055: // our record file
056: protected RecordFile _file;
057:
058: // our page manager
059: protected PageManager _pageman;
060:
061: /**
062: * Creates a new instance using the indicated record file and
063: * page manager.
064: */
065: FreePhysicalRowIdPageManager(RecordFile file, PageManager pageman)
066: throws IOException {
067: _file = file;
068: _pageman = pageman;
069: }
070:
071: /**
072: * Returns a free physical rowid of the indicated size, or
073: * null if nothing was found.
074: */
075: Location get(int size) throws IOException {
076: // Loop through the free physical rowid list until we find
077: // a rowid that's large enough.
078: Location retval = null;
079: PageCursor curs = new PageCursor(_pageman,
080: Magic.FREEPHYSIDS_PAGE);
081:
082: while (curs.next() != 0) {
083: FreePhysicalRowIdPage fp = FreePhysicalRowIdPage
084: .getFreePhysicalRowIdPageView(_file.get(curs
085: .getCurrent()));
086: int slot = fp.getFirstLargerThan(size);
087: if (slot != -1) {
088: // got one!
089: retval = new Location(fp.get(slot));
090:
091: int slotsize = fp.get(slot).getSize();
092: fp.free(slot);
093: if (fp.getCount() == 0) {
094: // page became empty - free it
095: _file.release(curs.getCurrent(), false);
096: _pageman.free(Magic.FREEPHYSIDS_PAGE, curs
097: .getCurrent());
098: } else {
099: _file.release(curs.getCurrent(), true);
100: }
101:
102: return retval;
103: } else {
104: // no luck, go to next page
105: _file.release(curs.getCurrent(), false);
106: }
107:
108: }
109: return null;
110: }
111:
112: /**
113: * Puts the indicated rowid on the free list
114: */
115: void put(Location rowid, int size) throws IOException {
116:
117: FreePhysicalRowId free = null;
118: PageCursor curs = new PageCursor(_pageman,
119: Magic.FREEPHYSIDS_PAGE);
120: long freePage = 0;
121: while (curs.next() != 0) {
122: freePage = curs.getCurrent();
123: BlockIo curBlock = _file.get(freePage);
124: FreePhysicalRowIdPage fp = FreePhysicalRowIdPage
125: .getFreePhysicalRowIdPageView(curBlock);
126: int slot = fp.getFirstFree();
127: if (slot != -1) {
128: free = fp.alloc(slot);
129: break;
130: }
131:
132: _file.release(curBlock);
133: }
134: if (free == null) {
135: // No more space on the free list, add a page.
136: freePage = _pageman.allocate(Magic.FREEPHYSIDS_PAGE);
137: BlockIo curBlock = _file.get(freePage);
138: FreePhysicalRowIdPage fp = FreePhysicalRowIdPage
139: .getFreePhysicalRowIdPageView(curBlock);
140: free = fp.alloc(0);
141: }
142:
143: free.setBlock(rowid.getBlock());
144: free.setOffset(rowid.getOffset());
145: free.setSize(size);
146: _file.release(freePage, true);
147: }
148: }
|