-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
168 lines (149 loc) · 4.32 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
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
var ranges = require('string-range')
module.exports = function (db) {
if(db.hooks) {
return
}
var posthooks = []
var prehooks = []
function getPrefix (p) {
return p && (
'string' === typeof p ? p
: 'string' === typeof p.prefix ? p.prefix
: 'function' === typeof p.prefix ? p.prefix()
: ''
)
}
function getKeyEncoding (db) {
if(db && db._getKeyEncoding)
return db._getKeyEncoding(db)
}
function getValueEncoding (db) {
if(db && db._getValueEncoding)
return db._getValueEncoding(db)
}
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,
safe: false !== prefix.safe
}
prehooks.push(h)
return remover(prehooks, h)
},
posthooks: posthooks,
prehooks: prehooks
}
//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) {
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 || ''
)
//don't leave a circular json object there incase using multilevel.
if(prefix) ch.prefix = prefix
ch.key = prefix + ch.key
if(h.safe && h.test(String(ch.key))) {
//this usually means a stack overflow.
throw new Error('prehook cannot insert into own range')
}
var ke = ch.keyEncoding || getKeyEncoding(ch.prefix)
var ve = ch.valueEncoding || getValueEncoding(ch.prefix)
if(ke) ch.keyEncoding = ke
if(ve) ch.valueEncoding = ve
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, b)
}
})
})
} 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)
}
}