From d57cf4d366647dd4fdf3d9fcaf74367b7f586389 Mon Sep 17 00:00:00 2001 From: Matt Zumwalt Date: Thu, 14 Apr 2016 14:03:04 -0400 Subject: [PATCH] Cleaner initialization of hypercore Change hyperkv back to hypercore temporarily Create framework for hyperkv Add add.js to lib Current work; move to cloud Make add.js in lib and create a test Add import method and test Finish work on delete case for hyperkv Change hyperkv to not use subleveldown when a db is provided Disabling iojs testing because of leveldown issues --- .gitignore | 2 ++ .travis.yml | 2 +- index.js | 61 +++++++++++++++++++++++++++++++++++++-- lib/add.js | 8 +++++ lib/delete.js | 11 +++++++ lib/importkv.js | 29 +++++++++++++++++++ test/hyperkv.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 lib/add.js create mode 100644 lib/delete.js create mode 100644 lib/importkv.js create mode 100644 test/hyperkv.js diff --git a/.gitignore b/.gitignore index d4b65b5..e4ec4f8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ tmp node_modules data.jawn +npm-debug.log + diff --git a/.travis.yml b/.travis.yml index 6160d82..368ecd1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,4 @@ node_js: - "0.12" - "0.11" - "0.10" - - "iojs" \ No newline at end of file +# - "iojs" diff --git a/index.js b/index.js index 0132dec..2ebce05 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,73 @@ var level = require('level') var hypercore = require('hypercore') + +var hyperkv = require('hyperkv') +var hyperlog = require('hyperlog') +var sub = require('subleveldown') + var createImportPipeline = require('./lib/import.js') +var importRowsKv = require('./lib/importkv.js') +var addRow = require('./lib/add.js') +var deleteRow = require('./lib/delete.js') module.exports = Jawn function Jawn (opts) { if (!opts) opts = {} - var db = opts.db || level('data.jawn') - this.core = opts.core || hypercore(db) + this.core = initializeHypercore(opts) + this.kv = initializeHyperkv(opts) this.db = this.core.db + this.db_kv = this.kv.db } Jawn.prototype.createImportPipeline = function (opts) { return createImportPipeline(this, opts) } + +Jawn.prototype.importRowsKv = function (file, keys) { + return importRowsKv(this, file, keys) +} + +Jawn.prototype.addRow = function (key, value) { + return addRow(this, key, value) +} + +Jawn.prototype.deleteRow = function (key) { + return deleteRow(this, key) +} + +// Initializes hypercore and its database +// @default Creates a leveldb database called `data.jawn` and initializes hypercore using that db +// @option 'core' the hypercore instance to use +// @option 'db' the db instace (leveldb) to initialize hypercore with. This is ignored if you use the `core` option +function initializeHypercore (opts) { + var core + if (opts.hasOwnProperty('core')) { + core = opts.core + } else { + var db = opts.db || level('data.jawn') + core = hypercore(db) + } + return core +} + +function initializeHyperkv (opts) { + var kv + if (opts.hasOwnProperty('core')) { + kv = opts.core + } else { + var db = opts.db || level('data.jawn') + if (!opts.db) { + kv = hyperkv({ + log: hyperlog(sub(db, 'log'), { valueEncoding: 'json' }), + db: sub(db, 'kv') + }) + } else { + kv = hyperkv({ + log: hyperlog(db, { valueEncoding: 'json' }), + db: db + }) + } + } + return kv +} diff --git a/lib/add.js b/lib/add.js new file mode 100644 index 0000000..1f619c1 --- /dev/null +++ b/lib/add.js @@ -0,0 +1,8 @@ +module.exports = addRow + +function addRow (jawn, key, value) { + jawn.kv.put(key, value, function (err, node) { + if (err) console.error(err) + else console.log(node.key + ': ' + node.value) + }) +} diff --git a/lib/delete.js b/lib/delete.js new file mode 100644 index 0000000..2d1ce12 --- /dev/null +++ b/lib/delete.js @@ -0,0 +1,11 @@ +module.exports = deleteRow + +function deleteRow (jawn, key) { + jawn.kv.del(key, function (err, node) { + if (err) console.log(err) + else { + console.log(node.key + ' has been deleted') + jawn.kv.emit('delete', node.key, node.value, node) + } + }) +} diff --git a/lib/importkv.js b/lib/importkv.js new file mode 100644 index 0000000..7455714 --- /dev/null +++ b/lib/importkv.js @@ -0,0 +1,29 @@ +var fs = require('fs') +var path = require('path') + +const MAX_KEY = 100000 + +module.exports = importRowsKv + +function importRowsKv (jawn, file, keys) { + var contents = fs.readFileSync(file).toString().split('\n') + contents = contents.slice(0, contents.length - 1) + console.log(contents) + + var obj = [] + if (path.extname(file) === '.json') { + for (var i = 0; i < contents.length; i++) { + obj.push(contents[i]) + } + } + + for (i = 0; i < obj.length; i++) { + var key + if (i < keys.length) { + key = keys[i] + } else { + key = Math.floor(Math.random() * MAX_KEY) + } + jawn.addRow(key, obj[i]) + } +} diff --git a/test/hyperkv.js b/test/hyperkv.js new file mode 100644 index 0000000..416a97a --- /dev/null +++ b/test/hyperkv.js @@ -0,0 +1,77 @@ +var test = require('tape') +var Jawn = require('../') +var memdb = require('memdb') + +var path = require('path') + +test('import file to hyperkv', function (t) { + var jawn = freshJawn() + + var file = 'dummy.json' + var expected = [ + '0: {"foo":"bar","name":"josie","age":"35"}', + '1: {"foo":"baz","name":"eloise","age":"71"}', + '2: {"foo":"baz","name":"francoise","age":"5"}' + ] + + jawn.importRowsKv(fixture(file), [0, 1, 2]) + + jawn.kv.on('put', function (key, value, node) { + t.equal(key + ': ' + value, expected.shift(), key + ' has been imported') + + if (expected.length === 0) { + t.end() + } + }) +}) + +var jawn = freshJawn() + +test('add 3 rows to hyperkv', function (t) { + var testValues = [ + '{"foo":"bar","name":"leslie","age":"46"}', + '{"foo":"baz","name":"jim","age":"25"}', + '{"foo":"baz","name":"pam","age":"17"}' + ] + + var expected = [ + '3: {"foo":"bar","name":"leslie","age":"46"}', + '4: {"foo":"baz","name":"jim","age":"25"}', + '5: {"foo":"baz","name":"pam","age":"17"}' + ] + + for (var i = 0; i < testValues.length; i++) { + jawn.addRow(i + 3, testValues[i]) + } + + jawn.kv.on('put', function (key, value, node) { + t.equal(key + ': ' + value, expected.shift(), key + ' is the right value') + + if (expected.length === 0) { + t.end() + } + }) +}) + +test('delete a row', function (t) { + jawn.deleteRow(3) + + jawn.kv.on('delete', function (key, value, node) { + jawn.kv.get(3, function (err, values) { + if (err) { + console.log(err) + } else { + t.same(values, {}, 'Row 3 has been successfully deleted') + } + t.end() + }) + }) +}) + +function freshJawn () { + return new Jawn({db: memdb()}) +} + +function fixture (name) { + return path.join(__dirname, 'data', name) +}