Ejemplo n.º 1
0
 /**
  * Queries a view.
  *
  * @param <K> Object type K (key)
  * @param <V> Object type V (value)
  * @param classOfK The class of type K.
  * @param classOfV The class of type V.
  * @param classOfT The class of type T.
  * @return The View result entries.
  */
 public <K, V, T> ViewResult<K, V, T> queryView(
     Class<K> classOfK, Class<V> classOfV, Class<T> classOfT) {
   InputStream instream = null;
   try {
     Reader reader = new InputStreamReader(instream = queryForStream(), Charsets.UTF_8);
     JsonObject json = new JsonParser().parse(reader).getAsJsonObject();
     ViewResult<K, V, T> vr = new ViewResult<K, V, T>();
     vr.setTotalRows(getAsLong(json, "total_rows"));
     vr.setOffset(getAsInt(json, "offset"));
     vr.setUpdateSeq(getAsLong(json, "update_seq"));
     JsonArray jsonArray = json.getAsJsonArray("rows");
     if (jsonArray.size() == 0) { // validate available rows
       throw new NoDocumentException("No result was returned by this view query.");
     }
     for (JsonElement e : jsonArray) {
       ViewResult<K, V, T>.Rows row = vr.new Rows();
       row.setId(JsonToObject(gson, e, "id", String.class));
       row.setKey(JsonToObject(gson, e, "key", classOfK));
       row.setValue(JsonToObject(gson, e, "value", classOfV));
       if (Boolean.TRUE.equals(this.includeDocs)) {
         row.setDoc(JsonToObject(gson, e, "doc", classOfT));
       }
       vr.getRows().add(row);
     }
     return vr;
   } finally {
     close(instream);
   }
 }
Ejemplo n.º 2
0
 /**
  * Queries a view.
  *
  * @param <T> Object type T
  * @param classOfT The class of type T
  * @return The result of the view query as a {@code List<T> }
  */
 public <T> List<T> query(Class<T> classOfT) {
   InputStream instream = null;
   try {
     Reader reader = new InputStreamReader(instream = queryForStream(), Charsets.UTF_8);
     JsonArray jsonArray = new JsonParser().parse(reader).getAsJsonObject().getAsJsonArray("rows");
     List<T> list = new ArrayList<T>();
     for (JsonElement jsonElem : jsonArray) {
       JsonElement elem = jsonElem.getAsJsonObject();
       if (Boolean.TRUE.equals(this.includeDocs)) {
         elem = jsonElem.getAsJsonObject().get("doc");
       }
       T t = this.gson.fromJson(elem, classOfT);
       list.add(t);
     }
     return list;
   } finally {
     close(instream);
   }
 }