| 1 | /* |
| 2 | Package lmdb provides bindings to the lmdb C API. The package bindings are |
| 3 | fairly low level and are designed to provide a minimal interface that prevents |
| 4 | misuse to a reasonable extent. When in doubt refer to the C documentation as a |
| 5 | reference. |
| 6 | |
| 7 | http://www.lmdb.tech/doc/ |
| 8 | http://www.lmdb.tech/doc/starting.html |
| 9 | http://www.lmdb.tech/doc/modules.html |
| 10 | |
| 11 | |
| 12 | Environment |
| 13 | |
| 14 | An LMDB environment holds named databases (key-value stores). An environment |
| 15 | is represented as one file on the filesystem (though often a corresponding lock |
| 16 | file exists). |
| 17 | |
| 18 | LMDB recommends setting an environment's size as large as possible at the time |
| 19 | of creation. On filesystems that support sparse files this should not |
| 20 | adversely affect disk usage. Resizing an environment is possible but must be |
| 21 | handled with care when concurrent access is involved. |
| 22 | |
| 23 | Note that the package lmdb forces all Env objects to be opened with the NoTLS |
| 24 | (MDB_NOTLS) flag. Without this flag LMDB would not be practically usable in Go |
| 25 | (in the author's opinion). However, even for environments opened with this |
| 26 | flag there are caveats regarding how transactions are used (see Caveats below). |
| 27 | |
| 28 | |
| 29 | Databases |
| 30 | |
| 31 | A database in an LMDB environment is an ordered key-value store that holds |
| 32 | arbitrary binary data. Typically the keys are unique but duplicate keys may be |
| 33 | allowed (DupSort), in which case the values for each duplicate key are ordered. |
| 34 | |
| 35 | A single LMDB environment can have multiple named databases. But there is also |
| 36 | a 'root' (unnamed) database that can be used to store data. Use caution |
| 37 | storing data in the root database when named databases are in use. The root |
| 38 | database serves as an index for named databases. |
| 39 | |
| 40 | A database is referenced by an opaque handle known as its DBI which must be |
| 41 | opened inside a transaction with the OpenDBI or OpenRoot methods. DBIs may be |
| 42 | closed but it is not required. Typically, applications acquire handles for all |
| 43 | their databases immediately after opening an environment and retain them for |
| 44 | the lifetime of the process. |
| 45 | |
| 46 | |
| 47 | Transactions |
| 48 | |
| 49 | View (readonly) transactions in LMDB operate on a snapshot of the database at |
| 50 | the time the transaction began. The number of simultaneously active view |
| 51 | transactions is bounded and configured when the environment is initialized. |
| 52 | |
| 53 | Update (read-write) transactions are serialized in LMDB. Attempts to create |
| 54 | update transactions block until a lock may be obtained. Update transactions |
| 55 | can create subtransactions which may be rolled back independently from their |
| 56 | parent. |
| 57 | |
| 58 | The lmdb package supplies managed and unmanaged transactions. Managed |
| 59 | transactions do not require explicit calling of Abort/Commit and are provided |
| 60 | through the Env methods Update, View, and RunTxn. The BeginTxn method on Env |
| 61 | creates an unmanaged transaction but its use is not advised in most |
| 62 | applications. |
| 63 | |
| 64 | To provide ACID guarantees, a readonly transaction must acquire a "lock" in the |
| 65 | LMDB environment to ensure that data it reads is consistent over the course of |
| 66 | the transaction's lifetime, and that updates happening concurrently will not be |
| 67 | seen. If a reader does not release its lock then stale data, which has been |
| 68 | overwritten by later transactions, cannot be reclaimed by LMDB -- resulting in |
| 69 | a rapid increase in file size. |
| 70 | |
| 71 | Long-running read transactions may cause increase an applications storage |
| 72 | requirements, depending on the application write workload. But, typically the |
| 73 | complete failure of an application to terminate a read transactions will result |
| 74 | in continual increase file size to the point where the storage volume becomes |
| 75 | full or a quota has been reached. |
| 76 | |
| 77 | There are steps an application may take to greatly reduce the possibility of |
| 78 | unterminated read transactions. The first safety measure is to avoid the use |
| 79 | of Env.BeginTxn, which creates unmanaged transactions, and always use Env.View |
| 80 | or Env.Update to create managed transactions that are (mostly) guaranteed to |
| 81 | terminate. If Env.BeginTxn must be used try to defer a call to the Txn's Abort |
| 82 | method (this is useful even for update transactions). |
| 83 | |
| 84 | txn, err := env.BeginTxn(nil, 0) |
| 85 | if err != nil { |
| 86 | // ... |
| 87 | } |
| 88 | defer txn.Abort() // Safe even if txn.Commit() is called later. |
| 89 | |
| 90 | Because application crashes and signals from the operation system may cause |
| 91 | unexpected termination of a readonly transaction before Txn.Abort may be called |
| 92 | it is also important that applications clear any readers held for dead OS |
| 93 | processes when they start. |
| 94 | |
| 95 | numStale, err := env.ReaderCheck() |
| 96 | if err != nil { |
| 97 | // ... |
| 98 | } |
| 99 | if numStale > 0 { |
| 100 | log.Printf("Released locks for %d dead readers", numStale) |
| 101 | } |
| 102 | |
| 103 | If an application gets accessed by multiple programs concurrently it is also a |
| 104 | good idea to periodically call Env.ReaderCheck during application execution. |
| 105 | However, note that Env.ReaderCheck cannot find readers opened by the |
| 106 | application itself which have since leaked. Because of this, the lmdb package |
| 107 | uses a finalizer to abort unreachable Txn objects. But of course, applications |
| 108 | must still be careful not to leak unterminated Txn objects in a way such that |
| 109 | they fail get garbage collected. |
| 110 | |
| 111 | |
| 112 | Caveats |
| 113 | |
| 114 | Write transactions (those created without the Readonly flag) must be created in |
| 115 | a goroutine that has been locked to its thread by calling the function |
| 116 | runtime.LockOSThread. Futhermore, all methods on such transactions must be |
| 117 | called from the goroutine which created them. This is a fundamental limitation |
| 118 | of LMDB even when using the NoTLS flag (which the package always uses). The |
| 119 | Env.Update method assists the programmer by calling runtime.LockOSThread |
| 120 | automatically but it cannot sufficiently abstract write transactions to make |
| 121 | them completely safe in Go. |
| 122 | |
| 123 | A goroutine must never create a write transaction if the application programmer |
| 124 | cannot determine whether the goroutine is locked to an OS thread. This is a |
| 125 | consequence of goroutine restrictions on write transactions and limitations in |
| 126 | the runtime's thread locking implementation. In such situations updates |
| 127 | desired by the goroutine in question must be proxied by a goroutine with a |
| 128 | known state (i.e. "locked" or "unlocked"). See the included examples for more |
| 129 | details about dealing with such situations. |
| 130 | */ |
| 131 | package lmdb |
| 132 | |
| 133 | /* |
| 134 | #cgo CFLAGS: -pthread -W -Wall -Wno-unused-parameter -Wno-format-extra-args -Wbad-function-cast -Wno-missing-field-initializers -O2 -g |
| 135 | #cgo linux,pwritev CFLAGS: -DMDB_USE_PWRITEV |
| 136 | |
| 137 | #include "lmdb.h" |
| 138 | */ |
| 139 | import "C" |
| 140 | |
| 141 | // Version return the major, minor, and patch version numbers of the LMDB C |
| 142 | // library and a string representation of the version. |
| 143 | // |
| 144 | // See mdb_version. |
| 145 | func Version() (major, minor, patch int, s string) { |
| 146 | var maj, min, pat C.int |
| 147 | verstr := C.mdb_version(&maj, &min, &pat) |
| 148 | return int(maj), int(min), int(pat), C.GoString(verstr) |
| 149 | } |
| 150 | |
| 151 | // VersionString returns a string representation of the LMDB C library version. |
| 152 | // |
| 153 | // See mdb_version. |
| 154 | func VersionString() string { |
| 155 | var maj, min, pat C.int |
| 156 | verstr := C.mdb_version(&maj, &min, &pat) |
| 157 | return C.GoString(verstr) |
| 158 | } |
| 159 | |
| 160 | func cbool(b bool) C.int { |
| 161 | if b { |
| 162 | return 1 |
| 163 | } |
| 164 | return 0 |
| 165 | } |