I'm copying entities from one kind to another, and want to map their long
ids in a predictable way. After the mapping is over, I want auto-generation of ids to kick in.
To protect the entities I copy, I want to use allocateIdRange
and manually allocate each id as I copy it. My hope is that this will cause the datastore to protect these new ids, and only assign other ids to new entities created after the copy.
One return code has me worried: CONTENTION
Indicates the given KeyRange is empty but the datastore's automatic ID allocator may assign new entities keys in this range. However it is safe to manually assign Keys in this range if either of the following is true:
No other request will insert entities with the same kind and parent as the given KeyRange until all entities with manually assigned keys from this range have been written.
Overwriting entities written by other requests with the same kind and parent as the given KeyRange is acceptable.
Number 2 is out for me. It is not acceptable for these entities to be overwritten.
Number 1 I think is acceptable, but the wording is scary enough that I want to make sure. If I allocate 5 ids, from 100
to 104
, and I get CONTENTION
back, this seems to indicate that the entities I copy MAY be overwritten with new entities with automatic ids in the future. BUT, if I hurry up and write my own entities with ids manually set to 100, 101, 102, 103, and 104, I will be safe and new entities with automatic ids will NOT receive these ids.
I'm worried because I don't understand how this would work. I don't think of the id allocator as paying attention to what gets written.
TL;DR
Imagine the following scenario:
allocateIdRange(100, 104); // returns CONTENTION
putEntityWithManualId(100);
putEntityWithManualId(101);
putEntityWithManualId(102);
putEntityWithManualId(103);
putEntityWithManualId(104);
// all puts succeed
now, when, later, I call
putNewEntityWithAutomaticId();
is there any risk that the automatic id will be 100, 101, 102, 103, or 104?
The documentation follows as bellow:
Thus, you don't need to worry that your newly copied entities will be overwritten.