Search in sources :

Example 6 with Element

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

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

the class LocalDatastoreService method getGroup.

/**
 * Returns the entity group for the specified key. This is simply a new {@code Path} instance
 * containing the first element in the specified key.
 */
private Path getGroup(Reference key) {
    Path path = key.getPath();
    Path group = new Path();
    group.addElement(path.getElement(0));
    return group;
}
Also used : Path(com.google.storage.onestore.v3.OnestoreEntity.Path)

Example 8 with Element

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

the class LocalDatastoreService method runQuery.

// status
@SuppressWarnings("unused")
public QueryResult runQuery(Status status, Query query) {
    // Construct a validated query right away so we can fail fast
    // if something is wrong.
    final LocalCompositeIndexManager.ValidatedQuery validatedQuery = new LocalCompositeIndexManager.ValidatedQuery(query);
    query = validatedQuery.getV3Query();
    // LocalCompositeIndexManager.ValidatedQuery.  I don't know why.
    try {
        CursorModernizer.modernizeQueryCursors(query);
    } catch (InvalidConversionException e) {
        throw newError(ErrorCode.BAD_REQUEST, "Invalid cursor");
    }
    String app = query.getApp();
    Profile profile = getOrCreateProfile(app);
    synchronized (profile) {
        // ancestor does not imply we have a transaction.
        if (query.hasTransaction() || query.hasAncestor()) {
            // Query can only have a txn if it is an ancestor query.  Either way we
            // know we've got an ancestor.
            Path groupPath = getGroup(query.getAncestor());
            Profile.EntityGroup eg = profile.getGroup(groupPath);
            if (query.hasTransaction()) {
                if (!app.equals(query.getTransaction().getApp())) {
                    throw newError(ErrorCode.INTERNAL_ERROR, "Can't query app " + app + "in a transaction on app " + query.getTransaction().getApp());
                }
                LiveTxn liveTxn = profile.getTxn(query.getTransaction().getHandle());
                // this will throw an exception if we attempt to read from
                // the wrong entity group
                eg.addTransaction(liveTxn);
                // Use snapshot profile.
                profile = eg.getSnapshot(liveTxn);
            }
            if (query.hasAncestor()) {
                if (query.hasTransaction() || !query.hasFailoverMs()) {
                    // Either we have a transaction or the user has requested strongly
                    // consistent results.  Either way, we need to apply jobs.
                    eg.rollForwardUnappliedJobs();
                }
            }
        }
        if (query.hasSearchQuery()) {
            throw newError(ErrorCode.BAD_REQUEST, "full-text search unsupported");
        }
        // Run as a PseudoKind query if necessary, otherwise check the actual local datastore
        List<EntityProto> queryEntities = pseudoKinds.runQuery(query);
        Map<Reference, Long> versions = null;
        if (queryEntities == null) {
            Collection<VersionedEntity> versionedEntities = null;
            Map<String, Extent> extents = profile.getExtents();
            Extent extent = extents.get(query.getKind());
            if (extent != null) {
                // Make a copy of the list of all the entities in the extent
                versionedEntities = extent.getAllEntities();
            } else if (!query.hasKind()) {
                // Kind-less query, so we need a list containing all entities of
                // all kinds.
                versionedEntities = profile.getAllEntities();
                if (query.orderSize() == 0) {
                    // add a sort by key asc to match the behavior of prod
                    query.addOrder(new Order().setDirection(Query.Order.Direction.ASCENDING).setProperty(Entity.KEY_RESERVED_PROPERTY));
                }
            } else {
            // no extent - we're querying for a kind without any entities
            }
            if (versionedEntities != null) {
                queryEntities = new ArrayList<>();
                versions = new HashMap<>();
                for (VersionedEntity entity : versionedEntities) {
                    queryEntities.add(entity.entityProto());
                    versions.put(entity.entityProto().getKey(), entity.version());
                }
            }
        }
        // Give all entity groups with unapplied jobs the opportunity to catch
        // up.  Note that this will not impact the result of the query we're
        // currently fulfilling since we already have the (unfiltered) result
        // set.
        profile.groom();
        if (queryEntities == null) {
            // so we don't need to check for null anywhere else down below
            queryEntities = Collections.emptyList();
        }
        // Building filter predicate
        List<Predicate<EntityProto>> predicates = new ArrayList<>();
        // apply ancestor restriction
        if (query.hasAncestor()) {
            final List<Element> ancestorPath = query.getAncestor().getPath().elements();
            predicates.add(new Predicate<EntityProto>() {

                @Override
                public boolean apply(EntityProto entity) {
                    List<Element> path = entity.getKey().getPath().elements();
                    return path.size() >= ancestorPath.size() && path.subList(0, ancestorPath.size()).equals(ancestorPath);
                }
            });
        }
        if (query.isShallow()) {
            final long keyPathLength = query.hasAncestor() ? query.getAncestor().getPath().elementSize() + 1 : 1;
            predicates.add(new Predicate<EntityProto>() {

                @Override
                public boolean apply(EntityProto entity) {
                    return entity.getKey().getPath().elementSize() == keyPathLength;
                }
            });
        }
        // apply namespace restriction
        final boolean hasNamespace = query.hasNameSpace();
        final String namespace = query.getNameSpace();
        predicates.add(new Predicate<EntityProto>() {

            @Override
            public boolean apply(EntityProto entity) {
                Reference ref = entity.getKey();
                // Filter all elements not in the query's namespace.
                if (hasNamespace) {
                    if (!ref.hasNameSpace() || !namespace.equals(ref.getNameSpace())) {
                        return false;
                    }
                } else {
                    if (ref.hasNameSpace()) {
                        return false;
                    }
                }
                return true;
            }
        });
        // Get entityComparator with filter matching capability
        final EntityProtoComparator entityComparator = new EntityProtoComparator(validatedQuery.getQuery().orders(), validatedQuery.getQuery().filters());
        // applying filter restrictions
        predicates.add(new Predicate<EntityProto>() {

            @Override
            public boolean apply(EntityProto entity) {
                return entityComparator.matches(entity);
            }
        });
        Predicate<EntityProto> queryPredicate = Predicates.<EntityProto>not(Predicates.<EntityProto>and(predicates));
        // The ordering of the following operations is important to maintain correct
        // query functionality.
        // Filtering entities
        Iterables.removeIf(queryEntities, queryPredicate);
        // Expanding projections
        if (query.propertyNameSize() > 0) {
            queryEntities = createIndexOnlyQueryResults(queryEntities, entityComparator);
        }
        // Sorting entities
        Collections.sort(queryEntities, entityComparator);
        // Apply group by. This must happen after sorting to select the correct first entity.
        queryEntities = applyGroupByProperties(queryEntities, query);
        // store the query and return the results
        LiveQuery liveQuery = new LiveQuery(queryEntities, versions, query, entityComparator, clock);
        // CompositeIndexManager does some filesystem reads/writes, so needs to
        // be privileged.
        AccessController.doPrivileged(new PrivilegedAction<Object>() {

            @Override
            public Object run() {
                LocalCompositeIndexManager.getInstance().processQuery(validatedQuery.getV3Query());
                return null;
            }
        });
        // Using next function to prefetch results and return them from runQuery
        QueryResult result = liveQuery.nextResult(query.hasOffset() ? query.getOffset() : null, query.hasCount() ? query.getCount() : null, query.isCompile());
        if (query.isCompile()) {
            result.setCompiledQuery(liveQuery.compileQuery());
        }
        if (result.isMoreResults()) {
            long cursor = queryId.getAndIncrement();
            profile.addQuery(cursor, liveQuery);
            result.getMutableCursor().setApp(query.getApp()).setCursor(cursor);
        }
        // Copy the index list for the query into the result.
        for (Index index : LocalCompositeIndexManager.getInstance().queryIndexList(query)) {
            result.addIndex(wrapIndexInCompositeIndex(app, index));
        }
        // for
        return result;
    }
}
Also used : Element(com.google.storage.onestore.v3.OnestoreEntity.Path.Element) Utils.getLastElement(com.google.appengine.api.datastore.dev.Utils.getLastElement) ArrayList(java.util.ArrayList) Index(com.google.storage.onestore.v3.OnestoreEntity.Index) CompositeIndex(com.google.storage.onestore.v3.OnestoreEntity.CompositeIndex) ByteString(com.google.protobuf.ByteString) Predicate(com.google.common.base.Predicate) QueryResult(com.google.apphosting.datastore.DatastoreV3Pb.QueryResult) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto) Path(com.google.storage.onestore.v3.OnestoreEntity.Path) Order(com.google.apphosting.datastore.DatastoreV3Pb.Query.Order) EntityProtoComparator(com.google.appengine.api.datastore.EntityProtoComparators.EntityProtoComparator) Reference(com.google.storage.onestore.v3.OnestoreEntity.Reference) InvalidConversionException(com.google.cloud.datastore.core.exception.InvalidConversionException) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

Path (com.google.storage.onestore.v3.OnestoreEntity.Path)6 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)6 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)4 EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)3 Utils.getLastElement (com.google.appengine.api.datastore.dev.Utils.getLastElement)2 ImmutableList (com.google.common.collect.ImmutableList)2 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 EntityProtoComparator (com.google.appengine.api.datastore.EntityProtoComparators.EntityProtoComparator)1 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)1 PutRequest (com.google.apphosting.datastore.DatastoreV3Pb.PutRequest)1 PutResponse (com.google.apphosting.datastore.DatastoreV3Pb.PutResponse)1 Order (com.google.apphosting.datastore.DatastoreV3Pb.Query.Order)1 QueryResult (com.google.apphosting.datastore.DatastoreV3Pb.QueryResult)1 InvalidConversionException (com.google.cloud.datastore.core.exception.InvalidConversionException)1 Predicate (com.google.common.base.Predicate)1 ImmutableMap (com.google.common.collect.ImmutableMap)1