Skip to content

Commit 4990168

Browse files
committed
Breaking: remove deprecated put, del & batch events
In favor of the `write` event. While still printing a warning if listeners are added for these events, because it's cheap and helps people who are upgrading. Category: removal
1 parent 9eeb291 commit 4990168

File tree

8 files changed

+16
-168
lines changed

8 files changed

+16
-168
lines changed

README.md

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,6 @@ Lastly, one way or another, every implementation _must_ support `data` of type S
848848

849849
An `abstract-level` database is an [`EventEmitter`](https://nodejs.org/api/events.html) and emits the events listed below.
850850

851-
The `put`, `del` and `batch` events are deprecated in favor of the `write` event and will be removed in a future version of `abstract-level`. If one or more `write` event listeners exist or if the [`prewrite`](#hook--dbhooksprewrite) hook is in use, either of which implies opting-in to the `write` event, then the deprecated events will not be emitted.
852-
853851
#### `opening`
854852

855853
Emitted when database is opening. Receives 0 arguments:
@@ -967,42 +965,6 @@ The same is true for `db.put()` and `db.del()`.
967965

968966
Emitted when a `db.clear()` call completed and entries were thus successfully deleted from the database. Receives a single `options` argument, which is the verbatim `options` argument that was passed to `db.clear(options)` (or an empty object if none) before having encoded range options.
969967

970-
#### `put` (deprecated)
971-
972-
Emitted when a `db.put()` call completed and an entry was thus successfully written to the database. Receives `key` and `value` arguments, which are the verbatim `key` and `value` that were passed to `db.put(key, value)` before having encoded them.
973-
974-
```js
975-
db.on('put', function (key, value) {
976-
console.log('Wrote', key, value)
977-
})
978-
```
979-
980-
#### `del` (deprecated)
981-
982-
Emitted when a `db.del()` call completed and an entry was thus successfully deleted from the database. Receives a single `key` argument, which is the verbatim `key` that was passed to `db.del(key)` before having encoded it.
983-
984-
```js
985-
db.on('del', function (key) {
986-
console.log('Deleted', key)
987-
})
988-
```
989-
990-
#### `batch` (deprecated)
991-
992-
Emitted when a `db.batch([])` or chained `db.batch().write()` call completed and the data was thus successfully written to the database. Receives a single `operations` argument, which is the verbatim `operations` array that was passed to `db.batch(operations)` before having encoded it, or the equivalent for a chained `db.batch().write()`.
993-
994-
```js
995-
db.on('batch', function (operations) {
996-
for (const op of operations) {
997-
if (op.type === 'put') {
998-
console.log('Wrote', op.key, op.value)
999-
} else {
1000-
console.log('Deleted', op.key)
1001-
}
1002-
}
1003-
})
1004-
```
1005-
1006968
### Order Of Operations
1007969

1008970
There is no defined order between parallel write operations. Consider:

abstract-chained-batch.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class AbstractChainedBatch {
1414
#length = 0
1515
#closePromise = null
1616
#publicOperations
17-
#legacyOperations
1817
#prewriteRun
1918
#prewriteBatch
2019
#prewriteData
@@ -33,11 +32,6 @@ class AbstractChainedBatch {
3332
// operations, which is the expensive part) if there are 0 write event listeners.
3433
this.#publicOperations = enableWriteEvent ? [] : null
3534

36-
// Operations for legacy batch event. If user opted-in to write event or prewrite
37-
// hook, skip legacy batch event. We can't skip the batch event based on listener
38-
// count, because a listener may be added between put() or del() and write().
39-
this.#legacyOperations = enableWriteEvent || enablePrewriteHook ? null : []
40-
4135
this.#addMode = getOptions(options, emptyOptions).add === true
4236

4337
if (enablePrewriteHook) {
@@ -148,14 +142,6 @@ class AbstractChainedBatch {
148142
}
149143

150144
this.#publicOperations.push(publicOperation)
151-
} else if (this.#legacyOperations !== null && !siblings) {
152-
const legacyOperation = Object.assign({}, original)
153-
154-
legacyOperation.type = 'put'
155-
legacyOperation.key = key
156-
legacyOperation.value = value
157-
158-
this.#legacyOperations.push(legacyOperation)
159145
}
160146

161147
// If we're forwarding the sublevel option then don't prefix the key yet
@@ -231,13 +217,6 @@ class AbstractChainedBatch {
231217
}
232218

233219
this.#publicOperations.push(publicOperation)
234-
} else if (this.#legacyOperations !== null) {
235-
const legacyOperation = Object.assign({}, original)
236-
237-
legacyOperation.type = 'del'
238-
legacyOperation.key = key
239-
240-
this.#legacyOperations.push(legacyOperation)
241220
}
242221

243222
op.key = this.db.prefixKey(encodedKey, keyFormat, true)
@@ -264,7 +243,6 @@ class AbstractChainedBatch {
264243
this._clear()
265244

266245
if (this.#publicOperations !== null) this.#publicOperations = []
267-
if (this.#legacyOperations !== null) this.#legacyOperations = []
268246
if (this.#prewriteData !== null) this.#prewriteData.clear()
269247

270248
this.#length = 0
@@ -333,8 +311,6 @@ class AbstractChainedBatch {
333311
// db close which in turn triggers (idempotently) closing this batch.
334312
if (this.#publicOperations !== null) {
335313
this.db.emit('write', this.#publicOperations)
336-
} else if (this.#legacyOperations !== null) {
337-
this.db.emit('batch', this.#legacyOperations)
338314
}
339315

340316
return this.#closePromise

abstract-level.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,7 @@ class AbstractLevel extends EventEmitter {
7171
})
7272
})
7373

74-
// Monitor event listeners
75-
this.#eventMonitor = new EventMonitor(this, [
76-
{ name: 'write' },
77-
{ name: 'put', deprecated: true, alt: 'write' },
78-
{ name: 'del', deprecated: true, alt: 'write' },
79-
{ name: 'batch', deprecated: true, alt: 'write' }
80-
])
81-
74+
this.#eventMonitor = new EventMonitor(this, [{ name: 'write' }])
8275
this.#transcoder = new Transcoder(formats(this))
8376
this.#keyEncoding = this.#transcoder.encoding(keyEncoding || 'utf8')
8477
this.#valueEncoding = this.#transcoder.encoding(valueEncoding || 'utf8')
@@ -591,9 +584,6 @@ class AbstractLevel extends EventEmitter {
591584
})
592585

593586
this.emit('write', [op])
594-
} else {
595-
// TODO (semver-major): remove
596-
this.emit('put', key, value)
597587
}
598588
}
599589

@@ -643,9 +633,6 @@ class AbstractLevel extends EventEmitter {
643633
})
644634

645635
this.emit('write', [op])
646-
} else {
647-
// TODO (semver-major): remove
648-
this.emit('del', key)
649636
}
650637
}
651638

@@ -799,9 +786,6 @@ class AbstractLevel extends EventEmitter {
799786

800787
if (enableWriteEvent) {
801788
this.emit('write', publicOperations)
802-
} else if (!enablePrewriteHook) {
803-
// TODO (semver-major): remove
804-
this.emit('batch', operations)
805789
}
806790
}
807791

lib/event-monitor.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ exports.EventMonitor = class EventMonitor {
77
for (const event of events) {
88
// Track whether listeners are present
99
this[event.name] = false
10-
11-
// Prepare deprecation message
12-
if (event.deprecated) {
13-
event.message = `The '${event.name}' event is deprecated in favor of '${event.alt}' and will be removed in a future version of abstract-level`
14-
}
1510
}
1611

1712
const map = new Map(events.map(e => [e.name, e]))
@@ -26,8 +21,8 @@ exports.EventMonitor = class EventMonitor {
2621
if (event !== undefined) {
2722
monitor[name] = true
2823

29-
if (event.deprecated) {
30-
deprecate(event.message)
24+
if (name === 'put' || name === 'del' || name === 'batch') {
25+
deprecate(`The '${name}' event has been removed in favor of 'write'`)
3126
}
3227
}
3328
}

test/batch-test.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -199,24 +199,6 @@ exports.atomic = function (test, testCommon) {
199199
})
200200
}
201201

202-
exports.events = function (test, testCommon) {
203-
test('batch([]) emits batch event', async function (t) {
204-
t.plan(2)
205-
206-
const db = testCommon.factory()
207-
await db.open()
208-
209-
t.ok(db.supports.events.batch)
210-
211-
db.on('batch', function (ops) {
212-
t.same(ops, [{ type: 'put', key: 456, value: 99, custom: 123 }])
213-
})
214-
215-
await db.batch([{ type: 'put', key: 456, value: 99, custom: 123 }])
216-
return db.close()
217-
})
218-
}
219-
220202
exports.tearDown = function (test, testCommon) {
221203
test('batch([]) teardown', async function (t) {
222204
return db.close()
@@ -228,6 +210,5 @@ exports.all = function (test, testCommon) {
228210
exports.args(test, testCommon)
229211
exports.batch(test, testCommon)
230212
exports.atomic(test, testCommon)
231-
exports.events(test, testCommon)
232213
exports.tearDown(test, testCommon)
233214
}

test/chained-batch-test.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,15 @@ exports.batch = function (test, testCommon) {
239239
const db = testCommon.factory()
240240
await db.open()
241241

242-
db.once('batch', function (operations) {
242+
const utf8 = db.keyEncoding('utf8')
243+
const json = db.valueEncoding('json')
244+
245+
db.once('write', function (operations) {
243246
t.same(operations, [
244-
{ type: 'put', key: 'a', value: 'a', valueEncoding: 'json' },
245-
{ type: 'put', key: 'b', value: 'b' },
246-
{ type: 'put', key: '"c"', value: 'c' },
247-
{ type: 'del', key: 'c', keyEncoding: 'json', arbitraryOption: true }
247+
{ type: 'put', key: 'a', value: 'a', keyEncoding: utf8, valueEncoding: json, encodedKey: 'a', encodedValue: '"a"' },
248+
{ type: 'put', key: 'b', value: 'b', keyEncoding: utf8, valueEncoding: utf8, encodedKey: 'b', encodedValue: 'b' },
249+
{ type: 'put', key: '"c"', value: 'c', keyEncoding: utf8, valueEncoding: utf8, encodedKey: '"c"', encodedValue: 'c' },
250+
{ type: 'del', key: 'c', keyEncoding: json, encodedKey: '"c"', arbitraryOption: true }
248251
])
249252
})
250253

@@ -265,32 +268,13 @@ exports.batch = function (test, testCommon) {
265268
}
266269

267270
exports.events = function (test, testCommon) {
268-
test('chained batch emits batch event', async function (t) {
269-
t.plan(2)
270-
271-
const db = testCommon.factory()
272-
await db.open()
273-
274-
t.ok(db.supports.events.batch)
275-
276-
db.on('batch', function (ops) {
277-
t.same(ops, [
278-
{ type: 'put', key: 987, value: 'b', custom: 123 },
279-
{ type: 'del', key: 216, custom: 999 }
280-
])
281-
})
282-
283-
await db.batch().put(987, 'b', { custom: 123 }).del(216, { custom: 999 }).write()
284-
await db.close()
285-
})
286-
287-
test('db.close() on chained batch event', async function (t) {
271+
test('db.close() on chained batch write event', async function (t) {
288272
const db = testCommon.factory()
289273
await db.open()
290274

291275
let promise
292276

293-
db.on('batch', function () {
277+
db.on('write', function () {
294278
// Should not interfere with the current write() operation
295279
promise = db.close()
296280
})

test/del-test.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,16 @@ exports.del = function (test, testCommon) {
4141

4242
traits.open('del()', testCommon, async function (t, db) {
4343
let emitted = false
44-
db.once('del', () => { emitted = true })
45-
t.is(await assertPromise(db.del('foo')), undefined, 'void promise')
46-
t.ok(emitted)
44+
db.once('write', () => { emitted = true })
45+
t.is(await db.del('foo'), undefined, 'void promise')
46+
t.ok(emitted) // Not sure what the purpose of this test is
4747
})
4848

4949
traits.closed('del()', testCommon, async function (t, db) {
5050
return db.del('foo')
5151
})
5252
}
5353

54-
exports.events = function (test, testCommon) {
55-
test('del() emits del event', async function (t) {
56-
t.plan(2)
57-
58-
const db = testCommon.factory()
59-
await db.open()
60-
61-
t.ok(db.supports.events.del)
62-
63-
db.on('del', function (key) {
64-
t.is(key, 456)
65-
})
66-
67-
await db.del(456)
68-
return db.close()
69-
})
70-
}
71-
7254
exports.tearDown = function (test, testCommon) {
7355
test('del() teardown', async function (t) {
7456
return db.close()
@@ -79,6 +61,5 @@ exports.all = function (test, testCommon) {
7961
exports.setUp(test, testCommon)
8062
exports.args(test, testCommon)
8163
exports.del(test, testCommon)
82-
exports.events(test, testCommon)
8364
exports.tearDown(test, testCommon)
8465
}

test/put-test.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,6 @@ exports.put = function (test, testCommon) {
5656
})
5757
}
5858

59-
exports.events = function (test, testCommon) {
60-
test('put() emits put event', async function (t) {
61-
t.plan(3)
62-
t.ok(db.supports.events.put)
63-
64-
db.on('put', function (key, value) {
65-
t.is(key, 123)
66-
t.is(value, 'b')
67-
})
68-
69-
await db.put(123, 'b')
70-
})
71-
}
72-
7359
exports.tearDown = function (test, testCommon) {
7460
test('put() teardown', async function (t) {
7561
return db.close()
@@ -80,6 +66,5 @@ exports.all = function (test, testCommon) {
8066
exports.setUp(test, testCommon)
8167
exports.args(test, testCommon)
8268
exports.put(test, testCommon)
83-
exports.events(test, testCommon)
8469
exports.tearDown(test, testCommon)
8570
}

0 commit comments

Comments
 (0)