/** * Accessor for the size of the TreeSet. * * @return The size. */ public int size() { if (useCache && isCacheLoaded) { // If the "delegate" is already loaded, use it return delegate.size(); } else if (backingStore != null) { return backingStore.size(ownerOP); } return delegate.size(); }
/** * Accessor for whether an element is contained in this Set. * * @param element The element * @return Whether it is contained. */ public boolean contains(Object element) { if (useCache && isCacheLoaded) { // If the "delegate" is already loaded, use it return delegate.contains(element); } else if (backingStore != null) { return backingStore.contains(ownerOP, element); } return delegate.contains(element); }
/** * Method to add an element to the TreeSet. * * @param element The new element * @return Whether it was added ok. */ public boolean add(Object element) { // Reject inappropriate elements if (!allowNulls && element == null) { throw new NullPointerException( "Nulls not allowed for collection at field " + ownerMmd.getName() + " but element is null"); } if (useCache) { loadFromStore(); } if (contains(element)) { return false; } if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { // Relationship management ownerOP .getExecutionContext() .getRelationshipManager(ownerOP) .relationAdd(ownerMmd.getAbsoluteFieldNumber(), element); } boolean backingSuccess = true; if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element)); } else { try { backingStore.add(ownerOP, element, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn(LOCALISER.msg("023013", "add", ownerMmd.getName(), dse)); backingSuccess = false; } } } // Only make it dirty after adding the field to the datastore so we give it time // to be inserted - otherwise jdoPreStore on this object would have been called before // completing the addition makeDirty(); boolean delegateSuccess = delegate.add(element); if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingSuccess : delegateSuccess); }
/** Method to load all elements from the "backing store" where appropriate. */ protected void loadFromStore() { if (backingStore != null && !isCacheLoaded) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg("023006", ownerOP.getObjectAsPrintable(), ownerMmd.getName())); } delegate.clear(); Iterator iter = backingStore.iterator(ownerOP); while (iter.hasNext()) { delegate.add(iter.next()); } isCacheLoaded = true; } }
/** * Method to remove an element from the collection, and observe the flag for whether to allow * cascade delete. * * @param element The element * @param allowCascadeDelete Whether to allow cascade delete */ public boolean remove(Object element, boolean allowCascadeDelete) { makeDirty(); if (useCache) { loadFromStore(); } int size = (useCache ? delegate.size() : -1); boolean contained = delegate.contains(element); boolean delegateSuccess = delegate.remove(element); if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { ownerOP .getExecutionContext() .getRelationshipManager(ownerOP) .relationRemove(ownerMmd.getAbsoluteFieldNumber(), element); } boolean backingSuccess = true; if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { backingSuccess = contained; if (backingSuccess) { ownerOP .getExecutionContext() .addOperationToQueue( new CollectionRemoveOperation( ownerOP, backingStore, element, allowCascadeDelete)); } } else { try { backingSuccess = backingStore.remove(ownerOP, element, size, allowCascadeDelete); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( LOCALISER.msg("023013", "remove", ownerMmd.getName(), dse)); backingSuccess = false; } } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingSuccess : delegateSuccess); }
/** * Method to add a collection to the TreeSet. * * @param elements The collection * @return Whether it was added ok. */ public boolean addAll(Collection elements) { if (useCache) { loadFromStore(); } if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { // Relationship management Iterator iter = elements.iterator(); RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP); while (iter.hasNext()) { relMgr.relationAdd(ownerMmd.getAbsoluteFieldNumber(), iter.next()); } } boolean backingSuccess = true; if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { for (Object element : elements) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element)); } } else { try { backingSuccess = backingStore.addAll(ownerOP, elements, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( LOCALISER.msg("023013", "addAll", ownerMmd.getName(), dse)); backingSuccess = false; } } } // Only make it dirty after adding the field to the datastore so we give it time // to be inserted - otherwise jdoPreStore on this object would have been called before // completing the addition makeDirty(); boolean delegateSuccess = delegate.addAll(elements); if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingSuccess : delegateSuccess); }
/** Method to clear the TreeSet */ public void clear() { makeDirty(); delegate.clear(); if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionClearOperation(ownerOP, backingStore)); } else { backingStore.clear(ownerOP); } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } }
/** * Method to remove all elements from the collection from the TreeSet. * * @param elements The collection of elements to remove * @return Whether it was removed ok. */ public boolean removeAll(java.util.Collection elements) { makeDirty(); if (useCache) { loadFromStore(); } int size = (useCache ? delegate.size() : -1); Collection contained = null; if (backingStore != null && SCOUtils.useQueuedUpdate(queued, ownerOP)) { // Check which are contained before updating the delegate contained = new java.util.HashSet(); for (Object elem : elements) { if (contains(elem)) { contained.add(elem); } } } boolean delegateSuccess = delegate.removeAll(elements); if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { // Relationship management Iterator iter = elements.iterator(); RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP); while (iter.hasNext()) { relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next()); } } if (backingStore != null) { boolean backingSuccess = true; if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { backingSuccess = false; for (Object element : contained) { backingSuccess = true; ownerOP .getExecutionContext() .addOperationToQueue( new CollectionRemoveOperation(ownerOP, backingStore, element, true)); } } else { try { backingSuccess = backingStore.removeAll(ownerOP, elements, size); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( LOCALISER.msg("023013", "removeAll", ownerMmd.getName(), dse)); backingSuccess = false; } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return backingSuccess; } else { if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return delegateSuccess; } }
/** * Method to update an embedded element in this collection. * * @param element The element * @param fieldNumber Number of field in the element * @param value New value for this field */ public void updateEmbeddedElement(Object element, int fieldNumber, Object value) { if (backingStore != null) { backingStore.updateEmbeddedElement(ownerOP, element, fieldNumber, value); } }
/** * Method to initialise the SCO from an existing value. * * @param o The object to set from * @param forInsert Whether the object needs inserting in the datastore with this value * @param forUpdate Whether to update the datastore with this value */ public void initialise(Object o, boolean forInsert, boolean forUpdate) { Collection c = (Collection) o; if (c != null) { // Check for the case of serialised PC elements, and assign ObjectProviders to the elements // without if (SCOUtils.collectionHasSerialisedElements(ownerMmd) && ownerMmd.getCollection().elementIsPersistent()) { ExecutionContext ec = ownerOP.getExecutionContext(); Iterator iter = c.iterator(); while (iter.hasNext()) { Object pc = iter.next(); ObjectProvider objSM = ec.findObjectProvider(pc); if (objSM == null) { objSM = ec.newObjectProviderForEmbedded( pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber()); } } } if (backingStore != null && useCache && !isCacheLoaded) { // Mark as loaded isCacheLoaded = true; } if (forInsert) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } if (useCache) { loadFromStore(); } if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) { // Relationship management Iterator iter = c.iterator(); RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP); while (iter.hasNext()) { relMgr.relationAdd(ownerMmd.getAbsoluteFieldNumber(), iter.next()); } } if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { for (Object element : c) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element)); } } else { try { backingStore.addAll(ownerOP, c, (useCache ? delegate.size() : -1)); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( LOCALISER.msg("023013", "addAll", ownerMmd.getName(), dse)); } } } delegate.addAll(c); makeDirty(); } else if (forUpdate) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg( "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } // Detect which objects are added and which are deleted if (useCache) { isCacheLoaded = false; // Mark as false since need to load the old collection loadFromStore(); for (Object elem : c) { if (!delegate.contains(elem)) { add(elem); } } java.util.TreeSet delegateCopy = new java.util.TreeSet(delegate); for (Object elem : delegateCopy) { if (!c.contains(elem)) { remove(elem); } } } else { for (Object elem : c) { if (!contains(elem)) { add(elem); } } Iterator iter = iterator(); while (iter.hasNext()) { Object elem = iter.next(); if (!c.contains(elem)) { remove(elem); } } } } else { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } delegate.clear(); delegate.addAll(c); } } }