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);
}
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();
}
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);
}
Aggregations