|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 Evolved Binary Ltd |
| 3 | + * |
| 4 | + * Changes made by Evolved Binary are proprietary and are not Open Source. |
| 5 | + * |
| 6 | + * NOTE: Parts of this file contain code from The eXist-db Authors. |
| 7 | + * The original license header is included below. |
| 8 | + * |
| 9 | + * ---------------------------------------------------------------------------- |
| 10 | + * |
| 11 | + * NOTE: This file is in part based on code from The dbXML Group. |
| 12 | + * The original license statement is also included below. |
| 13 | + * |
| 14 | + * eXist-db Open Source Native XML Database |
| 15 | + * Copyright (C) 2001 The eXist-db Authors |
| 16 | + * |
| 17 | + * info@exist-db.org |
| 18 | + * http://www.exist-db.org |
| 19 | + * |
| 20 | + * This library is free software; you can redistribute it and/or |
| 21 | + * modify it under the terms of the GNU Lesser General Public |
| 22 | + * License as published by the Free Software Foundation; either |
| 23 | + * version 2.1 of the License, or (at your option) any later version. |
| 24 | + * |
| 25 | + * This library is distributed in the hope that it will be useful, |
| 26 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 28 | + * Lesser General Public License for more details. |
| 29 | + * |
| 30 | + * You should have received a copy of the GNU Lesser General Public |
| 31 | + * License along with this library; if not, write to the Free Software |
| 32 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 33 | + * |
| 34 | + * --------------------------------------------------------------------- |
| 35 | + * |
| 36 | + * dbXML License, Version 1.0 |
| 37 | + * |
| 38 | + * Copyright (c) 1999-2001 The dbXML Group, L.L.C. |
| 39 | + * All rights reserved. |
| 40 | + * |
| 41 | + * Redistribution and use in source and binary forms, with or without |
| 42 | + * modification, are permitted provided that the following conditions |
| 43 | + * are met: |
| 44 | + * |
| 45 | + * 1. Redistributions of source code must retain the above copyright |
| 46 | + * notice, this list of conditions and the following disclaimer. |
| 47 | + * |
| 48 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 49 | + * notice, this list of conditions and the following disclaimer in |
| 50 | + * the documentation and/or other materials provided with the |
| 51 | + * distribution. |
| 52 | + * |
| 53 | + * 3. The end-user documentation included with the redistribution, |
| 54 | + * if any, must include the following acknowledgment: |
| 55 | + * "This product includes software developed by |
| 56 | + * The dbXML Group (http://www.dbxml.com/)." |
| 57 | + * Alternately, this acknowledgment may appear in the software itself, |
| 58 | + * if and wherever such third-party acknowledgments normally appear. |
| 59 | + * |
| 60 | + * 4. The names "dbXML" and "The dbXML Group" must |
| 61 | + * not be used to endorse or promote products derived from this |
| 62 | + * software without prior written permission. For written |
| 63 | + * permission, please contact info@dbxml.com. |
| 64 | + * |
| 65 | + * 5. Products derived from this software may not be called "dbXML", |
| 66 | + * nor may "dbXML" appear in their name, without prior written |
| 67 | + * permission of The dbXML Group. |
| 68 | + * |
| 69 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 70 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 71 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 72 | + * DISCLAIMED. IN NO EVENT SHALL THE DBXML GROUP OR |
| 73 | + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 74 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 75 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 76 | + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 77 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 78 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 79 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 80 | + * SUCH DAMAGE. |
| 81 | + */ |
| 82 | +package org.exist.storage.btree; |
| 83 | + |
| 84 | +import org.exist.storage.journal.Lsn; |
| 85 | +import org.exist.util.ByteConversion; |
| 86 | + |
| 87 | +import java.io.IOException; |
| 88 | + |
| 89 | +/** |
| 90 | + * Base class for page headers. |
| 91 | + * |
| 92 | + * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a> |
| 93 | + * @author <a href="mailto:wolfgang@exist-db.org">Wolfgang Meier</a> |
| 94 | + */ |
| 95 | +public abstract class AbstractPageHeader implements PageHeader { |
| 96 | + |
| 97 | + public static final int LENGTH_PAGE_STATUS = 1; //sizeof byte |
| 98 | + public static final int LENGTH_PAGE_DATA_LENGTH = 4; //sizeof int |
| 99 | + public static final int LENGTH_PAGE_NEXT_PAGE = 8; //sizeof long |
| 100 | + public static final int LENGTH_PAGE_LSN = Lsn.RAW_LENGTH; |
| 101 | + |
| 102 | + private int dataLen = 0; |
| 103 | + private long nextPage = Page.NO_PAGE; |
| 104 | + private boolean dirty = false; |
| 105 | + |
| 106 | + /** |
| 107 | + * The status of the current page. |
| 108 | + */ |
| 109 | + private PageStatus status = PageStatus.UNUSED; |
| 110 | + |
| 111 | + private Lsn lsn = Lsn.LSN_INVALID; |
| 112 | + |
| 113 | + public AbstractPageHeader() { |
| 114 | + } |
| 115 | + |
| 116 | + public AbstractPageHeader(final byte[] data, final int offset) throws IOException { |
| 117 | + read(data, offset); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public int getDataLen() { |
| 122 | + return this.dataLen; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void updateDataLen(final int newDataLength) { |
| 127 | + this.dataLen = newDataLength; |
| 128 | + setDirty(true); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public long getNextPage() { |
| 133 | + return this.nextPage; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void updateNextPage(final long newNextPage) { |
| 138 | + this.nextPage = newNextPage; |
| 139 | + setDirty(true); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean isDirty() { |
| 144 | + return this.dirty; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void setDirty(final boolean dirty) { |
| 149 | + this.dirty = dirty; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public PageStatus getStatus() { |
| 154 | + return this.status; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void updateStatus(final PageStatus newStatus) { |
| 159 | + this.status = newStatus; |
| 160 | + setDirty(true); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public Lsn getLsn() { |
| 165 | + return this.lsn; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void setLsn(final Lsn lsn) { |
| 170 | + this.lsn = lsn; |
| 171 | + } |
| 172 | + |
| 173 | + public int read(final byte[] data, int offset) throws IOException { |
| 174 | + status = PageStatus.fromValue(data[offset]); |
| 175 | + offset += LENGTH_PAGE_STATUS; |
| 176 | + dataLen = ByteConversion.byteToInt(data, offset); |
| 177 | + offset += LENGTH_PAGE_DATA_LENGTH; |
| 178 | + nextPage = ByteConversion.byteToLong(data, offset); |
| 179 | + offset += LENGTH_PAGE_NEXT_PAGE; |
| 180 | + lsn = Lsn.read(data, offset); |
| 181 | + offset += LENGTH_PAGE_LSN; |
| 182 | + |
| 183 | + // TODO(AR) should we mark this an non-dirty? |
| 184 | + // setDirty(false); |
| 185 | + |
| 186 | + return offset; |
| 187 | + } |
| 188 | + |
| 189 | + public int write(final byte[] data, int offset) throws IOException { |
| 190 | + data[offset] = status.getValue(); |
| 191 | + offset += LENGTH_PAGE_STATUS; |
| 192 | + ByteConversion.intToByte(dataLen, data, offset); |
| 193 | + offset += LENGTH_PAGE_DATA_LENGTH; |
| 194 | + ByteConversion.longToByte(nextPage, data, offset); |
| 195 | + offset += LENGTH_PAGE_NEXT_PAGE; |
| 196 | + lsn.write(data, offset); |
| 197 | + offset += LENGTH_PAGE_LSN; |
| 198 | + setDirty(false); |
| 199 | + return offset; |
| 200 | + } |
| 201 | +} |
0 commit comments