Search in sources :

Example 1 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class ManagedRelationsHandler method execute.

public void execute() {
    if (managedRelationDetails.isEmpty()) {
        return;
    }
    try {
        executing = true;
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("013000"));
        }
        if (performChecks) {
            // Tests for negative situations where inconsistently assigned
            Iterator<Map.Entry<ObjectProvider, RelationshipManager>> managedRelEntryIter = managedRelationDetails.entrySet().iterator();
            while (managedRelEntryIter.hasNext()) {
                Map.Entry<ObjectProvider, RelationshipManager> managedRelEntry = managedRelEntryIter.next();
                ObjectProvider op = managedRelEntry.getKey();
                LifeCycleState lc = op.getLifecycleState();
                if (lc == null || lc.isDeleted()) {
                    // Has been deleted so ignore all relationship changes
                    continue;
                }
                managedRelEntry.getValue().checkConsistency();
            }
        }
        // Process updates to manage the other side of the relations
        Iterator<Map.Entry<ObjectProvider, RelationshipManager>> managedRelEntryIter = managedRelationDetails.entrySet().iterator();
        while (managedRelEntryIter.hasNext()) {
            Map.Entry<ObjectProvider, RelationshipManager> managedRelEntry = managedRelEntryIter.next();
            ObjectProvider op = managedRelEntry.getKey();
            LifeCycleState lc = op.getLifecycleState();
            if (lc == null || lc.isDeleted()) {
                // Has been deleted so ignore all relationship changes
                continue;
            }
            RelationshipManager relMgr = managedRelEntry.getValue();
            relMgr.process();
            relMgr.clearFields();
        }
        managedRelationDetails.clear();
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("013001"));
        }
    } finally {
        executing = false;
    }
}
Also used : RelationshipManager(org.datanucleus.state.RelationshipManager) LifeCycleState(org.datanucleus.state.LifeCycleState) ObjectProvider(org.datanucleus.state.ObjectProvider) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class Set method addAll.

/**
 * Method to add a collection of elements.
 * @param c The collection of elements to add.
 * @return Whether they were added successfully.
 */
public boolean addAll(java.util.Collection c) {
    if (useCache) {
        loadFromStore();
    }
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        // Relationship management
        Iterator iter = c.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(ownerOP)) {
            for (Object element : c) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
            }
        } else {
            try {
                backingSuccess = backingStore.addAll(ownerOP, c, useCache ? delegate.size() : -1);
            } catch (NucleusDataStoreException dse) {
                throw new IllegalArgumentException(Localiser.msg("023013", "addAll", ownerMmd.getName(), dse), dse);
            }
        }
    }
    // 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(c);
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return backingStore != null ? backingSuccess : delegateSuccess;
}
Also used : CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator)

Example 3 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class Set 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 (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        // Relationship management
        Iterator iter = elements.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    if (backingStore != null && ownerOP != null) {
        boolean backingSuccess = false;
        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) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) Collection(java.util.Collection) SCOCollection(org.datanucleus.store.types.SCOCollection) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 4 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class SortedSet method removeAll.

/**
 * Method to remove all elements from the collection from the SortedSet.
 * @param elements The collection of elements to remove
 * @return Whether it was removed ok.
 */
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 (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        // Relationship management
        Iterator iter = elements.iterator();
        RelationshipManager relMgr = ownerOP.getExecutionContext().getRelationshipManager(ownerOP);
        while (iter.hasNext()) {
            relMgr.relationRemove(ownerMmd.getAbsoluteFieldNumber(), iter.next());
        }
    }
    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) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) Collection(java.util.Collection) SCOCollection(org.datanucleus.store.types.SCOCollection) CollectionRemoveOperation(org.datanucleus.flush.CollectionRemoveOperation)

Example 5 with RelationshipManager

use of org.datanucleus.state.RelationshipManager in project datanucleus-core by datanucleus.

the class SortedSet method addAll.

/**
 * Method to add a collection to the SortedSet.
 * @param elements The collection
 * @return Whether it was added ok.
 */
public boolean addAll(Collection elements) {
    if (useCache) {
        loadFromStore();
    }
    if (ownerOP != null && ownerOP.getExecutionContext().getManageRelations() && !initialising) {
        // 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(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) {
                throw new IllegalArgumentException(Localiser.msg("023013", "addAll", ownerMmd.getName(), dse), dse);
            }
        }
    }
    // 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;
}
Also used : CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator)

Aggregations

RelationshipManager (org.datanucleus.state.RelationshipManager)47 Iterator (java.util.Iterator)44 CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)28 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)24 ListIterator (java.util.ListIterator)20 SCOListIterator (org.datanucleus.store.types.SCOListIterator)20 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)12 CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)11 Collection (java.util.Collection)5 ListAddAtOperation (org.datanucleus.flush.ListAddAtOperation)5 SCOCollection (org.datanucleus.store.types.SCOCollection)5 SCOList (org.datanucleus.store.types.SCOList)5 ObjectProvider (org.datanucleus.state.ObjectProvider)2 AbstractList (java.util.AbstractList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionContext (org.datanucleus.ExecutionContext)1 FetchPlan (org.datanucleus.FetchPlan)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1