001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.kahadb.page;
018
019 import java.io.DataInput;
020 import java.io.DataOutput;
021 import java.io.IOException;
022
023 /**
024 * A Page within a file.
025 */
026 public class Page<T> {
027
028 public static final int PAGE_HEADER_SIZE = 21;
029
030 public static final byte PAGE_FREE_TYPE = 0;
031 public static final byte PAGE_PART_TYPE = 1;
032 public static final byte PAGE_END_TYPE = 2;
033
034 long pageId;
035
036 // The following fields are persisted
037 byte type = PAGE_FREE_TYPE;
038 long txId;
039 // A field reserved to hold checksums.. Not in use (yet)
040 int checksum;
041
042 // Points to the next page in the chunk stream
043 long next;
044 T data;
045
046 public Page() {
047 }
048
049 public Page(long pageId) {
050 this.pageId=pageId;
051 }
052
053 public Page<T> copy(Page<T> other) {
054 this.pageId = other.pageId;
055 this.txId = other.txId;
056 this.type = other.type;
057 this.next = other.next;
058 this.data = other.data;
059 return this;
060 }
061
062 Page<T> copy() {
063 return new Page<T>().copy(this);
064 }
065
066 void makeFree(long txId) {
067 this.type = Page.PAGE_FREE_TYPE;
068 this.txId = txId;
069 this.data = null;
070 this.next = 0;
071 }
072
073 public void makePagePart(long next, long txId) {
074 this.type = Page.PAGE_PART_TYPE;
075 this.next = next;
076 this.txId = txId;
077 }
078
079 public void makePageEnd(long size, long txId) {
080 this.type = Page.PAGE_END_TYPE;
081 this.next = size;
082 this.txId = txId;
083 }
084
085 void write(DataOutput os) throws IOException {
086 os.writeByte(type);
087 os.writeLong(txId);
088 os.writeLong(next);
089 os.writeInt(checksum);
090 }
091
092 void read(DataInput is) throws IOException {
093 type = is.readByte();
094 txId = is.readLong();
095 next = is.readLong();
096 checksum = is.readInt();
097 }
098
099 public long getPageId() {
100 return pageId;
101 }
102
103 public long getTxId() {
104 return txId;
105 }
106
107 public T get() {
108 return data;
109 }
110
111 public void set(T data) {
112 this.data = data;
113 }
114
115 public short getType() {
116 return type;
117 }
118
119 public long getNext() {
120 return next;
121 }
122
123 public String toString() {
124 return "[Page:" + getPageId()+", type: "+type+"]";
125 }
126
127 public int getChecksum() {
128 return checksum;
129 }
130
131 public void setChecksum(int checksum) {
132 this.checksum = checksum;
133 }
134 }