Search in sources :

Example 11 with PropertyValue

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

the class QueryTranslator method copyGeoFilterToPb.

/**
 * Converts the filter from a geo-spatial query into proto-buf form. Should only be called when
 * the filter indeed has a geo-spatial term; but the filter as a whole has not yet been entirely
 * validated, so we complete the validation here.
 */
private static void copyGeoFilterToPb(Query.Filter filter, DatastoreV3Pb.Query proto) {
    if (filter instanceof Query.CompositeFilter) {
        Query.CompositeFilter conjunction = (Query.CompositeFilter) filter;
        checkArgument(conjunction.getOperator() == Query.CompositeFilterOperator.AND, "Geo-spatial filters may only be composed with CompositeFilterOperator.AND");
        for (Query.Filter f : conjunction.getSubFilters()) {
            copyGeoFilterToPb(f, proto);
        }
    } else if (filter instanceof Query.StContainsFilter) {
        Query.StContainsFilter containmentFilter = (Query.StContainsFilter) filter;
        Filter f = proto.addFilter();
        f.setOp(Operator.CONTAINED_IN_REGION);
        f.setGeoRegion(convertGeoRegionToPb(containmentFilter.getRegion()));
        // It's a bit weird to add a Value with nothing in it; but we
        // need Property in order to convey the property name, and Value
        // is required in Property.  But in our case there is no value:
        // the geo region acts as the thing to which we "compare" the property.
        f.addProperty().setName(containmentFilter.getPropertyName()).setMultiple(false).setValue(new PropertyValue());
    } else {
        checkArgument(filter instanceof Query.FilterPredicate);
        Query.FilterPredicate predicate = (Query.FilterPredicate) filter;
        checkArgument(predicate.getOperator() == Query.FilterOperator.EQUAL, "Geo-spatial filters may only be combined with equality comparisons");
        Filter f = proto.addFilter();
        f.copyFrom(convertFilterPredicateToPb(predicate));
    }
}
Also used : Filter(com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter) Filter(com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter) PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)

Example 12 with PropertyValue

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

the class RawValue method readObject.

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    int version = in.read();
    if (version == 1) {
        valueV3 = new PropertyValue();
        boolean parsed = valueV3.parseFrom(in);
        if (!parsed || !valueV3.isInitialized()) {
            throw new InvalidProtocolBufferException("Could not parse PropertyValue");
        }
    } else if (version == 2) {
        valueV1 = Value.parseFrom(in);
    } else {
        throw new IllegalArgumentException("unknown RawValue format");
    }
    in.defaultReadObject();
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)

Example 13 with PropertyValue

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

the class ValidatedQuery method validateQuery.

/**
 * Determines if a given query is supported by the datastore.
 *
 * @throws IllegalQueryException If the provided query fails validation.
 */
private void validateQuery() {
    if (query.propertyNameSize() > 0 && query.isKeysOnly()) {
        throw new IllegalQueryException("projection and keys_only cannot both be set", IllegalQueryType.ILLEGAL_PROJECTION);
    }
    Set<String> projectionProperties = new HashSet<String>(query.propertyNameSize());
    for (String property : query.propertyNames()) {
        if (!projectionProperties.add(property)) {
            throw new IllegalQueryException("cannot project a property multiple times", IllegalQueryType.ILLEGAL_PROJECTION);
        }
    }
    Set<String> groupBySet = Sets.newHashSetWithExpectedSize(query.groupByPropertyNameSize());
    for (String name : query.groupByPropertyNames()) {
        if (!groupBySet.add(name)) {
            throw new IllegalQueryException("cannot group by a property multiple times", IllegalQueryType.ILLEGAL_GROUPBY);
        }
        // filters.
        if (Entity.RESERVED_NAME.matcher(name).matches()) {
            throw new IllegalQueryException("group by is not supported for the property: " + name, IllegalQueryType.ILLEGAL_GROUPBY);
        }
    }
    /* Validate group by properties in orderings. */
    Set<String> groupByInOrderSet = Sets.newHashSetWithExpectedSize(query.groupByPropertyNameSize());
    for (Order order : query.orders()) {
        if (groupBySet.contains(order.getProperty())) {
            groupByInOrderSet.add(order.getProperty());
        } else if (groupByInOrderSet.size() != groupBySet.size()) {
            throw new IllegalQueryException("must specify all group by orderings before any non group by orderings", IllegalQueryType.ILLEGAL_GROUPBY);
        }
    }
    // Transaction requires ancestor
    if (query.hasTransaction() && !query.hasAncestor()) {
        throw new IllegalQueryException("Only ancestor queries are allowed inside transactions.", IllegalQueryType.TRANSACTION_REQUIRES_ANCESTOR);
    }
    // Filters and sort orders require kind.
    if (!query.hasKind()) {
        for (Filter filter : query.filters()) {
            if (!filter.getProperty(0).getName().equals(Entity.KEY_RESERVED_PROPERTY)) {
                throw new IllegalQueryException("kind is required for non-__key__ filters", IllegalQueryType.KIND_REQUIRED);
            }
        }
        for (Order order : query.orders()) {
            if (!(order.getProperty().equals(Entity.KEY_RESERVED_PROPERTY) && order.getDirection() == Order.Direction.ASCENDING.getValue())) {
                throw new IllegalQueryException("kind is required for all orders except __key__ ascending", IllegalQueryType.KIND_REQUIRED);
            }
        }
    }
    /* Validate ancestor, if it exists. */
    if (query.hasAncestor()) {
        Reference ancestor = query.getAncestor();
        if (!ancestor.getApp().equals(query.getApp())) {
            throw new IllegalQueryException("The query app is " + query.getApp() + " but ancestor app is " + ancestor.getApp(), IllegalQueryType.ILLEGAL_VALUE);
        }
        if (!ancestor.getNameSpace().equals(query.getNameSpace())) {
            throw new IllegalQueryException("The query namespace is " + query.getNameSpace() + " but ancestor namespace is " + ancestor.getNameSpace(), IllegalQueryType.ILLEGAL_VALUE);
        }
    }
    // Check for unsupported filter values.  We only support one property
    // per filter and one property with an inequality filter.
    String ineqProp = null;
    this.isGeo = false;
    for (Filter filter : query.filters()) {
        int numProps = filter.propertySize();
        if (numProps != 1) {
            throw new IllegalQueryException(String.format("Filter has %s properties, expected 1", numProps), IllegalQueryType.FILTER_WITH_MULTIPLE_PROPS);
        }
        Property prop = filter.getProperty(0);
        String propName = prop.getName();
        /* Extra validation for __key__. The filter value must be a key,
       * if the query has a kind, the key's kind must match, and the
       * app and namespace must match the query. */
        if (Entity.KEY_RESERVED_PROPERTY.equals(propName)) {
            PropertyValue value = prop.getValue();
            if (!value.hasReferenceValue()) {
                throw new IllegalQueryException(Entity.KEY_RESERVED_PROPERTY + " filter value must be a Key", IllegalQueryType.ILLEGAL_VALUE);
            }
            ReferenceValue refVal = value.getReferenceValue();
            if (!refVal.getApp().equals(query.getApp())) {
                throw new IllegalQueryException(Entity.KEY_RESERVED_PROPERTY + " filter app is " + refVal.getApp() + " but query app is " + query.getApp(), IllegalQueryType.ILLEGAL_VALUE);
            }
            if (!refVal.getNameSpace().equals(query.getNameSpace())) {
                throw new IllegalQueryException(Entity.KEY_RESERVED_PROPERTY + " filter namespace is " + refVal.getNameSpace() + " but query namespace is " + query.getNameSpace(), IllegalQueryType.ILLEGAL_VALUE);
            }
        }
        if (INEQUALITY_OPERATORS.contains(filter.getOpEnum())) {
            if (ineqProp == null) {
                ineqProp = propName;
            } else if (!ineqProp.equals(propName)) {
                throw new IllegalQueryException(String.format("Only one inequality filter per query is supported.  " + "Encountered both %s and %s", ineqProp, propName), IllegalQueryType.MULTIPLE_INEQ_FILTERS);
            }
        } else if (filter.getOpEnum() == Operator.EQUAL) {
            if (projectionProperties.contains(propName)) {
                throw new IllegalQueryException("cannot use projection on a property with an equality filter", IllegalQueryType.ILLEGAL_PROJECTION);
            } else if (groupBySet.contains(propName)) {
                throw new IllegalQueryException("cannot use group by on a property with an equality filter", IllegalQueryType.ILLEGAL_GROUPBY);
            }
        } else if (filter.getOpEnum() == Operator.CONTAINED_IN_REGION) {
            isGeo = true;
            if (!filter.hasGeoRegion() || prop.getValue().hasPointValue()) {
                throw new IllegalQueryException(String.format("Geo-spatial filter on %s should specify GeoRegion rather than Property Value", propName), IllegalQueryType.UNSUPPORTED_FILTER);
            }
            GeoRegion region = filter.getGeoRegion();
            if ((region.hasCircle() && region.hasRectangle()) || (!region.hasCircle() && !region.hasRectangle())) {
                throw new IllegalQueryException(String.format("Geo-spatial filter on %s should specify Circle or Rectangle, but not both", propName), IllegalQueryType.UNSUPPORTED_FILTER);
            }
        } else if (UNSUPPORTED_OPERATORS.contains(filter.getOpEnum())) {
            throw new IllegalQueryException(String.format("Unsupported filter operator: %s", filter.getOp()), IllegalQueryType.UNSUPPORTED_FILTER);
        }
    }
    if (isGeo) {
        if (ineqProp != null) {
            throw new IllegalQueryException("Inequality filter with geo-spatial query is not supported.", IllegalQueryType.UNSUPPORTED_FILTER);
        }
        if (query.hasAncestor()) {
            throw new IllegalQueryException("Geo-spatial filter on ancestor query is not supported.", IllegalQueryType.UNSUPPORTED_FILTER);
        }
        if (query.hasCompiledCursor() || query.hasEndCompiledCursor()) {
            throw new IllegalQueryException("Start and end cursors are not supported on geo-spatial queries.", IllegalQueryType.CURSOR_NOT_SUPPORTED);
        }
    }
    if (ineqProp != null && query.groupByPropertyNameSize() > 0) {
        if (!groupBySet.contains(ineqProp)) {
            throw new IllegalQueryException(String.format("Inequality filter on %s must also be a group by property when " + "group by properties are set.", ineqProp), IllegalQueryType.ILLEGAL_GROUPBY);
        }
    }
    if (ineqProp != null) {
        if (query.orderSize() > 0) {
            if (!ineqProp.equals(query.getOrder(0).getProperty())) {
                // First order must match the inequality filter.
                throw new IllegalQueryException(String.format("The first sort property must be the same as the property to which " + "the inequality filter is applied.  In your query the first sort property " + "is %s but the inequality filter is on %s", query.getOrder(0).getProperty(), ineqProp), IllegalQueryType.FIRST_SORT_NEQ_INEQ_PROP);
            }
        }
    }
}
Also used : Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) Filter(com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) ReferenceValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.ReferenceValue) GeoRegion(com.google.apphosting.datastore.DatastoreV3Pb.GeoRegion) PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue) Property(com.google.storage.onestore.v3.OnestoreEntity.Property) HashSet(java.util.HashSet)

Example 14 with PropertyValue

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

the class LocalDatastoreService method validateLengthLimit.

private void validateLengthLimit(Property property) {
    String name = property.getName();
    PropertyValue value = property.getValue();
    if (value.hasStringValue()) {
        if (property.hasMeaning() && property.getMeaningEnum() == Property.Meaning.ATOM_LINK) {
            if (value.getStringValueAsBytes().length > MAX_LINK_LENGTH) {
                throw newError(ErrorCode.BAD_REQUEST, "Link property " + name + " is too long. Use TEXT for links over " + MAX_LINK_LENGTH + " bytes.");
            }
        } else if (property.hasMeaning() && property.getMeaningEnum() == Property.Meaning.ENTITY_PROTO) {
            if (value.getStringValueAsBytes().length > MAX_BLOB_LENGTH) {
                throw newError(ErrorCode.BAD_REQUEST, "embedded entity property " + name + " is too big.  It cannot exceed " + MAX_BLOB_LENGTH + " bytes.");
            }
        } else {
            if (value.getStringValueAsBytes().length > MAX_STRING_LENGTH) {
                throw newError(ErrorCode.BAD_REQUEST, "string property " + name + " is too long.  It cannot exceed " + MAX_STRING_LENGTH + " bytes.");
            }
        }
    }
}
Also used : PropertyValue(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue) ByteString(com.google.protobuf.ByteString)

Example 15 with PropertyValue

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

the class LocalDatastoreService method validateAndProcessProperty.

private void validateAndProcessProperty(Property prop) {
    if (RESERVED_NAME.matcher(prop.getName()).matches()) {
        throw newError(ErrorCode.BAD_REQUEST, String.format("illegal property.name: %s", prop.getName()));
    }
    PropertyValue val = prop.getMutableValue();
    if (val.hasUserValue() && !val.getUserValue().hasObfuscatedGaiaid()) {
        // If not already set, populate obfuscated gaia id with hash of email address.
        PropertyValue.UserValue userVal = val.getMutableUserValue();
        userVal.setObfuscatedGaiaid(Integer.toString(userVal.getEmail().hashCode()));
    }
}
Also used : 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