Search in sources :

Example 16 with CollectionAddOperation

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

the class List method addAll.

/**
 * Method to add a Collection to the ArrayList.
 * @param elements The collection
 * @return Whether it was added ok.
 */
public boolean addAll(Collection elements) {
    boolean success = delegate.addAll(elements);
    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());
        }
    }
    if (success) {
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            for (Object element : elements) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), element));
            }
        }
        makeDirty();
        if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
            ownerOP.getExecutionContext().processNontransactionalUpdate();
        }
    }
    return success;
}
Also used : CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) SCOListIterator(org.datanucleus.store.types.SCOListIterator)

Example 17 with CollectionAddOperation

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

the class Stack method addAll.

/**
 * Method to add a Collection to the Stack
 * @param elements The collection
 * @return Whether it was added ok.
 */
public synchronized boolean addAll(Collection elements) {
    boolean success = delegate.addAll(elements);
    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());
        }
    }
    if (success) {
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            for (Object element : elements) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), element));
            }
        }
        makeDirty();
        if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
            ownerOP.getExecutionContext().processNontransactionalUpdate();
        }
    }
    return success;
}
Also used : CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) SCOListIterator(org.datanucleus.store.types.SCOListIterator)

Example 18 with CollectionAddOperation

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

the class Vector method addAll.

/**
 * Method to add a Collection to the Vector.
 * @param elements The collection
 * @return Whether it was added ok.
 */
public synchronized boolean addAll(Collection elements) {
    boolean success = delegate.addAll(elements);
    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());
        }
    }
    if (success) {
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            for (Object element : elements) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, ownerMmd.getAbsoluteFieldNumber(), element));
            }
        }
        makeDirty();
        if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
            ownerOP.getExecutionContext().processNontransactionalUpdate();
        }
    }
    return success;
}
Also used : CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) RelationshipManager(org.datanucleus.state.RelationshipManager) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) SCOListIterator(org.datanucleus.store.types.SCOListIterator)

Example 19 with CollectionAddOperation

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

the class ArrayList method addAll.

/**
 * Method to add a Collection to the ArrayList.
 * @param elements The collection
 * @return Whether it was added ok.
 */
public boolean addAll(Collection elements) {
    if (useCache) {
        loadFromStore();
    }
    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 element(s) 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)

Example 20 with CollectionAddOperation

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

the class Collection method initialise.

/* (non-Javadoc)
     * @see org.datanucleus.store.types.wrappers.Collection#initialise(java.util.Collection, java.util.Collection)
     */
@Override
public void initialise(java.util.Collection<E> newValue, Object oldValue) {
    if (newValue instanceof java.util.List && !(delegate instanceof java.util.List)) {
        // Need to set the value to a List so we change our delegate to match
        delegate = new java.util.ArrayList();
    }
    if (newValue != 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 = newValue.iterator();
            while (iter.hasNext()) {
                Object pc = iter.next();
                ObjectProvider objOP = ec.findObjectProvider(pc);
                if (objOP == null) {
                    objOP = ec.getNucleusContext().getObjectProviderFactory().newForEmbedded(ec, pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber());
                }
            }
        }
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + newValue.size()));
        }
        if (delegate instanceof Set) {
            // Detect which objects are added and which are deleted
            initialising = true;
            if (useCache) {
                java.util.Collection oldColl = (java.util.Collection) oldValue;
                if (oldColl != null) {
                    delegate.addAll(oldColl);
                }
                isCacheLoaded = true;
                SCOUtils.updateCollectionWithCollection(ownerOP.getExecutionContext().getApiAdapter(), this, newValue);
            } else {
                java.util.Collection oldColl = (java.util.Collection) oldValue;
                if (oldColl instanceof SCOCollection) {
                    oldColl = (java.util.Collection) ((SCOCollection) oldColl).getValue();
                }
                for (E elem : newValue) {
                    if (oldColl == null || !oldColl.contains(elem)) {
                        add(elem);
                    }
                }
                if (oldColl != null) {
                    Iterator iter = oldColl.iterator();
                    while (iter.hasNext()) {
                        Object elem = iter.next();
                        if (!newValue.contains(elem)) {
                            remove(elem);
                        }
                    }
                }
            }
            initialising = false;
        } else {
            // TODO This does clear+addAll : Improve this and work out which elements are added and which deleted
            if (backingStore != null) {
                if (SCOUtils.useQueuedUpdate(ownerOP)) {
                    if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) {
                        ownerOP.getExecutionContext().addOperationToQueue(new CollectionClearOperation(ownerOP, backingStore));
                        for (Object element : newValue) {
                            ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
                        }
                    }
                } else {
                    backingStore.clear(ownerOP);
                    try {
                        backingStore.addAll(ownerOP, newValue, useCache ? 0 : -1);
                    } catch (NucleusDataStoreException dse) {
                        NucleusLogger.PERSISTENCE.warn(Localiser.msg("023013", "addAll", ownerMmd.getName(), dse));
                    }
                }
            }
            delegate.addAll(newValue);
            isCacheLoaded = true;
            makeDirty();
        }
    }
}
Also used : CollectionClearOperation(org.datanucleus.flush.CollectionClearOperation) Set(java.util.Set) SCOCollection(org.datanucleus.store.types.SCOCollection) CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Iterator(java.util.Iterator) SCOCollectionIterator(org.datanucleus.store.types.SCOCollectionIterator) SCOCollection(org.datanucleus.store.types.SCOCollection) ObjectProvider(org.datanucleus.state.ObjectProvider)

Aggregations

CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)40 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)34 Iterator (java.util.Iterator)19 RelationshipManager (org.datanucleus.state.RelationshipManager)11 ListIterator (java.util.ListIterator)10 SCOListIterator (org.datanucleus.store.types.SCOListIterator)10 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)9 ExecutionContext (org.datanucleus.ExecutionContext)8 CollectionClearOperation (org.datanucleus.flush.CollectionClearOperation)8 ObjectProvider (org.datanucleus.state.ObjectProvider)8 Set (java.util.Set)1 SCOCollection (org.datanucleus.store.types.SCOCollection)1