Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSV to JSON Project Take 2 #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tmp
node_modules
data.jawn
npm-debug.log

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ node_js:
- "0.12"
- "0.11"
- "0.10"
- "iojs"
# - "iojs"
61 changes: 59 additions & 2 deletions index.js
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
}
8 changes: 8 additions & 0 deletions lib/add.js
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)
})
}
11 changes: 11 additions & 0 deletions lib/delete.js
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)
}
})
}
29 changes: 29 additions & 0 deletions lib/importkv.js
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])
}
}
77 changes: 77 additions & 0 deletions test/hyperkv.js
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)
}