Search in sources :

Example 1 with SetStore

use of org.datanucleus.store.types.scostore.SetStore in project datanucleus-rdbms by datanucleus.

the class AbstractContainerMapping method replaceFieldWithWrapper.

/**
 * Method to replace the field that this mapping represents with a SCO wrapper.
 * The wrapper will be suitable for the passed instantiated type and if it is null will be for the declared type of the field.
 * @param op ObjectProvider for the owning object
 * @param value The value to create the wrapper with
 * @return The SCO wrapper object that the field was replaced with
 */
protected SCO replaceFieldWithWrapper(ObjectProvider op, Object value) {
    Class type = mmd.getType();
    if (value != null) {
        type = value.getClass();
    } else if (mmd.getOrderMetaData() != null && type.isAssignableFrom(java.util.List.class)) {
        type = java.util.List.class;
        Store backingStore = storeMgr.getExistingBackingStoreForMember(mmd);
        if (backingStore != null && (backingStore instanceof SetStore)) {
            // Member has already been instantiated as Set-based, so don't use List
            type = mmd.getType();
        }
    }
    ExecutionContext ec = op.getExecutionContext();
    if (mmd.getAbsoluteFieldNumber() < 0) {
        // The metadata being used here is an embedded form, so swap for the real one
        mmd = ec.getMetaDataManager().getMetaDataForClass(mmd.getClassName(true), ec.getClassLoaderResolver()).getMetaDataForMember(mmd.getName());
    }
    return ec.getTypeManager().createSCOInstance(op, mmd, type, value, true);
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) Store(org.datanucleus.store.types.scostore.Store) SetStore(org.datanucleus.store.types.scostore.SetStore) SetStore(org.datanucleus.store.types.scostore.SetStore)

Example 2 with SetStore

use of org.datanucleus.store.types.scostore.SetStore in project datanucleus-core by datanucleus.

the class SCOUtils method populateMapDelegateWithStoreData.

/**
 * Convenience method to populate the passed delegate Map with the keys/values from the associated Store.
 * <P>
 * The issue here is that we need to load the keys and values in as few calls as possible. The method
 * employed here reads in the keys (if persistable), then the values (if persistable), and then the
 * "entries" (ids of keys and values) so we can associate the keys to the values.
 * @param delegate The delegate
 * @param store The Store
 * @param ownerOP ObjectProvider of the owner of the map.
 */
public static void populateMapDelegateWithStoreData(Map delegate, MapStore store, ObjectProvider ownerOP) {
    // If we have persistable keys then load them. The keys query will pull in the key fetch plan
    // so this instantiates them in the cache
    java.util.Set keys = new java.util.HashSet();
    if (!store.keysAreEmbedded() && !store.keysAreSerialised()) {
        // Retrieve the persistable keys
        SetStore keystore = store.keySetStore();
        Iterator keyIter = keystore.iterator(ownerOP);
        while (keyIter.hasNext()) {
            keys.add(keyIter.next());
        }
    }
    // If we have persistable values then load them. The values query will pull in the value fetch plan
    // so this instantiates them in the cache
    java.util.List values = new java.util.ArrayList();
    if (!store.valuesAreEmbedded() && !store.valuesAreSerialised()) {
        // Retrieve the persistable values
        CollectionStore valuestore = store.valueCollectionStore();
        Iterator valueIter = valuestore.iterator(ownerOP);
        while (valueIter.hasNext()) {
            values.add(valueIter.next());
        }
    }
    // Retrieve the entries (key-value pairs so we can associate them)
    // TODO Ultimately would like to just call this, but the entry query can omit the inheritance level of a key or value
    SetStore entries = store.entrySetStore();
    Iterator entryIter = entries.iterator(ownerOP);
    while (entryIter.hasNext()) {
        Map.Entry entry = (Map.Entry) entryIter.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
        delegate.put(key, value);
    }
    if (!store.keysAreEmbedded() && !store.keysAreSerialised() && delegate.size() != keys.size()) {
        // With Derby 10.x we can get instances where the values query returns no values yet entries is not empty TODO Maybe make this throw an exception
        NucleusLogger.DATASTORE_RETRIEVE.warn("The number of Map key objects (" + keys.size() + ")" + " was different to the number of entries (" + delegate.size() + ")." + " Likely there is a bug in your datastore");
    }
    if (!store.valuesAreEmbedded() && !store.valuesAreSerialised() && delegate.size() != values.size()) {
        // With Derby 10.x we can get instances where the values query returns no values yet entries is not empty TODO Maybe make this throw an exception
        NucleusLogger.DATASTORE_RETRIEVE.warn("The number of Map value objects (" + values.size() + ")" + " was different to the number of entries (" + delegate.size() + ")." + " Likely there is a bug in your datastore, or you have null values?");
    }
    keys.clear();
    values.clear();
}
Also used : ArrayList(java.util.ArrayList) Set(java.util.Set) SetStore(org.datanucleus.store.types.scostore.SetStore) List(java.util.List) Iterator(java.util.Iterator) CollectionStore(org.datanucleus.store.types.scostore.CollectionStore) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) HashSet(java.util.HashSet)

Example 3 with SetStore

use of org.datanucleus.store.types.scostore.SetStore in project datanucleus-rdbms by datanucleus.

the class AbstractContainerMapping method replaceFieldWithWrapper.

/**
 * Method to replace the field that this mapping represents with a SCO wrapper.
 * The wrapper will be suitable for the passed instantiated type and if it is null will be for the declared type of the field.
 * @param sm StateManager for the owning object
 * @param value The value to create the wrapper with
 * @return The SCO wrapper object that the field was replaced with
 */
protected SCO replaceFieldWithWrapper(DNStateManager sm, Object value) {
    Class type = mmd.getType();
    if (value != null) {
        type = value.getClass();
    } else if (mmd.getOrderMetaData() != null && type.isAssignableFrom(java.util.List.class)) {
        type = java.util.List.class;
        Store backingStore = storeMgr.getExistingBackingStoreForMember(mmd);
        if (backingStore != null && (backingStore instanceof SetStore)) {
            // Member has already been instantiated as Set-based, so don't use List
            type = mmd.getType();
        }
    }
    ExecutionContext ec = sm.getExecutionContext();
    if (mmd.getAbsoluteFieldNumber() < 0) {
        // The metadata being used here is an embedded form, so swap for the real one
        mmd = ec.getMetaDataManager().getMetaDataForClass(mmd.getClassName(true), ec.getClassLoaderResolver()).getMetaDataForMember(mmd.getName());
    }
    return ec.getTypeManager().createSCOInstance(sm, mmd, type, value, true);
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) Store(org.datanucleus.store.types.scostore.Store) SetStore(org.datanucleus.store.types.scostore.SetStore) SetStore(org.datanucleus.store.types.scostore.SetStore)

Aggregations

SetStore (org.datanucleus.store.types.scostore.SetStore)3 ExecutionContext (org.datanucleus.ExecutionContext)2 Store (org.datanucleus.store.types.scostore.Store)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 CollectionStore (org.datanucleus.store.types.scostore.CollectionStore)1