Skip to content

Commit af3efca

Browse files
committed
Refresh the README's Julia-wrappers example.
The 1.0 calls (`Environment(path; ...)` bare, `start(env)`, `open(txn)`, `open(txn, dbi)`, `LMDB.tryget`, `LMDB.is_notfound`) are all gone in this branch. Switch the example to `LMDB.Environment(path; ...) do env`, `LMDB.Transaction`, `LMDB.Database`, `LMDB.Cursor`, and use `LMDB.get(..., nothing)` for the missing-key path.
1 parent 4a1137d commit af3efca

1 file changed

Lines changed: 19 additions & 22 deletions

File tree

README.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ LMDB.jl exposes the same database through three surfaces:
2222
`AbstractDict{K,V}` over a single LMDB file. Standard library
2323
machinery (`merge!`, `filter!`, `pairs`, iteration, …) works out
2424
of the box. Reach for this when you want a persistent `Dict`.
25-
- Julia wrappers: `Environment`, `Transaction`, `Database`, `Cursor`.
26-
Julia-shaped wrappers around handles, transactions, and cursors,
27-
with finalizers, `do`-block forms, and so on. Use these when you
28-
want explicit transactions.
25+
- Julia wrappers: `LMDB.Environment`, `LMDB.Transaction`,
26+
`LMDB.Database`, `LMDB.Cursor`. Julia-shaped wrappers around
27+
handles, transactions, and cursors, with finalizers, `do`-block
28+
forms, and so on. Use these when you want explicit transactions.
2929
- C API: `LMDB.mdb_*` and `LMDB.MDB_*`. Raw `ccall` bindings and
3030
status-code constants. Use this when the Julia wrappers don't expose
3131
a particular API or you want to inspect status codes directly.
@@ -55,47 +55,44 @@ close(d)
5555
```julia
5656
using LMDB
5757

58-
env = Environment("/tmp/mydb"; mapsize = 1<<30, maxreaders = 510,
59-
flags = LMDB.MDB_NOTLS | LMDB.MDB_NORDAHEAD)
60-
try
61-
start(env) do txn # auto-commits/aborts
62-
open(txn) do dbi
58+
LMDB.Environment("/tmp/mydb"; mapsize = 1<<30, maxreaders = 510,
59+
flags = LMDB.MDB_NOTLS | LMDB.MDB_NORDAHEAD) do env
60+
LMDB.Transaction(env) do txn # auto-commits/aborts
61+
LMDB.Database(txn) do dbi
6362
put!(txn, dbi, "k1", "hello")
6463
put!(txn, dbi, "k2", [1.0, 2.0, 3.0])
6564

66-
@show LMDB.tryget(txn, dbi, "k1", String)
65+
@show LMDB.get(txn, dbi, "k1", String, nothing)
6766
@show LMDB.get(txn, dbi, "missing", String, "default")
6867
@show LMDB.stat(txn, dbi).entries
6968
end
7069
end
7170

7271
# Cursor walk over the LMDB-owned mmap (zero-copy access).
73-
start(env; flags = LMDB.MDB_RDONLY) do txn
74-
open(txn) do dbi
75-
open(txn, dbi) do cur
72+
LMDB.Transaction(env; flags = LMDB.MDB_RDONLY) do txn
73+
LMDB.Database(txn) do dbi
74+
LMDB.Cursor(txn, dbi) do cur
7675
LMDB.walk(cur, String, String) do k, v
7776
println(k, " => ", v)
7877
end
7978
end
8079
end
8180
end
82-
finally
83-
close(env)
8481
end
8582
```
8683

8784
The package decodes `String`, `Vector{T}` for any bitstype `T`, and the
8885
primitive numeric types out of the box. To plug in a custom representation,
89-
define a `Base.read(io::IO, ::Type{T})` method; it will be picked up by `tryget`,
90-
`get`, `walk(f, cur, K, V)`, and the cursor accessors
86+
define a `Base.read(io::IO, ::Type{T})` method; it will be picked up by
87+
`LMDB.get`, `LMDB.walk(f, cur, K, V)`, and the cursor accessors
9188
`LMDB.key`/`LMDB.value`/`LMDB.item`.
92-
Status-code matchers live on `LMDBError`:
89+
90+
For a missing-key tolerant lookup, prefer
91+
`LMDB.get(txn, dbi, key, T, default)` over `try`/`catch` on `LMDBError`:
9392

9493
```julia
95-
try
96-
LMDB.get(txn, dbi, "missing", String)
97-
catch e
98-
e isa LMDBError && LMDB.is_notfound(e) || rethrow()
94+
v = LMDB.get(txn, dbi, "missing", String, nothing)
95+
if v === nothing
9996
# treat as missing
10097
end
10198
```

0 commit comments

Comments
 (0)