001 /**
002 * JDBM LICENSE v1.00
003 *
004 * Redistribution and use of this software and associated documentation
005 * ("Software"), with or without modification, are permitted provided
006 * that the following conditions are met:
007 *
008 * 1. Redistributions of source code must retain copyright
009 * statements and notices. Redistributions must also contain a
010 * copy of this document.
011 *
012 * 2. Redistributions in binary form must reproduce the
013 * above copyright notice, this list of conditions and the
014 * following disclaimer in the documentation and/or other
015 * materials provided with the distribution.
016 *
017 * 3. The name "JDBM" must not be used to endorse or promote
018 * products derived from this Software without prior written
019 * permission of Cees de Groot. For written permission,
020 * please contact cg@cdegroot.com.
021 *
022 * 4. Products derived from this Software may not be called "JDBM"
023 * nor may "JDBM" appear in their names without prior written
024 * permission of Cees de Groot.
025 *
026 * 5. Due credit should be given to the JDBM Project
027 * (http://jdbm.sourceforge.net/).
028 *
029 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
030 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
031 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
032 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
033 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
034 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
035 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
036 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
037 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
038 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
040 * OF THE POSSIBILITY OF SUCH DAMAGE.
041 *
042 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
043 * Contributions are Copyright (C) 2000 by their associated contributors.
044 *
045 * $Id: PageHeader.java,v 1.2 2003/09/21 15:47:01 boisvert Exp $
046 */
047 package jdbm.recman;
048
049
050 import org.apache.directory.server.i18n.I18n;
051
052
053 /**
054 * This class represents a page header. It is the common superclass for
055 * all different page views. It contains the following information:
056 *
057 * <ol>
058 * <li>2 bytes: the short block type code</li>
059 * <li>8 bytes: the long block id of the next block in the block list</li>
060 * <li>8 bytes: the long block id of the previous block in the block list</li>
061 * </ol>
062 *
063 * The page header block view hence sees 18 bytes of page header data.
064 */
065 public class PageHeader implements BlockView
066 {
067 // offsets into page header's (BlockIo's) buffer
068
069 /** the page (BlockIo's type code) short magic code */
070 private static final short O_MAGIC = 0;
071 /** the long block id of the next block in the block list */
072 private static final short O_NEXT = Magic.SZ_SHORT;
073 /** the long block id of the previous block in the block list */
074 private static final short O_PREV = O_NEXT + Magic.SZ_LONG;
075
076 /** the size of this page header = 18 bytes */
077 protected static final short SIZE = O_PREV + Magic.SZ_LONG;
078
079 /** the page header block this view is associated with */
080 protected BlockIo block;
081
082
083 /**
084 * Constructs a PageHeader object from a block
085 *
086 * @param block The block that contains the page header
087 * @throws IOException if the block is too short to keep the page header.
088 */
089 protected PageHeader( BlockIo block )
090 {
091 this.block = block;
092 block.setView( this );
093 if ( ! magicOk() )
094 {
095 throw new Error( I18n.err( I18n.ERR_546, block.getBlockId(), getMagic() ) );
096 }
097 }
098
099
100 /**
101 * Constructs a new PageHeader of the indicated type. Used for newly
102 * created pages.
103 */
104 PageHeader( BlockIo block, short type )
105 {
106 this.block = block;
107 block.setView( this );
108 setType( type );
109 }
110
111
112 /**
113 * Factory method to create or return a page header for the indicated block.
114 */
115 static PageHeader getView ( BlockIo block )
116 {
117 BlockView view = block.getView();
118 if ( view != null && view instanceof PageHeader )
119 {
120 return ( PageHeader ) view;
121 }
122 else
123 {
124 return new PageHeader( block );
125 }
126 }
127
128
129 /**
130 * Returns true if the magic corresponds with the fileHeader magic.
131 */
132 private boolean magicOk()
133 {
134 int magic = getMagic();
135 return magic >= Magic.BLOCK
136 && magic <= ( Magic.BLOCK + Magic.FREEPHYSIDS_PAGE );
137 }
138
139
140 /**
141 * For paranoia mode
142 */
143 protected void paranoiaMagicOk()
144 {
145 if ( ! magicOk() )
146 {
147 throw new Error( I18n.err( I18n.ERR_547, getMagic() ) );
148 }
149 }
150
151
152 /** Returns the magic code */
153 short getMagic()
154 {
155 return block.readShort( O_MAGIC );
156 }
157
158
159 /** Returns the next block. */
160 long getNext()
161 {
162 paranoiaMagicOk();
163 return block.readLong( O_NEXT );
164 }
165
166
167 /** Sets the next block. */
168 void setNext( long next )
169 {
170 paranoiaMagicOk();
171 block.writeLong( O_NEXT, next );
172 }
173
174
175 /** Returns the previous block. */
176 long getPrev()
177 {
178 paranoiaMagicOk();
179 return block.readLong( O_PREV );
180 }
181
182
183 /** Sets the previous block. */
184 void setPrev( long prev )
185 {
186 paranoiaMagicOk();
187 block.writeLong( O_PREV, prev );
188 }
189
190
191 /** Sets the type of the page header */
192 void setType ( short type )
193 {
194 block.writeShort( O_MAGIC, ( short ) ( Magic.BLOCK + type ) );
195 }
196 }