1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-10-05 19:18:01 +08:00
ULib/src/ulib/net/server/plugin/usp/mquery.usp
stefanocasazza 58edeb9b26 sync
2016-09-20 15:40:52 +02:00

103 lines
1.8 KiB
Plaintext

<!--#
Test type 3: Multiple database queries
TechEmpower Web Framework Benchmarks
-->
<!--#declaration
#include "world.h"
static UMongoDBClient* mc;
#ifndef AS_cpoll_cppsp_DO
static UValue* pvalue;
static UVector<World*>* pvworld_query;
#endif
static void usp_fork_mquery()
{
U_TRACE(5, "::usp_fork_mquery()")
U_NEW(UMongoDBClient, mc, UMongoDBClient);
if (mc->connect(0,0) == false)
{
U_WARNING("usp_fork_mquery(): connection disabled or failed");
return;
}
if (mc->selectCollection("hello_world", "World") == false)
{
U_WARNING("usp_fork_mquery(): selectCollection() failed");
return;
}
#ifndef AS_cpoll_cppsp_DO
U_NEW(UValue, pvalue, UValue);
U_NEW(UVector<World*>, pvworld_query, UVector<World*>(500));
#endif
}
#ifdef DEBUG
static void usp_end_mquery()
{
U_TRACE(5, "::usp_end_mquery()")
delete mc;
#ifndef AS_cpoll_cppsp_DO
if (pvalue)
{
delete pvalue;
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);
#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR('[');
#endif
while (true)
{
(void) mc->findOne(id = u_get_num_random(10000-1));
rnumber.clear();
(void) U_JFIND(mc->vitem[0], "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
}
#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR(']');
#else
USP_JSON_stringify(*pvalue, UVector<World*>, *pvworld_query);
pvalue->clear();
pvworld_query->clear();
#endif
-->