Skip to content

Instantly share code, notes, and snippets.

@jarrettmeyer
Last active August 29, 2015 14:20
Show Gist options
  • Save jarrettmeyer/0215eb274aca05d4cd8c to your computer and use it in GitHub Desktop.
Save jarrettmeyer/0215eb274aca05d4cd8c to your computer and use it in GitHub Desktop.
Cradle Bulk Insert Test
.idea/
node_modules/
*.stderr
*.stdout

Cradle Bulk Insert Test

This is just a test project for Cradle, verifying that bulk insert operations return the result in the same order as the original document order.

Prerequisites

  • CouchDB
  • NodeJS

Usage

$ node run

Options

  • --count: number of test documents to insert
  • --database: name of the database
  • --host: CouchDB host (e.g. http://localhost)
  • --port: CouchDB port (e.g. 5984)
{
"private": true,
"dependencies": {
"commander": "^2.8.1",
"cradle": "^0.6.9"
}
}
var assert = require('assert');
var cradle = require('cradle');
var program = require('commander');
program
.option('-c, --count [value]', 'Number of documents to bulk insert', int, 1000)
.option('-d, --database [value]', 'Name of database', 'test_bulk_insert')
.option('-h, --host [value]', 'CouchDB host', 'http://localhost')
.option('-p, --port [value]', 'CouchDB port', int, 5984)
.parse(process.argv);
console.log('Using database: %s', program.database);
console.log('Number of documents: %d', program.count);
var connection = new cradle.Connection({ host: program.host, port: program.port });
var database = connection.database(program.database);
var saveResult = [];
// Kick off the script!
start();
function createDatabase() {
console.log('Creating database %s.', database.name);
database.create(function (error, result) {
if (error || !result.ok) {
exitWithError(error);
}
console.log('Successfully created database: %s.', database.name);
insertDocuments();
});
}
function destroyDatabase() {
database.exists(function (error, result) {
if (error) {
exitWithError(error);
}
if (result) {
console.log('Database %s already exists. Deleting database.', database.name);
database.destroy(function (error, result) {
if (error || !result.ok) {
exitWithError(error);
}
console.log('Successfully deleted database %s.', database.name);
createDatabase();
});
} else {
createDatabase();
}
});
}
function exitWithError(error) {
console.error(error);
process.exit(-1);
}
function insertDocuments() {
var docs = [];
for (var i = 1; i <= program.count; i += 1) {
var doc = {
name: 'test',
value: i,
timestamp: new Date().toISOString()
};
docs.push(doc);
}
database.save(docs, function (error, result) {
if (error) {
exitWithError(error);
}
console.log('Successfully inserted %d documents.', result.length);
for (var i = 0; i < program.count; i += 1) {
docs[i]._id = result[i].id;
docs[i]._rev = result[i].rev;
}
saveResult = docs;
verifyResult();
});
}
function int(value) {
return parseInt(value, 10);
}
function start() {
destroyDatabase();
}
function verifyResult() {
var count = 0, errors = 0;
saveResult.forEach(function (doc) {
database.get(doc._id, function (error, result) {
if (error) {
exitWithError(error);
}
count += 1;
if (result.value === doc.value) {
console.log('+ id: %s, expected: %d, actual: %d', doc._id, doc.value, result.value);
} else {
console.error(' Expected document %s to have value %d, but found %d.', doc._id, doc.value, result.value);
errors += 1;
}
if (count === program.count) {
console.log('Done with %d documents.', count);
console.log('Number of errors: %d.', errors);
process.exit(0);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment