Search in sources :

Example 1 with SequenceAdapter

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

the class PersistFieldManager method processElementContainer.

private void processElementContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd) {
    TypeManager typeManager = op.getExecutionContext().getTypeManager();
    ElementContainerHandler<Object, ElementContainerAdapter<Object>> elementContainerHandler = typeManager.getContainerHandler(mmd.getType());
    // Process all elements of the container that are PC
    ElementContainerAdapter containerAdapter = elementContainerHandler.getAdapter(container);
    ApiAdapter api = op.getExecutionContext().getApiAdapter();
    int objectType = elementContainerHandler.getObjectType(mmd);
    if (objectType == ObjectProvider.PC) {
        int elementPosition = 0;
        for (Object element : containerAdapter) {
            if (api.isPersistable(element)) {
                Object newElement = processPersistable(element, -1, objectType);
                ObjectProvider elementSM = op.getExecutionContext().findObjectProvider(newElement);
                if (elementSM.getReferencedPC() != null) {
                    // Must be attaching this element, so swap element (detached -> attached)
                    if (containerAdapter instanceof SequenceAdapter) {
                        ((SequenceAdapter) containerAdapter).update(newElement, elementPosition);
                    } else {
                        containerAdapter.remove(elementSM);
                        containerAdapter.add(newElement);
                    }
                }
            }
            elementPosition++;
        }
    } else {
        // Embedded/Serialized
        for (Object element : containerAdapter) {
            if (api.isPersistable(element)) {
                processPersistable(element, fieldNumber, objectType);
            }
        }
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) ElementContainerAdapter(org.datanucleus.store.types.ElementContainerAdapter) SequenceAdapter(org.datanucleus.store.types.SequenceAdapter) TypeManager(org.datanucleus.store.types.TypeManager) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 2 with SequenceAdapter

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

the class L2CachePopulateFieldManager method processElementContainer.

private void processElementContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd, ElementContainerHandler<Object, ElementContainerAdapter<Object>> containerHandler) {
    RelationType relType = mmd.getRelationType(ec.getClassLoaderResolver());
    if (relType == RelationType.NONE) {
        // Container<Non-PC>
        boolean isContainerMutable = ec.getTypeManager().isSecondClassMutableType(mmd.getType().getName());
        String elementType = containerHandler.getElementTypeName(mmd.getContainer());
        boolean isElementMutable = ec.getTypeManager().isSecondClassMutableType(elementType);
        if (isContainerMutable || isElementMutable) {
            ElementContainerAdapter<Object> adapterToCache = containerHandler.getAdapter(newContainer(container, mmd, containerHandler));
            if (isElementMutable) {
                for (Object elementSCO : containerHandler.getAdapter(container)) {
                    adapterToCache.add(SCOUtils.copyValue(elementSCO));
                }
            } else {
                for (Object element : containerHandler.getAdapter(container)) {
                    adapterToCache.add(element);
                }
            }
            cachedPC.setFieldValue(fieldNumber, adapterToCache.getContainer());
        } else {
            // Both container and element are immutable so we can just cache it as it is
            cachedPC.setFieldValue(fieldNumber, container);
        }
    } else {
        // Container<PC>
        ElementContainerAdapter containerAdapter = containerHandler.getAdapter(container);
        if (containerAdapter instanceof SequenceAdapter && mmd.getOrderMetaData() != null && !mmd.getOrderMetaData().isIndexedList()) {
            // Ordered list so don't cache since dependent on datastore-retrieve order
            cachedPC.setLoadedField(fieldNumber, false);
            return;
        }
        try {
            // For arrays[PC] just use an Object array to store the ids because we can use the same type of the original container.
            // Later when restoring the values it will be based on the metadata instead of the actual container type, as opposed to how it's done for non-array containers.
            Object newContainer = mmd.hasArray() ? EMPTY_ARRAY : newContainer(container, mmd, containerHandler);
            ElementContainerAdapter containerToCacheAdapter = containerHandler.getAdapter(newContainer);
            ApiAdapter api = ec.getApiAdapter();
            if ((containerHandler.isSerialised(mmd) || containerHandler.isEmbedded(mmd)) && !ec.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_CACHE_L2_CACHE_EMBEDDED)) {
                // User not caching embedded/serialised
                cachedPC.setLoadedField(fieldNumber, false);
                return;
            }
            for (Object element : containerAdapter) {
                if (containerHandler.isSerialised(mmd) || containerHandler.isEmbedded(mmd)) {
                    // Store embedded/serialised element as nested collection element
                    containerToCacheAdapter.add(convertPersistableToCachedPC(element));
                } else {
                    // Store id of element, since cached in its own right
                    containerToCacheAdapter.add(getCacheableIdForId(api, element));
                }
            }
            cachedPC.setFieldValue(fieldNumber, containerToCacheAdapter.getContainer());
        } catch (Exception e) {
            NucleusLogger.CACHE.warn("Unable to create object of type " + container.getClass().getName() + " for L2 caching : " + e.getMessage());
            // Contents not loaded so just mark as unloaded
            cachedPC.setLoadedField(fieldNumber, false);
        }
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) ElementContainerAdapter(org.datanucleus.store.types.ElementContainerAdapter) RelationType(org.datanucleus.metadata.RelationType) SequenceAdapter(org.datanucleus.store.types.SequenceAdapter)

Aggregations

ApiAdapter (org.datanucleus.api.ApiAdapter)2 ElementContainerAdapter (org.datanucleus.store.types.ElementContainerAdapter)2 SequenceAdapter (org.datanucleus.store.types.SequenceAdapter)2 RelationType (org.datanucleus.metadata.RelationType)1 ObjectProvider (org.datanucleus.state.ObjectProvider)1 TypeManager (org.datanucleus.store.types.TypeManager)1