Search in sources :

Example 16 with Reference

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

the class DataTypeTranslator method toKey.

static Key toKey(KeyOrBuilder proto) {
    // Check that the reference contains elements first.
    if (proto.getPathCount() == 0) {
        throw new IllegalArgumentException("Invalid Key PB: no elements.");
    }
    AppIdNamespace appIdNamespace = toAppIdNamespace(proto.getPartitionId());
    Key key = null;
    for (PathElement e : proto.getPathList()) {
        String kind = e.getKind();
        key = new Key(kind, key, e.getId(), e.getIdTypeCase() == IdTypeCase.NAME ? e.getName() : null, appIdNamespace);
    }
    return requireNonNull(key);
}
Also used : PathElement(com.google.datastore.v1.Key.PathElement) ReferenceValuePathElement(com.google.storage.onestore.v3.OnestoreEntity.PropertyValue.ReferenceValuePathElement) ByteString(com.google.protobuf.ByteString) BlobKey(com.google.appengine.api.blobstore.BlobKey)

Example 17 with Reference

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

the class QueryTranslator method convertToPb.

@SuppressWarnings("deprecation")
public static DatastoreV3Pb.Query convertToPb(Query query, FetchOptions fetchOptions) {
    Key ancestor = query.getAncestor();
    List<Query.SortPredicate> sortPredicates = query.getSortPredicates();
    DatastoreV3Pb.Query proto = new DatastoreV3Pb.Query();
    if (query.getKind() != null) {
        proto.setKind(query.getKind());
    }
    proto.setApp(query.getAppIdNamespace().getAppId());
    String nameSpace = query.getAppIdNamespace().getNamespace();
    if (nameSpace.length() != 0) {
        proto.setNameSpace(nameSpace);
    }
    if (fetchOptions.getOffset() != null) {
        proto.setOffset(fetchOptions.getOffset());
    }
    if (fetchOptions.getLimit() != null) {
        proto.setLimit(fetchOptions.getLimit());
    }
    if (fetchOptions.getPrefetchSize() != null) {
        proto.setCount(fetchOptions.getPrefetchSize());
    } else if (fetchOptions.getChunkSize() != null) {
        proto.setCount(fetchOptions.getChunkSize());
    }
    if (fetchOptions.getStartCursor() != null) {
        if (!proto.getMutableCompiledCursor().parseFrom(fetchOptions.getStartCursor().toByteString())) {
            throw new IllegalArgumentException("Invalid cursor");
        }
    }
    if (fetchOptions.getEndCursor() != null) {
        if (!proto.getMutableEndCompiledCursor().parseFrom(fetchOptions.getEndCursor().toByteString())) {
            throw new IllegalArgumentException("Invalid cursor");
        }
    }
    if (fetchOptions.getCompile() != null) {
        proto.setCompile(fetchOptions.getCompile());
    }
    if (ancestor != null) {
        Reference ref = KeyTranslator.convertToPb(ancestor);
        if (!ref.getApp().equals(proto.getApp())) {
            throw new IllegalArgumentException("Query and ancestor appid/namespace mismatch");
        }
        proto.setAncestor(ref);
    }
    if (query.getDistinct()) {
        if (query.getProjections().isEmpty()) {
            throw new IllegalArgumentException("Projected properties must be set to " + "allow for distinct projections");
        }
        for (Projection projection : query.getProjections()) {
            proto.addGroupByPropertyName(projection.getPropertyName());
        }
    }
    proto.setKeysOnly(query.isKeysOnly());
    Query.Filter filter = query.getFilter();
    if (filter != null) {
        // At this point, all non-geo queries have had their filters
        // converted to sets of FilterPredicate objects; so this must be
        // a geo query.
        copyGeoFilterToPb(filter, proto);
    } else {
        for (Query.FilterPredicate filterPredicate : query.getFilterPredicates()) {
            Filter filterPb = proto.addFilter();
            filterPb.copyFrom(convertFilterPredicateToPb(filterPredicate));
        }
    }
    for (Query.SortPredicate sortPredicate : sortPredicates) {
        Order order = proto.addOrder();
        order.copyFrom(convertSortPredicateToPb(sortPredicate));
    }
    for (Projection projection : query.getProjections()) {
        proto.addPropertyName(projection.getPropertyName());
    }
    return proto;
}
Also used : Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) DatastoreV3Pb(com.google.apphosting.datastore.DatastoreV3Pb) Filter(com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter) Filter(com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter)

Example 18 with Reference

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

the class KeyFactory method stringToKey.

/**
 * Converts a {@code String}-representation of a {@code Key} into the {@code Key} instance it
 * represents. Note that <code>
 * str.equals(KeyFactory.keyToString(KeyFactory.stringToKey(str))
 * </code> should evaluate to {@code true} for all strings returned by {@link
 * KeyFactory#keyToString}.
 *
 * @param encoded The {@code String} representation of a {@code Key}.
 * @return The {@code Key} that the given {@code String} represents.
 * @throws IllegalArgumentException If the string cannot be parsed.
 */
public static Key stringToKey(String encoded) {
    // Note: This must be consistent across languages.  If it changes,
    // make sure that the similar logic in Python changes as well.
    // There are unit tests which also need to be changed.
    int modulo = encoded.length() % 4;
    if (modulo != 0) {
        // Pad the string to a 4-byte boundary.
        encoded += "====".substring(modulo);
    }
    byte[] decodedBytes;
    try {
        decodedBytes = base64Url().decode(CharMatcher.whitespace().removeFrom(encoded));
    } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException("Cannot parse: " + encoded, ex);
    }
    Reference reference = new Reference();
    boolean parsed = reference.parseFrom(decodedBytes);
    if (!parsed) {
        throw new IllegalArgumentException("Could not parse Reference");
    }
    // an isInitialized check.
    return KeyTranslator.createFromPb(reference);
}
Also used : Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference)

Example 19 with Reference

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

the class KeyTranslator method createFromPb.

public static Key createFromPb(Reference reference) {
    // Check that the reference contains elements first.
    Key parentKey = null;
    Path path = reference.getPath();
    List<Element> elements = path.elements();
    if (elements.isEmpty()) {
        throw new IllegalArgumentException("Invalid Key PB: no elements.");
    }
    AppIdNamespace appIdNamespace = new AppIdNamespace(reference.getApp(), reference.hasNameSpace() ? reference.getNameSpace() : "");
    for (Element e : elements) {
        String kind = e.getType();
        if (e.hasName() && e.hasId()) {
            throw new IllegalArgumentException("Invalid Key PB: both id and name are set.");
        } else if (e.hasName()) {
            parentKey = new Key(kind, parentKey, Key.NOT_ASSIGNED, e.getName(), appIdNamespace);
        } else if (e.hasId()) {
            parentKey = new Key(kind, parentKey, e.getId(), null, appIdNamespace);
        } else {
            // Incomplete key. Neither id nor name are set.
            // 
            // TODO: consider throwing an exception if this is not the last
            // path element.
            parentKey = new Key(kind, parentKey, Key.NOT_ASSIGNED, null, appIdNamespace);
        }
    }
    return parentKey;
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path) Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element)

Example 20 with Reference

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

the class KeyTranslator method updateKey.

public static void updateKey(Reference reference, Key key) {
    // Can only have id or name, not both.
    if (key.getName() == null) {
        Path path = reference.getPath();
        key.setId(path.getElement(path.elementSize() - 1).getId());
    }
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path)

Aggregations

Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)26 Path (com.google.storage.onestore.v3.OnestoreEntity.Path)16 Test (org.junit.Test)10 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)9 ByteString (com.google.protobuf.ByteString)5 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 PropertyValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)4 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)3 Order (com.google.apphosting.datastore.DatastoreV3Pb.Query.Order)3 ImmutableList (com.google.common.collect.ImmutableList)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3 Map (java.util.Map)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ReorderingMultiFuture (com.google.appengine.api.datastore.Batcher.ReorderingMultiFuture)2 MultiFuture (com.google.appengine.api.datastore.FutureHelper.MultiFuture)2 Utils.getLastElement (com.google.appengine.api.datastore.dev.Utils.getLastElement)2 GetResponse (com.google.apphosting.datastore.DatastoreV3Pb.GetResponse)2