Example #1
0
void JsonCursor::dump (triagens::basics::StringBuffer& buffer) {
  buffer.appendText("\"result\":[");

  size_t const n = batchSize();

  // reserve 48 bytes per result document by default, but only
  // if the specified batch size does not get out of hand
  // otherwise specifying a very high batch size would make the allocation fail
  // in every case, even if there were much less documents in the collection
  if (n <= 50000) {
    int res = buffer.reserve(n * 48);

    if (res != TRI_ERROR_NO_ERROR) {
      THROW_ARANGO_EXCEPTION(res);
    }
  }

  for (size_t i = 0; i < n; ++i) {
    if (! hasNext()) {
      break;
    }

    if (i > 0) {
      buffer.appendChar(',');
    }
    
    auto row = next();
    if (row == nullptr) {
      THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
    }

    int res = TRI_StringifyJson(buffer.stringBuffer(), row);

    if (res != TRI_ERROR_NO_ERROR) {
      THROW_ARANGO_EXCEPTION(res);
    }
  }

  buffer.appendText("],\"hasMore\":");
  buffer.appendText(hasNext() ? "true" : "false");

  if (hasNext()) {
    // only return cursor id if there are more documents
    buffer.appendText(",\"id\":\"");
    buffer.appendInteger(id());
    buffer.appendText("\"");
  }

  if (hasCount()) {
    buffer.appendText(",\"count\":");
    buffer.appendInteger(static_cast<uint64_t>(count()));
  }

  TRI_json_t const* extraJson = extra();

  if (TRI_IsObjectJson(extraJson)) {
    buffer.appendText(",\"extra\":");
    TRI_StringifyJson(buffer.stringBuffer(), extraJson);
  }
    
  if (! hasNext()) {
    // mark the cursor as deleted
    this->deleted();
  }
}