Search in sources :

Example 1 with ViewQuery

use of org.ektorp.ViewQuery in project apex-malhar by apache.

the class AbstractCouchDBInputOperator method emitTuples.

@Override
public void emitTuples() {
    ViewQuery viewQuery = getViewQuery();
    if (pageSize > 0) {
        viewQuery.limit(pageSize);
    }
    if (nextPageKey != null) {
        viewQuery.startKey(nextPageKey);
    }
    if (skip) {
        viewQuery.skip(1);
    }
    ViewResult result = store.queryStore(viewQuery);
    List<ViewResult.Row> rows = result.getRows();
    try {
        for (ViewResult.Row row : result.getRows()) {
            T tuple = getTuple(row);
            outputPort.emit(tuple);
        }
    } catch (Throwable cause) {
        Throwables.propagate(cause);
    }
    if (rows.size() > 0) {
        // Use the last row as the start key and skip one item
        // In case we reach the end we will continue to make the request with last row till there is more data available
        // in the store
        nextPageKey = rows.get(rows.size() - 1).getKey();
        // The skip option should only be used with small values, as skipping a large range of documents this way is inefficient.
        skip = true;
    }
}
Also used : ViewQuery(org.ektorp.ViewQuery) ViewResult(org.ektorp.ViewResult)

Example 2 with ViewQuery

use of org.ektorp.ViewQuery in project gora by apache.

the class CouchDBStore method deleteByQuery.

/**
   * Deletes all the objects matching the query.
   * See also the note on <a href="#visibility">visibility</a>.
   *
   * @param query matching records to this query will be deleted
   * @return number of deleted records
   */
@Override
public long deleteByQuery(Query<K, T> query) {
    final K key = query.getKey();
    final K startKey = query.getStartKey();
    final K endKey = query.getEndKey();
    if (key == null && startKey == null && endKey == null) {
        deleteSchema();
        createSchema();
        return -1;
    } else {
        final ViewQuery viewQuery = new ViewQuery().allDocs().includeDocs(true).key(key).startKey(startKey).endKey(endKey);
        final List<Map> result = db.queryView(viewQuery, Map.class);
        final Map<String, List<String>> revisionsToPurge = new HashMap<>();
        for (Map map : result) {
            final List<String> revisions = new ArrayList<>();
            String keyString = map.get("_id").toString();
            String rev = map.get("_rev").toString();
            revisions.add(rev);
            revisionsToPurge.put(keyString, revisions);
        }
        return db.purge(revisionsToPurge).getPurged().size();
    }
}
Also used : ViewQuery(org.ektorp.ViewQuery)

Example 3 with ViewQuery

use of org.ektorp.ViewQuery in project gora by apache.

the class CouchDBStore method execute.

/**
   * Execute the query and return the result.
   */
@Override
public Result<K, T> execute(Query<K, T> query) {
    query.setFields(getFieldsToQuery(query.getFields()));
    final ViewQuery viewQuery = new ViewQuery().allDocs().includeDocs(true).startKey(query.getStartKey()).endKey(query.getEndKey()).limit(//FIXME GORA have long value but ektorp client use integer
    Ints.checkedCast(query.getLimit()));
    CouchDBResult<K, T> couchDBResult = new CouchDBResult<>(this, query, db.queryView(viewQuery, Map.class));
    return couchDBResult;
}
Also used : CouchDBResult(org.apache.gora.couchdb.query.CouchDBResult) ViewQuery(org.ektorp.ViewQuery)

Example 4 with ViewQuery

use of org.ektorp.ViewQuery in project apex-malhar by apache.

the class CouchDBTestHelper method createAndFetchViewQuery.

public static ViewQuery createAndFetchViewQuery() {
    if (!connector.contains(DESIGN_DOC_ID)) {
        // The design document doesn't exist in the database so we create it.
        JsonNode rootNode = mapper.createObjectNode();
        ((ObjectNode) rootNode).put("language", "javascript");
        ((ObjectNode) rootNode).putObject("views").putObject(TEST_VIEW).put("map", "function(doc) {\n  emit(doc._id, doc);\n}");
        connector.create(DESIGN_DOC_ID, rootNode);
    }
    return new ViewQuery().designDocId(DESIGN_DOC_ID).viewName(TEST_VIEW);
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) ViewQuery(org.ektorp.ViewQuery) JsonNode(org.codehaus.jackson.JsonNode)

Aggregations

ViewQuery (org.ektorp.ViewQuery)4 CouchDBResult (org.apache.gora.couchdb.query.CouchDBResult)1 JsonNode (org.codehaus.jackson.JsonNode)1 ObjectNode (org.codehaus.jackson.node.ObjectNode)1 ViewResult (org.ektorp.ViewResult)1