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