1 14 15 package org.apache.activemq.kaha.impl.container; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import org.apache.activemq.kaha.ContainerId; 24 import org.apache.activemq.kaha.MapContainer; 25 import org.apache.activemq.kaha.Marshaller; 26 import org.apache.activemq.kaha.RuntimeStoreException; 27 import org.apache.activemq.kaha.Store; 28 import org.apache.activemq.kaha.StoreEntry; 29 import org.apache.activemq.kaha.StoreLocation; 30 import org.apache.activemq.kaha.impl.DataManager; 31 import org.apache.activemq.kaha.impl.data.Item; 32 import org.apache.activemq.kaha.impl.index.Index; 33 import org.apache.activemq.kaha.impl.index.IndexItem; 34 import org.apache.activemq.kaha.impl.index.IndexLinkedList; 35 import org.apache.activemq.kaha.impl.index.IndexManager; 36 import org.apache.activemq.kaha.impl.index.VMIndex; 37 import org.apache.activemq.kaha.impl.index.hash.HashIndex; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 46 public final class MapContainerImpl extends BaseContainerImpl implements MapContainer{ 47 48 private static final Log log=LogFactory.getLog(MapContainerImpl.class); 49 protected Index index; 50 protected Marshaller keyMarshaller=Store.ObjectMarshaller; 51 protected Marshaller valueMarshaller=Store.ObjectMarshaller; 52 protected File directory; 53 54 public MapContainerImpl(File directory,ContainerId id,IndexItem root,IndexManager indexManager, 55 DataManager dataManager,boolean persistentIndex){ 56 super(id,root,indexManager,dataManager,persistentIndex); 57 this.directory=directory; 58 } 59 60 public synchronized void init(){ 61 super.init(); 62 if(index==null){ 63 if(persistentIndex){ 64 String name=containerId.getDataContainerName()+"_"+containerId.getKey(); 65 name=name.replaceAll("[^a-zA-Z0-9\\.\\_\\-]", "_"); 66 try{ 67 this.index=new HashIndex(directory,name,indexManager); 68 }catch(IOException e){ 69 log.error("Failed to create HashIndex",e); 70 throw new RuntimeException (e); 71 } 72 }else{ 73 this.index=new VMIndex(indexManager); 74 } 75 } 76 index.setKeyMarshaller(keyMarshaller); 77 } 78 79 84 public synchronized void load(){ 85 checkClosed(); 86 if(!loaded){ 87 if(!loaded){ 88 loaded=true; 89 try{ 90 init(); 91 index.load(); 92 long nextItem=root.getNextItem(); 93 while(nextItem!=Item.POSITION_NOT_SET){ 94 IndexItem item=indexManager.getIndex(nextItem); 95 StoreLocation data=item.getKeyDataItem(); 96 Object key=dataManager.readItem(keyMarshaller,data); 97 if(index.isTransient()){ 98 index.store(key,item); 99 } 100 indexList.add(item); 101 nextItem=item.getNextItem(); 102 } 103 }catch(IOException e){ 104 log.error("Failed to load container "+getId(),e); 105 throw new RuntimeStoreException(e); 106 } 107 } 108 } 109 } 110 111 116 public synchronized void unload(){ 117 checkClosed(); 118 if(loaded){ 119 loaded=false; 120 try{ 121 index.unload(); 122 }catch(IOException e){ 123 log.warn("Failed to unload the index",e); 124 } 125 indexList.clear(); 126 } 127 } 128 129 public synchronized void setKeyMarshaller(Marshaller keyMarshaller){ 130 checkClosed(); 131 this.keyMarshaller=keyMarshaller; 132 if(index!=null){ 133 index.setKeyMarshaller(keyMarshaller); 134 } 135 } 136 137 public synchronized void setValueMarshaller(Marshaller valueMarshaller){ 138 checkClosed(); 139 this.valueMarshaller=valueMarshaller; 140 } 141 142 147 public synchronized int size(){ 148 load(); 149 return indexList.size(); 150 } 151 152 157 public synchronized boolean isEmpty(){ 158 load(); 159 return indexList.isEmpty(); 160 } 161 162 167 public synchronized boolean containsKey(Object key){ 168 load(); 169 try{ 170 return index.containsKey(key); 171 }catch(IOException e){ 172 log.error("Failed trying to find key: "+key,e); 173 throw new RuntimeException (e); 174 } 175 } 176 177 182 public synchronized Object get(Object key){ 183 load(); 184 Object result=null; 185 StoreEntry item=null; 186 try{ 187 item=index.get(key); 188 }catch(IOException e){ 189 log.error("Failed trying to get key: "+key,e); 190 throw new RuntimeException (e); 191 } 192 if(item!=null){ 193 result=getValue(item); 194 } 195 return result; 196 } 197 198 203 public synchronized StoreEntry getEntry(Object key) { 204 load(); 205 StoreEntry item=null; 206 try{ 207 item=index.get(key); 208 }catch(IOException e){ 209 log.error("Failed trying to get key: "+key,e); 210 throw new RuntimeException (e); 211 } 212 return item; 213 } 214 215 220 public synchronized boolean containsValue(Object o){ 221 load(); 222 boolean result=false; 223 if(o!=null){ 224 IndexItem item=indexList.getFirst(); 225 while(item!=null){ 226 Object value=getValue(item); 227 if(value!=null&&value.equals(o)){ 228 result=true; 229 break; 230 } 231 item=indexList.getNextEntry(item); 232 } 233 } 234 return result; 235 } 236 237 242 public synchronized void putAll(Map t){ 243 load(); 244 if(t!=null){ 245 for(Iterator i=t.entrySet().iterator();i.hasNext();){ 246 Map.Entry entry=(Map.Entry )i.next(); 247 put(entry.getKey(),entry.getValue()); 248 } 249 } 250 } 251 252 257 public synchronized Set keySet(){ 258 load(); 259 return new ContainerKeySet(this); 260 } 261 262 267 public synchronized Collection values(){ 268 load(); 269 return new ContainerValueCollection(this); 270 } 271 272 277 public synchronized Set entrySet(){ 278 load(); 279 return new ContainerEntrySet(this); 280 } 281 282 287 public synchronized Object put(Object key,Object value){ 288 load(); 289 Object result=remove(key); 290 IndexItem item=write(key,value); 291 try{ 292 index.store(key,item); 293 }catch(IOException e){ 294 log.error("Failed trying to insert key: "+key,e); 295 throw new RuntimeException (e); 296 } 297 indexList.add(item); 298 return result; 299 } 300 301 306 public synchronized Object remove(Object key){ 307 load(); 308 try{ 309 Object result=null; 310 IndexItem item=(IndexItem)index.remove(key); 311 if(item!=null){ 312 item=(IndexItem)indexList.refreshEntry(item); 314 result=getValue(item); 315 IndexItem prev=indexList.getPrevEntry(item); 316 IndexItem next=indexList.getNextEntry(item); 317 indexList.remove(item); 318 delete(item,prev,next); 319 } 320 return result; 321 }catch(IOException e){ 322 log.error("Failed trying to remove key: "+key,e); 323 throw new RuntimeException (e); 324 } 325 } 326 327 public synchronized boolean removeValue(Object o){ 328 load(); 329 boolean result=false; 330 if(o!=null){ 331 IndexItem item=indexList.getFirst(); 332 while(item!=null){ 333 Object value=getValue(item); 334 if(value!=null&&value.equals(o)){ 335 result=true; 336 Object key=getKey(item); 338 if(key!=null){ 339 remove(key); 340 } 341 break; 342 } 343 item=indexList.getNextEntry(item); 344 } 345 } 346 return result; 347 } 348 349 protected synchronized void remove(IndexItem item){ 350 Object key=getKey(item); 351 if(key!=null){ 352 remove(key); 353 } 354 } 355 356 361 public synchronized void clear(){ 362 checkClosed(); 363 loaded=true; 364 init(); 365 if(index!=null){ 366 try{ 367 index.clear(); 368 }catch(IOException e){ 369 log.error("Failed trying clear index",e); 370 throw new RuntimeException (e); 371 } 372 } 373 super.clear(); 374 doClear(); 375 } 376 377 384 public synchronized StoreEntry place(Object key,Object value){ 385 load(); 386 try{ 387 remove(key); 388 IndexItem item=write(key,value); 389 index.store(key,item); 390 indexList.add(item); 391 return item; 392 }catch(IOException e){ 393 log.error("Failed trying to place key: "+key,e); 394 throw new RuntimeException (e); 395 } 396 } 397 398 404 public synchronized void remove(StoreEntry entry){ 405 load(); 406 IndexItem item=(IndexItem)entry; 407 if(item!=null){ 408 Object key=getKey(item); 409 try{ 410 index.remove(key); 411 }catch(IOException e){ 412 log.error("Failed trying to remove entry: "+entry,e); 413 throw new RuntimeException (e); 414 } 415 IndexItem prev=indexList.getPrevEntry(item); 416 IndexItem next=indexList.getNextEntry(item); 417 indexList.remove(item); 418 delete(item,prev,next); 419 } 420 } 421 422 public synchronized StoreEntry getFirst(){ 423 load(); 424 return indexList.getFirst(); 425 } 426 427 public synchronized StoreEntry getLast(){ 428 load(); 429 return indexList.getLast(); 430 } 431 432 public synchronized StoreEntry getNext(StoreEntry entry){ 433 load(); 434 IndexItem item=(IndexItem)entry; 435 return indexList.getNextEntry(item); 436 } 437 438 public synchronized StoreEntry getPrevious(StoreEntry entry){ 439 load(); 440 IndexItem item=(IndexItem)entry; 441 return indexList.getPrevEntry(item); 442 } 443 444 public synchronized StoreEntry refresh(StoreEntry entry){ 445 load(); 446 return indexList.getEntry(entry); 447 } 448 449 455 public synchronized Object getValue(StoreEntry item){ 456 load(); 457 Object result=null; 458 if(item!=null){ 459 try{ 460 StoreLocation data=item.getValueDataItem(); 463 result=dataManager.readItem(valueMarshaller,data); 464 }catch(IOException e){ 465 log.error("Failed to get value for "+item,e); 466 throw new RuntimeStoreException(e); 467 } 468 } 469 return result; 470 } 471 472 478 public synchronized Object getKey(StoreEntry item){ 479 load(); 480 Object result=null; 481 if(item!=null){ 482 try{ 483 StoreLocation data=item.getKeyDataItem(); 484 result=dataManager.readItem(keyMarshaller,data); 485 }catch(IOException e){ 486 log.error("Failed to get key for "+item,e); 487 throw new RuntimeStoreException(e); 488 } 489 } 490 return result; 491 } 492 493 protected IndexLinkedList getItemList(){ 494 return indexList; 495 } 496 497 protected synchronized IndexItem write(Object key,Object value){ 498 IndexItem index=null; 499 try{ 500 if(key!=null){ 501 index=indexManager.createNewIndex(); 502 StoreLocation data=dataManager.storeDataItem(keyMarshaller,key); 503 index.setKeyData(data); 504 } 505 if(value!=null){ 506 StoreLocation data=dataManager.storeDataItem(valueMarshaller,value); 507 index.setValueData(data); 508 } 509 IndexItem prev=indexList.getLast(); 510 prev=prev!=null?prev:indexList.getRoot(); 511 IndexItem next=indexList.getNextEntry(prev); 512 prev.setNextItem(index.getOffset()); 513 index.setPreviousItem(prev.getOffset()); 514 updateIndexes(prev); 515 if(next!=null){ 516 next.setPreviousItem(index.getOffset()); 517 index.setNextItem(next.getOffset()); 518 updateIndexes(next); 519 } 520 storeIndex(index); 521 }catch(IOException e){ 522 log.error("Failed to write "+key+" , "+value,e); 523 throw new RuntimeStoreException(e); 524 } 525 return index; 526 } 527 } 528 | Popular Tags |