Search in sources :

Example 36 with Query

use of com.google.appengine.api.datastore.Query in project CodeU-Spring-2018 by maksymko.

the class PersistentDataStore method loadConversations.

/**
 * Loads all Conversation objects from the Datastore service and returns them in a List.
 *
 * @throws PersistentDataStoreException if an error was detected during the load from the
 *     Datastore service
 */
public List<Conversation> loadConversations() throws PersistentDataStoreException {
    List<Conversation> conversations = new ArrayList<>();
    // Retrieve all conversations from the datastore.
    Query query = new Query("chat-conversations");
    PreparedQuery results = datastore.prepare(query);
    for (Entity entity : results.asIterable()) {
        try {
            UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
            UUID ownerUuid = UUID.fromString((String) entity.getProperty("owner_uuid"));
            String title = (String) entity.getProperty("title");
            Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
            Conversation conversation = new Conversation(uuid, ownerUuid, title, creationTime);
            conversations.add(conversation);
        } catch (Exception e) {
            // database entity definition mismatches, or service mismatches.
            throw new PersistentDataStoreException(e);
        }
    }
    return conversations;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) PersistentDataStoreException(codeu.model.store.persistence.PersistentDataStoreException) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) Instant(java.time.Instant) ArrayList(java.util.ArrayList) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Conversation(codeu.model.data.Conversation) UUID(java.util.UUID) PersistentDataStoreException(codeu.model.store.persistence.PersistentDataStoreException)

Example 37 with Query

use of com.google.appengine.api.datastore.Query in project Cached-Datastore by Emperorlou.

the class CachedDatastoreService method fetchAsList_Keys.

public List<Key> fetchAsList_Keys(String kind, Filter filter, int limit) {
    Query q = new Query(kind);
    q.setFilter(filter);
    q.setKeysOnly();
    return fetchKeys(q, limit);
}
Also used : Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery)

Example 38 with Query

use of com.google.appengine.api.datastore.Query in project Cached-Datastore by Emperorlou.

the class QueryHelper method fetchChildren.

/**
 * This performs an ancestor query to fetch all children (within a limit)
 * @param ds
 * @param parent
 * @param limit
 * @return
 */
public List<CachedEntity> fetchChildren(Key parent, int limit) {
    Query q = new Query();
    q.setAncestor(parent);
    return ds.fetchAsList(q, limit);
}
Also used : Query(com.google.appengine.api.datastore.Query)

Example 39 with Query

use of com.google.appengine.api.datastore.Query in project Cached-Datastore by Emperorlou.

the class QueryHelper method getFilteredORList.

public List<CachedEntity> getFilteredORList(Cursor cursor, String kind, String fieldName, Object equalToValue, String fieldName2, Object equalToValue2, String fieldName3, Object equalToValue3) {
    FilterPredicate f1 = new FilterPredicate(fieldName, FilterOperator.EQUAL, equalToValue);
    FilterPredicate f2 = new FilterPredicate(fieldName2, FilterOperator.EQUAL, equalToValue2);
    FilterPredicate f3 = new FilterPredicate(fieldName3, FilterOperator.EQUAL, equalToValue3);
    Filter filter = CompositeFilterOperator.or(f1, f2, f3);
    Query q = new Query(kind);
    q.setFilter(filter);
    return ds.fetchAsList(q, 1000, cursor);
}
Also used : Query(com.google.appengine.api.datastore.Query) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) Filter(com.google.appengine.api.datastore.Query.Filter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate)

Example 40 with Query

use of com.google.appengine.api.datastore.Query in project Cached-Datastore by Emperorlou.

the class ShardedCounterService method readCounter.

/**
 * Retrieves the value of the counter identified by the entity and fieldName.
 *
 * It attempts to return the memcache cache value. If that is null then the shards are
 * queried for and counted instead.
 *
 * This method will return null if a sharded counter has not yet been initialized for the given entity-field.
 *
 * @param entity
 * @param fieldName
 * @return
 */
public Long readCounter(Key entityKey, String fieldName) {
    long value = 0;
    Query query = new Query(SHARD_COUNTER_KIND);
    query.setFilter(getShardQueryFilterFor(entityKey, fieldName));
    for (CachedEntity shard : ds.fetchAsList(query, 1000)) value += (Long) shard.getProperty("value");
    return value;
}
Also used : Query(com.google.appengine.api.datastore.Query)

Aggregations

Query (com.google.appengine.api.datastore.Query)74 Entity (com.google.appengine.api.datastore.Entity)59 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)47 Test (org.junit.Test)38 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)31 Filter (com.google.appengine.api.datastore.Query.Filter)20 Key (com.google.appengine.api.datastore.Key)16 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)16 DatastoreService (com.google.appengine.api.datastore.DatastoreService)13 ArrayList (java.util.ArrayList)13 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)6 FetchOptions (com.google.appengine.api.datastore.FetchOptions)6 PrintWriter (java.io.PrintWriter)6 Instant (java.time.Instant)6 UUID (java.util.UUID)6 Conversation (codeu.model.data.Conversation)2 Message (codeu.model.data.Message)2 User (codeu.model.data.User)2 Book (com.example.getstarted.objects.Book)2 Result (com.example.getstarted.objects.Result)2