Search in sources :

Example 1 with EntityProto

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.
}
Also used : EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 2 with EntityProto

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;
}
Also used : Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element) PutRequest(com.google.apphosting.datastore.DatastoreV3Pb.PutRequest) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 3 with EntityProto

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;
}
Also used : PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue) Property(com.google.storage.onestore.v3.OnestoreEntity.Property)

Example 4 with EntityProto

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);
        }
    }
}
Also used : Property(com.google.storage.onestore.v3.OnestoreEntity.Property)

Example 5 with EntityProto

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);
}
Also used : EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Aggregations

EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)32 Property (com.google.storage.onestore.v3.OnestoreEntity.Property)13 Path (com.google.storage.onestore.v3.OnestoreEntity.Path)9 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)9 ByteString (com.google.protobuf.ByteString)7 ImmutableList (com.google.common.collect.ImmutableList)6 Map (java.util.Map)6 PropertyValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)5 Entity (com.google.appengine.api.datastore.Entity)3 Extent (com.google.appengine.api.datastore.dev.LocalDatastoreService.Extent)3 Profile (com.google.appengine.api.datastore.dev.LocalDatastoreService.Profile)3 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 WeakHashMap (java.util.WeakHashMap)3