<!--#
Test type 5: Database updates
TechEmpower Web Framework Benchmarks
-->
<!--#declaration
#include "world.h"

static char* pbuffer1;
static char* pbuffer2;
static char buffer1[128];
static char buffer2[128];
static UElasticSearchClient* es;

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

#define ULEN U_CONSTANT_SIZE("/tfb/world/")
#define QLEN U_CONSTANT_SIZE("{\"doc\":{\"_id\":\"")

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

   U_NEW(UElasticSearchClient, es, UElasticSearchClient);

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

      delete es;
             es = U_NULLPTR;

      return;
      }

   U_MEMCPY(buffer1, "/tfb/world/", ULEN);

   pbuffer1 = buffer1 + ULEN;

   U_MEMCPY(buffer2, "{\"doc\":{\"_id\":\"", QLEN);

   pbuffer2 = buffer2 + QLEN;

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

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

   if (es)
      {
      delete es;

#  ifndef AS_cpoll_cppsp_DO
      delete pvworld_update;
#  endif
      }
}
#endif
-->
<!--#header
Content-Type: application/json
-->
<!--#code
uint32_t len1, len2, id, rnum;
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)
   {
   len1 = u__snprintf(pbuffer1, 100, U_CONSTANT_TO_PARAM("%u/_update"), id = u_get_num_random(10000-1));
   len2 = u__snprintf(pbuffer2, 100, U_CONSTANT_TO_PARAM("%u\"}}"),   rnum = u_get_num_random(10000-1));

   (void) es->sendPOST(buffer1, len1+ULEN, buffer2, len2+QLEN);

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

   U_NEW(World, pworld, World(id, rnum));

   pvworld_update->push_back(pworld);
#endif

   if (++i == num_queries) break;

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

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