Search in sources :

Example 1 with MapContainerAdapter

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

the class PersistFieldManager method processMapContainer.

private void processMapContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd) {
    TypeManager typeManager = op.getExecutionContext().getTypeManager();
    ContainerHandler<Object, MapContainerAdapter<Object>> containerHandler = typeManager.getContainerHandler(mmd.getType());
    ApiAdapter api = op.getExecutionContext().getApiAdapter();
    // Process all keys, values of the Map that are PC
    MapContainerAdapter<Object> mapAdapter = containerHandler.getAdapter(container);
    for (Entry<Object, Object> entry : mapAdapter.entries()) {
        Object mapKey = entry.getKey();
        Object mapValue = entry.getValue();
        Object newMapKey = mapKey;
        Object newMapValue = mapValue;
        if (api.isPersistable(mapKey)) {
            // Persist (or attach) the key
            int mapKeyObjectType = mmd.getMap().isEmbeddedKey() || mmd.getMap().isSerializedKey() ? ObjectProvider.EMBEDDED_MAP_KEY_PC : ObjectProvider.PC;
            newMapKey = processPersistable(mapKey, fieldNumber, mapKeyObjectType);
        }
        if (api.isPersistable(mapValue)) {
            // Persist (or attach) the value
            int mapValueObjectType = mmd.getMap().isEmbeddedValue() || mmd.getMap().isSerializedValue() ? ObjectProvider.EMBEDDED_MAP_VALUE_PC : ObjectProvider.PC;
            newMapValue = processPersistable(mapValue, fieldNumber, mapValueObjectType);
        }
        if (newMapKey != mapKey || newMapValue != mapValue) {
            // Maybe we have just have attached key or value
            boolean updateKey = false;
            boolean updateValue = false;
            if (newMapKey != mapKey) {
                ObjectProvider keyOP = op.getExecutionContext().findObjectProvider(newMapKey);
                if (keyOP.getReferencedPC() != null) {
                    // Attaching the key
                    updateKey = true;
                }
            }
            if (newMapValue != mapValue) {
                ObjectProvider valOP = op.getExecutionContext().findObjectProvider(newMapValue);
                if (valOP.getReferencedPC() != null) {
                    // Attaching the value
                    updateValue = true;
                }
            }
            if (updateKey) {
                mapAdapter.remove(mapKey);
                mapAdapter.put(newMapKey, updateValue ? newMapValue : mapValue);
            } else if (updateValue) {
                mapAdapter.put(mapKey, newMapValue);
            }
        }
    }
}
Also used : MapContainerAdapter(org.datanucleus.store.types.MapContainerAdapter) ApiAdapter(org.datanucleus.api.ApiAdapter) TypeManager(org.datanucleus.store.types.TypeManager) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 2 with MapContainerAdapter

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

the class L2CacheRetrieveFieldManager method processMapContainer.

private Object processMapContainer(int fieldNumber, Object cachedMapContainer, AbstractMemberMetaData mmd, ContainerHandler<Object, MapContainerAdapter<Object>> containerHandler) {
    // Map field, with fieldValue being Map<OID, OID>
    try {
        // Create Map<Key, Value> of same type as fieldValue
        MapContainerAdapter<Object> cachedMapContainerAdapter = containerHandler.getAdapter(cachedMapContainer);
        Object newContainer = newContainer(cachedMapContainer, mmd, containerHandler);
        MapContainerAdapter fieldMapContainerAdapter = containerHandler.getAdapter(newContainer);
        boolean keyIsPersistent = mmd.getMap().keyIsPersistent();
        boolean keyIsEmbedded = mmd.getMap().isEmbeddedKey();
        boolean keyIsSerialised = mmd.getMap().isSerializedKey();
        boolean valueIsPersistent = mmd.getMap().valueIsPersistent();
        boolean valueIsEmbedded = mmd.getMap().isEmbeddedValue();
        boolean valueIsSerialised = mmd.getMap().isSerializedValue();
        for (Entry<Object, Object> entry : cachedMapContainerAdapter.entries()) {
            Object mapKey = null;
            if (keyIsPersistent) {
                if (keyIsEmbedded || keyIsSerialised || mmd.isSerialized()) {
                    mapKey = convertCachedPCToPersistable((CachedPC) entry.getKey(), fieldNumber);
                } else {
                    mapKey = getObjectFromCachedId(entry.getKey());
                }
            } else {
                mapKey = entry.getKey();
            }
            Object mapValue = null;
            Object mapValueId = entry.getValue();
            if (mapValueId != null) {
                if (valueIsPersistent) {
                    if (valueIsEmbedded || valueIsSerialised || mmd.isSerialized()) {
                        mapValue = convertCachedPCToPersistable((CachedPC) entry.getValue(), fieldNumber);
                    } else {
                        mapValue = getObjectFromCachedId(entry.getValue());
                    }
                } else {
                    mapValue = entry.getValue();
                }
            }
            fieldMapContainerAdapter.put(mapKey, mapValue);
        }
        return SCOUtils.wrapSCOField(op, fieldNumber, fieldMapContainerAdapter.getContainer(), true);
    } catch (Exception e) {
        // Error creating field value
        if (fieldsNotLoaded == null) {
            fieldsNotLoaded = new ArrayList<Integer>();
        }
        fieldsNotLoaded.add(fieldNumber);
        NucleusLogger.CACHE.error("Exception thrown creating value for" + " field " + mmd.getFullFieldName() + " of type " + cachedMapContainer.getClass().getName(), e);
        return null;
    }
}
Also used : MapContainerAdapter(org.datanucleus.store.types.MapContainerAdapter) ArrayList(java.util.ArrayList) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException)

Example 3 with MapContainerAdapter

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

the class DetachFieldManager method processMapContainer.

private Object processMapContainer(int fieldNumber, Object mapContainer, AbstractMemberMetaData mmd, ContainerHandler<Object, MapContainerAdapter<Object>> containerHandler) {
    Object detachedMapContainer;
    // TODO If cascadeDetach is false then don't do this
    MapContainerAdapter<Object> mapAdapter = containerHandler.getAdapter(mapContainer);
    if (copy) {
        detachedMapContainer = containerHandler.newContainer(mmd);
        MapMetaData mapMd = mmd.getMap();
        MapContainerAdapter copyAdapter = containerHandler.getAdapter(detachedMapContainer);
        for (Entry<Object, Object> entry : mapAdapter.entries()) {
            Object key = entry.getKey();
            if (mapMd.keyIsPersistent()) {
                key = processPersistableCopy(key);
            }
            Object value = entry.getValue();
            if (mapMd.valueIsPersistent()) {
                value = processPersistableCopy(value);
            }
            copyAdapter.put(key, value);
        }
        // Get the updated version of the container
        detachedMapContainer = copyAdapter.getContainer();
    } else {
        detachedMapContainer = mapContainer;
        MapMetaData mapMd = mmd.getMap();
        for (Entry<Object, Object> entry : mapAdapter.entries()) {
            Object key = entry.getKey();
            if (mapMd.keyIsPersistent()) {
                processPersistable(key);
            }
            Object value = entry.getValue();
            if (mapMd.valueIsPersistent()) {
                processPersistable(value);
            }
        }
    }
    return detachedMapContainer;
}
Also used : MapContainerAdapter(org.datanucleus.store.types.MapContainerAdapter) MapMetaData(org.datanucleus.metadata.MapMetaData)

Aggregations

MapContainerAdapter (org.datanucleus.store.types.MapContainerAdapter)3 ArrayList (java.util.ArrayList)1 ApiAdapter (org.datanucleus.api.ApiAdapter)1 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)1 MapMetaData (org.datanucleus.metadata.MapMetaData)1 ObjectProvider (org.datanucleus.state.ObjectProvider)1 TypeManager (org.datanucleus.store.types.TypeManager)1