-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
172 lines (153 loc) · 4.1 KB
/
index.js
File metadata and controls
172 lines (153 loc) · 4.1 KB
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
169
170
171
172
var txdown = require('./leveldown.js')
var xtend = require('xtend')
var inherits = require('util').inherits
var LevelUP = require('levelup')
var lockCreator = require('./lock').creator
function isLevelUP (db) {
return db && (
db.toString() === 'LevelUP' ||
typeof db.sublevel === 'function'
)
}
function isFunction (val) {
return typeof val === 'function'
}
function noop () {}
function Transaction (db, options) {
if (!isLevelUP(db)) {
throw new Error('db must be a levelup instance')
}
if (!(this instanceof Transaction)) {
return new Transaction(db, options)
}
options = xtend({
ttl: 20 * 1000
}, db.options, options)
// get lock creator
if (!isFunction(options.createLock)) {
var levelup = isFunction(db.levelup) ? db.levelup() : db
// attach to levelup
options.createLock = levelup._createLock = levelup._createLock || lockCreator()
}
var location
// init txdown
if (db instanceof Transaction) {
// db is Transaction, get its levelup
this._levelup = db._levelup
options.db = txdown(db._levelup, options.createLock)
location = db.location
} else if (isFunction(db.sublevel) && isFunction(db.levelup)) {
// db is sublevelup, get its levelup
this._levelup = db.levelup()
options.db = txdown(db.levelup(), options.createLock)
location = db.location
} else {
// db is LevelUP, wrap txdown
this._levelup = db
options.db = txdown(db)
location = ''
}
// LevelUP.call(this, options.db(location), options)
LevelUP.call(this, location, options)
var self = this
this.once('closed', function () {
self.emit('end', self.db._error)
self.emit('release', self.db._error)
})
}
inherits(Transaction, LevelUP)
// override to bypass opening state and deferred
Transaction.prototype.open = function (cb) {
var self = this
function callback () {
if (isFunction(cb)) {
process.nextTick(function () { callback(null, self) })
}
return this
}
if (this.isOpen()) return callback()
this.db = this.options.db(this.location)
this.db._lock = this.options.createLock(this.options)
this._status = 'open'
this.emit('open')
this.emit('ready')
return callback()
}
// override to bypass deferred
Transaction.prototype.close = function (cb) {
var self = this
if (this.isOpen()) {
this._status = 'closing'
this.db.close(function () {
self._status = 'closed'
self.emit('closed')
if (cb) cb.apply(null, arguments)
})
this.emit('closing')
} else if (this._status === 'closed' && cb) {
return process.nextTick(cb)
} else if (this._status === 'closing' && cb) {
this.once('closed', cb)
} else if (this._isOpening()) {
this.once('open', function () {
self.close(cb)
})
}
}
Transaction.prototype._getOptions = function (opts) {
return xtend(
this.options,
opts && !isFunction(opts) ? opts : {}
)
}
Transaction.prototype._getCallback = function () {
var args = Array.prototype.slice.call(arguments)
for (var l = args.length, i = l - 1; i >= 0; i--) {
if (isFunction(args[i])) return args[i]
}
return noop
}
Transaction.prototype.commit = function (cb) {
cb = this._getCallback(cb)
var self = this
if (this.isClosed()) return cb(this.db._error)
var isCommitted = false
this.once('closed', function () {
if (!isCommitted) cb(self.db._error)
})
this.db._commit(function (err) {
if (self.isClosed()) return
isCommitted = true
if (err) {
self.rollback(err, function (errRB) {
cb(err)
})
} else {
self.close(cb)
}
})
return this
}
Transaction.prototype.rollback = function (err, cb) {
var self = this
cb = this._getCallback(err, cb)
this.db._rollback(err && !isFunction(err) ? err : null, function (err) {
self.close(function (errClose) {
cb(err || errClose || null)
})
})
return this
}
Transaction.prototype.lock = function (key, options, cb) {
this.db.lock(
key,
this._getOptions(options),
this._getCallback(options, cb)
)
return this
}
Transaction.prototype.defer = function (fn) {
this.db.defer(fn)
return this
}
module.exports = Transaction