Search in sources :

Example 16 with CollectionRemoveOperation

use of org.datanucleus.flush.CollectionRemoveOperation in project datanucleus-core by datanucleus.

the class PriorityQueue method remove.

/**
 * 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);
    boolean backingSuccess = true;
    if (backingStore != null) {
        if (SCOUtils.useQueuedUpdate(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;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 17 with CollectionRemoveOperation

use of org.datanucleus.flush.CollectionRemoveOperation in project datanucleus-core by datanucleus.

the class PriorityQueue method removeAll.

/**
 * Method to remove a Collection of elements.
 * @param elements The collection to remove
 * @return Whether they were removed successfully.
 */
public boolean removeAll(java.util.Collection elements) {
    if (elements == null) {
        throw new NullPointerException();
    } else if (elements.isEmpty()) {
        return true;
    }
    makeDirty();
    if (useCache) {
        loadFromStore();
    }
    int size = useCache ? delegate.size() : -1;
    Collection contained = null;
    if (backingStore != null && SCOUtils.useQueuedUpdate(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 (backingStore != null && ownerOP != null) {
        boolean backingSuccess = true;
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            if (contained != null && !contained.isEmpty()) {
                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.getExecutionContext().getTransaction().isActive()) {
            ownerOP.getExecutionContext().processNontransactionalUpdate();
        }
        return backingSuccess;
    }
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return delegateSuccess;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) Collection(java.util.Collection) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 18 with CollectionRemoveOperation

use of org.datanucleus.flush.CollectionRemoveOperation in project datanucleus-core by datanucleus.

the class LinkedHashSet method clear.

/**
 * Method to clear the LinkedHashSet
 */
public void clear() {
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
        // Relationship management
        Iterator iter = delegate.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    if (ownerOP != null && !delegate.isEmpty()) {
        // Cascade delete
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            // Queue the cascade delete
            Iterator iter = delegate.iterator();
            while (iter.hasNext()) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionRemoveOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), iter.next(), true));
            }
        } else if (SCOUtils.hasDependentElement(ownerMmd)) {
            // Perform the cascade delete
            Iterator iter = delegate.iterator();
            while (iter.hasNext()) {
                ownerOP.getExecutionContext().deleteObjectInternal(iter.next());
            }
        }
    }
    delegate.clear();
    makeDirty();
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
}
Also used : RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 19 with CollectionRemoveOperation

use of org.datanucleus.flush.CollectionRemoveOperation in project datanucleus-core by datanucleus.

the class LinkedList method clear.

/**
 * Method to clear the LinkedList.
 */
public void clear() {
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations()) {
        // Relationship management
        Iterator iter = delegate.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    if (ownerOP != null && !delegate.isEmpty()) {
        // Cascade delete
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            java.util.List copy = new java.util.ArrayList(delegate);
            Iterator iter = copy.iterator();
            while (iter.hasNext()) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionRemoveOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), iter.next(), true));
            }
        } else if (SCOUtils.hasDependentElement(ownerMmd)) {
            java.util.List copy = new java.util.ArrayList(delegate);
            Iterator iter = copy.iterator();
            while (iter.hasNext()) {
                ownerOP.getExecutionContext().deleteObjectInternal(iter.next());
            }
        }
    }
    delegate.clear();
    makeDirty();
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
}
Also used : RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) SCOListIterator(org.datanucleus.store.types.SCOListIterator) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation) SCOList(org.datanucleus.store.types.SCOList)

Example 20 with CollectionRemoveOperation

use of org.datanucleus.flush.CollectionRemoveOperation in project datanucleus-core by datanucleus.

the class PriorityQueue method clear.

/**
 * Method to clear the Collection.
 */
public void clear() {
    if (ownerOP != null && !delegate.isEmpty()) {
        // Cascade delete
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            java.util.List copy = new java.util.ArrayList(delegate);
            Iterator iter = copy.iterator();
            while (iter.hasNext()) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionRemoveOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), iter.next(), true));
            }
        } else if (SCOUtils.hasDependentElement(ownerMmd)) {
            java.util.List copy = new java.util.ArrayList(delegate);
            Iterator iter = copy.iterator();
            while (iter.hasNext()) {
                ownerOP.getExecutionContext().deleteObjectInternal(iter.next());
            }
        }
    }
    delegate.clear();
    makeDirty();
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
}
Also used : Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Aggregations

CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)56 Iterator (java.util.Iterator)36 RelationshipManager (org.datanucleus.state.RelationshipManager)28 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)26 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)26 Collection (java.util.Collection)17 ListIterator (java.util.ListIterator)10 SCOCollection (org.datanucleus.store.types.SCOCollection)10 SCOListIterator (org.datanucleus.store.types.SCOListIterator)10 SCOList (org.datanucleus.store.types.SCOList)5 AbstractList (java.util.AbstractList)1