Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.get("/queries").handler(ctx => {
let failed = false;
let worlds = [];
const queries = util.getQueries(ctx.request());
for (let i = 0; i < queries; i++) {
client.preparedQuery(SELECT_WORLD, Tuple.of(util.randomWorld()), ar => {
if (!failed) {
if (ar.failed()) {
failed = true;
ctx.fail(ar.cause());
return;
}
// we need a final reference
const row = ar.result().iterator().next();
worlds.push({ id: row.getInteger(0), randomNumber: row.getInteger(1) });
// stop condition
if (worlds.length === queries) {
ctx.response()
.putHeader("Server", SERVER)
.putHeader("Date", date)
.end(JSON.stringify({ message: 'Hello, World!' }));
});
const UPDATE_WORLD = "UPDATE world SET randomnumber=$1 WHERE id=$2";
const SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
const SELECT_FORTUNE = "SELECT id, message from FORTUNE";
let connectOptions = new PgConnectOptions()
.setCachePreparedStatements(true)
.setHost('tfb-database')
.setUser('benchmarkdbuser')
.setPassword('benchmarkdbpass')
.setDatabase('hello_world');
// Pool options
let poolOptions = new PoolOptions()
.setMaxSize(1);
// Create the client pool
let client = PgPool.pool(vertx, connectOptions, poolOptions);
/*
* This test exercises the framework's object-relational mapper (ORM), random number generator, database driver,
* and database connection pool.
*/
app.get("/db").handler(ctx => {
client.preparedQuery(SELECT_WORLD, Tuple.of(util.randomWorld()), res => {
if (res.succeeded()) {
let resultSet = res.result().iterator();
if (!resultSet.hasNext()) {
ctx.fail(404);
worlds.forEach(world => {
batch.push(Tuple.of(world.randomNumber, world.id));
});
app.route("/updates").handler(ctx => {
let failed = false;
let queryCount = 0;
let worlds = [];
const queries = util.getQueries(ctx.request());
for (let i = 0; i < queries; i++) {
const id = util.randomWorld();
const index = i;
client.preparedQuery(SELECT_WORLD, Tuple.of(id), ar => {
if (!failed) {
if (ar.failed()) {
failed = true;
ctx.fail(ar.cause());
return;
}
const row = ar.result().iterator().next();
worlds[index] = { id: row.getInteger(0), randomNumber: row.getInteger(1) };
worlds[index].randomNumber = util.randomWorld();
if (++queryCount === queries) {
worlds.sort((a, b) => {
return a.id - b.id;
});