| 1 |
package lmdb_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"log" |
| 8 |
"os" |
| 9 |
"runtime" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/bmatsuo/lmdb-go/lmdb" |
| 13 |
) |
| 14 |
|
| 15 |
// These values shouldn't actually be assigned to. The are used as stand-ins |
| 16 |
// for tests which do not act as tests. |
| 17 |
var EnvEx *lmdb.Env |
| 18 |
var DBIEx lmdb.DBI |
| 19 |
|
| 20 |
// These values can only be used is code-only examples (no test output). |
| 21 |
var env *lmdb.Env |
| 22 |
var txn *lmdb.Txn |
| 23 |
var dbi lmdb.DBI |
| 24 |
var dbname string |
| 25 |
var err error |
| 26 |
var stop chan struct{} |
| 27 |
|
| 28 |
// These values can be used as no-op placeholders in examples. |
| 29 |
func doUpdate(txn *lmdb.Txn) error { return nil } |
| 30 |
func doUpdate1(txn *lmdb.Txn) error { return nil } |
| 31 |
func doUpdate2(txn *lmdb.Txn) error { return nil } |
| 32 |
func doView(txn *lmdb.Txn) error { return nil } |
| 33 |
|
| 34 |
// This example demonstrates a complete workflow for a simple application |
| 35 |
// working with LMDB. First, an Env is configured and mapped to memory. Once |
| 36 |
// mapped, database handles are opened and normal database operations may |
| 37 |
// begin. |
| 38 |
func Example() { |
| 39 |
// Create an environment and make sure it is eventually closed. |
| 40 |
env, err := lmdb.NewEnv() |
| 41 |
if err != nil { |
| 42 |
// ... |
| 43 |
} |
| 44 |
defer env.Close() |
| 45 |
|
| 46 |
// Configure and open the environment. Most configuration must be done |
| 47 |
// before opening the environment. The go documentation for each method |
| 48 |
// should indicate if it must be called before calling env.Open() |
| 49 |
err = env.SetMaxDBs(1) |
| 50 |
if err != nil { |
| 51 |
// .. |
| 52 |
} |
| 53 |
err = env.SetMapSize(1 << 30) |
| 54 |
if err != nil { |
| 55 |
// .. |
| 56 |
} |
| 57 |
err = env.Open("/path/to/db/", 0, 0644) |
| 58 |
if err != nil { |
| 59 |
// .. |
| 60 |
} |
| 61 |
|
| 62 |
// In any real application it is important to check for readers that were |
| 63 |
// never closed by their owning process, and for which the owning process |
| 64 |
// has exited. See the documentation on transactions for more information. |
| 65 |
staleReaders, err := env.ReaderCheck() |
| 66 |
if err != nil { |
| 67 |
// ... |
| 68 |
} |
| 69 |
if staleReaders > 0 { |
| 70 |
log.Printf("cleared %d reader slots from dead processes", staleReaders) |
| 71 |
} |
| 72 |
|
| 73 |
// Open a database handle that will be used for the entire lifetime of this |
| 74 |
// application. Because the database may not have existed before, and the |
| 75 |
// database may need to be created, we need to get the database handle in |
| 76 |
// an update transacation. |
| 77 |
var dbi lmdb.DBI |
| 78 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 79 |
dbi, err = txn.CreateDBI("example") |
| 80 |
return err |
| 81 |
}) |
| 82 |
if err != nil { |
| 83 |
// ... |
| 84 |
} |
| 85 |
|
| 86 |
// The database referenced by our DBI handle is now ready for the |
| 87 |
// application to use. Here the application just opens a readonly |
| 88 |
// transaction and reads the data stored in the "hello" key and prints its |
| 89 |
// value to the application's standard output. |
| 90 |
err = env.View(func(txn *lmdb.Txn) (err error) { |
| 91 |
v, err := txn.Get(dbi, []byte("hello")) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
fmt.Println(string(v)) |
| 96 |
return nil |
| 97 |
}) |
| 98 |
if err != nil { |
| 99 |
// ... |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// This example demonstrates the simplest (and most naive) way to issue |
| 104 |
// database updates from a goroutine for which it cannot be known ahead of time |
| 105 |
// whether runtime.LockOSThread has been called. |
| 106 |
func Example_threads() { |
| 107 |
// Create a function that wraps env.Update and sends the resulting error |
| 108 |
// over a channel. Because env.Update is called our update function will |
| 109 |
// call runtime.LockOSThread to safely issue the update operation. |
| 110 |
update := func(res chan<- error, op lmdb.TxnOp) { |
| 111 |
res <- env.Update(op) |
| 112 |
} |
| 113 |
|
| 114 |
// ... |
| 115 |
|
| 116 |
// Now, in goroutine where we cannot determine if we are locked to a |
| 117 |
// thread, we can create a new goroutine to process the update(s) we want. |
| 118 |
res := make(chan error) |
| 119 |
go update(res, func(txn *lmdb.Txn) (err error) { |
| 120 |
return txn.Put(dbi, []byte("thisUpdate"), []byte("isSafe"), 0) |
| 121 |
}) |
| 122 |
err = <-res |
| 123 |
if err != nil { |
| 124 |
panic(err) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// This example demonstrates a more sophisticated way to issue database updates |
| 129 |
// from a goroutine for which it cannot be known ahead of time whether |
| 130 |
// runtime.LockOSThread has been called. |
| 131 |
func Example_worker() { |
| 132 |
// Wrap operations in a struct that can be passed over a channel to a |
| 133 |
// worker goroutine. |
| 134 |
type lmdbop struct { |
| 135 |
op lmdb.TxnOp |
| 136 |
res chan<- error |
| 137 |
} |
| 138 |
worker := make(chan *lmdbop) |
| 139 |
update := func(op lmdb.TxnOp) error { |
| 140 |
res := make(chan error) |
| 141 |
worker <- &lmdbop{op, res} |
| 142 |
return <-res |
| 143 |
} |
| 144 |
|
| 145 |
// Start issuing update operations in a goroutine in which we know |
| 146 |
// runtime.LockOSThread can be called and we can safely issue transactions. |
| 147 |
go func() { |
| 148 |
runtime.LockOSThread() |
| 149 |
defer runtime.LockOSThread() |
| 150 |
|
| 151 |
// Perform each operation as we get to it. Because this goroutine is |
| 152 |
// already locked to a thread, env.UpdateLocked is called to avoid |
| 153 |
// premature unlocking of the goroutine from its thread. |
| 154 |
for op := range worker { |
| 155 |
op.res <- env.UpdateLocked(op.op) |
| 156 |
} |
| 157 |
}() |
| 158 |
|
| 159 |
// ... |
| 160 |
|
| 161 |
// In another goroutine, where we cannot determine if we are locked to a |
| 162 |
// thread already. |
| 163 |
err = update(func(txn *lmdb.Txn) (err error) { |
| 164 |
// This function will execute safely in the worker goroutine, which is |
| 165 |
// locked to its thread. |
| 166 |
return txn.Put(dbi, []byte("thisUpdate"), []byte("isSafe"), 0) |
| 167 |
}) |
| 168 |
} |
| 169 |
|
| 170 |
// This example demonstrates how an application typically uses Env.SetMapSize. |
| 171 |
// The call to Env.SetMapSize() is made before calling env.Open(). Any calls |
| 172 |
// after calling Env.Open() must take special care to synchronize with other |
| 173 |
// goroutines. |
| 174 |
func ExampleEnv_SetMapSize() { |
| 175 |
env, err := lmdb.NewEnv() |
| 176 |
if err != nil { |
| 177 |
// ... |
| 178 |
} |
| 179 |
|
| 180 |
// set the memory map size (maximum database size) to 1GB. |
| 181 |
err = env.SetMapSize(1 << 30) |
| 182 |
if err != nil { |
| 183 |
// ... |
| 184 |
} |
| 185 |
|
| 186 |
err = env.Open("mydb", 0, 0644) |
| 187 |
if err != nil { |
| 188 |
// ... |
| 189 |
} |
| 190 |
// ... |
| 191 |
} |
| 192 |
|
| 193 |
// This example demonstrates how to handle a MapResized error, encountered |
| 194 |
// after another process has called mdb_env_set_mapsize (Env.SetMapSize). |
| 195 |
// Applications which don't expect another process to resize the mmap don't |
| 196 |
// need to check for the MapResized error. |
| 197 |
// |
| 198 |
// The example is simplified for clarity. Many real applications will need to |
| 199 |
// synchronize calls to Env.SetMapSize using something like a sync.RWMutex to |
| 200 |
// ensure there are no active readonly transactions (those opened successfully |
| 201 |
// before MapResized was encountered). |
| 202 |
func ExampleEnv_SetMapSize_mapResized() { |
| 203 |
retry: |
| 204 |
err := env.Update(doUpdate) |
| 205 |
if lmdb.IsMapResized(err) { |
| 206 |
// If concurrent read transactions are possible then a sync.RWMutex |
| 207 |
// must be used here to ensure they all terminate before calling |
| 208 |
// env.SetMapSize(). |
| 209 |
err = env.SetMapSize(0) |
| 210 |
if err != nil { |
| 211 |
panic(err) |
| 212 |
} |
| 213 |
|
| 214 |
// retry the update. a goto is not necessary but it simplifies error |
| 215 |
// handling with minimal overhead. |
| 216 |
goto retry |
| 217 |
} else if err != nil { |
| 218 |
// ... |
| 219 |
} |
| 220 |
// ... |
| 221 |
} |
| 222 |
|
| 223 |
func backupFailed(err error) {} |
| 224 |
|
| 225 |
// This example uses env.Copy to periodically create atomic backups of an |
| 226 |
// environment. A real implementation would need to solve problems with |
| 227 |
// failures, remote persistence, purging old backups, etc. But the core loop |
| 228 |
// would have the same form. |
| 229 |
func ExampleEnv_Copy() { |
| 230 |
go func(backup <-chan time.Time) { |
| 231 |
for { |
| 232 |
select { |
| 233 |
case <-backup: |
| 234 |
now := time.Now().UTC() |
| 235 |
backup := fmt.Sprintf("backup-%s", now.Format(time.RFC3339)) |
| 236 |
os.Mkdir(backup, 0755) |
| 237 |
err = env.Copy(backup) |
| 238 |
if err != nil { |
| 239 |
// ... |
| 240 |
continue |
| 241 |
} |
| 242 |
case <-stop: |
| 243 |
return |
| 244 |
} |
| 245 |
} |
| 246 |
}(time.Tick(time.Hour)) |
| 247 |
} |
| 248 |
|
| 249 |
// This example shows the basic use of Env.Update, the primary method lmdb-go |
| 250 |
// provides for to store data in an Env. |
| 251 |
func ExampleEnv_Update() { |
| 252 |
// It is not safe to call runtime.LockOSThread here because Env.Update |
| 253 |
// would later cause premature unlocking of the goroutine. If an |
| 254 |
// application requires that goroutines be locked to threads before |
| 255 |
// starting an an update on the Env then you must use Env.UpdateLocked |
| 256 |
// instead of Env.Update. |
| 257 |
|
| 258 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 259 |
// Write several keys to the database within one transaction. If |
| 260 |
// either write fails and this function returns an error then readers |
| 261 |
// in other transactions will not see either value because Env.Update |
| 262 |
// aborts the transaction if an error is returned. |
| 263 |
|
| 264 |
err = txn.Put(dbi, []byte("x"), []byte("hello"), 0) |
| 265 |
if err != nil { |
| 266 |
return err |
| 267 |
} |
| 268 |
err = txn.Put(dbi, []byte("y"), []byte("goodbye"), 0) |
| 269 |
if err != nil { |
| 270 |
return err |
| 271 |
} |
| 272 |
return nil |
| 273 |
}) |
| 274 |
if err != nil { |
| 275 |
panic(err) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// In this example, another C library requires the application to lock a |
| 280 |
// goroutine to its thread. When writing to the Env this goroutine must use |
| 281 |
// the method Env.UpdateLocked to prevent premature unlocking of the goroutine. |
| 282 |
// |
| 283 |
// Note that there is no way for a goroutine to determine if it is locked to a |
| 284 |
// thread failure to call Env.UpdateLocked in a scenario like this can lead to |
| 285 |
// unspecified and hard to debug failure modes for your application. |
| 286 |
func ExampleEnv_UpdateLocked() { |
| 287 |
runtime.LockOSThread() |
| 288 |
defer runtime.UnlockOSThread() |
| 289 |
|
| 290 |
// ... Do something that requires the goroutine be locked to its thread. |
| 291 |
|
| 292 |
// Create a transaction that will not interfere with thread locking and |
| 293 |
// issue some writes with it. |
| 294 |
err = env.UpdateLocked(func(txn *lmdb.Txn) (err error) { |
| 295 |
err = txn.Put(dbi, []byte("x"), []byte("hello"), 0) |
| 296 |
if err != nil { |
| 297 |
return err |
| 298 |
} |
| 299 |
err = txn.Put(dbi, []byte("y"), []byte("goodbye"), 0) |
| 300 |
if err != nil { |
| 301 |
return err |
| 302 |
} |
| 303 |
return nil |
| 304 |
}) |
| 305 |
if err != nil { |
| 306 |
panic(err) |
| 307 |
} |
| 308 |
|
| 309 |
// ... Do something requiring the goroutine still be locked to its thread. |
| 310 |
} |
| 311 |
|
| 312 |
// This example shows the general workflow of LMDB. An environment is created |
| 313 |
// and configured before being opened. After the environment is opened its |
| 314 |
// databases are created and their handles are saved for use in future |
| 315 |
// transactions. |
| 316 |
func ExampleEnv() { |
| 317 |
// open the LMDB environment and configure common options like its size and |
| 318 |
// maximum number of databases. |
| 319 |
env, err := lmdb.NewEnv() |
| 320 |
if err != nil { |
| 321 |
// ... |
| 322 |
} |
| 323 |
err = env.SetMapSize(100 * 1024 * 1024) // 100MB |
| 324 |
if err != nil { |
| 325 |
// ... |
| 326 |
} |
| 327 |
err = env.SetMaxDBs(1) |
| 328 |
if err != nil { |
| 329 |
// ... |
| 330 |
} |
| 331 |
|
| 332 |
// open the environment only after the it has been configured. some |
| 333 |
// settings may only be called before the environment is opened where |
| 334 |
// others may have caveats. |
| 335 |
err = env.Open("mydb/", 0, 0664) |
| 336 |
if err != nil { |
| 337 |
// ... |
| 338 |
} |
| 339 |
defer env.Close() |
| 340 |
|
| 341 |
var dbi lmdb.DBI |
| 342 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 343 |
// open a database, creating it if necessary. the database is stored |
| 344 |
// outside the transaction via closure and can be use after the |
| 345 |
// transaction is committed. |
| 346 |
dbi, err = txn.OpenDBI("exampledb", lmdb.Create) |
| 347 |
if err != nil { |
| 348 |
return err |
| 349 |
} |
| 350 |
|
| 351 |
// commit the transaction, writing an entry for the newly created |
| 352 |
// database if it was just created and allowing the dbi to be used in |
| 353 |
// future transactions. |
| 354 |
return nil |
| 355 |
}) |
| 356 |
if err != nil { |
| 357 |
panic(err) |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
// This example shows the basic operations used when creating and working with |
| 362 |
// Txn types. |
| 363 |
func ExampleTxn() { |
| 364 |
// open a database. |
| 365 |
var dbi lmdb.DBI |
| 366 |
err := env.Update(func(txn *lmdb.Txn) (err error) { |
| 367 |
dbi, err = txn.OpenDBI("exampledb", lmdb.Create) |
| 368 |
// the transaction will be commited if the database was successfully |
| 369 |
// opened/created. |
| 370 |
return err |
| 371 |
}) |
| 372 |
if err != nil { |
| 373 |
// ... |
| 374 |
} |
| 375 |
|
| 376 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 377 |
return txn.Put(dbi, []byte("k"), []byte("v"), 0) |
| 378 |
}) |
| 379 |
if err != nil { |
| 380 |
// ... |
| 381 |
} |
| 382 |
|
| 383 |
err = env.View(func(txn *lmdb.Txn) (err error) { |
| 384 |
v, err := txn.Get(dbi, []byte("k")) |
| 385 |
if err != nil { |
| 386 |
return err |
| 387 |
} |
| 388 |
fmt.Println(string(v)) |
| 389 |
return nil |
| 390 |
}) |
| 391 |
if err != nil { |
| 392 |
// ... |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
// This example demonstrates how to iterate a database opened with the DupSort |
| 397 |
// flag and get the number of values present for each distinct key. |
| 398 |
func ExampleCursor_Count() { |
| 399 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 400 |
cur, err := txn.OpenCursor(dbi) |
| 401 |
if err != nil { |
| 402 |
return err |
| 403 |
} |
| 404 |
defer cur.Close() |
| 405 |
|
| 406 |
for { |
| 407 |
k, _, err := cur.Get(nil, nil, lmdb.NextNoDup) |
| 408 |
if lmdb.IsNotFound(err) { |
| 409 |
return nil |
| 410 |
} |
| 411 |
if err != nil { |
| 412 |
return err |
| 413 |
} |
| 414 |
|
| 415 |
numdup, err := cur.Count() |
| 416 |
if err != nil { |
| 417 |
return err |
| 418 |
} |
| 419 |
fmt.Printf("%d values for key %q", numdup, k) |
| 420 |
} |
| 421 |
}) |
| 422 |
} |
| 423 |
|
| 424 |
// This simple example shows how to iterate a database. The Next flag may be |
| 425 |
// used without an initial call passing the First flag. |
| 426 |
func ExampleCursor_Get() { |
| 427 |
err = env.View(func(txn *lmdb.Txn) (err error) { |
| 428 |
cur, err := txn.OpenCursor(dbi) |
| 429 |
if err != nil { |
| 430 |
return err |
| 431 |
} |
| 432 |
defer cur.Close() |
| 433 |
|
| 434 |
for { |
| 435 |
k, v, err := cur.Get(nil, nil, lmdb.Next) |
| 436 |
if lmdb.IsNotFound(err) { |
| 437 |
return nil |
| 438 |
} |
| 439 |
if err != nil { |
| 440 |
return err |
| 441 |
} |
| 442 |
|
| 443 |
fmt.Printf("%s %s\n", k, v) |
| 444 |
} |
| 445 |
}) |
| 446 |
} |
| 447 |
|
| 448 |
// This simple example shows how to iterate a database in reverse. As when |
| 449 |
// passing the Next flag, the Prev flag may be used without an initial call |
| 450 |
// using Last. |
| 451 |
func ExampleCursor_Get_reverse() { |
| 452 |
err = env.View(func(txn *lmdb.Txn) (err error) { |
| 453 |
cur, err := txn.OpenCursor(dbi) |
| 454 |
if err != nil { |
| 455 |
return err |
| 456 |
} |
| 457 |
defer cur.Close() |
| 458 |
|
| 459 |
for { |
| 460 |
k, v, err := cur.Get(nil, nil, lmdb.Prev) |
| 461 |
if lmdb.IsNotFound(err) { |
| 462 |
return nil |
| 463 |
} |
| 464 |
if err != nil { |
| 465 |
return err |
| 466 |
} |
| 467 |
|
| 468 |
fmt.Printf("%s %s\n", k, v) |
| 469 |
} |
| 470 |
}) |
| 471 |
} |
| 472 |
|
| 473 |
// This example shows how duplicates can be processed using LMDB. It is |
| 474 |
// possible to iterate all key-value pairs (including duplicate key values) by |
| 475 |
// passing Next. But if special handling of duplicates is needed it may be |
| 476 |
// beneficial to use NextNoDup or NextDup. |
| 477 |
func ExampleCursor_Get_dupSort() { |
| 478 |
err = env.View(func(txn *lmdb.Txn) (err error) { |
| 479 |
cur, err := txn.OpenCursor(dbi) |
| 480 |
if err != nil { |
| 481 |
return err |
| 482 |
} |
| 483 |
|
| 484 |
for { |
| 485 |
k, v, err := cur.Get(nil, nil, lmdb.NextNoDup) |
| 486 |
if lmdb.IsNotFound(err) { |
| 487 |
// the database was exausted |
| 488 |
return nil |
| 489 |
} else if err != nil { |
| 490 |
return err |
| 491 |
} |
| 492 |
|
| 493 |
// process duplicates |
| 494 |
var dups [][]byte |
| 495 |
for { |
| 496 |
dups = append(dups, v) |
| 497 |
|
| 498 |
_, v, err = cur.Get(nil, nil, lmdb.NextDup) |
| 499 |
if lmdb.IsNotFound(err) { |
| 500 |
break |
| 501 |
} else if err != nil { |
| 502 |
return err |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
log.Printf("%q %q", k, dups) |
| 507 |
} |
| 508 |
}) |
| 509 |
} |
| 510 |
|
| 511 |
// This simple example shows how to iterate a database opened with the |
| 512 |
// DupSort|DupSort flags. It is not necessary to use the GetMultiple flag |
| 513 |
// before passing the NextMultiple flag. |
| 514 |
func ExampleCursor_Get_dupFixed() { |
| 515 |
err = env.View(func(txn *lmdb.Txn) (err error) { |
| 516 |
cur, err := txn.OpenCursor(dbi) |
| 517 |
if err != nil { |
| 518 |
return err |
| 519 |
} |
| 520 |
defer cur.Close() |
| 521 |
|
| 522 |
for { |
| 523 |
k, first, err := cur.Get(nil, nil, lmdb.NextNoDup) |
| 524 |
if lmdb.IsNotFound(err) { |
| 525 |
return nil |
| 526 |
} |
| 527 |
if err != nil { |
| 528 |
return err |
| 529 |
} |
| 530 |
|
| 531 |
stride := len(first) |
| 532 |
|
| 533 |
for { |
| 534 |
_, v, err := cur.Get(nil, nil, lmdb.NextMultiple) |
| 535 |
if lmdb.IsNotFound(err) { |
| 536 |
break |
| 537 |
} |
| 538 |
if err != nil { |
| 539 |
return err |
| 540 |
} |
| 541 |
|
| 542 |
multi := lmdb.WrapMulti(v, stride) |
| 543 |
for i := 0; i < multi.Len(); i++ { |
| 544 |
fmt.Printf("%s %s\n", k, multi.Val(i)) |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
}) |
| 549 |
} |
| 550 |
|
| 551 |
// This example shows a trivial case using Renew to service read requests on a |
| 552 |
// database. Close must be called when the cursor will no longer be renewed. |
| 553 |
// Before using Renew benchmark your application to understand its benefits. |
| 554 |
func ExampleCursor_Renew() { |
| 555 |
var cur *lmdb.Cursor |
| 556 |
err = env.View(func(txn *lmdb.Txn) (err error) { |
| 557 |
dbi, err := txn.OpenRoot(0) |
| 558 |
if err != nil { |
| 559 |
return err |
| 560 |
} |
| 561 |
cur, err = txn.OpenCursor(dbi) |
| 562 |
return err |
| 563 |
}) |
| 564 |
if err != nil { |
| 565 |
panic(err) |
| 566 |
} |
| 567 |
|
| 568 |
keys := make(chan []byte) |
| 569 |
go func() { |
| 570 |
|
| 571 |
// Close must called when the cursor is no longer needed. |
| 572 |
defer cur.Close() |
| 573 |
|
| 574 |
for key := range keys { |
| 575 |
err := env.View(func(txn *lmdb.Txn) (err error) { |
| 576 |
err = cur.Renew(txn) |
| 577 |
if err != nil { |
| 578 |
return err |
| 579 |
} |
| 580 |
|
| 581 |
// retrieve the number of items in the database with the given |
| 582 |
// key (DupSort). |
| 583 |
count := uint64(0) |
| 584 |
_, _, err = cur.Get(key, nil, lmdb.SetKey) |
| 585 |
if lmdb.IsNotFound(err) { |
| 586 |
err = nil |
| 587 |
} else if err == nil { |
| 588 |
count, err = cur.Count() |
| 589 |
} |
| 590 |
if err != nil { |
| 591 |
return err |
| 592 |
} |
| 593 |
|
| 594 |
log.Printf("%d %q", count, key) |
| 595 |
|
| 596 |
return nil |
| 597 |
}) |
| 598 |
if err != nil { |
| 599 |
panic(err) |
| 600 |
} |
| 601 |
} |
| 602 |
}() |
| 603 |
|
| 604 |
// ... |
| 605 |
} |
| 606 |
|
| 607 |
// This example shows how to write a page of contiguous, fixed-size values to a |
| 608 |
// database opened with DupSort|DupFixed. It doesn't matter if the values are |
| 609 |
// sorted. Values will be stored in sorted order. |
| 610 |
func ExampleCursor_PutMulti() { |
| 611 |
key := []byte("k") |
| 612 |
items := [][]byte{ |
| 613 |
[]byte("v0"), |
| 614 |
[]byte("v2"), |
| 615 |
[]byte("v1"), |
| 616 |
} |
| 617 |
page := bytes.Join(items, nil) |
| 618 |
stride := 2 |
| 619 |
|
| 620 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 621 |
cur, err := txn.OpenCursor(dbi) |
| 622 |
if err != nil { |
| 623 |
return err |
| 624 |
} |
| 625 |
defer cur.Close() |
| 626 |
|
| 627 |
return cur.PutMulti(key, page, stride, 0) |
| 628 |
}) |
| 629 |
} |
| 630 |
|
| 631 |
// This example demonstrates how to delete all elements in a database with a |
| 632 |
// key less than a given value (an RFC3339 timestamp in this case). |
| 633 |
func ExampleCursor_Del() { |
| 634 |
before := []byte("2014-05-06T03:04:02Z") |
| 635 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 636 |
cur, err := txn.OpenCursor(dbi) |
| 637 |
if err != nil { |
| 638 |
return err |
| 639 |
} |
| 640 |
defer cur.Close() |
| 641 |
|
| 642 |
for { |
| 643 |
k, _, err := cur.Get(nil, nil, lmdb.Next) |
| 644 |
if lmdb.IsNotFound(err) { |
| 645 |
return nil |
| 646 |
} |
| 647 |
if err != nil { |
| 648 |
return err |
| 649 |
} |
| 650 |
|
| 651 |
if bytes.Compare(k, before) != -1 { |
| 652 |
return nil |
| 653 |
} |
| 654 |
|
| 655 |
err = cur.Del(0) |
| 656 |
if err != nil { |
| 657 |
return err |
| 658 |
} |
| 659 |
} |
| 660 |
}) |
| 661 |
} |
| 662 |
|
| 663 |
// If the database being opened is known to exist then no flags need to be |
| 664 |
// passed. |
| 665 |
func ExampleTxn_OpenDBI() { |
| 666 |
// DBI handles can be saved after their opening transaction has committed |
| 667 |
// and may be reused as long as the environment remains open. |
| 668 |
var dbi lmdb.DBI |
| 669 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 670 |
dbi, err = txn.OpenDBI("dbfound", 0) |
| 671 |
return err |
| 672 |
}) |
| 673 |
if err != nil { |
| 674 |
panic(err) |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
// When Create is passed to Txn.OpenDBI() the database will be created if it |
| 679 |
// didn't already exist. An error will be returned if the name is occupied by |
| 680 |
// data written by Txn./Cursor.Put(). |
| 681 |
func ExampleTxn_OpenDBI_create() { |
| 682 |
var dbi lmdb.DBI |
| 683 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 684 |
dbi, err = txn.OpenDBI("dbnew", lmdb.Create) |
| 685 |
return err |
| 686 |
}) |
| 687 |
if err != nil { |
| 688 |
panic(err) |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
// When a non-existent database is opened without the Create flag the errno is |
| 693 |
// NotFound. If an application needs to handle this case the function |
| 694 |
// IsNotFound() will test an error for this condition. |
| 695 |
func ExampleTxn_OpenDBI_notFound() { |
| 696 |
var dbi lmdb.DBI |
| 697 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 698 |
dbi, err = txn.OpenDBI("dbnotfound", 0) |
| 699 |
return err |
| 700 |
}) |
| 701 |
log.Print(err) // mdb_dbi_open: MDB_NOTFOUND: No matching key/data pair found |
| 702 |
} |
| 703 |
|
| 704 |
// When the number of open named databases in an environment reaches the value |
| 705 |
// specified by Env.SetMaxDBs() attempts to open additional databases will |
| 706 |
// return an error with errno DBsFull. If an application needs to handle this |
| 707 |
// case then the function IsError() can test an error for this condition. |
| 708 |
func ExampleTxn_OpenDBI_dBsFull() { |
| 709 |
var dbi lmdb.DBI |
| 710 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 711 |
dbi, err = txn.OpenDBI("dbnotexist", 0) |
| 712 |
return err |
| 713 |
}) |
| 714 |
log.Print(err) // mdb_dbi_open: MDB_DBS_FULL: Environment maxdbs limit reached |
| 715 |
} |
| 716 |
|
| 717 |
// Txn.OpenRoot does not need to be called with the Create flag. And |
| 718 |
// Txn.OpenRoot, unlike Txn.OpenDBI, will never produce the error DBsFull. |
| 719 |
func ExampleTxn_OpenRoot() { |
| 720 |
var dbi lmdb.DBI |
| 721 |
err = env.Update(func(txn *lmdb.Txn) (err error) { |
| 722 |
dbi, err = txn.OpenRoot(0) |
| 723 |
return err |
| 724 |
}) |
| 725 |
if err != nil { |
| 726 |
panic(err) |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
// Txn.OpenRoot may also be called without flags inside View transactions |
| 731 |
// before being openend in an Update transaction. |
| 732 |
func ExampleTxn_OpenRoot_view() { |
| 733 |
err = env.View(func(txn *lmdb.Txn) (err error) { |
| 734 |
dbi, err := txn.OpenRoot(0) |
| 735 |
if err != nil { |
| 736 |
return err |
| 737 |
} |
| 738 |
cur, err := txn.OpenCursor(dbi) |
| 739 |
if err != nil { |
| 740 |
return err |
| 741 |
} |
| 742 |
for { |
| 743 |
k, v, err := cur.Get(nil, nil, lmdb.Next) |
| 744 |
if lmdb.IsNotFound(err) { |
| 745 |
return nil |
| 746 |
} |
| 747 |
if err != nil { |
| 748 |
return err |
| 749 |
} |
| 750 |
|
| 751 |
log.Printf("%s=%s\n", k, v) |
| 752 |
// ... |
| 753 |
} |
| 754 |
}) |
| 755 |
if err != nil { |
| 756 |
// ... |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
// This example shows how to properly handle data retrieved from the database |
| 761 |
// and applies to Txn.Get() as well as Cursor.Get(). It is important to handle |
| 762 |
// data retreival carefully to make sure the application does not retain |
| 763 |
// pointers to memory pages which may be reclaimed by LMDB after the |
| 764 |
// transaction terminates. Typically an application would define helper |
| 765 |
// functions/methods to conveniently handle data safe retrieval. |
| 766 |
func ExampleTxn_Get() { |
| 767 |
// variables to hold data extracted from the database |
| 768 |
var point struct{ X, Y int } |
| 769 |
var str string |
| 770 |
var p1, p2 []byte |
| 771 |
|
| 772 |
// extract data from an example environment/database. it is critical for application |
| 773 |
// code to handle errors but that is omitted here to save space. |
| 774 |
EnvEx.View(func(txn *lmdb.Txn) (err error) { |
| 775 |
// OK |
| 776 |
// A []byte to string conversion will always copy the data |
| 777 |
v, _ := txn.Get(DBIEx, []byte("mykey")) |
| 778 |
str = string(v) |
| 779 |
|
| 780 |
// OK |
| 781 |
// If []byte is the desired data type then an explicit copy is required |
| 782 |
// for safe access after the transaction returns. |
| 783 |
v, _ = txn.Get(DBIEx, []byte("mykey")) |
| 784 |
p1 = make([]byte, len(v)) |
| 785 |
copy(p1, v) |
| 786 |
|
| 787 |
// OK |
| 788 |
// The data does not need be copied because it is parsed while txn is |
| 789 |
// open. |
| 790 |
v, _ = txn.Get(DBIEx, []byte("mykey")) |
| 791 |
_ = json.Unmarshal(v, &point) |
| 792 |
|
| 793 |
// BAD |
| 794 |
// Assigning the result directly to p2 leaves its pointer volatile |
| 795 |
// after the transaction completes which can result in unpredictable |
| 796 |
// behavior. |
| 797 |
p2, _ = txn.Get(DBIEx, []byte("mykey")) |
| 798 |
|
| 799 |
return nil |
| 800 |
}) |
| 801 |
} |
| 802 |
|
| 803 |
// This example demonstrates the use of PutReserve to store a string value in |
| 804 |
// the root database. This may be faster than Put alone for large values |
| 805 |
// because a string to []byte conversion is not required. |
| 806 |
func ExampleTxn_PutReserve() { |
| 807 |
EnvEx.Update(func(txn *lmdb.Txn) (err error) { |
| 808 |
dbroot, err := txn.OpenRoot(0) |
| 809 |
if err != nil { |
| 810 |
return err |
| 811 |
} |
| 812 |
|
| 813 |
valstr := "value" |
| 814 |
p, err := txn.PutReserve(dbroot, []byte("key"), len(valstr), 0) |
| 815 |
if err != nil { |
| 816 |
return err |
| 817 |
} |
| 818 |
copy(p, valstr) |
| 819 |
|
| 820 |
return nil |
| 821 |
}) |
| 822 |
} |