| 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: PageManager.java,v 1.3 2005/06/25 23:12:32 doomdark Exp $ |
| 46 |
*/ |
| 47 |
package jdbm.recman; |
| 48 |
|
| 49 |
|
| 50 |
import java.io.IOException; |
| 51 |
|
| 52 |
import jdbm.I18n; |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* This class manages the linked lists of pages that make up a recordFile. |
| 57 |
*/ |
| 58 |
final class PageManager |
| 59 |
{ |
| 60 |
// our record recordFile |
| 61 |
private RecordFile recordFile; |
| 62 |
|
| 63 |
// header data |
| 64 |
private FileHeader header; |
| 65 |
|
| 66 |
// recordFile header containing block |
| 67 |
private BlockIo headerBuf; |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Creates a new page manager using the indicated record recordFile. |
| 72 |
*/ |
| 73 |
PageManager( RecordFile recordFile ) throws IOException |
| 74 |
{ |
| 75 |
this.recordFile = recordFile; |
| 76 |
|
| 77 |
// Note that we hold on to the recordFile header node. |
| 78 |
headerBuf = recordFile.get( 0 ); |
| 79 |
|
| 80 |
// Assume recordFile is new if the recordFile header's magic number is 0. |
| 81 |
if ( headerBuf.readShort( 0 ) == 0 ) |
| 82 |
{ |
| 83 |
header = new FileHeader( headerBuf, true ); |
| 84 |
} |
| 85 |
else |
| 86 |
// header is for existing recordFile |
| 87 |
{ |
| 88 |
header = new FileHeader( headerBuf, false ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Allocates a page of the indicated type. Returns recid of the page. |
| 95 |
*/ |
| 96 |
long allocate( short type ) throws IOException |
| 97 |
{ |
| 98 |
if ( type == Magic.FREE_PAGE ) |
| 99 |
{ |
| 100 |
throw new Error( I18n.err( I18n.ERR_548 ) ); |
| 101 |
} |
| 102 |
|
| 103 |
boolean isNew = false; |
| 104 |
|
| 105 |
// Do we have something on the free list? |
| 106 |
long retval = header.getFirstOf( Magic.FREE_PAGE ); |
| 107 |
if ( retval != 0 ) |
| 108 |
{ |
| 109 |
// yes. Point to it and make the next of that page the |
| 110 |
// new first free page. |
| 111 |
header.setFirstOf( Magic.FREE_PAGE, getNext( retval ) ); |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
// nope. make a new record |
| 116 |
retval = header.getLastOf( Magic.FREE_PAGE ); |
| 117 |
|
| 118 |
if ( retval == 0 ) |
| 119 |
{ |
| 120 |
// very new recordFile - allocate record #1 |
| 121 |
retval = 1; |
| 122 |
} |
| 123 |
|
| 124 |
header.setLastOf( Magic.FREE_PAGE, retval + 1 ); |
| 125 |
isNew = true; |
| 126 |
} |
| 127 |
|
| 128 |
// Cool. We have a record, add it to the correct list |
| 129 |
BlockIo buf = recordFile.get( retval ); |
| 130 |
PageHeader pageHdr = null; |
| 131 |
|
| 132 |
if ( isNew ) |
| 133 |
{ |
| 134 |
pageHdr = new PageHeader( buf, type ); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
pageHdr = PageHeader.getView( buf ); |
| 139 |
} |
| 140 |
|
| 141 |
long oldLast = header.getLastOf( type ); |
| 142 |
|
| 143 |
// Clean data. |
| 144 |
System.arraycopy( RecordFile.cleanData, 0, buf.getData(), 0, RecordFile.BLOCK_SIZE ); |
| 145 |
pageHdr.setType( type ); |
| 146 |
pageHdr.setPrev( oldLast ); |
| 147 |
pageHdr.setNext( 0 ); |
| 148 |
|
| 149 |
if ( oldLast == 0 ) |
| 150 |
{ |
| 151 |
// This was the first one of this type |
| 152 |
header.setFirstOf( type, retval ); |
| 153 |
} |
| 154 |
|
| 155 |
header.setLastOf( type, retval ); |
| 156 |
recordFile.release( retval, true ); |
| 157 |
|
| 158 |
// If there's a previous, fix up its pointer |
| 159 |
if ( oldLast != 0 ) |
| 160 |
{ |
| 161 |
buf = recordFile.get( oldLast ); |
| 162 |
pageHdr = PageHeader.getView( buf ); |
| 163 |
pageHdr.setNext( retval ); |
| 164 |
recordFile.release( oldLast, true ); |
| 165 |
} |
| 166 |
|
| 167 |
// remove the view, we have modified the type. |
| 168 |
buf.setView( null ); |
| 169 |
|
| 170 |
return retval; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Frees a page of the indicated type. |
| 176 |
*/ |
| 177 |
void free( short type, long recid ) throws IOException |
| 178 |
{ |
| 179 |
if ( type == Magic.FREE_PAGE ) |
| 180 |
{ |
| 181 |
throw new Error( I18n.err( I18n.ERR_549 ) ); |
| 182 |
} |
| 183 |
|
| 184 |
if ( recid == 0 ) |
| 185 |
{ |
| 186 |
throw new Error( I18n.err( I18n.ERR_550 ) ); |
| 187 |
} |
| 188 |
|
| 189 |
// get the page and read next and previous pointers |
| 190 |
BlockIo buf = recordFile.get( recid ); |
| 191 |
PageHeader pageHdr = PageHeader.getView( buf ); |
| 192 |
long prev = pageHdr.getPrev(); |
| 193 |
long next = pageHdr.getNext(); |
| 194 |
|
| 195 |
// put the page at the front of the free list. |
| 196 |
pageHdr.setType( Magic.FREE_PAGE ); |
| 197 |
pageHdr.setNext( header.getFirstOf( Magic.FREE_PAGE ) ); |
| 198 |
pageHdr.setPrev( 0 ); |
| 199 |
|
| 200 |
header.setFirstOf( Magic.FREE_PAGE, recid ); |
| 201 |
recordFile.release( recid, true ); |
| 202 |
|
| 203 |
// remove the page from its old list |
| 204 |
if ( prev != 0 ) |
| 205 |
{ |
| 206 |
buf = recordFile.get( prev ); |
| 207 |
pageHdr = PageHeader.getView( buf ); |
| 208 |
pageHdr.setNext( next ); |
| 209 |
recordFile.release( prev, true ); |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
header.setFirstOf( type, next ); |
| 214 |
} |
| 215 |
|
| 216 |
if ( next != 0 ) |
| 217 |
{ |
| 218 |
buf = recordFile.get( next ); |
| 219 |
pageHdr = PageHeader.getView( buf ); |
| 220 |
pageHdr.setPrev( prev ); |
| 221 |
recordFile.release( next, true ); |
| 222 |
} |
| 223 |
else |
| 224 |
{ |
| 225 |
header.setLastOf( type, prev ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/** |
| 231 |
* Returns the page following the indicated block |
| 232 |
*/ |
| 233 |
long getNext( long block ) throws IOException |
| 234 |
{ |
| 235 |
try |
| 236 |
{ |
| 237 |
return PageHeader.getView( recordFile.get( block ) ).getNext(); |
| 238 |
} |
| 239 |
finally |
| 240 |
{ |
| 241 |
recordFile.release( block, false ); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the page before the indicated block |
| 248 |
*/ |
| 249 |
long getPrev( long block ) throws IOException |
| 250 |
{ |
| 251 |
try |
| 252 |
{ |
| 253 |
return PageHeader.getView( recordFile.get( block ) ).getPrev(); |
| 254 |
} |
| 255 |
finally |
| 256 |
{ |
| 257 |
recordFile.release( block, false ); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Returns the first page on the indicated list. |
| 264 |
*/ |
| 265 |
long getFirst( short type ) throws IOException |
| 266 |
{ |
| 267 |
return header.getFirstOf( type ); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* Returns the last page on the indicated list. |
| 273 |
*/ |
| 274 |
long getLast( short type ) throws IOException |
| 275 |
{ |
| 276 |
return header.getLastOf( type ); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
/** |
| 281 |
* Commit all pending (in-memory) data by flushing the page manager. |
| 282 |
* This forces a flush of all outstanding blocks (this is an implicit |
| 283 |
* {@link RecordFile#commit} as well). |
| 284 |
*/ |
| 285 |
void commit() throws IOException |
| 286 |
{ |
| 287 |
// write the header out |
| 288 |
recordFile.release( headerBuf ); |
| 289 |
recordFile.commit(); |
| 290 |
|
| 291 |
// and obtain it again |
| 292 |
headerBuf = recordFile.get( 0 ); |
| 293 |
header = new FileHeader( headerBuf, false ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* Flushes the page manager. This forces a flush of all outstanding |
| 299 |
* blocks (this is an implicit {@link RecordFile#commit} as well). |
| 300 |
* |
| 301 |
* @TODO completely wrong description of method |
| 302 |
*/ |
| 303 |
void rollback() throws IOException |
| 304 |
{ |
| 305 |
// release header |
| 306 |
recordFile.discard( headerBuf ); |
| 307 |
recordFile.rollback(); |
| 308 |
// and obtain it again |
| 309 |
headerBuf = recordFile.get( 0 ); |
| 310 |
|
| 311 |
if ( headerBuf.readShort( 0 ) == 0 ) |
| 312 |
{ |
| 313 |
header = new FileHeader( headerBuf, true ); |
| 314 |
} |
| 315 |
else |
| 316 |
{ |
| 317 |
header = new FileHeader( headerBuf, false ); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
/** |
| 323 |
* Closes the page manager. This flushes the page manager and releases |
| 324 |
* the lock on the header. |
| 325 |
*/ |
| 326 |
void close() throws IOException |
| 327 |
{ |
| 328 |
recordFile.release( headerBuf ); |
| 329 |
recordFile.commit(); |
| 330 |
headerBuf = null; |
| 331 |
header = null; |
| 332 |
recordFile = null; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
/** |
| 337 |
* Returns the recordFile header. |
| 338 |
*/ |
| 339 |
FileHeader getFileHeader() |
| 340 |
{ |
| 341 |
return header; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
RecordFile getRecordFile() |
| 346 |
{ |
| 347 |
return recordFile; |
| 348 |
} |
| 349 |
} |