-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcollisions.js
174 lines (152 loc) · 3.73 KB
/
collisions.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const tape = require('tape')
const create = require('./helpers/create')
tape('two keys with same siphash', function (t) {
t.plan(2 + 2)
const db = create()
db.put('idgcmnmna', 'a', function () {
db.put('mpomeiehc', 'b', function () {
db.get('idgcmnmna', function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'a')
})
db.get('mpomeiehc', function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'b')
})
})
})
})
tape('two keys with same siphash batch', function (t) {
t.plan(1 + 3 * 2)
const db = create()
db.batch([
{key: 'idgcmnmna', value: 'a'},
{key: 'mpomeiehc', value: 'b'},
{key: 'foo', value: 'bar'}
], function (err) {
t.error(err, 'no error')
db.get('idgcmnmna', same('a'))
db.get('mpomeiehc', same('b'))
db.get('foo', same('bar'))
})
function same (v) {
return function (err, node) {
t.error(err, 'no error')
t.same(node.value, v)
}
}
})
tape('two keys with same siphash (iterator)', function (t) {
const db = create()
db.put('idgcmnmna', 'a', function () {
db.put('mpomeiehc', 'b', function () {
const ite = db.iterator()
ite.next(function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'a')
})
ite.next(function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'b')
})
ite.next(function (err, node) {
t.error(err, 'no error')
t.same(node, null)
t.end()
})
})
})
})
tape('two prefixes with same siphash (iterator)', function (t) {
const db = create()
db.put('idgcmnmna/a', 'a', function () {
db.put('mpomeiehc/b', 'b', function () {
const ite = db.iterator('idgcmnmna')
ite.next(function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'a')
})
ite.next(function (err, node) {
t.error(err, 'no error')
t.same(node, null)
t.end()
})
})
})
})
tape('two prefixes with same key', function (t) {
const db = create()
db.put('idgcmnmna/a', 'a', function () {
db.put('mpomeiehc/a', 'a', function () {
const ite = db.iterator({recursive: false})
ite.next(function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'a')
})
ite.next(function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'a')
t.end()
})
})
})
})
tape('sorts based on key when colliding', function (t) {
const db1 = create()
const db2 = create()
db1.batch([
{key: 'idgcmnmna'},
{key: 'mpomeiehc'},
{key: 'a'},
{key: 'b'},
{key: 'c'}
], function () {
db2.batch([
{key: 'b'},
{key: 'mpomeiehc'},
{key: 'a'},
{key: 'idgcmnmna'},
{key: 'c'}
], function () {
const i1 = db1.iterator()
const i2 = db2.iterator()
i1.next(function loop (err, n1) {
t.error(err, 'no error')
i2.next(function (err, n2) {
t.error(err, 'no error')
if (!n1 && !n2) return t.end()
t.same(n1.key, n2.key)
i1.next(loop)
})
})
})
})
})
tape('two keys with same siphash (diff)', function (t) {
const db = create()
db.batch([
{key: 'idgcmnmna'},
{key: 'mpomeiehc'},
{key: 'a'},
{key: 'b'},
{key: 'c'}
], function () {
const ite = db.diff(0)
const found = {}
ite.next(function loop (err, node) {
t.error(err, 'no error')
if (!node) {
t.same(found, {
idgcmnmna: true,
mpomeiehc: true,
a: true,
b: true,
c: true
})
return t.end()
}
found[node.key] = true
ite.next(loop)
})
})
})