Search in sources :

Example 16 with PropertyValue

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

the class LocalDatastoreService method createIndexEntities.

/**
 * Splits a full entity into all index entities seen in a projection.
 *
 * @param entity the entity to split
 * @param postfixProps the properties included in the postfix
 * @return A list of the index entities.
 */
private ImmutableList<EntityProto> createIndexEntities(EntityProto entity, Set<String> postfixProps, EntityProtoComparator entityComparator) {
    SetMultimap<String, PropertyValue> toSplit = MultimapBuilder.hashKeys(postfixProps.size()).hashSetValues(1).build();
    Set<String> seen = Sets.newHashSet();
    boolean splitRequired = false;
    for (Property prop : entity.propertys()) {
        if (postfixProps.contains(prop.getName())) {
            // If we have multiple values for any postfix property, we need to split.
            splitRequired |= !seen.add(prop.getName());
            // Only add the value if it matches the query filters
            if (entityComparator.matches(prop)) {
                toSplit.put(prop.getName(), prop.getValue());
            }
        }
    }
    if (!splitRequired) {
        // No need for splitting!
        return ImmutableList.of(entity);
    }
    EntityProto clone = new EntityProto();
    clone.getMutableKey().copyFrom(entity.getKey());
    clone.getMutableEntityGroup();
    List<EntityProto> results = Lists.newArrayList(clone);
    for (Map.Entry<String, Collection<PropertyValue>> entry : toSplit.asMap().entrySet()) {
        if (entry.getValue().size() == 1) {
            // No need for cloning!
            for (EntityProto result : results) {
                result.addProperty().setName(entry.getKey()).setMeaning(Property.Meaning.INDEX_VALUE).getMutableValue().copyFrom(Iterables.getOnlyElement(entry.getValue()));
            }
            continue;
        }
        List<EntityProto> splitResults = Lists.newArrayListWithCapacity(results.size() * entry.getValue().size());
        for (PropertyValue value : entry.getValue()) {
            for (EntityProto result : results) {
                EntityProto split = result.clone();
                split.addProperty().setName(entry.getKey()).setMeaning(Property.Meaning.INDEX_VALUE).getMutableValue().copyFrom(value);
                splitResults.add(split);
            }
        }
        results = splitResults;
    }
    return ImmutableList.copyOf(results);
}
Also used : PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue) Collection(java.util.Collection) ByteString(com.google.protobuf.ByteString) Property(com.google.storage.onestore.v3.OnestoreEntity.Property) 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 17 with PropertyValue

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

the class QueryTranslatorTest method testGeoFilter_withPreintersection.

@Test
public void testGeoFilter_withPreintersection() {
    Query query = new Query("foo");
    float lat = 1;
    float lng = 2;
    GeoPt point = new GeoPt(lat, lng);
    double radius = 1609.34;
    String rating = "3-stars";
    query.setFilter(new Query.CompositeFilter(Query.CompositeFilterOperator.AND, Arrays.asList(new Query.StContainsFilter("location", new Query.GeoRegion.Circle(point, radius)), Query.FilterOperator.EQUAL.of("rating", rating))));
    DatastoreV3Pb.Query proto = QueryTranslator.convertToPb(query, withDefaults());
    assertThat(proto.filterSize()).isEqualTo(2);
    Filter filter1 = proto.getFilter(0);
    assertThat(filter1.getProperty(0).getName()).isEqualTo("location");
    assertThat(filter1.getOpEnum()).isEqualTo(Operator.CONTAINED_IN_REGION);
    Filter filter2 = proto.getFilter(1);
    assertThat(filter2.getProperty(0).getName()).isEqualTo("rating");
    assertThat(filter2.getOpEnum()).isEqualTo(Operator.EQUAL);
    assertThat(filter2.hasGeoRegion()).isFalse();
    PropertyValue value = filter2.getProperty(0).getValue();
    assertThat(value.getStringValue()).isEqualTo(rating);
}
Also used : Filter(com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter) PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue) DatastoreV3Pb(com.google.apphosting.datastore.DatastoreV3Pb) Test(org.junit.Test)

Example 18 with PropertyValue

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

the class ReferenceValues method toReferenceProperty.

/**
 * Converts a {@link Reference} to a {@link PropertyValue}
 *
 * <p>with the same {@link PropertyValue.ReferenceValue}.
 *
 * <p>Doesn't do any validation!
 *
 * @return the corresponding {@link PropertyValue}
 */
public static PropertyValue toReferenceProperty(Reference key) {
    PropertyValue prop = new PropertyValue();
    PropertyValue.ReferenceValue ref = prop.getMutableReferenceValue();
    if (key.hasApp()) {
        ref.setAppAsBytes(key.getAppAsBytes());
    }
    if (key.hasDatabaseId()) {
        ref.setDatabaseIdAsBytes(key.getDatabaseIdAsBytes());
    }
    if (key.hasNameSpace()) {
        ref.setNameSpaceAsBytes(key.getNameSpaceAsBytes());
    }
    for (Path.Element keyElem : key.getPath().elements()) {
        PropertyValue.ReferenceValuePathElement refElem = ref.addPathElement();
        if (keyElem.hasType()) {
            refElem.setTypeAsBytes(keyElem.getTypeAsBytes());
        }
        if (keyElem.hasId()) {
            refElem.setId(keyElem.getId());
        }
        if (keyElem.hasName()) {
            refElem.setNameAsBytes(keyElem.getNameAsBytes());
        }
    }
    return prop;
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)

Aggregations

PropertyValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)18 Test (org.junit.Test)5 Filter (com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter)4 Property (com.google.storage.onestore.v3.OnestoreEntity.Property)4 ByteString (com.google.protobuf.ByteString)3 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)3 DatastoreV3Pb (com.google.apphosting.datastore.DatastoreV3Pb)2 Path (com.google.storage.onestore.v3.OnestoreEntity.Path)2 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)2 BlobKey (com.google.appengine.api.blobstore.BlobKey)1 GeoRegion (com.google.apphosting.datastore.DatastoreV3Pb.GeoRegion)1 Order (com.google.apphosting.datastore.DatastoreV3Pb.Query.Order)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Meaning (com.google.storage.onestore.v3.OnestoreEntity.Property.Meaning)1 ReferenceValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.ReferenceValue)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1