Search in sources :

Example 1 with ProjectionEntity

use of com.google.cloud.datastore.ProjectionEntity in project jetty.project by eclipse.

the class GCloudSessionDataStore method queryExpiryByIndex.

/** An efficient query to find sessions whose expiry time has passed:
     * uses a projection query, which requires indexes to be uploaded.
     * @return id,lastnode and expiry time of sessions that have expired
     * @throws Exception if datastore experiences a problem
     */
protected Set<ExpiryInfo> queryExpiryByIndex() throws Exception {
    Set<ExpiryInfo> info = new HashSet<>();
    Query<ProjectionEntity> query = Query.newProjectionEntityQueryBuilder().setKind(_model.getKind()).setProjection(_model.getId(), _model.getLastNode(), _model.getExpiry()).setFilter(CompositeFilter.and(PropertyFilter.gt(_model.getExpiry(), 0), PropertyFilter.le(_model.getExpiry(), System.currentTimeMillis()))).setLimit(_maxResults).build();
    QueryResults<ProjectionEntity> presults;
    if (LOG.isDebugEnabled()) {
        long start = System.currentTimeMillis();
        presults = _datastore.run(query);
        LOG.debug("Expiry query by index in {}ms", System.currentTimeMillis() - start);
    } else
        presults = _datastore.run(query);
    while (presults.hasNext()) {
        ProjectionEntity pe = presults.next();
        info.add(new ExpiryInfo(pe.getString(_model.getId()), pe.getString(_model.getLastNode()), pe.getLong(_model.getExpiry())));
    }
    return info;
}
Also used : ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) HashSet(java.util.HashSet)

Example 2 with ProjectionEntity

use of com.google.cloud.datastore.ProjectionEntity in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreTest method testRunGqlQueryNoCasting.

@Test
public void testRunGqlQueryNoCasting() throws InterruptedException {
    Query<Entity> query1 = Query.newGqlQueryBuilder(ResultType.ENTITY, "select * from " + KIND1).setNamespace(NAMESPACE).build();
    Query<Entity> scQuery1 = Query.newEntityQueryBuilder().setNamespace(NAMESPACE).setKind(KIND1).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).build();
    Iterator<Entity> results1 = getStronglyConsistentResults(scQuery1, query1);
    assertTrue(results1.hasNext());
    assertEquals(ENTITY1, results1.next());
    assertFalse(results1.hasNext());
    DATASTORE.put(ENTITY3);
    Query<? extends Entity> query2 = Query.newGqlQueryBuilder(ResultType.ENTITY, "select * from " + KIND2 + " order by __key__").setNamespace(NAMESPACE).build();
    Query<? extends Entity> scQuery2 = Query.newEntityQueryBuilder().setNamespace(NAMESPACE).setKind(KIND2).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).setOrderBy(OrderBy.asc("__key__")).build();
    Iterator<Entity> results2 = getStronglyConsistentResults(scQuery2, query2);
    assertTrue(results2.hasNext());
    assertEquals(ENTITY2, results2.next());
    assertTrue(results2.hasNext());
    assertEquals(ENTITY3, results2.next());
    assertFalse(results2.hasNext());
    query1 = Query.newGqlQueryBuilder(ResultType.ENTITY, "select * from bla").setNamespace(NAMESPACE).build();
    scQuery1 = Query.newEntityQueryBuilder().setNamespace(NAMESPACE).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).setKind("bla").build();
    results1 = getStronglyConsistentResults(scQuery1, query1);
    assertFalse(results1.hasNext());
    Query<Key> keyOnlyQuery = Query.newGqlQueryBuilder(ResultType.KEY, "select __key__ from " + KIND1).setNamespace(NAMESPACE).build();
    Query<Key> scKeyOnlyQuery = Query.newKeyQueryBuilder().setNamespace(NAMESPACE).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).setKind(KIND1).build();
    Iterator<Key> keyOnlyResults = getStronglyConsistentResults(scKeyOnlyQuery, keyOnlyQuery);
    assertTrue(keyOnlyResults.hasNext());
    assertEquals(KEY1, keyOnlyResults.next());
    assertFalse(keyOnlyResults.hasNext());
    GqlQuery<ProjectionEntity> keyProjectionQuery = Query.newGqlQueryBuilder(ResultType.PROJECTION_ENTITY, "select __key__ from " + KIND1).setNamespace(NAMESPACE).build();
    Query<ProjectionEntity> scKeyProjectionQuery = Query.newProjectionEntityQueryBuilder().addProjection("__key__").setNamespace(NAMESPACE).setKind(KIND1).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).build();
    Iterator<ProjectionEntity> keyProjectionResult = getStronglyConsistentResults(scKeyProjectionQuery, keyProjectionQuery);
    assertTrue(keyProjectionResult.hasNext());
    ProjectionEntity projectionEntity = keyProjectionResult.next();
    assertEquals(KEY1, projectionEntity.getKey());
    assertTrue(projectionEntity.getNames().isEmpty());
    assertFalse(keyProjectionResult.hasNext());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Example 3 with ProjectionEntity

use of com.google.cloud.datastore.ProjectionEntity in project jetty.project by eclipse.

the class GCloudSessionDataStore method exists.

/** 
     * @see org.eclipse.jetty.server.session.SessionDataStore#exists(java.lang.String)
     */
@Override
public boolean exists(String id) throws Exception {
    if (_indexesPresent) {
        Query<ProjectionEntity> query = Query.newProjectionEntityQueryBuilder().setKind(_model.getKind()).setProjection(_model.getExpiry()).setFilter(PropertyFilter.eq(_model.getId(), id)).build();
        QueryResults<ProjectionEntity> presults;
        if (LOG.isDebugEnabled()) {
            long start = System.currentTimeMillis();
            presults = _datastore.run(query);
            LOG.debug("Exists query by index in {}ms", System.currentTimeMillis() - start);
        } else
            presults = _datastore.run(query);
        if (presults.hasNext()) {
            ProjectionEntity pe = presults.next();
            return !isExpired(pe.getLong(_model.getExpiry()));
        } else
            return false;
    } else {
        Query<Entity> query = Query.newEntityQueryBuilder().setKind(_model.getKind()).setFilter(PropertyFilter.eq(_model.getId(), id)).build();
        QueryResults<Entity> results;
        if (LOG.isDebugEnabled()) {
            long start = System.currentTimeMillis();
            results = _datastore.run(query);
            LOG.debug("Exists query no index in {}ms", System.currentTimeMillis() - start);
        } else
            results = _datastore.run(query);
        if (results.hasNext()) {
            Entity entity = results.next();
            return !isExpired(entity.getLong(_model.getExpiry()));
        } else
            return false;
    }
}
Also used : Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity)

Example 4 with ProjectionEntity

use of com.google.cloud.datastore.ProjectionEntity in project google-cloud-java by GoogleCloudPlatform.

the class ITDatastoreTest method testRunStructuredQuery.

@Test
public void testRunStructuredQuery() throws InterruptedException {
    Query<Entity> query = Query.newEntityQueryBuilder().setKind(KIND1).setOrderBy(OrderBy.asc("__key__")).build();
    Query<Entity> scQuery = Query.newEntityQueryBuilder().setKind(KIND1).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).setOrderBy(OrderBy.asc("__key__")).build();
    Iterator<Entity> results1 = getStronglyConsistentResults(scQuery, query);
    assertTrue(results1.hasNext());
    assertEquals(ENTITY1, results1.next());
    assertFalse(results1.hasNext());
    Query<Key> keyOnlyQuery = Query.newKeyQueryBuilder().setKind(KIND1).build();
    Query<Key> scKeyOnlyQuery = Query.newKeyQueryBuilder().setKind(KIND1).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).build();
    Iterator<Key> results2 = getStronglyConsistentResults(scKeyOnlyQuery, keyOnlyQuery);
    assertTrue(results2.hasNext());
    assertEquals(ENTITY1.getKey(), results2.next());
    assertFalse(results2.hasNext());
    StructuredQuery<ProjectionEntity> keyOnlyProjectionQuery = Query.newProjectionEntityQueryBuilder().setKind(KIND1).setProjection("__key__").build();
    StructuredQuery<ProjectionEntity> scKeyOnlyProjectionQuery = Query.newProjectionEntityQueryBuilder().setKind(KIND1).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).setProjection("__key__").build();
    Iterator<ProjectionEntity> results3 = getStronglyConsistentResults(scKeyOnlyProjectionQuery, keyOnlyProjectionQuery);
    assertTrue(results3.hasNext());
    ProjectionEntity projectionEntity = results3.next();
    assertEquals(ENTITY1.getKey(), projectionEntity.getKey());
    assertTrue(projectionEntity.getNames().isEmpty());
    assertFalse(results3.hasNext());
    StructuredQuery<ProjectionEntity> projectionQuery = Query.newProjectionEntityQueryBuilder().setKind(KIND2).setProjection("age").setFilter(PropertyFilter.gt("age", 18)).setDistinctOn("age").setOrderBy(OrderBy.asc("age")).setLimit(10).build();
    StructuredQuery<ProjectionEntity> scProjectionQuery = Query.newProjectionEntityQueryBuilder().setKind(KIND2).setFilter(PropertyFilter.hasAncestor(ROOT_KEY)).setProjection("age").setFilter(PropertyFilter.gt("age", 18)).setDistinctOn("age").setOrderBy(OrderBy.asc("age")).setLimit(10).build();
    Iterator<ProjectionEntity> results4 = getStronglyConsistentResults(scProjectionQuery, projectionQuery);
    assertTrue(results4.hasNext());
    ProjectionEntity entity = results4.next();
    assertEquals(ENTITY2.getKey(), entity.getKey());
    assertEquals(20, entity.getLong("age"));
    assertEquals(1, entity.getNames().size());
    assertFalse(results4.hasNext());
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Entity(com.google.cloud.datastore.Entity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) ProjectionEntity(com.google.cloud.datastore.ProjectionEntity) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) Test(org.junit.Test)

Aggregations

ProjectionEntity (com.google.cloud.datastore.ProjectionEntity)4 Entity (com.google.cloud.datastore.Entity)3 FullEntity (com.google.cloud.datastore.FullEntity)2 IncompleteKey (com.google.cloud.datastore.IncompleteKey)2 Key (com.google.cloud.datastore.Key)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1