{
// Try to get the internal ID from the value itself
boolean isOwnValue = isOwnValue(value);
if (isOwnValue) {
NativeValue nativeValue = (NativeValue)value;
if (revisionIsCurrent(nativeValue)) {
// Value's ID is still current
int id = nativeValue.getInternalID();
if (id != NativeValue.UNKNOWN_ID) {
return id;
}
}
}
// ID not stored in value itself, try the ID cache
Integer cachedID = valueIDCache.get(value);
if (cachedID != null) {
int id = cachedID.intValue();
if (isOwnValue) {
// Store id in value for fast access in any consecutive calls
((NativeValue)value).setInternalID(id, revision);
}
return id;
}
// Unable to get internal ID in a cheap way, just store it in the data
// store which will handle duplicates
byte[] valueData = value2data(value, true);
int id = dataStore.storeData(valueData);
NativeValue nv = isOwnValue ? (NativeValue)value : getNativeValue(value);
// Store id in value for fast access in any consecutive calls
nv.setInternalID(id, revision);
// Update cache
valueIDCache.put(nv, new Integer(id));
return id;