Skip to content

Commit c8649b9

Browse files
committed
Add import method and test
1 parent 728b358 commit c8649b9

File tree

4 files changed

+94
-38
lines changed

4 files changed

+94
-38
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var hyperlog = require('hyperlog')
66
var sub = require('subleveldown')
77

88
var createImportPipeline = require('./lib/import.js')
9-
var importRowKv = require('./lib/importkv.js')
9+
var importRowsKv = require('./lib/importkv.js')
1010
var addRow = require('./lib/add.js')
1111
var deleteRow = require('./lib/delete.js')
1212

@@ -24,8 +24,8 @@ Jawn.prototype.createImportPipeline = function (opts) {
2424
return createImportPipeline(this, opts)
2525
}
2626

27-
Jawn.prototype.importRowKv = function (file) {
28-
return importRowKv(this, file)
27+
Jawn.prototype.importRowsKv = function (file, keys) {
28+
return importRowsKv(this, file, keys)
2929
}
3030

3131
Jawn.prototype.addRow = function (key, value) {

lib/importkv.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
4+
const MAX_KEY = 100000
5+
6+
module.exports = importRowsKv
7+
8+
function importRowsKv (jawn, file, keys) {
9+
var contents = fs.readFileSync(file).toString().split('\n')
10+
contents = contents.slice(0, contents.length - 1)
11+
console.log(contents)
12+
13+
var obj = []
14+
if (path.extname(file) === '.json') {
15+
for (var i = 0; i < contents.length; i++) {
16+
obj.push(contents[i])
17+
}
18+
}
19+
20+
for (i = 0; i < obj.length; i++) {
21+
var key
22+
if (i < keys.length) {
23+
key = keys[i]
24+
} else {
25+
key = Math.floor(Math.random() * MAX_KEY)
26+
}
27+
jawn.addRow(key, obj[i])
28+
}
29+
}

test/add.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/hyperkv.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var test = require('tape')
2+
var Jawn = require('../')
3+
var memdb = require('memdb')
4+
5+
var path = require('path')
6+
7+
test('import file to hyperkv', function (t) {
8+
var jawn = freshJawn()
9+
10+
var file = 'dummy.json'
11+
var expected = [
12+
'0: {"foo":"bar","name":"josie","age":"35"}',
13+
'1: {"foo":"baz","name":"eloise","age":"71"}',
14+
'2: {"foo":"baz","name":"francoise","age":"5"}'
15+
]
16+
17+
jawn.importRowsKv(fixture(file), [0, 1, 2])
18+
19+
jawn.kv.on('put', function (key, value, node) {
20+
t.equal(key + ': ' + value, expected.shift(), key + ' has been imported')
21+
22+
if (expected.length === 0) {
23+
t.end()
24+
}
25+
})
26+
})
27+
28+
test('add 3 rows to hyperkv', function (t) {
29+
var jawn = freshJawn()
30+
31+
var testValues = [
32+
'{"foo":"bar","name":"leslie","age":"46"}',
33+
'{"foo":"baz","name":"jim","age":"25"}',
34+
'{"foo":"baz","name":"pam","age":"17"}'
35+
]
36+
37+
var expected = [
38+
'3: {"foo":"bar","name":"leslie","age":"46"}',
39+
'4: {"foo":"baz","name":"jim","age":"25"}',
40+
'5: {"foo":"baz","name":"pam","age":"17"}'
41+
]
42+
43+
for (var i = 0; i < testValues.length; i++) {
44+
jawn.addRow(i + 3, testValues[i])
45+
}
46+
47+
jawn.kv.on('put', function (key, value, node) {
48+
t.equal(key + ': ' + value, expected.shift(), key + ' is the right value')
49+
50+
if (expected.length === 0) {
51+
t.end()
52+
}
53+
})
54+
})
55+
56+
function freshJawn () {
57+
return new Jawn({db: memdb()})
58+
}
59+
60+
function fixture (name) {
61+
return path.join(__dirname, 'data', name)
62+
}

0 commit comments

Comments
 (0)