isTip
versionId
consistentId
firstEntityInChain
public class ImmutableDatastoreEntity { @Id Long versionId; @Parent Key<T> firstEntityInChain; protected Long consistentId; protected boolean isTip; Key<User> savedByUser; }
ImmutableDatastoreEntity entity = new ImmutableDatastoreEntity(); entity.setVersionId(DAO.allocateId(this.getClass())); entity.setConsistentId(entity.getVersionId()); entity.setFirstEntityInChain((Key<T>) Key.create(entity.getClass(), entity.versionId)); entity.setTip(true);
// start transaction ImmutableDatastoreEntity oldVersion = getImmutableEntity(immutableId) oldVersion.setTip(false); ImmutableDatastoreEntity newVersion = oldVersion.clone(); // make the user edits needed newVersion.setVersionId(null); newVersion.setConsistentId(this.getConsistentId()); newVersion.setFirstEntityInChain(oldVersion.getFirstEntityInChain()); // .clone also performs the last two lines but just to be explicit this, just fyi newVersion.setTip(true); ofy().save(oldVersion, newVersion).now(); // end transaction
Key ancestorKey = KeyFactory.createKey(ImmutableDatastoreEntity.class, consistentId); ImmutableDatastoreEntity e = ofy().load() .kind(ImmutableDatastoreEntity.class) .filter("consistentId", consistentId) .filter("isTip", true) .ancestor(ancestorKey) // this limits our query to just the 1 entity group .list() .first();
// wrap block in transaction ImmutableDatastoreEntity oldVersion = getImmutableEntity(immutableId); oldVersion.setTip(false); ofy().save(oldVersion, newVersion).now();
List<ImmutableDatastoreEntity> results = ofy().load() .kind(ImmutableDatastoreEntity.class) .filter("isTip", true) .filter(/** apply other filters here */) .list();
Key ancestorKey = KeyFactory.createKey(ImmutableDatastoreEntity.class, consistentId); List<ImmutableDatastoreEntity> versions = ofy().load() .kind(ImmutableDatastoreEntity.class) .filter("consistentId", consistentId) .ancestor(ancestorKey) .list();
Demonstrate your proficiency to design, build and manage solutions on Google Cloud Platform.