| 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: PageHeader.java,v 1.2 2003/09/21 15:47:01 boisvert Exp $ |
| 46 |
*/ |
| 47 |
package jdbm.recman; |
| 48 |
|
| 49 |
|
| 50 |
import java.io.IOException; |
| 51 |
|
| 52 |
import jdbm.I18n; |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* This class represents a page header. It is the common superclass for |
| 57 |
* all different page views. It contains the following information: |
| 58 |
* |
| 59 |
* <ol> |
| 60 |
* <li>2 bytes: the short block type code</li> |
| 61 |
* <li>8 bytes: the long block id of the next block in the block list</li> |
| 62 |
* <li>8 bytes: the long block id of the previous block in the block list</li> |
| 63 |
* </ol> |
| 64 |
* |
| 65 |
* The page header block view hence sees 18 bytes of page header data. |
| 66 |
*/ |
| 67 |
public class PageHeader implements BlockView |
| 68 |
{ |
| 69 |
// offsets into page header's (BlockIo's) buffer |
| 70 |
|
| 71 |
/** the page (BlockIo's type code) short magic code */ |
| 72 |
private static final short O_MAGIC = 0; |
| 73 |
/** the long block id of the next block in the block list */ |
| 74 |
private static final short O_NEXT = Magic.SZ_SHORT; |
| 75 |
/** the long block id of the previous block in the block list */ |
| 76 |
private static final short O_PREV = O_NEXT + Magic.SZ_LONG; |
| 77 |
|
| 78 |
/** the size of this page header = 18 bytes */ |
| 79 |
protected static final short SIZE = O_PREV + Magic.SZ_LONG; |
| 80 |
|
| 81 |
/** the page header block this view is associated with */ |
| 82 |
protected BlockIo block; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Constructs a PageHeader object from a block |
| 87 |
* |
| 88 |
* @param block The block that contains the page header |
| 89 |
* @throws IOException if the block is too short to keep the page header. |
| 90 |
*/ |
| 91 |
protected PageHeader( BlockIo block ) |
| 92 |
{ |
| 93 |
this.block = block; |
| 94 |
block.setView( this ); |
| 95 |
if ( !magicOk() ) |
| 96 |
{ |
| 97 |
throw new Error( I18n.err( I18n.ERR_546, block.getBlockId(), getMagic() ) ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Constructs a new PageHeader of the indicated type. Used for newly |
| 104 |
* created pages. |
| 105 |
*/ |
| 106 |
PageHeader( BlockIo block, short type ) |
| 107 |
{ |
| 108 |
this.block = block; |
| 109 |
block.setView( this ); |
| 110 |
setType( type ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Factory method to create or return a page header for the indicated block. |
| 116 |
*/ |
| 117 |
static PageHeader getView( BlockIo block ) |
| 118 |
{ |
| 119 |
BlockView view = block.getView(); |
| 120 |
if ( view != null && view instanceof PageHeader ) |
| 121 |
{ |
| 122 |
return ( PageHeader ) view; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
return new PageHeader( block ); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Returns true if the magic corresponds with the fileHeader magic. |
| 133 |
*/ |
| 134 |
private boolean magicOk() |
| 135 |
{ |
| 136 |
int magic = getMagic(); |
| 137 |
return magic >= Magic.BLOCK |
| 138 |
&& magic <= ( Magic.BLOCK + Magic.FREEPHYSIDS_PAGE ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* For paranoia mode |
| 144 |
*/ |
| 145 |
protected void paranoiaMagicOk() |
| 146 |
{ |
| 147 |
if ( !magicOk() ) |
| 148 |
{ |
| 149 |
throw new Error( I18n.err( I18n.ERR_547, getMagic() ) ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
/** Returns the magic code */ |
| 155 |
short getMagic() |
| 156 |
{ |
| 157 |
return block.readShort( O_MAGIC ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/** Returns the next block. */ |
| 162 |
long getNext() |
| 163 |
{ |
| 164 |
paranoiaMagicOk(); |
| 165 |
return block.readLong( O_NEXT ); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** Sets the next block. */ |
| 170 |
void setNext( long next ) |
| 171 |
{ |
| 172 |
paranoiaMagicOk(); |
| 173 |
block.writeLong( O_NEXT, next ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
/** Returns the previous block. */ |
| 178 |
long getPrev() |
| 179 |
{ |
| 180 |
paranoiaMagicOk(); |
| 181 |
return block.readLong( O_PREV ); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
/** Sets the previous block. */ |
| 186 |
void setPrev( long prev ) |
| 187 |
{ |
| 188 |
paranoiaMagicOk(); |
| 189 |
block.writeLong( O_PREV, prev ); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
/** Sets the type of the page header */ |
| 194 |
void setType( short type ) |
| 195 |
{ |
| 196 |
block.writeShort( O_MAGIC, ( short ) ( Magic.BLOCK + type ) ); |
| 197 |
} |
| 198 |
} |