1 package org.sourceforge.jemm.database.persistent.berkeley; 2 3 import java.util.Iterator; 4 5 import com.sleepycat.je.DatabaseException; 6 import com.sleepycat.persist.EntityCursor; 7 import com.sleepycat.persist.EntityStore; 8 import com.sleepycat.persist.PrimaryIndex; 9 import com.sleepycat.persist.SecondaryIndex; 10 11 import org.sourceforge.jemm.database.ClientId; 12 import org.sourceforge.jemm.database.components.StorageEngineException; 13 import org.sourceforge.jemm.database.components.se.StorageEngineClientRefIF; 14 import org.sourceforge.jemm.database.persistent.berkeley.objects.ClientRefInfo; 15 import org.sourceforge.jemm.database.persistent.berkeley.objects.ClientRefKey; 16 import org.sourceforge.jemm.types.ID; 17 18 public class BDbStorageEngineClientRefImpl implements StorageEngineClientRefIF { 19 20 private final PrimaryIndex<ClientRefKey, ClientRefInfo> pIdx; 21 private final SecondaryIndex<String,ClientRefKey, ClientRefInfo> byClientIdIdx; 22 23 public BDbStorageEngineClientRefImpl(EntityStore store) { 24 try { 25 pIdx = store.getPrimaryIndex(ClientRefKey.class, ClientRefInfo.class); 26 byClientIdIdx = store.getSecondaryIndex(pIdx, String.class, "clientId"); 27 EntityCursor<ClientRefKey> keyCursor = pIdx.keys(); 28 while(keyCursor.next() != null) 29 keyCursor.delete(); 30 keyCursor.close(); 31 } catch (DatabaseException de) { 32 throw new StorageEngineException("Error initialsing client ref handler",de); 33 } 34 } 35 36 37 @Override 38 public void clearReference(ClientId clientId, ID id) { 39 try { 40 ClientRefKey key = new ClientRefKey(clientId,id); 41 if(!pIdx.contains(key)) 42 throw new IllegalStateException("Illegal reference clear on " + id + " by " 43 + clientId + " reference not held"); 44 else 45 pIdx.delete(key); 46 } catch (DatabaseException de) { 47 throw new StorageEngineException("error whilst calling clearReference(" + clientId +"," + id +")",de); 48 } 49 } 50 51 @Override 52 public boolean hasReference(ClientId clientId, ID id) { 53 try { 54 return pIdx.contains(new ClientRefKey(clientId,id)); 55 } catch (DatabaseException de) { 56 throw new StorageEngineException("error whilst calling hasReference(" + clientId +"," + id +")",de); 57 } 58 } 59 60 @Override 61 public boolean recordReference(ClientId clientId, ID id) { 62 try { 63 return pIdx.putNoOverwrite(new ClientRefInfo(clientId,id)); 64 } catch (DatabaseException de) { 65 throw new StorageEngineException("error whilst calling recordReference(" + clientId +"," + id +")",de); 66 } 67 } 68 69 @Override 70 public Iterator<ID> referenceIterator(ClientId clientId) { 71 String key = clientId.getInternalRep(); 72 73 try { 74 return new EnityCursorIterator<ClientRefInfo, ID>(byClientIdIdx.entities(key,true,key,true)) 75 { 76 @Override 77 protected ID convert(ClientRefInfo internalValue) { 78 return internalValue.getObjectId(); 79 } 80 81 }; 82 } catch (DatabaseException de) { 83 throw new StorageEngineException("error whilst calling referenceIterator(" + clientId + ")",de); 84 } 85 } 86 87 @Override public void removeAllRefs(ClientId clientId) { 88 try { 89 byClientIdIdx.delete(clientId.getInternalRep()); 90 } catch (DatabaseException de) { 91 throw new StorageEngineException("error whilst calling removeAllRefs(" + clientId + ")",de); 92 } 93 } 94 }