use of org.datanucleus.identity.IdentityStringTranslator in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method findObjectsById.
/**
* Accessor for objects with the specified identities.
* @param identities Ids of the object(s).
* @param validate Whether to validate the object state
* @return The Objects with these ids (same order)
* @throws NucleusObjectNotFoundException if an object doesn't exist in the datastore
*/
public Object[] findObjectsById(Object[] identities, boolean validate) {
if (identities == null) {
return null;
} else if (identities.length == 1) {
return new Object[] { findObject(identities[0], validate, validate, null) };
}
for (int i = 0; i < identities.length; i++) {
if (identities[i] == null) {
throw new NucleusUserException(Localiser.msg("010044"));
}
}
// Set the identities array
Object[] ids = new Object[identities.length];
for (int i = 0; i < identities.length; i++) {
// Translate the identity if required
if (identities[i] instanceof String) {
IdentityStringTranslator idStringTranslator = getNucleusContext().getIdentityManager().getIdentityStringTranslator();
if (idStringTranslator != null) {
// DataNucleus extension to translate input identities into valid persistent identities.
ids[i] = idStringTranslator.getIdentity(this, (String) identities[i]);
continue;
}
}
ids[i] = identities[i];
}
Map pcById = new HashMap(identities.length);
List idsToFind = new ArrayList();
ApiAdapter api = getApiAdapter();
// Check the L1 cache
for (int i = 0; i < ids.length; i++) {
Object pc = getObjectFromLevel1Cache(ids[i]);
if (pc != null) {
if (ids[i] instanceof SCOID) {
if (api.isPersistent(pc) && !api.isNew(pc) && !api.isDeleted(pc) && !api.isTransactional(pc)) {
// JDO [5.4.4] Can't return HOLLOW nondurable objects
throw new NucleusUserException(Localiser.msg("010005"));
}
}
pcById.put(ids[i], pc);
} else {
idsToFind.add(ids[i]);
}
}
if (!idsToFind.isEmpty() && l2CacheEnabled) {
// Check the L2 cache for those not found
Map pcsById = getObjectsFromLevel2Cache(idsToFind);
if (!pcsById.isEmpty()) {
// Found some so add to the values, and remove from the "toFind" list
Iterator<Map.Entry> entryIter = pcsById.entrySet().iterator();
while (entryIter.hasNext()) {
Map.Entry entry = entryIter.next();
pcById.put(entry.getKey(), entry.getValue());
idsToFind.remove(entry.getKey());
}
}
}
boolean performValidationWhenCached = nucCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_FIND_OBJECT_VALIDATE_WHEN_CACHED);
List<ObjectProvider> opsToValidate = new ArrayList<>();
if (validate) {
if (performValidationWhenCached) {
// Mark all ObjectProviders for validation (performed at end)
Collection pcValues = pcById.values();
for (Object pc : pcValues) {
if (api.isTransactional(pc)) {
// This object is transactional, so no need to validate
continue;
}
// Mark this object for validation
ObjectProvider op = findObjectProvider(pc);
opsToValidate.add(op);
}
}
}
Object[] foundPcs = null;
if (!idsToFind.isEmpty()) {
// Try to find unresolved objects direct from the datastore if supported by the datastore (e.g ODBMS)
foundPcs = getStoreManager().getPersistenceHandler().findObjects(this, idsToFind.toArray());
}
int foundPcIdx = 0;
for (Object id : idsToFind) {
// Id target class could change due to inheritance level
Object idOrig = id;
Object pc = foundPcs != null ? foundPcs[foundPcIdx++] : null;
ObjectProvider op = null;
if (pc != null) {
// Object created by store plugin
op = findObjectProvider(pc);
putObjectIntoLevel1Cache(op);
} else {
// Object not found yet, so maybe class name is not correct inheritance level
ClassDetailsForId details = getClassDetailsForId(id, null, validate);
String className = details.className;
id = details.id;
if (details.pc != null) {
// Found in cache from updated id
pc = details.pc;
op = findObjectProvider(pc);
if (performValidationWhenCached && validate) {
if (!api.isTransactional(pc)) {
// Mark this object for validation
opsToValidate.add(op);
}
}
} else {
// Still not found so create a Hollow instance with the supplied field values
try {
Class pcClass = clr.classForName(className, (id instanceof DatastoreId) ? null : id.getClass().getClassLoader());
if (Modifier.isAbstract(pcClass.getModifiers())) {
// This class is abstract so impossible to have an instance of this type
throw new NucleusObjectNotFoundException(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id), className));
}
op = nucCtx.getObjectProviderFactory().newForHollow(this, pcClass, id);
pc = op.getObject();
if (!validate) {
// Mark the ObjectProvider as needing to validate this object before loading fields
op.markForInheritanceValidation();
}
// Cache it in case we have bidir relations
putObjectIntoLevel1Cache(op);
} catch (ClassNotResolvedException e) {
NucleusLogger.PERSISTENCE.warn(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id)));
throw new NucleusUserException(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id)), e);
}
if (validate) {
// Mark this object for validation
opsToValidate.add(op);
}
}
}
// Put in map under input id, so we find it later
pcById.put(idOrig, pc);
}
if (!opsToValidate.isEmpty()) {
// Validate the objects that need it
try {
getStoreManager().getPersistenceHandler().locateObjects(opsToValidate.toArray(new ObjectProvider[opsToValidate.size()]));
} catch (NucleusObjectNotFoundException nonfe) {
NucleusObjectNotFoundException[] nonfes = (NucleusObjectNotFoundException[]) nonfe.getNestedExceptions();
if (nonfes != null) {
for (int i = 0; i < nonfes.length; i++) {
Object missingId = nonfes[i].getFailedObject();
removeObjectFromLevel1Cache(missingId);
}
}
throw nonfe;
}
}
Object[] objs = new Object[ids.length];
for (int i = 0; i < ids.length; i++) {
Object id = ids[i];
objs[i] = pcById.get(id);
}
return objs;
}
use of org.datanucleus.identity.IdentityStringTranslator in project datanucleus-core by datanucleus.
the class ExecutionContextImpl method findObject.
/**
* Accessor for an object given the object id. If validate is false, we return the object
* if found in the cache, or otherwise a Hollow object with that id. If validate is true
* we check with the datastore and return an object with the FetchPlan fields loaded.
* TODO Would be nice, when using checkInheritance, to be able to specify the "id" is an instance of class X or subclass. See IdentityUtils where we have the min class
* @param id Id of the object.
* @param validate Whether to validate the object state
* @param checkInheritance Whether look to the database to determine which class this object is.
* @param objectClassName Class name for the object with this id (if known, optional)
* @return The Object with this id
* @throws NucleusObjectNotFoundException if the object doesn't exist in the datastore
*/
public Object findObject(Object id, boolean validate, boolean checkInheritance, String objectClassName) {
if (id == null) {
throw new NucleusUserException(Localiser.msg("010044"));
}
IdentityStringTranslator translator = getNucleusContext().getIdentityManager().getIdentityStringTranslator();
if (translator != null && id instanceof String) {
// DataNucleus extension to translate input identities into valid persistent identities.
id = translator.getIdentity(this, (String) id);
}
ApiAdapter api = getApiAdapter();
boolean fromCache = false;
// try to find object in cache(s)
Object pc = getObjectFromCache(id);
ObjectProvider op = null;
if (pc != null) {
// Found in L1/L2 cache
fromCache = true;
if (id instanceof SCOID) {
if (api.isPersistent(pc) && !api.isNew(pc) && !api.isDeleted(pc) && !api.isTransactional(pc)) {
// JDO [5.4.4] Cant return HOLLOW nondurable objects
throw new NucleusUserException(Localiser.msg("010005"));
}
}
if (api.isTransactional(pc)) {
// JDO [12.6.5] If there's already an object with the same id and it's transactional, return it
return pc;
}
op = findObjectProvider(pc);
} else {
// Find it direct from the store if the store supports that
pc = getStoreManager().getPersistenceHandler().findObject(this, id);
if (pc != null) {
op = findObjectProvider(pc);
putObjectIntoLevel1Cache(op);
putObjectIntoLevel2Cache(op, false);
} else {
// Object not found yet, so maybe class name is not correct inheritance level
ClassDetailsForId details = getClassDetailsForId(id, objectClassName, checkInheritance);
String className = details.className;
id = details.id;
if (details.pc != null) {
// Found during inheritance check via the cache
pc = details.pc;
op = findObjectProvider(pc);
fromCache = true;
} else {
// Still not found, so create a Hollow instance with supplied PK values if possible
try {
Class pcClass = clr.classForName(className, (id instanceof DatastoreId) ? null : id.getClass().getClassLoader());
if (Modifier.isAbstract(pcClass.getModifiers())) {
// This class is abstract so impossible to have an instance of this type
throw new NucleusObjectNotFoundException(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id), className));
}
op = nucCtx.getObjectProviderFactory().newForHollow(this, pcClass, id);
pc = op.getObject();
if (!checkInheritance && !validate) {
// Mark the ObjectProvider as needing to validate this object before loading fields
op.markForInheritanceValidation();
}
// Cache the object in case we have bidirectional relations that would need to find this
putObjectIntoLevel1Cache(op);
} catch (ClassNotResolvedException e) {
NucleusLogger.PERSISTENCE.warn(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id)));
throw new NucleusUserException(Localiser.msg("010027", IdentityUtils.getPersistableIdentityForId(id)), e);
}
}
}
}
boolean performValidationWhenCached = nucCtx.getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_FIND_OBJECT_VALIDATE_WHEN_CACHED);
if (validate && (!fromCache || performValidationWhenCached)) {
// loading any fetchplan fields that are needed in the process.
if (!fromCache) {
// Cache the object in case we have bidirectional relations that would need to find this
putObjectIntoLevel1Cache(op);
}
try {
op.validate();
if (op.getObject() != pc) {
// Underlying object was changed in the validation process. This can happen when the datastore
// is responsible for managing object references and it no longer recognises the cached value.
fromCache = false;
pc = op.getObject();
putObjectIntoLevel1Cache(op);
}
} catch (NucleusObjectNotFoundException onfe) {
// Object doesn't exist, so remove from L1 cache
removeObjectFromLevel1Cache(op.getInternalObjectId());
throw onfe;
}
}
if (!fromCache) {
// Cache the object (update it if already present)
putObjectIntoLevel2Cache(op, false);
}
return pc;
}
Aggregations