Skip to content

Commit

Permalink
Breaking: change _checkKey and _checkValue to assertions (#108)
Browse files Browse the repository at this point in the history
Only matters for implementations that override these methods, and not
for end users.

Category: change
  • Loading branch information
vweevers authored Dec 31, 2024
1 parent efd4175 commit ca3c368
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 39 deletions.
9 changes: 3 additions & 6 deletions abstract-chained-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ class AbstractChainedBatch {
const delegated = options.sublevel != null
const db = delegated ? options.sublevel : this.db
const original = options
const keyError = db._checkKey(key)
const valueError = db._checkValue(value)

if (keyError != null) throw keyError
if (valueError != null) throw valueError
db._assertValidKey(key)
db._assertValidValue(value)

const op = {
...options,
Expand Down Expand Up @@ -185,9 +183,8 @@ class AbstractChainedBatch {
const delegated = options.sublevel != null
const db = delegated ? options.sublevel : this.db
const original = options
const keyError = db._checkKey(key)

if (keyError != null) throw keyError
db._assertValidKey(key)

const op = {
...options,
Expand Down
40 changes: 13 additions & 27 deletions abstract-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@ class AbstractLevel extends EventEmitter {
}

this.#assertOpen()

const err = this._checkKey(key)
if (err) throw err
this._assertValidKey(key)

const snapshot = options.snapshot
const keyEncoding = this.keyEncoding(options.keyEncoding)
Expand Down Expand Up @@ -398,9 +396,7 @@ class AbstractLevel extends EventEmitter {

for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const err = this._checkKey(key)
if (err) throw err

this._assertValidKey(key)
mappedKeys[i] = this.prefixKey(keyEncoding.encode(key), keyFormat, true)
}

Expand Down Expand Up @@ -444,10 +440,7 @@ class AbstractLevel extends EventEmitter {
}

this.#assertOpen()

// TODO (next major): change this to an assert
const err = this._checkKey(key)
if (err) throw err
this._assertValidKey(key)

const snapshot = options.snapshot
const keyEncoding = this.keyEncoding(options.keyEncoding)
Expand Down Expand Up @@ -512,9 +505,7 @@ class AbstractLevel extends EventEmitter {

for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const err = this._checkKey(key)
if (err) throw err

this._assertValidKey(key)
mappedKeys[i] = this.prefixKey(keyEncoding.encode(key), keyFormat, true)
}

Expand Down Expand Up @@ -551,8 +542,8 @@ class AbstractLevel extends EventEmitter {

this.#assertOpen()

const err = this._checkKey(key) || this._checkValue(value)
if (err) throw err
this._assertValidKey(key)
this._assertValidValue(value)

// Encode data for private API
const keyEncoding = this.keyEncoding(options.keyEncoding)
Expand Down Expand Up @@ -609,9 +600,7 @@ class AbstractLevel extends EventEmitter {
}

this.#assertOpen()

const err = this._checkKey(key)
if (err) throw err
this._assertValidKey(key)

// Encode data for private API
const keyEncoding = this.keyEncoding(options.keyEncoding)
Expand Down Expand Up @@ -699,15 +688,12 @@ class AbstractLevel extends EventEmitter {
const delegated = op.sublevel != null
const db = delegated ? op.sublevel : this

const keyError = db._checkKey(op.key)
if (keyError != null) throw keyError
db._assertValidKey(op.key)

op.keyEncoding = db.keyEncoding(op.keyEncoding)

if (isPut) {
const valueError = db._checkValue(op.value)
if (valueError != null) throw valueError

db._assertValidValue(op.value)
op.valueEncoding = db.valueEncoding(op.valueEncoding)
} else if (op.type !== 'del') {
throw new TypeError("A batch operation must have a type property that is 'put' or 'del'")
Expand Down Expand Up @@ -1006,17 +992,17 @@ class AbstractLevel extends EventEmitter {
return new DefaultChainedBatch(this)
}

_checkKey (key) {
_assertValidKey (key) {
if (key === null || key === undefined) {
return new ModuleError('Key cannot be null or undefined', {
throw new ModuleError('Key cannot be null or undefined', {
code: 'LEVEL_INVALID_KEY'
})
}
}

_checkValue (value) {
_assertValidValue (value) {
if (value === null || value === undefined) {
return new ModuleError('Value cannot be null or undefined', {
throw new ModuleError('Value cannot be null or undefined', {
code: 'LEVEL_INVALID_VALUE'
})
}
Expand Down
8 changes: 2 additions & 6 deletions lib/prewrite-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ class PrewriteBatch {
const delegated = op.sublevel != null
const db = delegated ? op.sublevel : this.#db

const keyError = db._checkKey(op.key)
if (keyError != null) throw keyError

db._assertValidKey(op.key)
op.keyEncoding = db.keyEncoding(op.keyEncoding)

if (isPut) {
const valueError = db._checkValue(op.value)
if (valueError != null) throw valueError

db._assertValidValue(op.value)
op.valueEncoding = db.valueEncoding(op.valueEncoding)
} else if (op.type !== 'del') {
throw new TypeError("A batch operation must have a type property that is 'put' or 'del'")
Expand Down

0 comments on commit ca3c368

Please sign in to comment.