Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"quotes": [
"warn",
"double"
"single"
],
"semi": [
"error",
Expand Down
2 changes: 2 additions & 0 deletions examples/voter/voter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/package-lock.json
10 changes: 5 additions & 5 deletions examples/voter/voter/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
*/

var express = require('express'),
routes = require('./routes'), volt = require('./models/volt'),
votes = require('./jsons/votes'), util = require('util'),
cluster = require('cluster'), numCPUs = require('os').cpus().length;
routes = require('./routes'), volt = require('./models/volt'),
votes = require('./jsons/votes'), util = require('util'),
cluster = require('cluster'), numCPUs = require('os').cpus().length;

function webserverProcess() {
var app = module.exports = express.createServer();
Expand Down Expand Up @@ -68,7 +68,7 @@ function webserverProcess() {

app.listen(3000);

util.log(util.format("Express server listening on port %d in %s mode",
util.log(util.format('Express server listening on port %d in %s mode',
app.address().port, app.settings.env));
}

Expand All @@ -77,7 +77,7 @@ function startup() {
numCPUs /=2;
// TODO: Add command line to override whatever numCPUs is set to so we don't
// use all the cores.
util.log("Using CPUs: " + numCPUs);
util.log('Using CPUs: ' + numCPUs);
for(var i = 0; i < (numCPUs); i++) {
cluster.fork();
}
Expand Down
12 changes: 6 additions & 6 deletions examples/voter/voter/jsons/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/

var volt = require("../models/volt"),
VoltConstants = require(__dirname + '/../../../../lib/voltconstants');
var volt = require('../models/volt'),
VoltConstants = require(__dirname + '/../../../../lib/voltconstants');

exports.votes = function(req, res) {
return volt.getVoteResults(function displayResults(code, event, results) {
if(code == VoltConstants.STATUS_CODES.SUCCESS) {
return volt.getVoteResults().then(function displayResults({code, results}) {
if(code === VoltConstants.STATUS_CODES.SUCCESS && results.status === VoltConstants.RESULT_STATUS.SUCCESS) {
res.json({
'rows' : results.table[0]
'rows' : results.table[0].data.map( row => Object.assign(row, { TOTAL_VOTES : row.TOTAL_VOTES.toString()}) )
});
} else {
res.json({
'critical fault, database down': 500
});
}
});
}
};
97 changes: 49 additions & 48 deletions examples/voter/voter/models/volt.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
* 2. Creates a connection
* 3. Invokes stored procedures and processes the results.
*/
const debug = require('debug')('voltdb-client-nodejs:voter');

var util = require('util');
var cluster = require('cluster');
require('cluster');

// VoltClient manages all communication with VoltDB
var VoltClient = require(__dirname + '/../../../../lib/client');
Expand All @@ -54,7 +55,7 @@ var VoltProcedure = require(__dirname + '/../../../../lib/query');

// VoltQuery is a specific instance of a VoltProcedure. Your code will
// always call stored procedures using a VoltQuery object.
var VoltQuery = require(__dirname + '/../../../../lib/query');
//var VoltQuery = require(__dirname + "/../../../../lib/query");

// These are a set of stored procedure definitions.
// See VoltConstants to see the data types supported by the driver.
Expand All @@ -69,25 +70,25 @@ var transactionCounter = 0;
var statsLoggingInterval = 10000;

var area_codes = [907, 205, 256, 334, 251, 870, 501, 479, 480, 602, 623, 928,
520, 341, 764, 628, 831, 925, 909, 562, 661, 510, 650, 949, 760, 415, 951, 209,
669, 408, 559, 626, 442, 530, 916, 627, 714, 707, 310, 323, 213, 424, 747, 818,
858, 935, 619, 805, 369, 720, 303, 970, 719, 860, 203, 959, 475, 202, 302, 689,
407, 239, 850, 727, 321, 754, 954, 927, 352, 863, 386, 904, 561, 772, 786, 305,
941, 813, 478, 770, 470, 404, 762, 706, 678, 912, 229, 808, 515, 319, 563, 641,
712, 208, 217, 872, 312, 773, 464, 708, 224, 847, 779, 815, 618, 309, 331, 630,
317, 765, 574, 260, 219, 812, 913, 785, 316, 620, 606, 859, 502, 270, 504, 985,
225, 318, 337, 774, 508, 339, 781, 857, 617, 978, 351, 413, 443, 410, 301, 240,
207, 517, 810, 278, 679, 313, 586, 947, 248, 734, 269, 989, 906, 616, 231, 612,
320, 651, 763, 952, 218, 507, 636, 660, 975, 816, 573, 314, 557, 417, 769, 601,
662, 228, 406, 336, 252, 984, 919, 980, 910, 828, 704, 701, 402, 308, 603, 908,
848, 732, 551, 201, 862, 973, 609, 856, 575, 957, 505, 775, 702, 315, 518, 646,
347, 212, 718, 516, 917, 845, 631, 716, 585, 607, 914, 216, 330, 234, 567, 419,
440, 380, 740, 614, 283, 513, 937, 918, 580, 405, 503, 541, 971, 814, 717, 570,
878, 835, 484, 610, 267, 215, 724, 412, 401, 843, 864, 803, 605, 423, 865, 931,
615, 901, 731, 254, 325, 713, 940, 817, 430, 903, 806, 737, 512, 361, 210, 979,
936, 409, 972, 469, 214, 682, 832, 281, 830, 956, 432, 915, 435, 801, 385, 434,
804, 757, 703, 571, 276, 236, 540, 802, 509, 360, 564, 206, 425, 253, 715, 920,
262, 414, 608, 304, 307];
520, 341, 764, 628, 831, 925, 909, 562, 661, 510, 650, 949, 760, 415, 951, 209,
669, 408, 559, 626, 442, 530, 916, 627, 714, 707, 310, 323, 213, 424, 747, 818,
858, 935, 619, 805, 369, 720, 303, 970, 719, 860, 203, 959, 475, 202, 302, 689,
407, 239, 850, 727, 321, 754, 954, 927, 352, 863, 386, 904, 561, 772, 786, 305,
941, 813, 478, 770, 470, 404, 762, 706, 678, 912, 229, 808, 515, 319, 563, 641,
712, 208, 217, 872, 312, 773, 464, 708, 224, 847, 779, 815, 618, 309, 331, 630,
317, 765, 574, 260, 219, 812, 913, 785, 316, 620, 606, 859, 502, 270, 504, 985,
225, 318, 337, 774, 508, 339, 781, 857, 617, 978, 351, 413, 443, 410, 301, 240,
207, 517, 810, 278, 679, 313, 586, 947, 248, 734, 269, 989, 906, 616, 231, 612,
320, 651, 763, 952, 218, 507, 636, 660, 975, 816, 573, 314, 557, 417, 769, 601,
662, 228, 406, 336, 252, 984, 919, 980, 910, 828, 704, 701, 402, 308, 603, 908,
848, 732, 551, 201, 862, 973, 609, 856, 575, 957, 505, 775, 702, 315, 518, 646,
347, 212, 718, 516, 917, 845, 631, 716, 585, 607, 914, 216, 330, 234, 567, 419,
440, 380, 740, 614, 283, 513, 937, 918, 580, 405, 503, 541, 971, 814, 717, 570,
878, 835, 484, 610, 267, 215, 724, 412, 401, 843, 864, 803, 605, 423, 865, 931,
615, 901, 731, 254, 325, 713, 940, 817, 430, 903, 806, 737, 512, 361, 210, 979,
936, 409, 972, 469, 214, 682, 832, 281, 830, 956, 432, 915, 435, 801, 385, 434,
804, 757, 703, 571, 276, 236, 540, 802, 509, 360, 564, 206, 425, 253, 715, 920,
262, 414, 608, 304, 307];

var voteCandidates = 'Edwina Burnam,Tabatha Gehling,Kelly Clauss,' +
'Jessie Alloway,Alana Bregman,Jessie Eichman,Allie Rogalski,Nita Coster,' +
Expand Down Expand Up @@ -121,10 +122,12 @@ function voltInit() {
// VoltConstant source to see the possible values.
// The result object depends on the operation. Queries will always return a
// VoltTable array
client.callProcedure(query, function initVoter(code, event, results) {
var val = results.table[0][0];
client.callProcedure(query).read.then( function initVoter({ results }) {
if ( results.status !== VoltConstants.RESULT_STATUS.SUCCESS ) return debug( results.status.string );

const val = results.table[0].data[0];
util.log('Initialized app for ' + val[''] + ' candidates.');
});
}).catch( debug );
}


Expand All @@ -138,9 +141,11 @@ function eventListener(code, event, message) {
}

// This is a generic configuration object factory.
function getConfiguration(host) {
function getConfiguration(host,user,password) {
var cfg = new VoltConfiguration();
cfg.host = host;
cfg.username = user;
cfg.password = password;
// The messageQueueSize sets how many messages to buffer before dispatching
// them to the server. The messages are dispatched when either a timeout is
// reached or the queue fills up. It is best to keep this number
Expand All @@ -155,9 +160,9 @@ function getConfiguration(host) {
// Connect to the server
exports.initClient = function(startLoop) {
if(client == null) {
var configs = []
var configs = [];

configs.push(getConfiguration('localhost'));
configs.push(getConfiguration('localhost','operator','mech'));
// The client is only configured at this point. The connection
// is not made until the call to client.connect().
client = new VoltClient(configs);
Expand All @@ -178,8 +183,8 @@ exports.initClient = function(startLoop) {
// a success, though it is possible for one of the connections to the
// volt cluster to fail.
// The second handler is more for catastrophic failures.
client.connect(function startup(code, event,results) {
if(code == VoltConstants.STATUS_CODES.SUCCESS) {
client.connect().then(function startup({ connected, errors }) {
if( connected ) {
util.log('Node connected to VoltDB');
if(startLoop) {
setInterval(logResults, statsLoggingInterval);
Expand All @@ -188,14 +193,12 @@ exports.initClient = function(startLoop) {
voltInit();
}
} else {
util.log(`Unexpected status while initClient: ${VoltConstants.STATUS_CODE_STRINGS[code]}`);
util.log('Unexpected status while initClient:', errors.map( e => VoltConstants.LOGIN_ERRORS[e]) );
process.exit(1);
}
}, function loginError(code, event, results) {
util.log('Node did not connect to VoltDB');
});
}
}
};

// This method will vote several times and will run in the background.
function voteInsertLoop() {
Expand All @@ -218,34 +221,32 @@ function voteInsertLoop() {
// readyToWrite() callback gives you a way to interrupt looping type
// operations so that the socket.read events can be processed by the
// connection.
client.callProcedure(query,
function displayResults(code, event, results) {
transactionCounter++;
}, function readyToWrite(code, event, results) {

});
let call = client.callProcedure(query);
call.read.then(function displayResults() {
transactionCounter++;
}).catch( debug );
}
setImmediate(innerLoop);
}
setImmediate(innerLoop);
};
process.nextTick(innerLoop);

}

// This just displays how many votes we issued every 10 seconds, per node
// instance
function logResults() {
logTime("Voted", statsLoggingInterval, transactionCounter);
logTime('Voted', statsLoggingInterval, transactionCounter);
transactionCounter = 0;
}

function logTime(operation, totalTime, count) {
util.log(util.format('%d: %s %d times in %d milliseconds. %d TPS',
process.pid, operation, count, totalTime,
Math.floor((count / totalTime) * 1000)));
process.pid, operation, count, totalTime,
Math.floor((count / totalTime) * 1000)));
}

// Call the stored proc to collect all votes.
exports.getVoteResults = function(callback) {
var query = resultsProc.getQuery();
client.callProcedure(query, callback);
}
exports.getVoteResults = function() {
var query = resultsProc.getQuery();
return client.callProcedure(query).read;
};
16 changes: 8 additions & 8 deletions examples/voter/voter/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "Voterjs"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.5.8"
, "jade": ">= 0.0.1"
}
}
"name": "Voterjs",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "2.5.8",
"jade": ">= 0.0.1"
}
}
6 changes: 3 additions & 3 deletions examples/voter/voter/public/javascripts/jquery-1.7.1.min.js

Large diffs are not rendered by default.

Loading