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

static bson_t* query;  
static UMongoDBClient* mc;

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

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

   U_NEW(UMongoDBClient, mc, UMongoDBClient);

   if (mc->connect(U_NULLPTR, 0) == false)
      {
      U_WARNING("usp_fork_mupdate(): connection disabled or failed");

      delete mc;
             mc = U_NULLPTR;

      return;
      }

   if (mc->selectCollection("hello_world", "world") == false)
      {
      U_WARNING("usp_fork_mupdate(): selectCollection() failed");

      delete mc;
             mc = U_NULLPTR;

      return;
      }

   query = (bson_t*) U_SYSCALL_NO_PARAM(bson_new);  

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

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

   if (query)
      {
      delete mc;

      U_SYSCALL_VOID(bson_destroy, "%p", query);

#  ifndef AS_cpoll_cppsp_DO
      delete pvworld_update;
#  endif
      }
}
#endif
-->
<!--#header
Content-Type: application/json
-->
<!--#code
uint32_t id, rnum;
//mongoc_bulk_operation_t* bulk = mc->createBulk(false);
int i = 0, num_queries = UHTTP::getFormFirstNumericValue(1, 500);

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

#ifdef AS_cpoll_cppsp_DO
char* ptr = UClientImage_Base::wbuffer->pend();

*ptr++ = '[';
#endif

while (true)
   {
// mc->updateOneBulk(bulk, id = u_get_num_random(10000-1), "randomNumber", rnum = u_get_num_random(10000-1));

   if (mc->findOne(id = u_get_num_random(10000-1), query) &&
       mc->update( id, "randomNumber", rnum = u_get_num_random(10000-1)))
      {
#  ifdef AS_cpoll_cppsp_DO
      u_put_unalignedp32(ptr,   U_MULTICHAR_CONSTANT32('{','"','i','d'));
      u_put_unalignedp16(ptr+4, U_MULTICHAR_CONSTANT16('"',':'));

      ptr = u_num2str32(id, ptr+6);

      u_put_unalignedp64(ptr,   U_MULTICHAR_CONSTANT64(',','"','r','a','n','d','o','m'));
      u_put_unalignedp64(ptr+8, U_MULTICHAR_CONSTANT64('N','u','m','b','e','r','"',':'));

      ptr = u_num2str32(rnum, ptr+16);

      *ptr++ = '}';
#  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
   *ptr++ = ',';
#endif
   }

// (void) mc->executeBulk(bulk);

#ifdef AS_cpoll_cppsp_DO
*ptr++ = ']';

UClientImage_Base::wbuffer->size_adjust(ptr);
#else
USP_OBJ_JSON_stringify(*pvworld_update);
pvworld_update->clear();
#endif
-->
