@Override public Observable<RawJsonDocument> call(AsyncN1qlQueryRow row) { String id = new String(row.byteValue()).trim(); return bucket.async().get(id.substring(1, id.length()-1), RawJsonDocument.class); } })
/** * Return the {@link JsonObject} representation of the JSON corresponding to this row. * The {@link JsonObject} is lazily created from {@link #byteValue()} the first time it is requested. * * @return the JsonObject representation of the value. * @throws TranscodingException if the lazy deserialization couldn't be performed due to a Jackson error. */ @Override public JsonObject value() { return asyncRow.value(); }
@Override public IndexInfo call(AsyncN1qlQueryRow asyncN1qlQueryRow) { return new IndexInfo(asyncN1qlQueryRow.value()); } };
@Override public <T> Observable<List<T>> query(final N1qlQuery query) { return bucket.query(query) .flatMap(aqr -> Observable.zip(aqr.rows().toList(), aqr.errors().toList(), aqr.finalSuccess().singleOrDefault(Boolean.FALSE), (rows, errors, finalSuccess) -> { if (!finalSuccess) { throw new QueryExecutionException( "execution of query resulted in exception: ", Try.apply(() -> errors.get(0)).orElse(null)); } List<T> value = new ArrayList<>(); for (AsyncN1qlQueryRow row : rows) { try { T v = converter.fromBytes(row.byteValue()); value.add(v); } catch (IOException ex) { throw new QueryExecutionException( "execution of query resulted in exception", null, ex); } } return value; })); }
@Override public <T>Observable<T> findByN1QLProjection(N1qlQuery query, Class<T> entityClass) { return queryN1QL(query) .flatMap(asyncN1qlQueryResult -> asyncN1qlQueryResult.errors() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute n1ql query due to error:" + error.toString()))) .switchIfEmpty(asyncN1qlQueryResult.rows())) .map(row -> { JsonObject json = ((AsyncN1qlQueryRow)row).value(); T decoded = translationService.decodeFragment(json.toString(), entityClass); return decoded; }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute n1ql query", throwable))); }
@Override public byte[] byteValue() { return asyncRow.byteValue(); }
@Override public <T>Observable<T> findByN1QLProjection(N1qlQuery query, Class<T> entityClass) { return queryN1QL(query) .flatMap(asyncN1qlQueryResult -> asyncN1qlQueryResult.errors() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute n1ql query due to error:" + error.toString()))) .switchIfEmpty(asyncN1qlQueryResult.rows())) .map(row -> { JsonObject json = ((AsyncN1qlQueryRow)row).value(); T decoded = translationService.decodeFragment(json.toString(), entityClass); return decoded; }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute n1ql query", throwable))); }
@Override public <T>Observable<T> findByN1QL(N1qlQuery query, Class<T> entityClass) { return queryN1QL(query) .flatMap(asyncN1qlQueryResult -> asyncN1qlQueryResult.errors() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute n1ql query due to error:" + error.toString()))) .switchIfEmpty(asyncN1qlQueryResult.rows())) .map(row -> { JsonObject json = ((AsyncN1qlQueryRow)row).value(); String id = json.getString(TemplateUtils.SELECT_ID); Long cas = json.getLong(TemplateUtils.SELECT_CAS); if (id == null || cas == null) { throw new CouchbaseQueryExecutionException("Unable to retrieve enough metadata for N1QL to entity mapping, " + "have you selected " + TemplateUtils.SELECT_ID + " and " + TemplateUtils.SELECT_CAS + "?"); } json = json.removeKey(TemplateUtils.SELECT_ID).removeKey(TemplateUtils.SELECT_CAS); RawJsonDocument entityDoc = RawJsonDocument.create(id, json.toString(), cas); T decoded = mapToEntity(id, entityDoc, entityClass); return decoded; }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute n1ql query", throwable))); }
@Override public <T>Observable<T> findByN1QL(N1qlQuery query, Class<T> entityClass) { return queryN1QL(query) .flatMap(asyncN1qlQueryResult -> asyncN1qlQueryResult.errors() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute n1ql query due to error:" + error.toString()))) .switchIfEmpty(asyncN1qlQueryResult.rows())) .map(row -> { JsonObject json = ((AsyncN1qlQueryRow)row).value(); String id = json.getString(TemplateUtils.SELECT_ID); Long cas = json.getLong(TemplateUtils.SELECT_CAS); if (id == null || cas == null) { throw new CouchbaseQueryExecutionException("Unable to retrieve enough metadata for N1QL to entity mapping, " + "have you selected " + TemplateUtils.SELECT_ID + " and " + TemplateUtils.SELECT_CAS + "?"); } json = json.removeKey(TemplateUtils.SELECT_ID).removeKey(TemplateUtils.SELECT_CAS); RawJsonDocument entityDoc = RawJsonDocument.create(id, json.toString(), cas); T decoded = mapToEntity(id, entityDoc, entityClass); return decoded; }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute n1ql query", throwable))); }