Debian Codegraph @

jdbm1/src/main/java/jdbm/recman/FreePhysicalRowIdPageManager.java

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