Search in sources :

Example 6 with EntityProto

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

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

the class LocalDatastoreService method applyGroupByProperties.

/**
 * Creates a new List of entities after applying group by properties.
 *
 * @param queryEntities a sorted list of entities.
 * @param query the current query.
 * @return a new list of entities with unique properties.
 */
private List<EntityProto> applyGroupByProperties(List<EntityProto> queryEntities, Query query) {
    Set<String> groupByProperties = Sets.newHashSet(query.groupByPropertyNames());
    // Nothing to do if there are no group by properties.
    if (groupByProperties.isEmpty()) {
        return queryEntities;
    }
    Set<NameValue> lastEntity = Sets.newHashSet();
    List<EntityProto> results = Lists.newArrayList();
    for (EntityProto entity : queryEntities) {
        boolean isFirst = false;
        for (Property prop : entity.propertys()) {
            if (groupByProperties.contains(prop.getName()) && !lastEntity.contains(NameValue.of(prop.getName(), prop.getValue()))) {
                isFirst = true;
                break;
            }
        }
        if (isFirst) {
            results.add(entity);
            // Set lastEntity to be the new set of properties.
            lastEntity.clear();
            for (Property prop : entity.propertys()) {
                if (groupByProperties.contains(prop.getName())) {
                    lastEntity.add(NameValue.of(prop.getName(), prop.getValue()));
                }
            }
        }
    }
    return results;
}
Also used : ByteString(com.google.protobuf.ByteString) Property(com.google.storage.onestore.v3.OnestoreEntity.Property) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 8 with EntityProto

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

the class LocalDatastoreService method validateAndProcessEntityProto.

private void validateAndProcessEntityProto(EntityProto entity) {
    validatePathForPut(entity.getKey());
    for (Property prop : entity.propertys()) {
        validateAndProcessProperty(prop);
        validateLengthLimit(prop);
    }
    for (Property prop : entity.rawPropertys()) {
        validateAndProcessProperty(prop);
        validateRawPropLengthLimit(prop);
    }
}
Also used : Property(com.google.storage.onestore.v3.OnestoreEntity.Property)

Example 9 with EntityProto

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

the class LocalDatastoreService method createIndexOnlyQueryResults.

/**
 * Converts a normal result set into the results seen in an index-only query (a projection).
 *
 * @param queryEntities the results to convert
 * @param entityComparator the comparator derived from the query
 * @return the converted results
 */
private List<EntityProto> createIndexOnlyQueryResults(List<EntityProto> queryEntities, EntityProtoComparator entityComparator) {
    Set<String> postfixProps = Sets.newHashSetWithExpectedSize(entityComparator.getAdjustedOrders().size());
    for (Query.Order order : entityComparator.getAdjustedOrders()) {
        postfixProps.add(order.getProperty());
    }
    List<EntityProto> results = Lists.newArrayListWithExpectedSize(queryEntities.size());
    for (EntityProto entity : queryEntities) {
        List<EntityProto> indexEntities = createIndexEntities(entity, postfixProps, entityComparator);
        results.addAll(indexEntities);
    }
    return results;
}
Also used : Query(com.google.apphosting.datastore.DatastoreV3Pb.Query) CompiledQuery(com.google.apphosting.datastore.DatastoreV3Pb.CompiledQuery) Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) ByteString(com.google.protobuf.ByteString) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 10 with EntityProto

use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto 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)

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