-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
60 lines (45 loc) · 1.55 KB
/
index.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
var after = require('after')
, garbageCollection = require('./lib/util/gc')
, statistics = require('./lib/util/statistics')
, tree = require('./lib/util/property-tree')
, Index = require('./lib/index')
module.exports = function createIndex(db, props, opts) {
if (!db.indexes) initDatabase(db, { gc: opts && opts.gc })
var name = (props = [].concat(props)).join()
if (!db.indexes[name]) {
db.indexes[name] = new Index(db, name, props, opts)
tree.add(db.propertyTree, db.indexes[name])
}
return db.indexes[name]
}
module.exports.install = function(db) {
db.index = module.exports.bind(null, db)
}
function initDatabase(db, opts) {
// TODO: wrap db, set options there
db.indexes = Object.create(null)
db.propertyTree = tree.create()
var gc = db.gc = garbageCollection(db, opts && opts.gc)
statistics(db, db.indexes)
db.pre(function(op, add, ops){
if (op.type != 'del') {
var before = ops.length
for(var k in db.indexes)
db.indexes[k].update(op.key, op.value, add)
// Cancel scheduled gc ops, if any
var added = ops.length - before
gc.cancel(op.key, added > 0 && ops.slice(-added))
}
// A preAll() hook would be nice
gc.clear().forEach(add)
})
db.post(function(op){
if (op.type != 'del') return
var add = gc.start(op.key)
, done = after(Object.keys(db.indexes).length, function(){
gc.end(op.key)
db.emit('gc', op.key)
})
for(var k in db.indexes) db.indexes[k].deleted(op.key, add, done)
})
}