View Javadoc

1   /**
2    * JDBM LICENSE v1.00
3    *
4    * Redistribution and use of this software and associated documentation
5    * ("Software"), with or without modification, are permitted provided
6    * that the following conditions are met:
7    *
8    * 1. Redistributions of source code must retain copyright
9    *    statements and notices.  Redistributions must also contain a
10   *    copy of this document.
11   *
12   * 2. Redistributions in binary form must reproduce the
13   *    above copyright notice, this list of conditions and the
14   *    following disclaimer in the documentation and/or other
15   *    materials provided with the distribution.
16   *
17   * 3. The name "JDBM" must not be used to endorse or promote
18   *    products derived from this Software without prior written
19   *    permission of Cees de Groot.  For written permission,
20   *    please contact cg@cdegroot.com.
21   *
22   * 4. Products derived from this Software may not be called "JDBM"
23   *    nor may "JDBM" appear in their names without prior written
24   *    permission of Cees de Groot.
25   *
26   * 5. Due credit should be given to the JDBM Project
27   *    (http://jdbm.sourceforge.net/).
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
33   * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40   * OF THE POSSIBILITY OF SUCH DAMAGE.
41   *
42   * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
43   * Contributions are Copyright (C) 2000 by their associated contributors.
44   *
45   * $Id: LogicalRowIdManager.java,v 1.3 2005/06/25 23:12:32 doomdark Exp $
46   */
47  
48  package jdbm.recman;
49  
50  import java.io.IOException;
51  
52  /**
53   *  This class manages the linked lists of logical rowid pages.
54   */
55  final class LogicalRowIdManager {
56      // our record file and associated page manager
57      private RecordFile file;
58      private PageManager pageman;
59      private FreeLogicalRowIdPageManager freeman;
60  
61      /**
62       *  Creates a log rowid manager using the indicated record file and
63       *  page manager
64       */
65      LogicalRowIdManager(RecordFile file, PageManager pageman)
66    throws IOException {
67    this.file = file;
68    this.pageman = pageman;
69    this.freeman = new FreeLogicalRowIdPageManager(file, pageman);
70  
71      }
72  
73      /**
74       *  Creates a new logical rowid pointing to the indicated physical
75       *  id
76       */
77      Location insert(Location loc)
78      throws IOException {
79    // check whether there's a free rowid to reuse
80    Location retval = freeman.get();
81    if (retval == null) {
82        // no. This means that we bootstrap things by allocating
83        // a new translation page and freeing all the rowids on it.
84        long firstPage = pageman.allocate(Magic.TRANSLATION_PAGE);
85        short curOffset = TranslationPage.O_TRANS;
86        for (int i = 0; i < TranslationPage.ELEMS_PER_PAGE; i++) {
87      freeman.put(new Location(firstPage, curOffset));
88      curOffset += PhysicalRowId.SIZE;
89        }
90        retval = freeman.get();
91        if (retval == null) {
92      throw new Error("couldn't obtain free translation");
93        }
94    }
95    // write the translation.
96    update(retval, loc);
97    return retval;
98      }
99  
100     /**
101      *  Releases the indicated logical rowid.
102      */
103     void delete(Location rowid)
104   throws IOException {
105 
106   freeman.put(rowid);
107     }
108 
109     /**
110      *  Updates the mapping
111      *
112      *  @param rowid The logical rowid
113      *  @param loc The physical rowid
114      */
115     void update(Location rowid, Location loc)
116     throws IOException {
117 
118         TranslationPage xlatPage = TranslationPage.getTranslationPageView(
119                                        file.get(rowid.getBlock()));
120         PhysicalRowId physid = xlatPage.get(rowid.getOffset());
121         physid.setBlock(loc.getBlock());
122         physid.setOffset(loc.getOffset());
123         file.release(rowid.getBlock(), true);
124     }
125 
126     /**
127      *  Returns a mapping
128      *
129      *  @param rowid The logical rowid
130      *  @return The physical rowid
131      */
132     Location fetch(Location rowid)
133     throws IOException {
134 
135         TranslationPage xlatPage = TranslationPage.getTranslationPageView(
136                                        file.get(rowid.getBlock()));
137         try {
138             Location retval = new Location(xlatPage.get(rowid.getOffset()));
139             return retval;
140         } finally {
141             file.release(rowid.getBlock(), false);
142         }
143     }
144 
145 }