use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project appengine-java-standard by GoogleCloudPlatform.
the class RemoteApiServlet method assertEntityResultMatchesPrecondition.
/**
* Throws a CONCURRENT_TRANSACTION exception if the entity does not match the precondition.
*/
private void assertEntityResultMatchesPrecondition(GetResponse.Entity entityResult, Precondition precondition) {
// This handles the case where the Entity was missing in one of the two params.
if (precondition.hasHash() != entityResult.hasEntity()) {
throw new ApiProxy.ApplicationException(CONCURRENT_TRANSACTION.getValue(), "Transaction precondition failed");
}
if (entityResult.hasEntity()) {
// Both params have an Entity. Make sure the Entities match using a SHA-1 hash.
EntityProto entity = entityResult.getEntity();
if (Arrays.equals(precondition.getHashAsBytes(), computeSha1(entity))) {
// They match. We're done.
return;
}
// See javadoc of computeSha1OmittingLastByteForBackwardsCompatibility for explanation.
byte[] backwardsCompatibleHash = computeSha1OmittingLastByteForBackwardsCompatibility(entity);
if (!Arrays.equals(precondition.getHashAsBytes(), backwardsCompatibleHash)) {
throw new ApiProxy.ApplicationException(CONCURRENT_TRANSACTION.getValue(), "Transaction precondition failed");
}
}
// Else, the Entity was missing from both.
}
use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project appengine-java-standard by GoogleCloudPlatform.
the class RemoteApiServlet method executeGetIDs.
private byte[] executeGetIDs(Request request, boolean isXG) {
PutRequest putRequest = new PutRequest();
parseFromBytes(putRequest, request.getRequestAsBytes());
for (EntityProto entity : putRequest.entitys()) {
assert (entity.propertySize() == 0);
assert (entity.rawPropertySize() == 0);
assert (entity.getEntityGroup().elementSize() == 0);
List<Element> elementList = entity.getKey().getPath().elements();
Element lastPart = elementList.get(elementList.size() - 1);
assert (lastPart.getId() == 0);
assert (!lastPart.hasName());
}
// Start a Transaction.
// TODO: Shouldn't this use allocateIds instead?
byte[] tx = beginTransaction(isXG);
parseFromBytes(putRequest.getMutableTransaction(), tx);
// Make a put request for a bunch of empty entities with the requisite
// paths.
byte[] res = ApiProxy.makeSyncCall("datastore_v3", "Put", putRequest.toByteArray());
// Roll back the transaction so we don't actually insert anything.
rollback(tx);
return res;
}
use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project appengine-java-standard by GoogleCloudPlatform.
the class DataTypeTranslator method buildImplicitKeyProperty.
private static Property buildImplicitKeyProperty(EntityProto proto) {
Property keyProp = new Property();
keyProp.setName(Entity.KEY_RESERVED_PROPERTY);
PropertyValue propVal = new PropertyValue();
propVal.setReferenceValue(KeyType.toReferenceValue(proto.getKey()));
keyProp.setValue(propVal);
return keyProp;
}
use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project appengine-java-standard by GoogleCloudPlatform.
the class DataTypeTranslator method addListPropertyToPb.
private static void addListPropertyToPb(EntityProto proto, String name, boolean indexed, Collection<?> values, boolean forceIndexedEmbeddedEntity) {
if (values.isEmpty()) {
Property property = new Property();
property.setName(name);
property.setMultiple(false);
if (DatastoreServiceConfig.getEmptyListSupport()) {
// DS now supports empty lists, so we write a real empty list
property.setMeaning(Meaning.EMPTY_LIST);
} else {
// Backward compatible behavior: Write an empty collection as null.
// If the value is indexed it appears in queries, but distinction between
// null and empty list is lost.
}
// Indicate to the proto that we have set this field
property.getMutableValue();
if (indexed) {
proto.addProperty(property);
} else {
proto.addRawProperty(property);
}
} else {
// Write every element to the PB
for (Object listValue : values) {
addPropertyToPb(name, listValue, indexed, forceIndexedEmbeddedEntity, true, /* multiple */
proto);
}
}
}
use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project appengine-java-standard by GoogleCloudPlatform.
the class EntityTranslator method createFromPbBytes.
public static Entity createFromPbBytes(byte[] pbBytes) {
EntityProto proto = new EntityProto();
boolean parsed = proto.mergeFrom(pbBytes);
if (!parsed || !proto.isInitialized()) {
throw new IllegalArgumentException("Could not parse EntityProto bytes");
}
return createFromPb(proto);
}
Aggregations