private Cache getCacheForId(Object id) { if (IdentityUtils.isSingleFieldIdentity(id) || IdentityUtils.isDatastoreIdentity(id)) { return getCacheForClass(IdentityUtils.getTargetClassNameForIdentity(id)); } return defaultCache; } }
protected String getCacheKeyForId(Object id) { // Because single-field id doesn't include target class name in toString() if (IdentityUtils.isSingleFieldIdentity(id)) { String targetClassName = ((SingleFieldId)id).getTargetClassName(); return cacheName + targetClassName + ":" + id.toString().hashCode(); } return cacheName + id.toString().hashCode(); } }
public static SingleFieldId getDataNucleusIdentityForSingleFieldIdentity(SingleFieldIdentity sfid) { if (sfid instanceof javax.jdo.identity.LongIdentity) { return new LongId(sfid.getTargetClass(), sfid.toString()); } else if (sfid instanceof javax.jdo.identity.IntIdentity) { return new IntId(sfid.getTargetClass(), sfid.toString()); } else if (sfid instanceof javax.jdo.identity.ShortIdentity) { return new ShortId(sfid.getTargetClass(), sfid.toString()); } else if (sfid instanceof javax.jdo.identity.ByteIdentity) { return new ByteId(sfid.getTargetClass(), sfid.toString()); } else if (sfid instanceof javax.jdo.identity.StringIdentity) { return new StringId(sfid.getTargetClass(), sfid.toString()); } else if (sfid instanceof javax.jdo.identity.CharIdentity) { return new CharId(sfid.getTargetClass(), sfid.toString()); } return new ObjectId(sfid.getTargetClass(), sfid.getKeyAsObject()); }
if (IdentityUtils.isDatastoreIdentity(oid) || IdentityUtils.isSingleFieldIdentity(oid)) if (IdentityUtils.isDatastoreIdentity(oid)) oid = ec.getNucleusContext().getIdentityManager().getDatastoreId(subclasses[i], IdentityUtils.getTargetKeyForDatastoreIdentity(oid)); else if (IdentityUtils.isSingleFieldIdentity(oid)) oid = ec.getNucleusContext().getIdentityManager().getSingleFieldId(oid.getClass(), ec.getClassLoaderResolver().classForName(subclasses[i]), IdentityUtils.getTargetKeyForSingleFieldIdentity(oid));
if (value != null) if (IdentityUtils.isDatastoreIdentity(value)) value = ec.getNucleusContext().getIdentityManager().getDatastoreId(className, IdentityUtils.getTargetKeyForDatastoreIdentity(value)); return ec.findObject(value, false, true, null);
/** * Convenience method to return the (DataNucleus) identity for an object. * @param obj The object * @param acmd MetaData for the object * @param ec execution context * @param odb ODB to use * @return The DataNucleus identity */ public static Object getIdentityForObject(Object obj, AbstractClassMetaData acmd, ExecutionContext ec, ODB odb) { if (acmd.getIdentityType() == IdentityType.DATASTORE) { long datastoreId = odb.getObjectId(obj).getObjectId(); return ec.getNucleusContext().getIdentityManager().getDatastoreId(datastoreId); } else if (acmd.getIdentityType() == IdentityType.APPLICATION) { return ec.getNucleusContext().getIdentityManager().getApplicationId(obj, acmd); } else { // Nondurable identity return null; } } }
if (acmd.getIdentityType() == IdentityType.APPLICATION) { FieldManager fm = new QueryEntityPKFetchFieldManager(acmd, entity); id = IdentityUtils.getApplicationIdentityForResultSetRow(ec, acmd, cls, true, fm); if (key.getName() != null) { id = OIDFactory.getInstance(ec.getNucleusContext(), key.getName()); } else { id = OIDFactory.getInstance(ec.getNucleusContext(), key.getId());
@Override public String getClassNameForObjectID(Object id, ClassLoaderResolver clr, ExecutionContext ec) { String targetClassName = null; if (IdentityUtils.isSingleFieldIdentity(id)) { // Using SingleFieldIdentity so can assume that object is of the target class or a subclass targetClassName = IdentityUtils.getTargetClassNameForIdentity(id); String[] subclasses = getMetaDataManager().getSubclassesForClass(targetClassName, true); if (subclasses == null) { // No subclasses so must be the specified type return targetClassName; } String[] possibleNames = new String[subclasses.length+1]; possibleNames[0] = targetClassName; for (int i=0;i<subclasses.length;i++) { possibleNames[i+1] = subclasses[i]; } return getClassNameForIdentity(ec, possibleNames, id); } return super.getClassNameForObjectID(id, clr, ec); }
protected void processSingleRelationField(Object value, ExecutionContext ec, String fieldName) { Object valuePC = op.getExecutionContext().persistObjectInternal(value, null, -1, -1); Object valueId = ec.getApiAdapter().getIdForObject(valuePC); // TODO Add option to store DBRef here instead of just the id string dbObject.put(fieldName, IdentityUtils.getPersistableIdentityForId(valueId)); // Store the id String form }
@Override public String getClassNameForObjectID(Object id, ClassLoaderResolver clr, ExecutionContext ec) { if (id == null) { // User stupidity return null; } else if (id instanceof SCOID) { // Object is a SCOID return ((SCOID) id).getSCOClass(); } String rootClassName = super.getClassNameForObjectID(id, clr, ec); // TODO Allow for use of users-own PK class in multiple inheritance trees String[] subclasses = getMetaDataManager().getSubclassesForClass(rootClassName, true); if (subclasses == null || subclasses.length == 0) { // No subclasses so no need to go to the datastore return rootClassName; } AbstractClassMetaData rootCmd = getMetaDataManager().getMetaDataForClass(rootClassName, clr); return Neo4jUtils.getClassNameForIdentity(id, rootCmd, ec, clr); }
void evictAllOfClass(String className) { AbstractClassMetaData cmd = nucleusCtx.getMetaDataManager().getMetaDataForClass(className, nucleusCtx.getClassLoaderResolver(null)); Iterator keyIter = cache.getKeys().iterator(); while (keyIter.hasNext()) { Object key = keyIter.next(); if (cmd.getIdentityType() == IdentityType.APPLICATION) { String targetClassName = IdentityUtils.getTargetClassNameForIdentity(key); if (className.equals(targetClassName)) { keyIter.remove(); } } else if (cmd.getIdentityType() == IdentityType.DATASTORE) { String targetClassName = IdentityUtils.getTargetClassNameForIdentity(key); if (className.equals(targetClassName)) { keyIter.remove(); } } } }
Class keyType = IdentityUtils.getKeyTypeForSingleFieldIdentityType(objectIdClass); idObj = ClassUtils.convertValue(idObj, keyType); return ec.getNucleusContext().getIdentityManager().getSingleFieldId(objectIdClass, pcClass, idObj);
public static Key getPrimaryKeyAsKey(ApiAdapter apiAdapter, ObjectProvider op) { if (op.getClassMetaData().getIdentityType() == IdentityType.DATASTORE) { OID oid = (OID)op.getInternalObjectId(); Object keyValue = oid.getKeyValue(); return getPrimaryKeyAsKey(keyValue, op.getExecutionContext(), op.getClassMetaData()); } else { Object primaryKey = apiAdapter.getTargetKeyForSingleFieldIdentity(op.getInternalObjectId()); return getPrimaryKeyAsKey(primaryKey, op.getExecutionContext(), op.getClassMetaData()); } }
final Object keyValue = dnOid.getKeyAsObject();
private Cache getCacheForId(Object id) { if (IdentityUtils.isSingleFieldIdentity(id) || IdentityUtils.isDatastoreIdentity(id)) { return getCacheForClass(IdentityUtils.getTargetClassNameForIdentity(id)); } return defaultCache; } }
protected String getCacheKeyForId(Object id) { if (IdentityUtils.isSingleFieldIdentity(id)) { String targetClassName = ((SingleFieldId) id).getTargetClassName(); return cacheName + targetClassName + ":" + id.toString().hashCode(); } return cacheName + id.toString().hashCode(); } }
@Override public String getClassNameForObjectID(Object id, ClassLoaderResolver clr, ExecutionContext ec) { if (id == null) { // User stupidity return null; } else if (id instanceof SCOID) { // Object is a SCOID return ((SCOID) id).getSCOClass(); } // Find overall root class possible for this id String rootClassName = super.getClassNameForObjectID(id, clr, ec); if (rootClassName != null) { // User could have passed in a superclass of the real class, so consult the datastore for the precise table/class String[] subclasses = getMetaDataManager().getSubclassesForClass(rootClassName, true); if (subclasses == null || subclasses.length == 0) { // No subclasses so no need to go to the datastore return rootClassName; } AbstractClassMetaData rootCmd = getMetaDataManager().getMetaDataForClass(rootClassName, clr); return MongoDBUtils.getClassNameForIdentity(id, rootCmd, ec, clr); } return null; }
public static Key getKeyForObject(Object pc, ExecutionContext ec) { AbstractClassMetaData cmd = ec.getMetaDataManager().getMetaDataForClass(pc.getClass(), ec.getClassLoaderResolver()); if (cmd.getIdentityType() == IdentityType.DATASTORE) { OID oid = (OID)ec.getApiAdapter().getIdForObject(pc); if (oid == null) { // Not yet persistent, so return null return null; } Object keyValue = oid.getKeyValue(); return EntityUtils.getPkAsKey(keyValue, cmd, ec); } else { // TODO Cater for composite identity Object internalPk = ec.getApiAdapter().getTargetKeyForSingleFieldIdentity(ec.getApiAdapter().getIdForObject(pc)); if (internalPk == null) { // Not yet persistent, so return null return null; } return EntityUtils.getPkAsKey(internalPk, ec.getMetaDataManager().getMetaDataForClass(pc.getClass(), ec.getClassLoaderResolver()), ec); } }
protected String getCacheKeyForId(Object id) { // Because single-field id doesn't include target class name in toString() if (IdentityUtils.isSingleFieldIdentity(id)) { String targetClassName = ((SingleFieldId)id).getTargetClassName(); return cacheName + targetClassName + ":" + id.toString().hashCode(); } return cacheName + id.toString().hashCode(); } }
public static Key getPkAsKey(ObjectProvider op) { if (op.getClassMetaData().getIdentityType() == IdentityType.DATASTORE) { OID oid = (OID)op.getInternalObjectId(); Object keyValue = oid.getKeyValue(); return EntityUtils.getPkAsKey(keyValue, op.getClassMetaData(), op.getExecutionContext()); } else { // TODO Support composite PK Object pk = op.getExecutionContext().getApiAdapter().getTargetKeyForSingleFieldIdentity(op.getInternalObjectId()); if (pk == null) { throw new IllegalStateException("Primary key for object of type " + op.getClassMetaData().getName() + " is null."); } return EntityUtils.getPkAsKey(pk, op.getClassMetaData(), op.getExecutionContext()); } }