You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-24
Original file line number
Diff line number
Diff line change
@@ -127,7 +127,7 @@ Get a value from the database by `key`. The optional `options` object may contai
127
127
128
128
-`keyEncoding`: custom key encoding for this operation, used to encode the `key`.
129
129
-`valueEncoding`: custom value encoding for this operation, used to decode the value.
130
-
-`snapshot`: explicit [snapshot](#snapshot--dbsnapshot) to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot for this operation.
130
+
-`snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot for this operation.
131
131
132
132
Returns a promise for the value. If the `key` was not found then the value will be `undefined`.
133
133
@@ -137,7 +137,7 @@ Get multiple values from the database by an array of `keys`. The optional `optio
137
137
138
138
-`keyEncoding`: custom key encoding for this operation, used to encode the `keys`.
139
139
-`valueEncoding`: custom value encoding for this operation, used to decode values.
140
-
-`snapshot`: explicit [snapshot](#snapshot--dbsnapshot) to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot for this operation.
140
+
-`snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot for this operation.
141
141
142
142
Returns a promise for an array of values with the same order as `keys`. If a key was not found, the relevant value will be `undefined`.
143
143
@@ -233,7 +233,7 @@ The `gte` and `lte` range options take precedence over `gt` and `lt` respectivel
233
233
-`keyEncoding`: custom key encoding for this iterator, used to encode range options, to encode `seek()` targets and to decode keys.
234
234
-`valueEncoding`: custom value encoding for this iterator, used to decode values.
235
235
-`signal`: an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to [abort read operations on the iterator](#aborting-iterators).
236
-
-`snapshot`: explicit [snapshot](#snapshot--dbsnapshot) for the iterator to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot before returning an iterator.
236
+
-`snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) for the iterator to read from. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database will create its own internal snapshot before returning an iterator.
237
237
238
238
Lastly, an implementation is free to add its own options.
239
239
@@ -276,7 +276,7 @@ Delete all entries or a range. Not guaranteed to be atomic. Returns a promise. A
276
276
-`reverse` (boolean, default: `false`): delete entries in reverse order. Only effective in combination with `limit`, to delete the last N entries.
277
277
-`limit` (number, default: `Infinity`): limit the number of entries to be deleted. This number represents a _maximum_ number of entries and will not be reached if the end of the range is reached first. A value of `Infinity` or `-1` means there is no limit. When `reverse` is true the entries with the highest keys will be deleted instead of the lowest keys.
278
278
-`keyEncoding`: custom key encoding for this operation, used to encode range options.
279
-
-`snapshot`: explicit [snapshot](#snapshot--dbsnapshot) to read from, such that entries not present in the snapshot will not be deleted. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database may create its own internal snapshot but (unlike on other methods) this is currently not a hard requirement for implementations.
279
+
-`snapshot`: explicit [snapshot](#snapshot--dbsnapshotoptions) to read from, such that entries not present in the snapshot will not be deleted. If no `snapshot` is provided and `db.supports.implicitSnapshots` is true, the database may create its own internal snapshot but (unlike on other methods) this is currently not a hard requirement for implementations.
280
280
281
281
The `gte` and `lte` range options take precedence over `gt` and `lt` respectively. If no options are provided, all entries will be deleted.
**This is an experimental API and not widely supported at the time of writing ([Level/community#118](https://github.com/Level/community/issues/118)).**
388
+
**This is an experimental API ([Level/community#118](https://github.com/Level/community/issues/118)).**
389
389
390
-
Create an explicit [snapshot](#snapshot). Throws a [`LEVEL_NOT_SUPPORTED`](#level_not_supported) error if `db.supports.explicitSnapshots` is not true. For details, see [Reading From Snapshots](#reading-from-snapshots).
390
+
Create an explicit [snapshot](#snapshot). Throws a [`LEVEL_NOT_SUPPORTED`](#level_not_supported) error if `db.supports.explicitSnapshots` is false. For details, see [Reading From Snapshots](#reading-from-snapshots).
391
+
392
+
There are currently no options but specific implementations may add their own.
393
+
394
+
Don't forget to call `snapshot.close()` when done.
Free up underlying resources. Be sure to call this when the snapshot is no longer needed, because snapshots may cause the database to temporarily pause internal storage optimizations. Returns a promise. Closing the snapshot is an idempotent operation, such that calling `snapshot.close()` more than once is allowed and makes no difference.
714
+
Increment reference count, to register work that should delay closing until `snapshot.unref()` is called an equal amount of times. The promise that will be returned by `snapshot.close()` will not resolve until the reference count returns to 0. This prevents prematurely closing underlying resources while the snapshot is in use.
711
715
712
-
After `snapshot.close()` has been called, no further operations are allowed. For example, `db.get(key, { snapshot })` will yield an error with code [`LEVEL_SNAPSHOT_NOT_OPEN`](#level_snapshot_not_open). Any unclosed iterators (that use this snapshot) will be closed by `snapshot.close()` and can then no longer be used.
716
+
It is normally not necessary to call `snapshot.ref()` and `snapshot.unref()` because builtin database methods automatically do.
717
+
718
+
#### `snapshot.unref()`
719
+
720
+
Decrement reference count, to indicate that the work has finished.
721
+
722
+
#### `snapshot.close()`
713
723
714
-
#### `snapshot.db`
724
+
Free up underlying resources. Be sure to call this when the snapshot is no longer needed, because snapshots may cause the database to temporarily pause internal storage optimizations. Returns a promise. Closing the snapshot is an idempotent operation, such that calling `snapshot.close()` more than once is allowed and makes no difference.
715
725
716
-
A reference to the database that created this snapshot.
726
+
After `snapshot.close()` has been called, no further operations are allowed. For example, `db.get(key, { snapshot })` will throw an error with code [`LEVEL_SNAPSHOT_NOT_OPEN`](#level_snapshot_not_open).
717
727
718
728
### Encodings
719
729
@@ -950,10 +960,10 @@ Removing this concern (if necessary) must be done on an application-level. For e
950
960
951
961
### Reading From Snapshots
952
962
953
-
A snapshot is a lightweight "token" that represents the version of a database at a particular point in time. This allows for reading data without seeing subsequent writes made on the database. It comes in two forms:
963
+
A snapshot is a lightweight "token" that represents a version of a database at a particular point in time. This allows for reading data without seeing subsequent writes made on the database. It comes in two forms:
954
964
955
965
1. Implicit snapshots: created internally by the database and not visible to the outside world.
956
-
2. Explicit snapshots: created with `snapshot = db.snapshot()`. Because it acts as a token, `snapshot` has no methods of its own besides `snapshot.close()`. Instead the snapshot is to be passed to database (or [sublevel](#sublevel)) methods like `db.iterator()`.
966
+
2. Explicit snapshots: created with `snapshot = db.snapshot()`. Because it acts as a token, `snapshot` has no read methods of its own. Instead the snapshot is to be passed to database methods like `db.get()` and `db.iterator()`. This also works on sublevels.
957
967
958
968
Use explicit snapshots wisely, because their lifetime must be managed manually. Implicit snapshots are typically more convenient and possibly more performant because they can handled natively and have their lifetime limited by the surrounding operation. That said, explicit snapshots can be useful to make multiple read operations that require a shared, consistent view of the data.
959
969
@@ -1625,19 +1635,19 @@ class ExampleSublevel extends AbstractSublevel {
1625
1635
}
1626
1636
```
1627
1637
1628
-
### `snapshot = db._snapshot()`
1638
+
### `snapshot = db._snapshot(options)`
1639
+
1640
+
Create a snapshot. The `options` argument is guaranteed to be an object. There are currently no options but implementations may add their own.
1629
1641
1630
-
The default `_snapshot()` throws a [`LEVEL_NOT_SUPPORTED`](#errors) error. To implement this method, extend `AbstractSnapshot`, return an instance of this class in an overridden `_snapshot()` method and set `manifest.explicitSnapshots` to `true`:
1642
+
The default `_snapshot()` throws a [`LEVEL_NOT_SUPPORTED`](#level_not_supported) error. To implement this method, extend `AbstractSnapshot`, return an instance of this class in an overridden `_snapshot()` method and set `manifest.explicitSnapshots` to `true`:
@@ -1650,8 +1660,8 @@ class ExampleLevel extends AbstractLevel {
1650
1660
super(manifest, options)
1651
1661
}
1652
1662
1653
-
_snapshot () {
1654
-
returnnewExampleSnapshot(this)
1663
+
_snapshot (options) {
1664
+
returnnewExampleSnapshot(options)
1655
1665
}
1656
1666
}
1657
1667
```
@@ -1762,11 +1772,11 @@ The default `_close()` returns a resolved promise. Overriding is optional.
1762
1772
1763
1773
### `snapshot = new AbstractSnapshot(db)`
1764
1774
1765
-
The first argument to this constructor must be an instance of the relevant `AbstractLevel` implementation. The constructor will set `snapshot.db` which ensures that `db` will not be garbage collected in case there are no other references to it.
1775
+
The first argument to this constructor must be an instance of the relevant `AbstractLevel` implementation.
1766
1776
1767
1777
#### `snapshot._close()`
1768
1778
1769
-
Free up underlying resources. This method is guaranteed to only be called once. Must return a promise.
1779
+
Free up underlying resources. This method is guaranteed to only be called once and will not be called while read operations like `db._get()` are inflight. Must return a promise.
1770
1780
1771
1781
The default `_close()` returns a resolved promise. Overriding is optional.
0 commit comments