Search in sources :

Example 1 with Element

use of com.google.storage.onestore.v3.OnestoreEntity.Path.Element 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 2 with Element

use of com.google.storage.onestore.v3.OnestoreEntity.Path.Element 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 3 with Element

use of com.google.storage.onestore.v3.OnestoreEntity.Path.Element in project appengine-java-standard by GoogleCloudPlatform.

the class KeyTranslator method convertToPb.

public static Reference convertToPb(Key key) {
    Reference reference = new Reference();
    reference.setApp(key.getAppId());
    String nameSpace = key.getNamespace();
    if (!nameSpace.isEmpty()) {
        reference.setNameSpace(nameSpace);
    }
    Path path = reference.getMutablePath();
    while (key != null) {
        Element pathElement = new Element();
        pathElement.setType(key.getKind());
        if (key.getName() != null) {
            pathElement.setName(key.getName());
        } else if (key.getId() != Key.NOT_ASSIGNED) {
            pathElement.setId(key.getId());
        }
        path.addElement(pathElement);
        key = key.getParent();
    }
    Collections.reverse(path.mutableElements());
    return reference;
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element)

Example 4 with Element

use of com.google.storage.onestore.v3.OnestoreEntity.Path.Element in project appengine-java-standard by GoogleCloudPlatform.

the class LocalDatastoreService method putImpl.

// status
@SuppressWarnings("unused")
public PutResponse putImpl(Status status, PutRequest request) {
    PutResponse response = new PutResponse();
    if (request.entitySize() == 0) {
        return response;
    }
    Cost totalCost = response.getMutableCost();
    String app = request.entitys().get(0).getKey().getApp();
    List<EntityProto> clones = new ArrayList<>();
    for (EntityProto entity : request.entitys()) {
        validateAndProcessEntityProto(entity);
        EntityProto clone = entity.clone();
        clones.add(clone);
        checkArgument(clone.hasKey());
        Reference key = clone.getKey();
        checkArgument(key.getPath().elementSize() > 0);
        clone.getMutableKey().setApp(app);
        Element lastPath = getLastElement(key);
        if (lastPath.getId() == 0 && !lastPath.hasName()) {
            if (autoIdAllocationPolicy == AutoIdAllocationPolicy.SEQUENTIAL) {
                lastPath.setId(entityIdSequential.getAndIncrement());
            } else {
                lastPath.setId(toScatteredId(entityIdScattered.getAndIncrement()));
            }
        }
        preprocessEntity(clone);
        if (clone.getEntityGroup().elementSize() == 0) {
            // The entity needs its entity group set.
            Path group = clone.getMutableEntityGroup();
            Element root = key.getPath().elements().get(0);
            Element pathElement = group.addElement();
            pathElement.setType(root.getType());
            if (root.hasName()) {
                pathElement.setName(root.getName());
            } else {
                pathElement.setId(root.getId());
            }
        } else {
            // update an existing entity
            checkState(clone.hasEntityGroup() && clone.getEntityGroup().elementSize() > 0);
        }
    }
    Map<Path, List<EntityProto>> entitiesByEntityGroup = new LinkedHashMap<>();
    Map<Reference, Long> writtenVersions = new HashMap<>();
    final Profile profile = getOrCreateProfile(app);
    synchronized (profile) {
        LiveTxn liveTxn = null;
        for (EntityProto clone : clones) {
            Profile.EntityGroup eg = profile.getGroup(clone.getEntityGroup());
            if (request.hasTransaction()) {
                // the transaction is committed.
                if (liveTxn == null) {
                    liveTxn = profile.getTxn(request.getTransaction().getHandle());
                }
                checkRequest(!liveTxn.isReadOnly(), "Cannot modify entities in a read-only transaction.");
                // this will throw an exception if we attempt to
                // modify the wrong entity group
                eg.addTransaction(liveTxn).addWrittenEntity(clone);
            } else {
                List<EntityProto> entities = entitiesByEntityGroup.get(clone.getEntityGroup());
                if (entities == null) {
                    entities = new ArrayList<>();
                    entitiesByEntityGroup.put(clone.getEntityGroup(), entities);
                }
                entities.add(clone);
            }
            response.mutableKeys().add(clone.getKey());
        }
        for (final Map.Entry<Path, List<EntityProto>> entry : entitiesByEntityGroup.entrySet()) {
            Profile.EntityGroup eg = profile.getGroup(entry.getKey());
            eg.incrementVersion();
            LocalDatastoreJob job = new WriteJob(highRepJobPolicy, eg, profile, entry.getValue(), Collections.<Reference>emptyList());
            addTo(totalCost, job.calculateJobCost());
            eg.addJob(job);
            for (EntityProto entity : entry.getValue()) {
                writtenVersions.put(entity.getKey(), job.getMutationTimestamp(entity.getKey()));
            }
        }
    }
    if (!request.hasTransaction()) {
        logger.fine("put: " + request.entitySize() + " entities");
        // Fill the version numbers, in the same order
        for (Reference key : response.keys()) {
            response.addVersion(writtenVersions.get(key));
        }
    }
    response.setCost(totalCost);
    return response;
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element) Utils.getLastElement(com.google.appengine.api.datastore.dev.Utils.getLastElement) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) PutResponse(com.google.apphosting.datastore.DatastoreV3Pb.PutResponse) Cost(com.google.apphosting.datastore.DatastoreV3Pb.Cost) LinkedHashMap(java.util.LinkedHashMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 5 with Element

use of com.google.storage.onestore.v3.OnestoreEntity.Path.Element in project appengine-java-standard by GoogleCloudPlatform.

the class KeyTranslatorTest method testUpdateKey_Id.

@Test
public void testUpdateKey_Id() {
    Key key = new Key("yam");
    AppIdNamespace appIdNamespace = key.getAppIdNamespace();
    Reference ref = new Reference();
    ref.setApp("my app");
    Path path = new Path();
    Element ele = path.addElement();
    ele.setId(23);
    ref.setPath(path);
    KeyTranslator.updateKey(ref, key);
    assertThat(key.getAppIdNamespace()).isEqualTo(appIdNamespace);
    // coverage
    assertThat(key.getAppId()).isEqualTo(appIdNamespace.getAppId());
    assertThat(key.getId()).isEqualTo(23);
    assertThat(key.getName()).isNull();
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element) Test(org.junit.Test)

Aggregations

Path (com.google.storage.onestore.v3.OnestoreEntity.Path)6 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)6 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)4 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)3 Utils.getLastElement (com.google.appengine.api.datastore.dev.Utils.getLastElement)2 ImmutableList (com.google.common.collect.ImmutableList)2 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 EntityProtoComparator (com.google.appengine.api.datastore.EntityProtoComparators.EntityProtoComparator)1 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)1 PutRequest (com.google.apphosting.datastore.DatastoreV3Pb.PutRequest)1 PutResponse (com.google.apphosting.datastore.DatastoreV3Pb.PutResponse)1 Order (com.google.apphosting.datastore.DatastoreV3Pb.Query.Order)1 QueryResult (com.google.apphosting.datastore.DatastoreV3Pb.QueryResult)1 InvalidConversionException (com.google.cloud.datastore.core.exception.InvalidConversionException)1 Predicate (com.google.common.base.Predicate)1 ImmutableMap (com.google.common.collect.ImmutableMap)1