org.datanucleus.cache.L2CachePopulateFieldManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-core Show documentation
Show all versions of datanucleus-core Show documentation
DataNucleus Core provides the primary components of a heterogenous Java persistence solution.
It supports persistence API's being layered on top of the core functionality.
/**********************************************************************
Copyright (c) 2011 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus.cache;
import static org.datanucleus.cache.L2CacheRetrieveFieldManager.newContainer;
import java.util.Map.Entry;
import org.datanucleus.ExecutionContext;
import org.datanucleus.PropertyNames;
import org.datanucleus.api.ApiAdapter;
import org.datanucleus.cache.CachedPC.CachedId;
import org.datanucleus.identity.IdentityUtils;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.FieldPersistenceModifier;
import org.datanucleus.metadata.MetaDataUtils;
import org.datanucleus.metadata.RelationType;
import org.datanucleus.state.DNStateManager;
import org.datanucleus.store.fieldmanager.AbstractFieldManager;
import org.datanucleus.store.types.SCO;
import org.datanucleus.store.types.SCOContainer;
import org.datanucleus.store.types.SCOUtils;
import org.datanucleus.store.types.TypeManager;
import org.datanucleus.store.types.containers.ElementContainerAdapter;
import org.datanucleus.store.types.containers.ElementContainerHandler;
import org.datanucleus.store.types.containers.MapContainerAdapter;
import org.datanucleus.store.types.containers.MapHandler;
import org.datanucleus.store.types.containers.SequenceAdapter;
import org.datanucleus.store.types.wrappers.Collection;
import org.datanucleus.util.NucleusLogger;
/**
* FieldManager responsible for populating the provided CachedPC object.
*/
public class L2CachePopulateFieldManager extends AbstractFieldManager
{
private static final Object[] EMPTY_ARRAY = {};
/** StateManager of the object we are copying values from. */
DNStateManager sm;
ExecutionContext ec;
/** CachedPC that we are copying values into. */
CachedPC cachedPC;
public L2CachePopulateFieldManager(DNStateManager sm, CachedPC cachedpc)
{
this.sm = sm;
this.ec = sm.getExecutionContext();
this.cachedPC = cachedpc;
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeBooleanField(int, boolean)
*/
@Override
public void storeBooleanField(int fieldNumber, boolean value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeCharField(int, char)
*/
@Override
public void storeCharField(int fieldNumber, char value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeByteField(int, byte)
*/
@Override
public void storeByteField(int fieldNumber, byte value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeShortField(int, short)
*/
@Override
public void storeShortField(int fieldNumber, short value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeIntField(int, int)
*/
@Override
public void storeIntField(int fieldNumber, int value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeLongField(int, long)
*/
@Override
public void storeLongField(int fieldNumber, long value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeFloatField(int, float)
*/
@Override
public void storeFloatField(int fieldNumber, float value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeDoubleField(int, double)
*/
@Override
public void storeDoubleField(int fieldNumber, double value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeStringField(int, java.lang.String)
*/
@Override
public void storeStringField(int fieldNumber, String value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true);
cachedPC.setFieldValue(fieldNumber, value);
}
/* (non-Javadoc)
* @see org.datanucleus.store.fieldmanager.AbstractFieldManager#storeObjectField(int, java.lang.Object)
*/
@Override
public void storeObjectField(int fieldNumber, Object value)
{
AbstractMemberMetaData mmd = sm.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
if (mmd.getPersistenceModifier() == FieldPersistenceModifier.TRANSACTIONAL)
{
// Cannot cache transactional fields
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (!mmd.isCacheable())
{
// Field is marked as not cacheable so unset its loaded flag and return null
cachedPC.setLoadedField(fieldNumber, false);
return;
}
if (value != null && value instanceof Collection)
{
// Field value is a DN Collection instance, so not instantiated as set / list hence cannot store since we would then not be able to instantiate the specific type on retrieval
cachedPC.setLoadedField(fieldNumber, false);
return;
}
cachedPC.setLoadedField(fieldNumber, true); // Overridden later if necessary
if (value == null)
{
cachedPC.setFieldValue(fieldNumber, null);
}
else if (mmd.hasContainer())
{
// 1-N, M-N Container
processContainer(fieldNumber, value, mmd);
}
else
{
processField(fieldNumber, value, mmd);
}
}
private Object getCacheableIdForId(ApiAdapter api, Object pc)
{
if (pc == null)
{
return null;
}
Object id = api.getIdForObject(pc);
if (IdentityUtils.isDatastoreIdentity(id) || IdentityUtils.isSingleFieldIdentity(id))
{
return id;
}
// Doesn't store the class name, so wrap it in a CachedId
return new CachedId(pc.getClass().getName(), id);
}
private void processContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd)
{
Object unwrappedContainer = container;
if (container instanceof SCOContainer)
{
if (!((SCOContainer)container).isLoaded())
{
// Contents not loaded so just mark as unloaded
cachedPC.setLoadedField(fieldNumber, false);
return;
}
unwrappedContainer = ((SCO)container).getValue();
}
TypeManager typeManager = sm.getExecutionContext().getTypeManager();
if (mmd.hasMap())
{
MapHandler mapHandler = typeManager.getContainerHandler(mmd.getType());
processMapContainer(fieldNumber, unwrappedContainer, mmd, mapHandler);
}
else
{
ElementContainerHandler elementContainerHandler = typeManager.getContainerHandler(mmd.getType());
processElementContainer(fieldNumber, unwrappedContainer, mmd, elementContainerHandler);
}
}
private void processMapContainer(int fieldNumber, Object mapContainer, AbstractMemberMetaData mmd, MapHandler