Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (176 loc) · 5.07 KB
/
Copy pathindex.js
File metadata and controls
198 lines (176 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
8000
2
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
186
187
188
189
190
191
192
193
194
195
196
197
198
var ranges = require('string-range')
var locker = require('lock')
module.exports = function (db) {
var lock = locker()
if(db.hooks) {
return
}
var posthooks = []
var prehooks = []
var asynchooks = []
function getPrefix (p) {
return p && (
'string' === typeof p ? p
: 'string' === typeof p.prefix ? p.prefix
: 'function' === typeof p.prefix ? p.prefix()
: ''
)
}
function remover (array, item) {
return function () {
var i = array.indexOf(item)
if(!~i) return false
array.splice(i, 1)
return true
}
}
db.hooks = {
post: function (prefix, hook) {
if(!hook) hook = prefix, prefix = ''
var h = {test: ranges.checker(prefix), hook: hook}
posthooks.push(h)
return remover(posthooks, h)
},
pre: function (prefix, hook) {
if(!hook) hook = prefix, prefix = ''
var h = {test: ranges.checker(prefix), hook: hook}
prehooks.push(h)
return remover(prehooks, h)
},
async: function (prefix, hook) {
if(!hook) hook = prefix, prefix = ''
var h = {test: ranges.checker(prefix), hook: hook, async: true}
asynchooks.push(h)
return remover(asynchooks, h)
},
posthooks : posthooks,
prehooks : prehooks,
asynchooks: asynchooks
}
//POST HOOKS
function each (e) {
if(e && e.type) {
posthooks.forEach(function (h) {
if(h.test(e.key)) h.hook(e)
})
}
}
db.on('put', function (key, val) {
each({type: 'put', key: key, value: val})
})
db.on('del', function (key, val) {
each({type: 'del', key: key, value: val})
})
db.on('batch', function onBatch (ary) {
ary.forEach(each)
})
//PRE HOOKS
var put = db.put
var del = db.del
var batch = db.batch
function callHooks (isBatch, b, opts, cb) {
if(!cb)
cb = opts, opts = {}
//ASYNC HOOKS
var toHook = [], toLock = [], n = 0, locked = false
//skip this if there are no async hooks.
if(!asynchooks.length)
return n=1, sync()
b.forEach(function (e) {
asynchooks.forEach(function (h) {
if(h.test(String(e.key))) {
locked = true
// should I lock the whole batch?
// or just the keys that have asynchooks?
// just locking the hooked keys for now...
toLock.push(e.key)
toHook.push(function (cb) {
h.hook(e, cb)
})
n++
}
})
})
if(toLock.length)
lock(toLock, function (release) {
//release the lock when the callback is called
//after the batch is processed!
cb = release(cb)
toHook.forEach(function (f) { f(sync) })
})
else
n=1, sync()
//SYNC HOOKS
function sync () {
if(--n) return
try {
b.forEach(function hook(e, i) {
prehooks.forEach(function (h) {
if(h.test(String(e.key))) {
//optimize this?
//maybe faster to not create a new object each time?
//have one object and expose scope to it?
var context = {
add: function (ch, db) {
if(typeof ch === 'undefined') {
return this
}
if(ch === false)
return delete b[i]
var prefix = (
getPrefix(ch.prefix) ||
getPrefix(db) ||
h.prefix || ''
)
ch.key = prefix + ch.key
if(h.test(String(ch.key))) {
//this usually means a stack overflow.
throw new Error('prehook cannot insert into own range')
}
b.push(ch)
hook(ch, b.length - 1)
return this
},
put: function (ch, db) {
if('object' === typeof ch) ch.type = 'put'
return this.add(ch, db)
},
del: function (ch, db) {
if('object' === typeof ch) ch.type = 'del'
return this.add(ch, db)
},
veto: function () {
return this.add(false)
}
}
h.hook.call(context, e, context.add)
}
})
})
} catch (err) {
return (cb || opts)(err)
}
b = b.filter(function (e) {
return e && e.type //filter out empty items
})
if(b.length == 1 && !isBatch) {
var change = b[0]
return change.type == 'put'
? put.call(db, change.key, change.value, opts, cb)
: del.call(db, change.key, opts, cb)
}
return batch.call(db, b, opts, cb)
}
}
db.put = function (key, value, opts, cb ) {
var batch = [{key: key, value: value, type: 'put'}]
return callHooks(false, batch, opts, cb)
}
db.del = function (key, opts, cb) {
var batch = [{key: key, type: 'del'}]
return callHooks(false, batch, opts, cb)
}
db.batch = function (batch, opts, cb) {
return callHooks(true, batch, opts, cb)
}
}