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: FreePhysicalRowIdPageManager.java,v 1.2 2001/11/17 16:14:25 boisvert Exp $
46   */
47  
48  package jdbm.recman;
49  
50  import java.io.IOException;
51  
52  /**
53   *  This class manages free physical rowid pages and provides methods
54   *  to free and allocate physical rowids on a high level.
55   */
56  final class FreePhysicalRowIdPageManager
57  {
58      // our record file
59      protected RecordFile _file;
60  
61      // our page manager
62      protected PageManager _pageman;
63  
64      /**
65       *  Creates a new instance using the indicated record file and
66       *  page manager.
67       */
68      FreePhysicalRowIdPageManager( RecordFile file, PageManager pageman )
69          throws IOException
70      {
71          _file = file;
72          _pageman = pageman;
73      }
74  
75  
76      /**
77       *  Returns a free physical rowid of the indicated size, or
78       *  null if nothing was found.
79       */
80      Location get( int size )
81          throws IOException
82      {
83          // Loop through the free physical rowid list until we find
84          // a rowid that's large enough.
85          Location retval = null;
86          PageCursor curs = new PageCursor( _pageman, Magic.FREEPHYSIDS_PAGE );
87  
88          while (curs.next() != 0) {
89              FreePhysicalRowIdPage fp = FreePhysicalRowIdPage
90                  .getFreePhysicalRowIdPageView( _file.get( curs.getCurrent() ) );
91              int slot = fp.getFirstLargerThan( size );
92              if ( slot != -1 ) {
93                  // got one!
94                  retval = new Location( fp.get( slot ) );
95  
96                  int slotsize = fp.get( slot ).getSize();
97                  fp.free( slot );
98                  if ( fp.getCount() == 0 ) {
99                      // page became empty - free it
100                     _file.release( curs.getCurrent(), false );
101                     _pageman.free( Magic.FREEPHYSIDS_PAGE, curs.getCurrent() );
102                 } else {
103                     _file.release( curs.getCurrent(), true );
104                 }
105 
106                 return retval;
107             } else {
108                 // no luck, go to next page
109                 _file.release( curs.getCurrent(), false );
110             }
111 
112         }
113         return null;
114     }
115 
116     /**
117      *  Puts the indicated rowid on the free list
118      */
119     void put(Location rowid, int size)
120   throws IOException {
121 
122   FreePhysicalRowId free = null;
123   PageCursor curs = new PageCursor(_pageman, Magic.FREEPHYSIDS_PAGE);
124   long freePage = 0;
125   while (curs.next() != 0) {
126       freePage = curs.getCurrent();
127       BlockIo curBlock = _file.get(freePage);
128       FreePhysicalRowIdPage fp = FreePhysicalRowIdPage
129     .getFreePhysicalRowIdPageView(curBlock);
130       int slot = fp.getFirstFree();
131       if (slot != -1) {
132     free = fp.alloc(slot);
133     break;
134       }
135 
136       _file.release(curBlock);
137   }
138   if (free == null) {
139       // No more space on the free list, add a page.
140       freePage = _pageman.allocate(Magic.FREEPHYSIDS_PAGE);
141       BlockIo curBlock = _file.get(freePage);
142       FreePhysicalRowIdPage fp =
143     FreePhysicalRowIdPage.getFreePhysicalRowIdPageView(curBlock);
144       free = fp.alloc(0);
145   }
146 
147   free.setBlock(rowid.getBlock());
148   free.setOffset(rowid.getOffset());
149   free.setSize(size);
150   _file.release(freePage, true);
151     }
152 }