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    import java.util.ArrayList;
023    import java.util.Iterator;
024    import java.util.List;
025    
026    import org.apache.kahadb.util.ByteSequence;
027    import org.apache.kahadb.util.DataByteArrayInputStream;
028    import org.apache.kahadb.util.DataByteArrayOutputStream;
029    import org.apache.kahadb.util.Marshaller;
030    
031    /**
032     * A Page within a file.
033     * 
034     * 
035     */
036    public class Page<T> {
037    
038        public static final int PAGE_HEADER_SIZE = 21;
039    
040        public static final byte PAGE_FREE_TYPE = 0;
041        public static final byte PAGE_PART_TYPE = 1;
042        public static final byte PAGE_END_TYPE = 2;
043    
044        long pageId;
045    
046        // The following fields are persisted
047        byte type = PAGE_FREE_TYPE;
048        long txId;
049        // A field reserved to hold checksums..  Not in use (yet)
050        int checksum;
051        
052        // Points to the next page in the chunk stream
053        long next;
054        T data;
055    
056        public Page() {
057        }
058    
059        public Page(long pageId) {
060            this.pageId=pageId;
061        }
062    
063        public void copy(Page<T> other) {
064            this.pageId = other.pageId;
065            this.txId = other.txId;
066            this.type = other.type;
067            this.next = other.next;
068            this.data = other.data;
069        }
070    
071        Page<T> copy() {
072            Page<T> rc = new Page<T>();
073            rc.copy(this);
074            return rc;
075        }
076    
077        void makeFree(long txId) {
078            this.type = Page.PAGE_FREE_TYPE;
079            this.txId = txId;
080            this.data = null;
081            this.next = 0;
082        }
083        
084        public void makePagePart(long next, long txId) {
085            this.type = Page.PAGE_PART_TYPE;
086            this.next = next;
087            this.txId = txId;
088        }
089        
090        public void makePageEnd(long size, long txId) {
091            this.type = Page.PAGE_END_TYPE;
092            this.next = size;
093            this.txId = txId;
094        }
095    
096        void write(DataOutput os) throws IOException {
097            os.writeByte(type);
098            os.writeLong(txId);
099            os.writeLong(next);
100            os.writeInt(checksum);
101        }
102    
103        void read(DataInput is) throws IOException {
104            type = is.readByte();
105            txId = is.readLong();
106            next = is.readLong();
107            checksum = is.readInt();
108        }
109    
110        public long getPageId() {
111            return pageId;
112        }
113    
114        public long getTxId() {
115            return txId;
116        }
117    
118        public T get() {
119            return data;
120        }
121    
122        public void set(T data) {
123            this.data = data;
124        }
125    
126        public short getType() {
127            return type;
128        }
129    
130        public long getNext() {
131            return next;
132        }
133    
134        public String toString() {
135            return "[Page:" + getPageId()+", type: "+type+"]";
136        }
137    
138        public int getChecksum() {
139            return checksum;
140        }
141    
142        public void setChecksum(int checksum) {
143            this.checksum = checksum;
144        }
145    
146    
147    }