1 14 15 package org.apache.activemq.kaha.impl.index.hash; 16 17 import java.io.DataInput ; 18 import java.io.DataOutput ; 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.HashSet ; 22 import java.util.List ; 23 import java.util.Set ; 24 import org.apache.activemq.kaha.Marshaller; 25 26 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 <HashEntry> hashIndexEntries; 42 45 private long nextFreePageId=HashEntry.NOT_SET; 46 private boolean active=true; 47 48 56 HashPage(long id,int maximumEntries){ 57 this(maximumEntries); 58 59 this.id=id; 60 } 61 62 67 public HashPage(int maximumEntries){ 68 this.maximumEntries=maximumEntries; 69 this.hashIndexEntries=new ArrayList <HashEntry>(maximumEntries); 70 } 71 72 public String toString(){ 73 return "HashPage["+getId()+":" + binId + "]"; 74 } 75 76 public boolean equals(Object 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 dataOut) throws IOException { 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 dataIn) throws IOException { 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 dataIn) throws IOException { 137 active=dataIn.readBoolean(); 138 nextFreePageId=dataIn.readLong(); 139 binId=dataIn.readInt(); 140 persistedSize=dataIn.readInt(); 141 } 142 143 void writeHeader(DataOutput dataOut) throws IOException { 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 <HashEntry> getEntries(){ 167 return hashIndexEntries; 168 } 169 170 void setEntries(List <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 { 188 hashIndexEntries.clear(); 189 setNextFreePageId(HashEntry.NOT_SET); 190 } 191 192 193 194 void addHashEntry(int index,HashEntry entry) throws IOException { 195 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 { 207 HashEntry result=hashIndexEntries.remove(index); 208 return result; 209 } 210 211 void removeAllTreeEntries(List <HashEntry> c){ 212 hashIndexEntries.removeAll(c); 213 } 214 215 List <HashEntry> getSubList(int from,int to){ 216 return new ArrayList <HashEntry>(hashIndexEntries.subList(from,to)); 217 } 218 219 220 221 224 int getBinId(){ 225 return this.binId; 226 } 227 228 229 232 void setBinId(int binId){ 233 this.binId=binId; 234 } 235 236 237 void dump() { 238 239 String str = this + ": "; 240 for(HashEntry entry: hashIndexEntries) { 241 str += entry + ","; 242 } 243 System.out.println(str); 244 } 245 246 247 } 248 | Popular Tags |