Java Code Examples for com.couchbase.lite.Query

The following examples show how to use com.couchbase.lite.Query. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mini-hacks   Source File: SyncManager.java    License: MIT License 6 votes vote down vote up
private void queryCities() {
    final Query query = database.getView(CITIES_VIEW).createQuery();
    query.setGroupLevel(1);

    LiveQuery liveQuery = query.toLiveQuery();
    liveQuery.addChangeListener(new LiveQuery.ChangeListener() {
        @Override
        public void changed(LiveQuery.ChangeEvent event) {
            try {
                QueryEnumerator enumeration = query.run();
                for (QueryRow row : enumeration) {
                    Log.d("CityExplorer", "Row is " + row.getValue() + " and key " + row.getKey());
                }

            } catch (CouchbaseLiteException e) {
                e.printStackTrace();
            }
        }
    });

    liveQuery.start();
}
 
Example 2
Source Project: couchbase-lite-android   Source File: QueryPerfTest.java    License: Apache License 2.0 6 votes vote down vote up
int query() {
    int count = 0;
    bench.start();
    Query q = QueryBuilder.select(SelectResult.expression(Meta.id))
            .from(DataSource.database(db));
    ResultSet rs = null;
    try {
        rs = q.execute();
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
    for (Result r : rs) {
        count++;
    }
    double t = bench.stop();
    System.err.print(String.format("Query %d documents in %.06f sec\n", count, t));
    return count;
}
 
Example 3
private void deleteAll() throws CouchbaseLiteException {
    // query all documents, mark them as deleted
    Query query = database.createAllDocumentsQuery();
    QueryEnumerator result = query.run();
    database.beginTransaction();
    while (result.hasNext()) {
        QueryRow row = result.next();
        row.getDocument().purge();
    }
    database.endTransaction(true);
}
 
Example 4
Source Project: couchbase-lite-android   Source File: TunesPerfTest.java    License: Apache License 2.0 5 votes vote down vote up
List<String> collectQueryResults(Query query) throws CouchbaseLiteException {
    List<String> results = new ArrayList<>();
    ResultSet rs = query.execute();
    for(Result r : rs){
        results.add(r.getString(0));
    }
    return results;
}
 
Example 5
private void indexedStringEntityQueriesRun(View indexedStringView, int count)
        throws CouchbaseLiteException {
    // create entities
    String[] fixedRandomStrings = StringGenerator.createFixedRandomStrings(count);
    database.beginTransaction();
    for (int i = 0; i < count; i++) {
        Document entity = database.getDocument(String.valueOf(i));
        Map<String, Object> properties = new HashMap<>();
        properties.put("indexedString", fixedRandomStrings[i]);
        entity.putProperties(properties);
    }
    database.endTransaction(true);
    log("Built and inserted entities.");

    // query for entities by indexed string at random
    int[] randomIndices = StringGenerator.getFixedRandomIndices(getQueryCount(), count - 1);

    // clear the document cache to force loading properties from the database
    database.clearDocumentCache();

    startClock();
    for (int i = 0; i < getQueryCount(); i++) {
        int nextIndex = randomIndices[i];
        List<Object> keyToQuery = new ArrayList<>(1);
        keyToQuery.add(fixedRandomStrings[nextIndex]);

        Query query = indexedStringView.createQuery();
        query.setKeys(keyToQuery);
        QueryEnumerator result = query.run();
        while (result.hasNext()) {
            QueryRow row = result.next();
            //noinspection unused
            Document document = row.getDocument();
        }
    }
    stopClock(Benchmark.Type.QUERY_INDEXED);

    // delete all entities
    deleteAll();
    log("Deleted all entities.");
}