Breaking: upgrade to abstract-level 2 · Level/memory-level@77dac64

@@ -62,19 +62,19 @@ class MemoryIterator extends AbstractIterator {

6262

this[kInit](db[kTree], options)

6363

}

646465-

_next (callback) {

66-

if (!this[kIterator].valid) return this.nextTick(callback)

65+

async _next () {

66+

if (!this[kIterator].valid) return undefined

67676868

const key = this[kIterator].key

6969

const value = this[kIterator].value

707071-

if (!this[kTest](key)) return this.nextTick(callback)

71+

if (!this[kTest](key)) return undefined

72727373

this[kIterator][this[kAdvance]]()

74-

this.nextTick(callback, null, key, value)

74+

return [key, value]

7575

}

767677-

_nextv (size, options, callback) {

77+

async _nextv (size, options) {

7878

const it = this[kIterator]

7979

const entries = []

8080

@@ -83,10 +83,10 @@ class MemoryIterator extends AbstractIterator {

8383

it[this[kAdvance]]()

8484

}

858586-

this.nextTick(callback, null, entries)

86+

return entries

8787

}

888889-

_all (options, callback) {

89+

async _all (options) {

9090

const size = this.limit - this.count

9191

const it = this[kIterator]

9292

const entries = []

@@ -96,7 +96,7 @@ class MemoryIterator extends AbstractIterator {

9696

it[this[kAdvance]]()

9797

}

989899-

this.nextTick(callback, null, entries)

99+

return entries

100100

}

101101

}

102102

@@ -106,17 +106,17 @@ class MemoryKeyIterator extends AbstractKeyIterator {

106106

this[kInit](db[kTree], options)

107107

}

108108109-

_next (callback) {

110-

if (!this[kIterator].valid) return this.nextTick(callback)

109+

async _next () {

110+

if (!this[kIterator].valid) return undefined

111111112112

const key = this[kIterator].key

113-

if (!this[kTest](key)) return this.nextTick(callback)

113+

if (!this[kTest](key)) return undefined

114114115115

this[kIterator][this[kAdvance]]()

116-

this.nextTick(callback, null, key)

116+

return key

117117

}

118118119-

_nextv (size, options, callback) {

119+

async _nextv (size, options) {

120120

const it = this[kIterator]

121121

const keys = []

122122

@@ -125,10 +125,10 @@ class MemoryKeyIterator extends AbstractKeyIterator {

125125

it[this[kAdvance]]()

126126

}

127127128-

this.nextTick(callback, null, keys)

128+

return keys

129129

}

130130131-

_all (options, callback) {

131+

async _all (options) {

132132

const size = this.limit - this.count

133133

const it = this[kIterator]

134134

const keys = []

@@ -138,7 +138,7 @@ class MemoryKeyIterator extends AbstractKeyIterator {

138138

it[this[kAdvance]]()

139139

}

140140141-

this.nextTick(callback, null, keys)

141+

return keys

142142

}

143143

}

144144

@@ -148,19 +148,19 @@ class MemoryValueIterator extends AbstractValueIterator {

148148

this[kInit](db[kTree], options)

149149

}

150150151-

_next (callback) {

152-

if (!this[kIterator].valid) return this.nextTick(callback)

151+

async _next (options) {

152+

if (!this[kIterator].valid) return undefined

153153154154

const key = this[kIterator].key

155155

const value = this[kIterator].value

156156157-

if (!this[kTest](key)) return this.nextTick(callback)

157+

if (!this[kTest](key)) return undefined

158158159159

this[kIterator][this[kAdvance]]()

160-

this.nextTick(callback, null, value)

160+

return value

161161

}

162162163-

_nextv (size, options, callback) {

163+

async _nextv (size, options) {

164164

const it = this[kIterator]

165165

const values = []

166166

@@ -169,10 +169,10 @@ class MemoryValueIterator extends AbstractValueIterator {

169169

it[this[kAdvance]]()

170170

}

171171172-

this.nextTick(callback, null, values)

172+

return values

173173

}

174174175-

_all (options, callback) {

175+

async _all (options) {

176176

const size = this.limit - this.count

177177

const it = this[kIterator]

178178

const values = []

@@ -182,7 +182,7 @@ class MemoryValueIterator extends AbstractValueIterator {

182182

it[this[kAdvance]]()

183183

}

184184185-

this.nextTick(callback, null, values)

185+

return values

186186

}

187187

}

188188

@@ -270,6 +270,7 @@ class MemoryLevel extends AbstractLevel {

270270

}

271271272272

// To help migrating from level-mem to abstract-level

273+

// TODO (v2): remove

273274

if (typeof location === 'function' || typeof options === 'function' || typeof _ === 'function') {

274275

throw new ModuleError('The levelup-style callback argument has been removed', {

275276

code: 'LEVEL_LEGACY'

@@ -291,45 +292,40 @@ class MemoryLevel extends AbstractLevel {

291292

permanence: false,

292293

createIfMissing: false,

293294

errorIfExists: false,

294-

encodings: { [storeEncoding]: true }

295+

encodings: { [storeEncoding]: true },

296+

signals: {

297+

// Would have no value here because the operations are synchronous

298+

iterators: false

299+

}

295300

}, forward)

296301297302

this[kTree] = createRBT(compare)

298303

}

299304300-

_put (key, value, options, callback) {

305+

async _put (key, value, options) {

301306

const it = this[kTree].find(key)

302307303308

if (it.valid) {

304309

this[kTree] = it.update(value)

305310

} else {

306311

this[kTree] = this[kTree].insert(key, value)

307312

}

308-309-

this.nextTick(callback)

310313

}

311314312-

_get (key, options, callback) {

313-

const value = this[kTree].get(key)

314-315-

if (typeof value === 'undefined') {

316-

// TODO: use error code (not urgent, abstract-level normalizes this)

317-

return this.nextTick(callback, new Error('NotFound'))

318-

}

319-320-

this.nextTick(callback, null, value)

315+

async _get (key, options) {

316+

// Is undefined if not found

317+

return this[kTree].get(key)

321318

}

322319323-

_getMany (keys, options, callback) {

324-

this.nextTick(callback, null, keys.map(key => this[kTree].get(key)))

320+

async _getMany (keys, options) {

321+

return keys.map(key => this[kTree].get(key))

325322

}

326323327-

_del (key, options, callback) {

324+

async _del (key, options) {

328325

this[kTree] = this[kTree].remove(key)

329-

this.nextTick(callback)

330326

}

331327332-

_batch (operations, options, callback) {

328+

async _batch (operations, options) {

333329

let tree = this[kTree]

334330335331

for (const op of operations) {

@@ -344,38 +340,35 @@ class MemoryLevel extends AbstractLevel {

344340

}

345341346342

this[kTree] = tree

347-

this.nextTick(callback)

348343

}

349344350-

_clear (options, callback) {

345+

async _clear (options) {

351346

if (options.limit === -1 && !Object.keys(options).some(isRangeOption)) {

352347

// Delete everything by creating a new empty tree.

353348

this[kTree] = createRBT(compare)

354-

return this.nextTick(callback)

349+

return

355350

}

356351357352

const iterator = this._keys({ ...options })

358353

const limit = iterator.limit

359354360355

let count = 0

361356362-

const loop = () => {

357+

while (true) {

363358

// TODO: add option to control "batch size"

364359

for (let i = 0; i < 500; i++) {

365-

if (++count > limit) return callback()

366-

if (!iterator[kIterator].valid) return callback()

367-

if (!iterator[kTest](iterator[kIterator].key)) return callback()

360+

if (++count > limit) return

361+

if (!iterator[kIterator].valid) return

362+

if (!iterator[kTest](iterator[kIterator].key)) return

368363369364

// Must also include changes made in parallel to clear()

370365

this[kTree] = this[kTree].remove(iterator[kIterator].key)

371366

iterator[kIterator][iterator[kAdvance]]()

372367

}

373368374369

// Some time to breathe

375-

this.nextTick(loop)

370+

await breathe()

376371

}

377-378-

this.nextTick(loop)

379372

}

380373381374

_iterator (options) {

@@ -393,18 +386,17 @@ class MemoryLevel extends AbstractLevel {

393386394387

exports.MemoryLevel = MemoryLevel

395388396-

// Use setImmediate() in Node.js to allow IO in between our callbacks

389+

let breathe

390+391+

// Use setImmediate() in Node.js to allow IO in between work

397392

if (typeof process !== 'undefined' && !process.browser && typeof global !== 'undefined' && typeof global.setImmediate === 'function') {

398393

const setImmediate = global.setImmediate

399394400-

// Automatically applies to iterators, sublevels and chained batches as well

401-

MemoryLevel.prototype.nextTick = function (fn, ...args) {

402-

if (args.length === 0) {

403-

setImmediate(fn)

404-

} else {

405-

setImmediate(() => fn(...args))

406-

}

395+

breathe = function () {

396+

return new Promise(setImmediate)

407397

}

398+

} else {

399+

breathe = async function () {}

408400

}

409401410402

function isRangeOption (k) {