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: LogicalRowIdManager.java,v 1.3 2005/06/25 23:12:32 doomdark Exp $
046: */package jdbm.recman;
047:
048: import java.io.IOException;
049:
050: /**
051: * This class manages the linked lists of logical rowid pages.
052: */
053: final class LogicalRowIdManager {
054: // our record file and associated page manager
055: private RecordFile file;
056: private PageManager pageman;
057: private FreeLogicalRowIdPageManager freeman;
058:
059: /**
060: * Creates a log rowid manager using the indicated record file and
061: * page manager
062: */
063: LogicalRowIdManager(RecordFile file, PageManager pageman)
064: throws IOException {
065: this .file = file;
066: this .pageman = pageman;
067: this .freeman = new FreeLogicalRowIdPageManager(file, pageman);
068:
069: }
070:
071: /**
072: * Creates a new logical rowid pointing to the indicated physical
073: * id
074: */
075: Location insert(Location loc) throws IOException {
076: // check whether there's a free rowid to reuse
077: Location retval = freeman.get();
078: if (retval == null) {
079: // no. This means that we bootstrap things by allocating
080: // a new translation page and freeing all the rowids on it.
081: long firstPage = pageman.allocate(Magic.TRANSLATION_PAGE);
082: short curOffset = TranslationPage.O_TRANS;
083: for (int i = 0; i < TranslationPage.ELEMS_PER_PAGE; i++) {
084: freeman.put(new Location(firstPage, curOffset));
085: curOffset += PhysicalRowId.SIZE;
086: }
087: retval = freeman.get();
088: if (retval == null) {
089: throw new Error("couldn't obtain free translation");
090: }
091: }
092: // write the translation.
093: update(retval, loc);
094: return retval;
095: }
096:
097: /**
098: * Releases the indicated logical rowid.
099: */
100: void delete(Location rowid) throws IOException {
101:
102: freeman.put(rowid);
103: }
104:
105: /**
106: * Updates the mapping
107: *
108: * @param rowid The logical rowid
109: * @param loc The physical rowid
110: */
111: void update(Location rowid, Location loc) throws IOException {
112:
113: TranslationPage xlatPage = TranslationPage
114: .getTranslationPageView(file.get(rowid.getBlock()));
115: PhysicalRowId physid = xlatPage.get(rowid.getOffset());
116: physid.setBlock(loc.getBlock());
117: physid.setOffset(loc.getOffset());
118: file.release(rowid.getBlock(), true);
119: }
120:
121: /**
122: * Returns a mapping
123: *
124: * @param rowid The logical rowid
125: * @return The physical rowid
126: */
127: Location fetch(Location rowid) throws IOException {
128:
129: TranslationPage xlatPage = TranslationPage
130: .getTranslationPageView(file.get(rowid.getBlock()));
131: try {
132: Location retval = new Location(xlatPage.get(rowid
133: .getOffset()));
134: return retval;
135: } finally {
136: file.release(rowid.getBlock(), false);
137: }
138: }
139:
140: }
|