KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > index > hash > HashPage


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
4  * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
5  * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  */

14
15 package org.apache.activemq.kaha.impl.index.hash;
16
17 import java.io.DataInput JavaDoc;
18 import java.io.DataOutput JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24 import org.apache.activemq.kaha.Marshaller;
25
26 /**
27  * A Page within a HashPage
28  *
29  * @version $Revision: 1.1.1.1 $
30  */

31 class HashPage{
32
33     static final int PAGE_HEADER_SIZE=17;
34
35     
36    
37     private int maximumEntries;
38     private long id;
39     private int binId;
40     private int persistedSize = 0;
41     private List JavaDoc<HashEntry> hashIndexEntries;
42     /*
43      * for persistence only
44      */

45     private long nextFreePageId=HashEntry.NOT_SET;
46     private boolean active=true;
47
48     /**
49      * Constructor
50      *
51      * @param hashIndex
52      * @param id
53      * @param parentId
54      * @param maximumEntries
55      */

56     HashPage(long id,int maximumEntries){
57         this(maximumEntries);
58        
59         this.id=id;
60     }
61
62     /**
63      * Constructor
64      *
65      * @param maximumEntries
66      */

67     public HashPage(int maximumEntries){
68         this.maximumEntries=maximumEntries;
69         this.hashIndexEntries=new ArrayList JavaDoc<HashEntry>(maximumEntries);
70     }
71
72     public String JavaDoc toString(){
73         return "HashPage["+getId()+":" + binId + "]";
74     }
75
76     public boolean equals(Object JavaDoc o){
77         boolean result=false;
78         if(o instanceof HashPage){
79             HashPage other=(HashPage)o;
80             result=other.id==id;
81         }
82         return result;
83     }
84
85     public int hashCode(){
86         return (int)id;
87     }
88
89     boolean isActive(){
90         return this.active;
91     }
92
93     void setActive(boolean active){
94         this.active=active;
95     }
96
97     long getNextFreePageId(){
98         return this.nextFreePageId;
99     }
100
101     void setNextFreePageId(long nextPageId){
102         this.nextFreePageId=nextPageId;
103     }
104
105     long getId(){
106         return id;
107     }
108
109     void setId(long id){
110         this.id=id;
111     }
112     
113     int getPersistedSize() {
114         return persistedSize;
115     }
116
117     void write(Marshaller keyMarshaller,DataOutput JavaDoc dataOut) throws IOException JavaDoc{
118         writeHeader(dataOut);
119         dataOut.writeInt(hashIndexEntries.size());
120         for(HashEntry entry:hashIndexEntries){
121             entry.write(keyMarshaller,dataOut);
122         }
123     }
124
125     void read(Marshaller keyMarshaller,DataInput JavaDoc dataIn) throws IOException JavaDoc{
126         readHeader(dataIn);
127         int size=dataIn.readInt();
128         hashIndexEntries.clear();
129         for(int i=0;i<size;i++){
130             HashEntry entry=new HashEntry();
131             entry.read(keyMarshaller,dataIn);
132             hashIndexEntries.add(entry);
133         }
134     }
135
136     void readHeader(DataInput JavaDoc dataIn) throws IOException JavaDoc{
137         active=dataIn.readBoolean();
138         nextFreePageId=dataIn.readLong();
139         binId=dataIn.readInt();
140         persistedSize=dataIn.readInt();
141     }
142
143     void writeHeader(DataOutput JavaDoc dataOut) throws IOException JavaDoc{
144         dataOut.writeBoolean(isActive());
145         dataOut.writeLong(nextFreePageId);
146         dataOut.writeInt(binId);
147         dataOut.writeInt(size());
148     }
149
150     boolean isEmpty(){
151         return hashIndexEntries.isEmpty();
152     }
153
154     boolean isFull(){
155         return(hashIndexEntries.size()>=maximumEntries);
156     }
157
158     boolean isUnderflowed(){
159         return hashIndexEntries.size()<(maximumEntries/2);
160     }
161
162     boolean isOverflowed(){
163         return hashIndexEntries.size()>maximumEntries;
164     }
165
166     List JavaDoc<HashEntry> getEntries(){
167         return hashIndexEntries;
168     }
169
170     void setEntries(List JavaDoc<HashEntry> newEntries){
171         this.hashIndexEntries=newEntries;
172     }
173
174     int getMaximumEntries(){
175         return this.maximumEntries;
176     }
177
178     void setMaximumEntries(int maximumEntries){
179         this.maximumEntries=maximumEntries;
180     }
181
182     int size(){
183         return hashIndexEntries.size();
184     }
185
186     
187     void reset() throws IOException JavaDoc{
188         hashIndexEntries.clear();
189         setNextFreePageId(HashEntry.NOT_SET);
190     }
191
192         
193
194     void addHashEntry(int index,HashEntry entry) throws IOException JavaDoc{
195         //index = index >= 0 ? index : 0;
196
//index = (index == 0 || index< size()) ? index : size()-1;
197
hashIndexEntries.add(index,entry);
198     }
199     
200
201     HashEntry getHashEntry(int index){
202         HashEntry result=hashIndexEntries.get(index);
203         return result;
204     }
205
206     HashEntry removeHashEntry(int index) throws IOException JavaDoc{
207         HashEntry result=hashIndexEntries.remove(index);
208         return result;
209     }
210
211     void removeAllTreeEntries(List JavaDoc<HashEntry> c){
212         hashIndexEntries.removeAll(c);
213     }
214
215     List JavaDoc<HashEntry> getSubList(int from,int to){
216         return new ArrayList JavaDoc<HashEntry>(hashIndexEntries.subList(from,to));
217     }
218
219
220     
221     /**
222      * @return the binId
223      */

224     int getBinId(){
225         return this.binId;
226     }
227
228     
229     /**
230      * @param binId the binId to set
231      */

232     void setBinId(int binId){
233         this.binId=binId;
234     }
235     
236     
237     void dump() {
238         
239         String JavaDoc str = this + ": ";
240         for(HashEntry entry: hashIndexEntries) {
241             str += entry + ",";
242         }
243         System.out.println(str);
244     }
245
246     
247 }
248
Popular Tags