-
Notifications
You must be signed in to change notification settings - Fork 32
/
iterator.coffee
83 lines (59 loc) · 1.97 KB
/
iterator.coffee
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
DB = require('../build/default/leveldb.node').DB
uuid = require('node-uuid')
td = require("twisted-deferred")
start = Date.now()
entryCount = 1000
readCount = 0
defer = td.toDeferred
DeferredList = td.DeferredList
console.log("Creating test database")
path = "/tmp/iterator.db"
db = new DB()
d = defer(db.open.bind(db), path, {create_if_missing: true})
d.addCallback () ->
console.log('!! creating ' + entryCount + ' random key entries')
deferreds = []
for i in [0 .. entryCount]
id = uuid()
put_d = defer db.put.bind(db), id, JSON.stringify({id: id, name: 'Bob', age: 33})
deferreds.push(put_d)
return DeferredList(deferreds)
d.addCallback () ->
console.log('created in ' + (Date.now() - start) + 'ms')
console.log('!! iterating db in key order')
# reset the start counter
start = Date.now()
# iterate over the test database
iterator = db.newIterator({})
deferred = new td.Deferred()
iterator.seekToFirst () ->
while iterator.valid()
key = iterator.key().toString('utf8')
if lastKey && lastKey > key
console.log('found sorting error')
lastKey = key
readCount++
iterator.next()
console.log('read sequential ' + readCount + ' db contents in ' + (Date.now() - start) + 'ms')
deferred.callback()
deferred
d.addCallback () ->
console.log 'Start Seek test'
deferred = new td.Deferred()
iterator = db.newIterator({})
testUUID = uuid()
iterator.seek "" + testUUID, () ->
console.log('looking for first key after: ' + testUUID)
# if we found something the report
if (iterator.valid())
console.log('FOUND: ' + iterator.key().toString('utf-8'))
deferred.callback()
deferred
d.addCallback () ->
console.log "Success"
d.addErrback (err) ->
console.log err.message.stack
d.addBoth () ->
db.close()
DB.destroyDB(path, {})
console.log "Database removed and cleaned up."