diff --git a/README.md b/README.md index baff05ca2..3808ce1a3 100644 --- a/README.md +++ b/README.md @@ -229,12 +229,12 @@ db.getRange({ start, end, offset: 10, limit: 10 }) // skip first 10 and get next If you want to get a true array from the range results, the `asArray` property will return the results as an array. ### Catching Errors in Range Iteration -With an array, `map` and `filter` callbacks are immediately executed, but with range iterators, they are executed during iteration, so if an error occurs during iteration, the error will be thrown when the iteration is attempted. It is also critical that when an iteration is finished, the cursor is closed, so by default, if an error occurs during iteration, the cursor will immediately be closed. However, if you want to catch errors that occur in `map` (and `flatMap`) callbacks during iteration, you can use the `mapCatch` method to catch errors that occur during iteration, and allow iteration to continue (without closing the cursor). For example: +With an array, `map` and `filter` callbacks are immediately executed, but with range iterators, they are executed during iteration, so if an error occurs during iteration, the error will be thrown when the iteration is attempted. It is also critical that when an iteration is finished, the cursor is closed, so by default, if an error occurs during iteration, the cursor will immediately be closed. However, if you want to catch errors that occur in `map` (and `flatMap`) callbacks during iteration, you can use the `mapError` method to catch errors that occur during iteration, and allow iteration to continue (without closing the cursor). For example: ```js let mapped = db.getRange({ start, end }).map(({ key, value }) => { return thisMightThrowError(value); -}).mapCatch((error) => { +}).mapError((error) => { // rather than letting the error terminate the iteration, we can catch it here and return a value to continue iterating: return 'error occurred'; }) @@ -242,7 +242,7 @@ for (let entry of mapped) { ... } ``` -A `mapCatch` callback can return a value to continue iterating, or throw an error to terminate the iteration. +A `mapError` callback can return a value to continue iterating, or throw an error to terminate the iteration. #### Snapshots By default, a range iterator will use a database snapshot, using a single read transaction that remains open and gives a consistent view of the database at the time it was started, for the duration of iterating through the range. However, if the iteration will take place over a long period of time, keeping a read transaction open for a long time can interfere with LMDB's free space collection and reuse and increase the database size. If you will be using a long duration iterator, you can specify `snapshot: false` flag in the range options to indicate that it snapshotting is not necessary, and it can reset and renew read transactions while iterating, to allow LMDB to collect any space that was freed during iteration. diff --git a/test/index.test.js b/test/index.test.js index 96ce99663..a9c3710aa 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1967,7 +1967,7 @@ describe('lmdb-js', function () { if (v === 4) throw new Error('test'); return [v, v + 1]; }) - .mapCatch((error) => { + .mapError((error) => { return { error: error.toString() }; }); all = []; @@ -2006,7 +2006,7 @@ describe('lmdb-js', function () { } }).to.throw(); expect(finished).to.be.equal(1); - let aMappedWithCaught = aMappedWithError.mapCatch((error) => { + let aMappedWithCaught = aMappedWithError.mapError((error) => { return { error: error.toString() }; }); all = []; diff --git a/util/RangeIterable.js b/util/RangeIterable.js index 1d52ffb95..1b19d0254 100644 --- a/util/RangeIterable.js +++ b/util/RangeIterable.js @@ -86,12 +86,12 @@ export class RangeIterable { throw error; // throw to next catch to handle } } catch (error) { - if (iterable.mapError) { - // if we have mapError, we can use it to further handle errors + if (iterable.handleError) { + // if we have handleError, we can use it to further handle errors try { - result = iterable.mapError(error, i); - } catch (mapError) { - return this.throw(mapError); + result = iterable.handleError(error, i); + } catch (error2) { + return this.throw(error2); } } else return this.throw(error); } @@ -212,7 +212,7 @@ export class RangeIterable { try { let result = iterator.next(); if (result.then) { - if (!async) + if (!options.async) throw new Error( 'Can not synchronously iterate with promises as iterator results', ); @@ -355,7 +355,7 @@ export class RangeIterable { } return element; }); - iterable.mapError = (error, i) => { + iterable.handleError = (error, i) => { if (i < start) return SKIP; if (i >= end) { return DONE; @@ -364,7 +364,7 @@ export class RangeIterable { }; return iterable; } - mapCatch(catch_callback) { + mapError(catch_callback) { let iterable = this.map((element) => { return element; });