Search in sources :

Example 1 with Path

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

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

the class EntityTranslator method convertToPb.

public static EntityProto convertToPb(Entity entity) {
    Reference reference = KeyTranslator.convertToPb(entity.getKey());
    EntityProto proto = new EntityProto();
    proto.setKey(reference);
    // If we've already been stored, make sure the entity group is set
    // to match our key.
    Path entityGroup = proto.getMutableEntityGroup();
    Key key = entity.getKey();
    if (key.isComplete()) {
        entityGroup.addElement(reference.getPath().elements().get(0));
    }
    DataTypeTranslator.addPropertiesToPb(entity.getPropertyMap(), proto);
    return proto;
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 3 with Path

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

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

the class EntityGroupPseudoKind method get.

@Override
@Nullable
public EntityProto get(@Nullable LiveTxn txn, EntityGroup eg, Reference key, boolean eventualConsistency) {
    // We plan to add support to query the set of entity groups by querying this pseudo-kind.  Such
    // queries would return pseudo-entities as direct children of the entity-group root with
    // numeric key ID. Thus to make sure we can get() and queries are consistent, we require that
    // key match that format.
    Path path = key.getPath();
    if (path.elementSize() != 2 || path.getElement(1).getId() != ENTITY_GROUP_METADATA_ID) {
        return null;
    }
    long version;
    if (txn == null) {
        // Can just grab the entity-group version as its incremented eagerly rather than on job-apply.
        version = eg.getVersion();
    } else {
        version = txn.trackEntityGroup(eg).getEntityGroupVersion();
    }
    // return a version even when the entity group is empty(new or not).
    return makeEntityGroupEntity(key, version + baseVersion);
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 5 with Path

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

the class NamespacePseudoKind method makeNamespaceEntities.

private List<EntityProto> makeNamespaceEntities(Set<String> namespaceSet, String app, String executionNamespace) {
    List<EntityProto> namespaces = Lists.newArrayListWithCapacity(namespaceSet.size());
    for (String namespace : namespaceSet) {
        // Create namespace entity and set its key based on the namespace
        EntityProto namespaceEntity = new EntityProto();
        namespaces.add(namespaceEntity);
        Path path = new Path();
        // Empty namespaces use an EMPTY_NAMESPACE_ID key
        if (namespace.isEmpty()) {
            path.addElement().setType(NAMESPACE_METADATA_KIND).setId(EMPTY_NAMESPACE_ID);
        } else {
            path.addElement().setType(NAMESPACE_METADATA_KIND).setName(namespace);
        }
        Reference key = new Reference().setApp(app).setPath(path);
        if (executionNamespace.length() > 0) {
            key.setNameSpace(executionNamespace);
        }
        namespaceEntity.setKey(key);
        // EntityProto.entity_group is a required PB field.
        namespaceEntity.getMutableEntityGroup().addElement(path.getElement(0));
    }
    return namespaces;
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Aggregations

Path (com.google.storage.onestore.v3.OnestoreEntity.Path)15 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)11 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)8 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)5 ByteString (com.google.protobuf.ByteString)4 ImmutableList (com.google.common.collect.ImmutableList)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Utils.getLastElement (com.google.appengine.api.datastore.dev.Utils.getLastElement)2 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 WeakHashMap (java.util.WeakHashMap)2 Test (org.junit.Test)2 EntityProtoComparator (com.google.appengine.api.datastore.EntityProtoComparators.EntityProtoComparator)1 DeleteResponse (com.google.apphosting.datastore.DatastoreV3Pb.DeleteResponse)1