/** * Constructor, using the ObjectProvider of the "owner" and the field name. * * @param op The owner ObjectProvider * @param mmd Metadata for the member */ public ArrayList(ObjectProvider op, AbstractMemberMetaData mmd) { super(op, mmd); // Set up our delegate this.delegate = new java.util.ArrayList(); ExecutionContext ec = op.getExecutionContext(); allowNulls = SCOUtils.allowNullsInContainer(allowNulls, mmd); queued = ec.isDelayDatastoreOperationsEnabled(); useCache = SCOUtils.useContainerCache(op, mmd); if (!SCOUtils.collectionHasSerialisedElements(mmd) && mmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) { ClassLoaderResolver clr = ec.getClassLoaderResolver(); this.backingStore = (ListStore) ((BackedSCOStoreManager) op.getStoreManager()) .getBackingStoreForField(clr, mmd, java.util.ArrayList.class); } if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( SCOUtils.getContainerInfoMessage( op, ownerMmd.getName(), this, useCache, queued, allowNulls, SCOUtils.useCachedLazyLoading(op, ownerMmd))); } }
/** * Constructor, using the ObjectProvider of the "owner" and the field name. * * @param op The owner ObjectProvider * @param mmd Metadata for the member */ public Map(ObjectProvider op, AbstractMemberMetaData mmd) { super(op, mmd); // Set up our "delegate" this.delegate = new java.util.HashMap(); ExecutionContext ec = ownerOP.getExecutionContext(); allowNulls = SCOUtils.allowNullsInContainer(allowNulls, mmd); useCache = SCOUtils.useContainerCache(ownerOP, mmd); if (!SCOUtils.mapHasSerialisedKeysAndValues(mmd) && mmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT) { ClassLoaderResolver clr = ec.getClassLoaderResolver(); this.backingStore = (MapStore) ((BackedSCOStoreManager) ownerOP.getStoreManager()) .getBackingStoreForField(clr, mmd, java.util.Map.class); } if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( SCOUtils.getContainerInfoMessage( ownerOP, ownerMmd.getName(), this, useCache, allowNulls, SCOUtils.useCachedLazyLoading(ownerOP, ownerMmd))); } }
/** Method to initialise the SCO for use. */ public void initialise() { initialiseDelegate(); if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg( "023003", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + size(), SCOUtils.getSCOWrapperOptionsMessage(true, false, false, false))); } }
/** 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(); // Populate the delegate with the keys/values from the store SCOUtils.populateMapDelegateWithStoreData(delegate, backingStore, ownerOP); isCacheLoaded = true; } }
/** 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 ArrayList. * * @param index The element position. * @return The object that was removed */ public synchronized Object remove(int index) { makeDirty(); if (useCache) { loadFromStore(); } int size = (useCache ? delegate.size() : -1); Object delegateObject = (useCache ? delegate.remove(index) : null); Object backingObject = null; if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { backingObject = delegateObject; ownerOP .getExecutionContext() .addOperationToQueue(new ListRemoveAtOperation(ownerOP, backingStore, index)); } else { try { backingObject = backingStore.remove(ownerOP, index, size); } catch (NucleusDataStoreException dse) { NucleusLogger.PERSISTENCE.warn( Localiser.msg("023013", "remove", ownerMmd.getName(), dse)); backingObject = null; } } } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return (backingStore != null ? backingObject : delegateObject); }
public void initialise(java.util.LinkedHashSet c) { if (c != null) { delegate = c; } else { delegate = new java.util.LinkedHashSet(); } if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023003", this.getClass().getName(), ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + size(), SCOUtils.getSCOWrapperOptionsMessage(true, false, true, false))); } }
/** * Method to initialise the SCO from an existing value. * * @param o Object to set value using. * @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) { java.util.Map m = (java.util.Map) o; if (m != null) { delegate = (java.util.HashMap) m; } else { delegate = new java.util.HashMap(); } if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( LOCALISER.msg( "023003", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + size(), SCOUtils.getSCOWrapperOptionsMessage(true, false, true, false))); } }
/** * Method to initialise the SCO from an existing value. * * @param c 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(java.util.PriorityQueue c, boolean forInsert, boolean forUpdate) { if (c != null) { initialiseDelegate(); delegate.addAll(c); } else { initialiseDelegate(); } if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023003", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + size(), SCOUtils.getSCOWrapperOptionsMessage(true, false, false, false))); } }
/** * Method to remove a collection of elements from the List. * * @param elements Collection of elements to remove * @return Whether it was successful. */ public boolean removeAll(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 (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; } if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) { ownerOP.getExecutionContext().processNontransactionalUpdate(); } return delegateSuccess; }
/** * 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 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 initialise the SCO from an existing value. * * @param m 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 synchronized void initialise(java.util.Map m, boolean forInsert, boolean forUpdate) { if (m != null) { // Check for the case of serialised maps, and assign ObjectProviders to any PC keys/values // without if (SCOUtils.mapHasSerialisedKeysAndValues(ownerMmd) && (ownerMmd.getMap().keyIsPersistent() || ownerMmd.getMap().valueIsPersistent())) { ExecutionContext ec = ownerOP.getExecutionContext(); Iterator iter = m.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object value = entry.getValue(); if (ownerMmd.getMap().keyIsPersistent()) { ObjectProvider objSM = ec.findObjectProvider(key); if (objSM == null) { objSM = ec.getNucleusContext() .getObjectProviderFactory() .newForEmbedded(ec, key, false, ownerOP, ownerMmd.getAbsoluteFieldNumber()); } } if (ownerMmd.getMap().valueIsPersistent()) { ObjectProvider objSM = ec.findObjectProvider(value); if (objSM == null) { objSM = ec.getNucleusContext() .getObjectProviderFactory() .newForEmbedded(ec, value, 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(), "" + m.size())); } makeDirty(); if (useCache) { // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry) loadFromStore(); } if (backingStore != null) { if (SCOUtils.useQueuedUpdate(ownerOP)) { Iterator iter = m.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); ownerOP .getExecutionContext() .addOperationToQueue( new MapPutOperation(ownerOP, backingStore, entry.getKey(), entry.getValue())); } } else { backingStore.putAll(ownerOP, m); } } delegate.putAll(m); } else if (forUpdate) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size())); } // TODO This is clear+putAll. Improve it to work out what is changed delegate.clear(); if (backingStore != null) { if (SCOUtils.useQueuedUpdate(ownerOP)) { // If not yet flushed to store then no need to add to queue (since will be handled via // insert) if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) { ownerOP .getExecutionContext() .addOperationToQueue(new MapClearOperation(ownerOP, backingStore)); } } else { backingStore.clear(ownerOP); } } if (useCache) { // Make sure we have all values loaded (e.g if in optimistic tx and we put new entry) loadFromStore(); } if (backingStore != null) { if (SCOUtils.useQueuedUpdate(ownerOP)) { // If not yet flushed to store then no need to add to queue (since will be handled via // insert) if (ownerOP.isFlushedToDatastore() || !ownerOP.getLifecycleState().isNew()) { Iterator iter = m.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); ownerOP .getExecutionContext() .addOperationToQueue( new MapPutOperation( ownerOP, backingStore, entry.getKey(), entry.getValue())); } } } else { backingStore.putAll(ownerOP, m); } } delegate.putAll(m); makeDirty(); } else { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + m.size())); } delegate.clear(); delegate.putAll(m); } } }
/** * 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); } } }
/** * Method to initialise the SCO from an existing value. * * @param c The object to set from * @param forInsert Whether the object needs inserting in the datastore with this value * @param forUpdate Whether to update the SCO in the datastore with this value */ public void initialise(java.util.ArrayList c, boolean forInsert, boolean forUpdate) { 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 objOP = ec.findObjectProvider(pc); if (objOP == null) { objOP = ec.getNucleusContext() .getObjectProviderFactory() .newForEmbedded(ec, pc, false, ownerOP, ownerMmd.getAbsoluteFieldNumber()); } } } if (backingStore != null && useCache && !isCacheLoaded) { // Mark as loaded so we just use the value isCacheLoaded = true; } if (forInsert) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } if (useCache) { loadFromStore(); } 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)); } } } makeDirty(); delegate.addAll(c); } else if (forUpdate) { if (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023008", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } // TODO This does clear+addAll : Improve this and work out which elements are added and // which deleted if (backingStore != null) { if (SCOUtils.useQueuedUpdate(queued, ownerOP)) { ownerOP .getExecutionContext() .addOperationToQueue(new CollectionClearOperation(ownerOP, backingStore)); } else { backingStore.clear(ownerOP); } } if (useCache) { loadFromStore(); } 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 (NucleusLogger.PERSISTENCE.isDebugEnabled()) { NucleusLogger.PERSISTENCE.debug( Localiser.msg( "023007", ownerOP.getObjectAsPrintable(), ownerMmd.getName(), "" + c.size())); } delegate.clear(); delegate.addAll(c); } } }