Skip to content

Instantly share code, notes, and snippets.

@akidee
Created June 29, 2012 08:47
Show Gist options
  • Save akidee/3016722 to your computer and use it in GitHub Desktop.
Save akidee/3016722 to your computer and use it in GitHub Desktop.
Strata B+tree example
async = require('asyncjs')
Strata = require('strata/src/lib/strata').Strata
tree = new Strata(
directory: __dirname + '/strata_data'
)
tree.create(_)
# Tree starts dancing
balance = (cb) ->
tree.balance((e, r) ->
if e
console.log e, e.rawStack
else
console.log 'balanced'
if cb then cb(e, r)
)
balance_interval_stop = no
(balance_interval = ->
balance((e, result) ->
if balance_interval_stop
return
timeout = if result
10
else
500
setTimeout(balance_interval, timeout)
)
)()
# number of records to insert + get
max = 10000
console.time('insert')
async.range(1, max).forEach(
(i, next) ->
process.nextTick(->
tree.mutator(
''+i
(e, c) ->
# console.log arguments
record = { i: i }
c.insert(record, ''+i, c.offset, (e) ->
c.unlock()
next()
)
)
)
).end(
on
_
)
console.timeEnd('insert')
console.time('get')
async.range(1, max).forEach(
(i, next) ->
process.nextTick(->
tree.iterator(
''+i
(e, c) ->
# console.log c
c.get(c.index, (e, r) ->
console.log(arguments) if !(i % 50)
c.unlock()
next()
)
)
)
).end(
on
_
)
console.timeEnd('get')
# Make sure that the tree is balanced
setTimeout(
_
1000
)
balance_interval_stop = on
tree.close(_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment