-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread.js
130 lines (110 loc) · 3.1 KB
/
read.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var pull = require('pull-stream')
var compare = require('typewise').compare
var tape = require('tape')
const os = require('os')
const path = require('path')
module.exports = function (create, N) {
N = N || 10
var data = []
for(var i = 0; i < N; i++)
data.push({key: [~~(Math.random()*10), '#'+Math.random()], value: {foo: true, bar: Date.now(), i: i}})
var seed = Date.now()
var filename = path.join(os.tmpdir(), `test-flumeview-index-${seed}`)
var db = create(filename, seed)
var sorted = data.slice().sort(function (a, b) {
return compare(a.key, b.key)
})
tape('simple', function (t) {
db.append(data, function (err, m) {
if(err) throw err
t.end()
})
})
function all (opts, cb) {
pull(
db.index.read(opts),
pull.collect(cb)
)
}
function test (t) {
all({old: true, live: false}, function (err, ary) {
t.deepEqual(ary.map(function (e) { return {key:e.value.key, value:e.value.value}}), sorted)
all({keys: true, values: false}, function (err, _ary) {
t.deepEqual(_ary, ary.map(function (e) { return {key: e.key, seq: e.seq} }))
all({keys: false, values: true}, function (err, _ary) {
t.deepEqual(_ary, ary.map(function (e) { return {seq: e.seq, value: e.value} }))
all({values: true, seqs: false, keys: false}, function (err, _ary) {
t.deepEqual(_ary, ary.map(function (e) { return e.value }))
t.end()
})
})
})
})
}
tape('test', test)
tape('close', function (t) {
db.close(t.end)
})
tape('reload', function (t) {
db = create(filename, seed)
t.end()
})
tape('retest', test)
tape('empty close 1', function (t) {
var db = create(filename+2, seed)
pull(
db.index.read({}),
pull.collect(function (err, ary) {
//ignore err. might be an error or not,
//depending if read() was called before empty log loaded.
t.deepEqual(ary, [])
t.end()
})
)
setTimeout(function () {
db.close(function () {
})
})
})
tape('empty close 2', function (t) {
var db = create(filename+2, seed)
pull(
db.index.read({}),
pull.collect(function (_, ary) {
//ignore err. might be an error or not,
//depending if read() was called before empty log loaded.
t.deepEqual(ary, [])
t.end()
})
)
setTimeout(function () {
db.close(function () {})
})
})
tape('empty close 3', function (t) {
var db = create(filename+3, seed)
pull(
db.index.read({}),
pull.collect(function (err, ary) {
//should definitely be an error because db.close is called sync
t.ok(err)
t.deepEqual(ary, [])
t.end()
})
)
db.close(function () {})
})
tape('empty close 4', function (t) {
var db = create(filename+4, seed)
pull(
db.index.read({}),
pull.collect(function (err, ary) {
//should not be an error because db.close not called yet.
t.notOk(err)
t.deepEqual(ary, [])
t.end()
db.close(function () {})
})
)
})
}