-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
7 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
tmp | ||
node_modules | ||
data.jawn | ||
npm-debug.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ node_js: | |
- "0.12" | ||
- "0.11" | ||
- "0.10" | ||
- "iojs" | ||
# - "iojs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |