Skip to content

Commit a9f6968

Browse files
committed
docs: add multi_get to GETTING_STARTED.md
1 parent ba98698 commit a9f6968

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

GETTING_STARTED.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Or add it to your rebar config
1616
```erlang
1717
{deps, [
1818
....
19-
{rocksdb, "2.0.0"}
19+
{rocksdb, "2.2.0"}
2020
]}.
2121
```
2222

@@ -68,6 +68,41 @@ end,
6868
rocksdb:delete(Db, <<"my key">>).
6969
```
7070

71+
### Batch Reads with multi_get
72+
73+
When you need to retrieve multiple keys at once, `multi_get/3` is more efficient than calling `get/3` multiple times. It retrieves all keys in a single operation:
74+
75+
```erlang
76+
%% Insert some data
77+
ok = rocksdb:put(Db, <<"key1">>, <<"value1">>, []),
78+
ok = rocksdb:put(Db, <<"key2">>, <<"value2">>, []),
79+
ok = rocksdb:put(Db, <<"key3">>, <<"value3">>, []),
80+
81+
%% Retrieve multiple keys at once
82+
Keys = [<<"key1">>, <<"key2">>, <<"key3">>, <<"missing">>],
83+
Results = rocksdb:multi_get(Db, Keys, []),
84+
%% Results = [{ok, <<"value1">>}, {ok, <<"value2">>}, {ok, <<"value3">>}, not_found]
85+
```
86+
87+
The results are returned in the same order as the input keys. Each result is either:
88+
- `{ok, Value}` - the key was found
89+
- `not_found` - the key does not exist
90+
- `{error, Reason}` - an error occurred
91+
92+
For column families, use `multi_get/4`:
93+
94+
```erlang
95+
Results = rocksdb:multi_get(Db, ColumnFamily, Keys, []).
96+
```
97+
98+
You can also use snapshots with `multi_get` to get a consistent view:
99+
100+
```erlang
101+
{ok, Snapshot} = rocksdb:snapshot(Db),
102+
Results = rocksdb:multi_get(Db, Keys, [{snapshot, Snapshot}]),
103+
rocksdb:release_snapshot(Snapshot).
104+
```
105+
71106
### Atomic Updates
72107

73108
Note that if the process dies after the Put of key2 but before the delete of key1, the same value may be left stored under multiple keys. Such problems can be avoided by using the function `rocksdb:write/3` class to atomically apply a set of updates:

0 commit comments

Comments
 (0)