<!--#
Test type 3: Multiple database queries
TechEmpower Web Framework Benchmarks
-->
<!--#declaration
#include "world.h"

static char buffer[128];
static UElasticSearchClient* es;

#ifndef AS_cpoll_cppsp_DO
static UVector<World*>* pvworld_query;
#endif

#define QLEN U_CONSTANT_SIZE("{\"query\":{\"match\":{\"_id\":\"")

static void usp_fork_equery()
{
   U_TRACE(5, "::usp_fork_equery()")

   U_NEW(UElasticSearchClient, es, UElasticSearchClient);

   if (es->connect() == false)
      {
      U_WARNING("usp_fork_equery(): connection disabled or failed");

      delete es;
             es = U_NULLPTR;

      return;
      }

   U_MEMCPY(buffer, "{\"query\":{\"match\":{\"_id\":\"", QLEN);

#ifndef AS_cpoll_cppsp_DO
   U_NEW(UVector<World*>, pvworld_query, UVector<World*>(500));
#endif
}

#ifdef DEBUG
static void usp_end_equery()
{
   U_TRACE(5, "::usp_end_equery()")

   if (es)
      {
      delete es;

#  ifndef AS_cpoll_cppsp_DO
      delete pvworld_query;
#  endif
      }
}
#endif
-->
<!--#header
Content-Type: application/json
-->
<!--#code
uint32_t id;
UString rnumber;
int i = 0, num_queries = UHTTP::getFormFirstNumericValue(1, 500);

(void) UClientImage_Base::wbuffer->reserve(36U * num_queries);

#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR('[');
#endif

while (true)
   {
   (void) es->sendPOST(U_CONSTANT_TO_PARAM("/tfb/world/_search"), buffer, QLEN+u__snprintf(buffer+QLEN, 128, U_CONSTANT_TO_PARAM("%u\"}}}"), id = u_get_num_random(10000-1)));

   rnumber.clear();

   (void) U_JFIND(es->getContent(), "randomNumber", rnumber);

#ifdef AS_cpoll_cppsp_DO
   USP_PRINTF("{\"id\":%u,\"randomNumber\":%v}", id, rnumber.rep);
#else
   World* pworld;

   U_NEW(World, pworld, World(id, rnumber.strtoul()));

   pvworld_query->push_back(pworld);
#endif

   if (++i == num_queries) break;

#ifdef AS_cpoll_cppsp_DO
   USP_PUTS_CHAR(',');
#endif

   rnumber.clear();
   }

#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR(']');
#else
USP_OBJ_JSON_stringify(*pvworld_query);
pvworld_query->clear();
#endif
-->
