-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdeletes.js
155 lines (125 loc) · 3.29 KB
/
deletes.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const tape = require('tape')
const create = require('./helpers/create')
const messages = require('../lib/messages')
tape('basic delete', function (t) {
const db = create()
db.put('hello', 'world', function () {
db.get('hello', function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'world')
db.del('hello', function (err) {
t.error(err, 'no error')
db.get('hello', function (err, node) {
t.error(err, 'no error')
t.ok(!node, 'was deleted')
db.iterator().next(function (err, node) {
t.error(err, 'no error')
t.ok(!node, 'db is empty')
t.end()
})
})
})
})
})
})
tape('delete one in many', function (t) {
t.plan(1 + 2 + 2 + 2)
const db = create()
const batch = []
for (var i = 0; i < 50; i++) {
batch.push({key: '' + i, value: '' + i})
}
db.batch(batch, function () {
db.del('42', done)
})
function done (err) {
t.error(err, 'no error')
db.get('42', function (err, node) {
t.error(err, 'no error')
t.ok(!node, 'was deleted')
})
db.get('43', function (err, node) {
t.error(err, 'no error')
t.same(node.value, '43')
})
db.get('15', function (err, node) {
t.error(err, 'no error')
t.same(node.value, '15')
})
}
})
tape('delete one in many (iteration)', function (t) {
const db = create()
const batch = []
for (var i = 0; i < 50; i++) {
batch.push({key: '' + i, value: '' + i})
}
const keys = batch.map(b => b.key)
db.batch(batch, function () {
db.del('42', done)
})
function done (err) {
t.error(err, 'no error')
const ite = db.iterator()
const actual = []
ite.next(function loop (err, node) {
if (err) return t.error(err, 'no error')
if (!node) {
const expected = keys.slice(0, 42).concat(keys.slice(43))
t.same(actual.sort(), expected.sort(), 'all except deleted one')
t.end()
return
}
actual.push(node.value)
ite.next(loop)
})
}
})
tape('delete many in many (iteration)', function (t) {
const db = create()
const batch = []
for (var i = 0; i < 50; i++) {
batch.push({key: '' + i, value: '' + i})
}
const keys = batch.map(b => b.key)
db.batch(batch, function () {
const dels = batch.slice(0, 25).map(toDel)
db.batch(dels, done)
})
function done (err) {
t.error(err, 'no error')
const ite = db.iterator()
const actual = []
ite.next(function loop (err, node) {
if (err) return t.error(err, 'no error')
if (!node) {
const expected = keys.slice(25)
t.same(actual.sort(), expected.sort(), 'all except deleted ones')
t.end()
return
}
actual.push(node.value)
ite.next(loop)
})
}
})
tape('deletion with a single record, and a valueEncoding', function (t) {
const db = create(null, { valueEncoding: messages.Header })
db.put('hello', { type: 'some-type' }, function (err) {
t.error(err, 'no error')
db.del('hello', function (err) {
t.error(err, 'no error')
db.get('hello', function (err, val) {
t.error(err, 'no error')
t.same(val, null)
t.end()
})
})
})
})
function toDel (e) {
return {
type: 'del',
key: e.key
}
}