| 1514 | } |
| 1515 | |
| 1516 | int HTTPServer::queryResultsJSON(http::Context& ctx, reindexer::QueryResults& res, const IQRSerializingOption& qrOption, bool withColumns, |
| 1517 | int width) { |
| 1518 | const auto offset = qrOption.ExternalOffset(); |
| 1519 | const auto limit = qrOption.ExternalLimit(); |
| 1520 | |
| 1521 | auto ser = makeRestrictedWrSerializer(ctx); |
| 1522 | JsonBuilder builder(ser); |
| 1523 | |
| 1524 | auto iarray = builder.Array(kParamItems); |
| 1525 | auto it = offset < res.Count() ? (res.begin() + offset) : res.end(); |
| 1526 | std::vector<std::string> jsonData; |
| 1527 | if (withColumns) { |
| 1528 | size_t size = res.Count(); |
| 1529 | if (limit > offset && limit - offset < size) { |
| 1530 | size = limit - offset; |
| 1531 | } |
| 1532 | jsonData.reserve(size); |
| 1533 | } |
| 1534 | WrSerializer itemSer; |
| 1535 | std::optional<Reindexer> db; |
| 1536 | auto cjsonViewer = [this, &res, &ctx, &db](std::string_view cjson) { |
| 1537 | if (!db.has_value()) { |
| 1538 | db.emplace(getDB<kRoleDataRead>(ctx)); |
| 1539 | } |
| 1540 | auto item = db->NewItem(res.GetNamespaces()[0]); |
| 1541 | const auto err = item.FromCJSON(cjson); |
| 1542 | if (!err.ok()) { |
| 1543 | throw Error(err.code(), "Unable to parse CJSON for WAL item: {}", err.whatStr()); |
| 1544 | } |
| 1545 | return std::string(item.GetJSON()); |
| 1546 | }; |
| 1547 | const bool isWALQuery = res.IsWALQuery(); |
| 1548 | for (size_t i = 0; it != res.end() && i < limit; ++i, ++it) { |
| 1549 | if (!isWALQuery) { |
| 1550 | iarray.Raw(""); |
| 1551 | if (withColumns) { |
| 1552 | itemSer.Reset(); |
| 1553 | const auto err = it.GetJSON(itemSer, false); |
| 1554 | if (!err.ok()) { |
| 1555 | return jsonStatus(ctx, http::HttpStatus(err)); |
| 1556 | } |
| 1557 | jsonData.emplace_back(itemSer.Slice()); |
| 1558 | ser.Write(itemSer.Slice()); |
| 1559 | } else { |
| 1560 | const auto err = it.GetJSON(ser, false); |
| 1561 | if (!err.ok()) { |
| 1562 | return jsonStatus(ctx, http::HttpStatus(err)); |
| 1563 | } |
| 1564 | } |
| 1565 | } else { |
| 1566 | auto obj = iarray.Object(); |
| 1567 | { |
| 1568 | auto lsnObj = obj.Object(kWALParamLsn); |
| 1569 | it.GetLSN().GetJSON(lsnObj); |
| 1570 | } |
| 1571 | if (!it.IsRaw()) { |
| 1572 | iarray.Raw(kWALParamItem, ""); |
| 1573 | if (withColumns) { |
nothing calls this directly
no test coverage detected