-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
185 lines (180 loc) · 9.18 KB
/
test.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
175
176
177
178
179
180
181
182
183
184
185
const { KvStorage } = require('.')
const { statSync, writeFileSync, readFileSync, readdirSync } = require('node:fs')
const { join } = require('path')
test().catch(e => {
console.error(e)
process.exit(1)
})
function assert(truth, msg) {
if (!truth) {
console.log("FAILED " + msg)
process.exit(1)
}
console.log('OK', msg)
}
async function test() {
const bytes = []
for (let i = 0; i < 1000; i++) bytes.push(i % 256) // 1000 plus b64 encoding
const bufToOffload = Buffer.from(bytes)
for (let i = 0; i < 20_000 - bufToOffload.length; i++) bytes.push(i % 256)
const buf = Buffer.from(bytes)
const bigBuf = Buffer.from(bytes.concat(bytes, bytes, bytes, bytes, bytes))
const FN = 'test.db'
let db = new KvStorage()
const original = db.keyToFileName
db.keyToFileName = k => {
const x = original(k)
return join(x[0], x)
}
try {
await measure('all', async () => {
await measure('basics', async () => {
assert(!db.isOpening(), 'before isOpening')
db.open(FN, { clear: true })
assert(db.isOpening(), 'while isOpening')
await db.isOpening()
assert(db.isOpen(), "isOpen")
assert(db.size() === 0, "empty")
assert(db.firstKey() === undefined, "no firstKey")
db.put('k1', 'v1')
assert(await db.get('k1') === 'v1', "no-await")
db.put('k2', 2)
db.put('delete', 3)
db.del('delete')
db.put('b', bigBuf)
assert(db.has('b'), "has")
assert(!db.has('delete'), "has deleted")
assert(!db.has('never'), "has never")
assert(!db.firstKey({ startsWith: 'delete' }), "deleted firstKey")
await db.put('jb', { bigBuf })
db.put('k2', 22)
let n = 0
let valueFound = false
for await (const [k,v] of db.iterator({ startsWith: 'k' })) {
n++
valueFound ||= k === 'k2' && v === 22
}
assert(valueFound, "iterator value")
assert(n === 2, "iterator length")
const sub1 = db.sublevel('P1')
const date = new Date()
sub1.put('k under 1', 11)
sub1.put('k2 under 1', 2)
sub1.put('k2 under 1', date)
const sep = KvStorage.subSeparator
assert(await db.get(`P1${sep}k under 1`), "sub from above")
assert(await sub1.get('k under 1'), "sub from inside")
assert(sub1.size() === 2, "sub size")
assert(Array.from(sub1.keys()).length === 2, "sub length")
const sub2 = sub1.sublevel('P2')
sub2.put('k under 2', 21)
sub2.put('k2 under 2', 22)
assert(await db.get(`P1${sep}P2${sep}k under 2`), "sub2 from above")
assert(sub1.size() === 4, "sub father size")
assert(sub2.size() === 2, "sub child size")
assert(await sub2.get('k under 2') === 21, "sub get")
await db.put('off', bufToOffload)
assert(!db.getSync('off'), 'memory offloaded')
assert(bufToOffload.equals(await db.get('off')), 'get offloaded')
// reopen to check persistency
await db.close()
const expectedSize = 9
assert(db.size() === expectedSize, "bad size")
db = new KvStorage({ rewriteLater: true })
db.on('rewrite', () => {
console.log('rewriting to save', db.wouldSave.toLocaleString())
db.put('while-rewriting', 1)
const t = Date.now()
db.lockFlush.then(() => console.log('rewrite finished in', Date.now() - t, 'ms'))
})
db.on('rewriteBucket', () =>
console.log('rewriting bucket to save', db.bucketWouldSave.toLocaleString()))
assert(! readdirSync('.').filter(x => x.startsWith(FN + '-win-')).length, "rewrite leftovers") // these would accumulate in time (until process exit)
writeFileSync(FN, readFileSync(FN, 'utf8').replaceAll('\n', '\r\n')) // resist to editors messing with new lines
await db.open(FN)
assert(db.size() === expectedSize, "bad size after reload")
assert(await db.get('k1') === 'v1', "put+get")
assert(await db.get('k2') === 22, "numbers")
assert(await db.get('delete') === undefined, "delete")
assert(db.getSync(`P1${sep}k2 under 1`).getTime() === date.getTime(), "Date+getSync")
assert(db.map.get('b')?.file, "binary file")
assert(db.map.get('jb')?.file, "json file")
assert((await db.get('b'))?.toString('base64') === bigBuf.toString('base64'), "buffer")
assert((await db.get('jb'))?.bigBuf?.toString('base64') === bigBuf.toString('base64'), "json-buffer")
assert(!db.getSync('off'), 'offloaded after open')
assert(bufToOffload.equals(await db.get('off')), 'get offloaded after open')
db.del('off')
})
const lastOften = await new Promise(res => {
const K = 'often'
let wrote = 0
db.on('wrote', ({ key }) => (key === K) && ++wrote) // count
let insteadOf = 0
const h = setInterval(() => db.put(K, ++insteadOf, { delay: 200, maxDelay: 1000, maxDelayCreate: 0 }), 100)
setTimeout(() => {
clearInterval(h)
assert(wrote === 3, `often ${wrote}/${insteadOf}`)
db.flush().then(() => res(insteadOf))
}, 1800)
})
const MUL = 10000
const BN = MUL / 10
await measure('write', async () => {
// these should not be written because overwritten
for (let i = 1; i <= MUL; i++) db.put('o'+i, { prop: "first" + i })
for (let i = 1; i <= BN; i++) db.put('b'+i, buf)
db.put('jb64', { buf }) // this is supposed to end in bucket
db.put('b0', Buffer.from(bigBuf)) // this should write a separate binary file, because it's a Buffer
db.del('b0')
for (let i = 1; i <= MUL; i++) db.put('o'+i, { prop: "second" + i })
await db.flush()
// these should trigger rewrite
for (let i = 1; i <= MUL; i++) db.put('o'+i, { prop: "rewritten" + i })
db.put('z', 1)
})
await measure('flush', () => db.flush()) // first write previous ones
assert(!db.getSync('b1'), 'bucket offloaded')
assert(buf.equals(await db.get('b1')), 'get bucket')
assert(buf.equals(await db.get('b' + BN)), 'get other bucket')
await measure('put+rewrite', async () => { // these should trigger rewrite
// time here is both for put-s and the triggered rewrite
for (let i = 1; i <= MUL; i++) db.put('o' + i, { prop: "rewrittenAgain" + i })
await db.flush() // first write previous ones
})
await measure('rewrite-bucket', async () => { // these should trigger rewrite
for (let i = 1; i <= BN / 2; i++) db.del('b' + i)
//db.wouldSave = db.fileSize // trick the lib into causing rewrite of main file as bucket is rewritten
await db.flush()
})
await measure('close', () => db.close())
await measure('read', () => db.open(FN)) // just a benchmark
const content = readFileSync(FN, 'utf-8')
assert(content.includes(`{"k":"often","v":${lastOften}`), 'lastOften')
assert(!content.includes('"b0"'), 'b0')
// half buckets were deleted
assert(content.includes(`{"k":"jb64","bucket":[${BN/2 * buf.length},26705],"format":"json"}`), 'jb64')
assert(content.includes(`{"k":"k1","v":"v1"}`), 'k1')
assert(content.includes(`{"k":"b${BN}","bucket"`), 'bN')
const jbPath = JSON.stringify(join('j', 'jb'))
assert(content.includes(`{"k":"jb","file":${jbPath},"format":"json"}`), 'jb')
assert(content.includes(`{"k":"o1","v":{"prop":"rewrittenAgain1"}}`), 'o0')
assert(content.includes(`{"k":"o${MUL}","v":{"prop":"rewrittenAgain${MUL}"}}`), 'oMUL')
assert(content.includes('while-rewriting'), 'while-rewriting')
let extra = 0
for (const v of db.map.values())
extra += v.file?.match(/\\/g)?.length || 0 // this path separator uses 1 extra byte once encoded
const finalSize = statSync(FN).size
assert(finalSize === 541896 + extra, `final size ${finalSize}`)
console.log('final size: ', finalSize.toLocaleString())
})
}
finally {
//db.unlink()
}
}
async function measure(label, cb) {
console.log('START', label)
const t = Date.now()
await cb()
console.log('FINISHED', label, Date.now() - t, 'ms')
}