| 1 7 package com.jofti.store; 8 9 import java.nio.ByteBuffer ; 10 import java.util.Arrays ; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 import com.jofti.btree.IPage; 16 import com.jofti.btree.LeafNodeEntry; 17 import com.jofti.core.IStoreManager; 18 import com.jofti.exception.JoftiException; 19 20 24 public class Page implements Cloneable , IPage{ 25 26 private static Log log = LogFactory.getLog(Page.class); 27 28 public int[] pointers =null; 29 public ByteBuffer buf = null; 30 IStoreManager manager =null; 31 IEntrySerializer serializer=null; 32 int bufSize =0; 33 34 int reference =0; 35 36 public Page(int[] pointers, ByteBuffer buf, IStoreManager manager, IEntrySerializer serializer){ 37 this.pointers = pointers; 38 this.buf =buf; 39 bufSize = buf.capacity(); 40 this.manager = manager; 41 this.serializer = serializer; 42 } 43 44 45 46 public LeafNodeEntry getEntry(int position){ 47 LeafNodeEntry obj =null; 48 49 50 51 52 if (position >= pointers.length){ 53 log.warn("error in page for position "+ position); 54 return null; 55 }else{ 56 int pos = pointers[position]; 57 if (pos != -1){ 58 ByteBuffer tempBuffer = buf.duplicate(); 59 tempBuffer.position(pos); 60 tempBuffer.getInt(); 61 try{ 62 obj= serializer.convertFromStorage(tempBuffer); 63 } catch (Throwable e){ 64 e.printStackTrace(); 65 throw new RuntimeException (e); 66 } 67 }else{ 69 log.warn("expected entry in page found -1 at pos "+ position); 70 71 for (int i=0;i<position;i++){ 72 log.warn(i + "["+pointers[i] + "],"); 73 } 74 throw new RuntimeException ("expected entry found -1 "+ position +" "+ this); 75 } 76 if (obj ==null){ 77 log.warn("returning null for "+ position + " on buf "+ buf); 78 } 79 return obj; 80 } 81 } 82 83 public void setEntry(int position, LeafNodeEntry entry){ 84 85 byte[] bytes = null; 86 try { 87 bytes = serializer.convertForStorage(entry); 88 } catch(JoftiException e){ 89 throw new RuntimeException ("unable to convert entry to bytes "+ entry,e); 90 } 91 if (buf.capacity() < buf.limit() + (bytes.length+4)){ 93 ByteBuffer temp = ByteBuffer.allocate((int)(buf.capacity() *1.5)); 95 buf.position(0); 96 temp.put(buf); 97 temp.flip(); 98 ByteBuffer oldBuf =buf; 99 buf = temp; 101 log.info("new buffer assigned in page "+ buf + " " + oldBuf); 102 } 103 104 105 106 107 int pos = pointers[position]; 109 110 111 112 int length = 4 + bytes.length; 114 115 if (pos == -1 ){ 117 if (position ==0){ 119 pos =0; 120 }else{ 121 pos = buf.limit(); 122 } 123 buf.limit(buf.limit()+length); 126 buf.position(pos); 127 buf.putInt(bytes.length); 128 buf.put(bytes); 129 131 }else{ 132 buf.position(pos); 134 buf.mark(); 135 ByteBuffer copy = buf.duplicate(); 137 buf.limit(buf.capacity()); 139 buf.position(buf.position() + length); 141 try { 143 buf.put(copy); 144 } catch (Throwable t){ 145 log.warn("inserting failure for " + buf,t); 146 throw new RuntimeException ("inserting failure for " + buf,t); 147 } 148 buf.limit(buf.position()); 150 151 152 buf.reset(); 154 155 buf.putInt(bytes.length); 157 buf.put(bytes); 158 } 159 160 if (pointers[position] != -1){ 162 System.arraycopy(pointers,position,pointers,position+1,(pointers.length-1) -position); 163 164 for (int i=position+1;i<pointers.length && pointers[i]!=-1;i++){ 165 pointers[i]=pointers[i]+ length; 166 } 167 } 168 pointers[position]=pos; 169 170 171 172 } 173 174 175 176 177 178 public void removeEntry(int position){ 179 180 if (position >= pointers.length || pointers[position] ==-1){ 181 return; 182 }else{ 183 184 int pos = pointers[position]; 185 int overWriteStart =0; 187 if (position != pointers.length-1){ 189 overWriteStart = pointers[position+1]; 190 } 191 if (overWriteStart ==-1 || position == pointers.length-1){ 192 buf.position(pos); 194 buf.limit(pos); 195 pointers[position]=-1; 196 }else{ 197 buf.position(pos); 199 200 buf.mark(); 202 ByteBuffer temp = buf.duplicate(); 204 int length = temp.getInt()+4; 206 temp.position(overWriteStart); 208 209 buf.reset(); 211 try { 213 buf.put(temp); 214 } catch (Exception e){ 215 log.warn("problem moving buffer posoition ",e ); 216 217 } 218 219 buf.limit(buf.position()); 221 222 223 224 int start =position+1; 226 int arrayLength = (pointers.length) -(position+1); 227 System.arraycopy(pointers,start,pointers,position,arrayLength); 228 229 231 pointers[pointers.length-1]=-1; 232 233 234 235 for (int i=position;i<pointers.length && pointers[i]!=-1;i++){ 237 if (position == pointers.length-1 || pointers[i]==-1){ 238 pointers[i]=-1; 239 }else{ 240 pointers[i]=pointers[i]- length; 241 } 242 } 243 } 244 245 246 } 247 } 248 249 public void updateEntry(int location, LeafNodeEntry entry){ 250 removeEntry(location); 251 setEntry(location, entry); 252 } 253 254 255 256 public ByteBuffer copyBuffer(ByteBuffer newBuf){ 257 258 buf.rewind(); 259 newBuf.clear(); 260 newBuf.put(buf); 261 newBuf.flip(); 262 return newBuf; 263 } 264 265 public void reset(){ 266 Arrays.fill(pointers,-1); 267 268 buf.clear(); 269 buf.flip(); 270 271 } 272 273 274 275 278 public ByteBuffer getBuffer() { 279 280 return buf; 281 } 282 283 284 285 288 public int[] getPointers() { 289 return pointers; 290 } 291 292 293 294 297 public void setManager(IStoreManager manager) { 298 this.manager = manager; 299 300 } 301 302 } 303 304 | Popular Tags |