1 18 package org.apache.activemq.kaha.impl.index; 19 20 import java.io.IOException ; 21 import org.apache.activemq.kaha.StoreEntry; 22 27 public class DiskIndexLinkedList implements IndexLinkedList{ 28 protected IndexManager indexManager; 29 protected transient IndexItem root; 30 protected transient IndexItem last; 31 protected transient int size=0; 32 33 36 public DiskIndexLinkedList(IndexManager im,IndexItem header){ 37 this.indexManager=im; 38 this.root=header; 39 } 40 41 public synchronized IndexItem getRoot(){ 42 return root; 43 } 44 45 void setRoot(IndexItem e){ 46 this.root=e; 47 } 48 49 54 public synchronized IndexItem getFirst(){ 55 if(size==0) 56 return null; 57 return getNextEntry(root); 58 } 59 60 65 public synchronized IndexItem getLast(){ 66 if(size==0) 67 return null; 68 if(last!=null){ 69 last.next=null; 70 last.setNextItem(IndexItem.POSITION_NOT_SET); 71 } 72 return last; 73 } 74 75 80 public synchronized StoreEntry removeFirst(){ 81 if(size==0){ 82 return null; 83 } 84 IndexItem result=getNextEntry(root); 85 remove(result); 86 return result; 87 } 88 89 94 public synchronized Object removeLast(){ 95 if(size==0) 96 return null; 97 StoreEntry result=last; 98 remove(last); 99 return result; 100 } 101 102 107 public synchronized void addFirst(IndexItem item){ 108 if(size==0){ 109 last=item; 110 } 111 size++; 112 } 113 114 120 public synchronized void addLast(IndexItem item){ 121 size++; 122 last=item; 123 } 124 125 130 public synchronized int size(){ 131 return size; 132 } 133 134 139 public synchronized boolean isEmpty(){ 140 return size==0; 141 } 142 143 149 public synchronized boolean add(IndexItem item){ 150 addLast(item); 151 return true; 152 } 153 154 157 public synchronized void clear(){ 158 last=null; 159 size=0; 160 } 161 162 171 public synchronized IndexItem get(int index){ 172 return entry(index); 173 } 174 175 184 public synchronized void add(int index,IndexItem element){ 185 if(index==size-1){ 186 last=element; 187 } 188 size++; 189 } 190 191 200 public synchronized Object remove(int index){ 201 IndexItem e=entry(index); 202 remove(e); 203 return e; 204 } 205 206 209 private IndexItem entry(int index){ 210 if(index<0||index>=size) 211 throw new IndexOutOfBoundsException ("Index: "+index+", Size: "+size); 212 IndexItem e=root; 213 214 for(int i=0;i<=index;i++) 215 e=getNextEntry(e); 216 if(e != null &&last!=null && last.equals(e)){ 217 last = e; 218 } 219 return e; 220 } 221 222 232 public synchronized int indexOf(StoreEntry o){ 233 int index=0; 234 if(size>0){ 235 for(IndexItem e=getNextEntry(root);e!=null;e=getNextEntry(e)){ 236 if(o.equals(e)){ 237 return index; 238 } 239 index++; 240 } 241 } 242 return -1; 243 } 244 245 251 public synchronized IndexItem getNextEntry(IndexItem current){ 252 IndexItem result=null; 253 if(current!=null&¤t.getNextItem()>=0){ 254 try{ 255 result=indexManager.getIndex(current.getNextItem()); 256 }catch(IOException e){ 257 throw new RuntimeException ("Failed to get next index from " + indexManager + " for " + current,e); 258 } 259 } 260 if(result != null &&last!=null && last.equals(result)){ 262 result = last; 263 } 264 return result; 265 } 266 267 273 public synchronized IndexItem getPrevEntry(IndexItem current){ 274 IndexItem result=null; 275 if(current!=null&¤t.getPreviousItem()>=0){ 276 try{ 277 result=indexManager.getIndex(current.getPreviousItem()); 278 }catch(IOException e){ 279 throw new RuntimeException ("Failed to get current index for "+current,e); 280 } 281 } 282 if(result!=null&&root!=null&&root.equals(result)){ 284 return root; 285 } 286 return result; 287 } 288 289 public synchronized StoreEntry getEntry(StoreEntry current){ 290 StoreEntry result=null; 291 if(current != null && current.getOffset() >= 0){ 292 try{ 293 result=indexManager.getIndex(current.getOffset()); 294 }catch(IOException e){ 295 throw new RuntimeException ("Failed to index",e); 296 } 297 } 298 if(result != null &&root!=null && root.equals(result)){ 300 return root; 301 } 302 return result; 303 } 304 305 309 public synchronized StoreEntry refreshEntry(StoreEntry current){ 310 StoreEntry result=null; 311 if(current != null && current.getOffset() >= 0){ 312 try{ 313 result=indexManager.refreshIndex((IndexItem)current); 314 }catch(IOException e){ 315 throw new RuntimeException ("Failed to index",e); 316 } 317 } 318 if(result != null &&root!=null && root.equals(result)){ 320 return root; 321 } 322 return result; 323 } 324 325 public synchronized void remove(IndexItem e){ 326 if(e==root||e.equals(root)) 327 return; 328 if(e==last||e.equals(last)){ 329 if(size>1){ 330 last = (IndexItem)refreshEntry(last); 331 last=getPrevEntry(last); 332 }else{ 333 last=null; 334 } 335 } 336 size--; 337 } 338 } 339 | Popular Tags |