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);
}
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);
}
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;
}
Aggregations