| 1 | #lmdb-go [](releases) [](https://github.com/LMDB/lmdb/blob/mdb.RE/0.9/libraries/liblmdb/CHANGES) [](https://travis-ci.org/bmatsuo/lmdb-go) |
| 2 | |
| 3 | Go bindings to the OpenLDAP Lightning Memory-Mapped Database (LMDB). |
| 4 | |
| 5 | ## Packages |
| 6 | |
| 7 | Functionality is logically divided into several packages. Applications will |
| 8 | usually need to import **lmdb** but may import other packages on an as needed |
| 9 | basis. |
| 10 | |
| 11 | Packages in the `exp/` directory are not stable and may change without warning. |
| 12 | That said, they are generally usable if application dependencies are managed |
| 13 | and pinned by tag/commit. |
| 14 | |
| 15 | Developers concerned with package stability should consult the documentation. |
| 16 | |
| 17 | ####lmdb [](https://godoc.org/github.com/bmatsuo/lmdb-go/lmdb) [](#user-content-versioning-and-stability) |
| 18 | |
| 19 | ```go |
| 20 | import "github.com/bmatsuo/lmdb-go/lmdb" |
| 21 | ``` |
| 22 | |
| 23 | Core bindings allowing low-level access to LMDB. |
| 24 | |
| 25 | ####lmdbscan [](https://godoc.org/github.com/bmatsuo/lmdb-go/lmdbscan) [](#user-content-versioning-and-stability) |
| 26 | |
| 27 | ```go |
| 28 | import "github.com/bmatsuo/lmdb-go/lmdbscan" |
| 29 | ``` |
| 30 | |
| 31 | A utility package for scanning database ranges. The API is inspired by |
| 32 | [bufio.Scanner](https://godoc.org/bufio#Scanner) and the python cursor |
| 33 | [implementation](https://lmdb.readthedocs.org/en/release/#cursor-class). |
| 34 | |
| 35 | ####exp/lmdbpool [](https://godoc.org/github.com/bmatsuo/lmdb-go/exp/lmdbpool) [](#user-content-versioning-and-stability) |
| 36 | |
| 37 | |
| 38 | ```go |
| 39 | import "github.com/bmatsuo/lmdb-go/exp/lmdbpool" |
| 40 | ``` |
| 41 | |
| 42 | A utility package which facilitates reuse of lmdb.Txn objects using a |
| 43 | sync.Pool. Naively storing lmdb.Txn objects in sync.Pool can be troublesome. |
| 44 | And the lmdbpool.TxnPool type has been defined as a complete pooling solution |
| 45 | and as reference for applications attempting to write their own pooling |
| 46 | implementation. |
| 47 | |
| 48 | The lmdbpool package is relatively new. But it has a lot of potential utility. |
| 49 | And once the lmdbpool API has been ironed out, and the implementation hardened |
| 50 | through use by real applications it can be integrated directly into the lmdb |
| 51 | package for more transparent integration. Please test this package and provide |
| 52 | feedback to speed this process up. |
| 53 | |
| 54 | ####exp/lmdbsync [](https://godoc.org/github.com/bmatsuo/lmdb-go/exp/lmdbsync) [](#user-content-versioning-and-stability) |
| 55 | |
| 56 | |
| 57 | ```go |
| 58 | import "github.com/bmatsuo/lmdb-go/exp/lmdbsync" |
| 59 | ``` |
| 60 | |
| 61 | An experimental utility package that provides synchronization necessary to |
| 62 | change an environment's map size after initialization. The package provides |
| 63 | error handlers to automatically manage database size and retry failed |
| 64 | transactions. |
| 65 | |
| 66 | The **lmdbsync** package is usable but the implementation of Handlers are |
| 67 | unstable and may change in incompatible ways without notice. The use cases of |
| 68 | dynamic map sizes and multiprocessing are niche and the package requires much |
| 69 | more development driven by practical feedback before the Handler API and the |
| 70 | provided implementations can be considered stable. |
| 71 | |
| 72 | ## Key Features |
| 73 | |
| 74 | ###Idiomatic API |
| 75 | |
| 76 | API inspired by [BoltDB](https://github.com/boltdb/bolt) with automatic |
| 77 | commit/rollback of transactions. The goal of lmdb-go is to provide idiomatic |
| 78 | database interactions without compromising the flexibility of the C API. |
| 79 | |
| 80 | **NOTE:** While the lmdb package tries hard to make LMDB as easy to use as |
| 81 | possible there are compromises, gotchas, and caveats that application |
| 82 | developers must be aware of when relying on LMDB to store their data. All |
| 83 | users are encouraged to fully read the |
| 84 | [documentation](https://godoc.org/github.com/bmatsuo/lmdb-go/lmdb) so they are |
| 85 | aware of these caveats. |
| 86 | |
| 87 | Where the lmdb package and its implementation decisions do not meet the needs |
| 88 | of application developers in terms of safety or operational use the lmdbsync |
| 89 | package has been designed to wrap lmdb and safely fill in additional |
| 90 | functionality. Consult the |
| 91 | [documentation](https://godoc.org/github.com/bmatsuo/lmdb-go/exp/lmdbsync) for |
| 92 | more information about the lmdbsync package. |
| 93 | |
| 94 | ###API coverage |
| 95 | |
| 96 | The lmdb-go project aims for complete coverage of the LMDB C API (within |
| 97 | reason). Some notable features and optimizations that are supported: |
| 98 | |
| 99 | - Idiomatic subtransactions ("sub-updates") that allow the batching of updates. |
| 100 | |
| 101 | - Batch IO on databases utilizing the `MDB_DUPSORT` and `MDB_DUPFIXED` flags. |
| 102 | |
| 103 | - Reserved writes than can save in memory copies converting/buffering into |
| 104 | `[]byte`. |
| 105 | |
| 106 | For tracking purposes a list of unsupported features is kept in an |
| 107 | [issue](https://github.com/bmatsuo/lmdb-go/issues/1). |
| 108 | |
| 109 | ###Zero-copy reads |
| 110 | |
| 111 | Applications with high performance requirements can opt-in to fast, zero-copy |
| 112 | reads at the cost of runtime safety. Zero-copy behavior is specified at the |
| 113 | transaction level to reduce instrumentation overhead. |
| 114 | |
| 115 | ``` |
| 116 | err := lmdb.View(func(txn *lmdb.Txn) error { |
| 117 | // RawRead enables zero-copy behavior with some serious caveats. |
| 118 | // Read the documentation carefully before using. |
| 119 | txn.RawRead = true |
| 120 | |
| 121 | val, err := txn.Get(dbi, []byte("largevalue"), 0) |
| 122 | // ... |
| 123 | }) |
| 124 | ``` |
| 125 | |
| 126 | ###Documentation |
| 127 | |
| 128 | Comprehensive documentation and examples are provided to demonstrate safe usage |
| 129 | of lmdb. In addition to [godoc](https://godoc.org/github.com/bmatsuo/lmdb-go) |
| 130 | documentation, implementations of the standand LMDB commands (`mdb_stat`, etc) |
| 131 | can be found in the [cmd/](cmd/) directory and some simple experimental |
| 132 | commands can be found in the [exp/cmd/](exp/cmd) directory. Aside from |
| 133 | providing minor utility these programs are provided as examples of lmdb in |
| 134 | practice. |
| 135 | |
| 136 | ##LMDB compared to BoltDB |
| 137 | |
| 138 | BoltDB is a quality database with a design similar to LMDB. Both store |
| 139 | key-value data in a file and provide ACID transactions. So there are often |
| 140 | questions of why to use one database or the other. |
| 141 | |
| 142 | ###Advantages of BoltDB |
| 143 | |
| 144 | - Nested databases allow for hierarchical data organization. |
| 145 | |
| 146 | - Far more databases can be accessed concurrently. |
| 147 | |
| 148 | - Operating systems that do not support sparse files do not use up excessive |
| 149 | space due to a large pre-allocation of file space. The exp/lmdbsync package |
| 150 | is intended to resolve this problem with LMDB but it is not ready. |
| 151 | |
| 152 | - As a pure Go package bolt can be easily cross-compiled using the `go` |
| 153 | toolchain and `GOOS`/`GOARCH` variables. |
| 154 | |
| 155 | - Its simpler design and implementation in pure Go mean it is free of many |
| 156 | caveats and gotchas which are present using the lmdb package. For more |
| 157 | information about caveats with the lmdb package, consult its |
| 158 | [documentation](https://godoc.org/github.com/bmatsuo/lmdb-go/lmdb). |
| 159 | |
| 160 | ###Advantages of LMDB |
| 161 | |
| 162 | - Keys can contain multiple values using the DupSort flag. |
| 163 | |
| 164 | - Updates can have sub-updates for atomic batching of changes. |
| 165 | |
| 166 | - Databases typically remain open for the application lifetime. This limits |
| 167 | the number of concurrently accessible databases. But, this minimizes the |
| 168 | overhead of database accesses and typically produces cleaner code than |
| 169 | an equivalent BoltDB implementation. |
| 170 | |
| 171 | - Significantly faster than BoltDB. The raw speed of LMDB easily surpasses |
| 172 | BoltDB. Additionally, LMDB provides optimizations ranging from safe, |
| 173 | feature-specific optimizations to generally unsafe, extremely situational |
| 174 | ones. Applications are free to enable any optimizations that fit their data, |
| 175 | access, and reliability models. |
| 176 | |
| 177 | - LMDB allows multiple applications to access a database simultaneously. |
| 178 | Updates from concurrent processes are synchronized using a database lock |
| 179 | file. |
| 180 | |
| 181 | - As a C library, applications in any language can interact with LMDB |
| 182 | databases. Mission critical Go applications can use a database while Python |
| 183 | scripts perform analysis on the side. |
| 184 | |
| 185 | ##Build |
| 186 | |
| 187 | There is no dependency on shared libraries. So most users can simply install |
| 188 | using `go get`. |
| 189 | |
| 190 | `go get github.com/bmatsuo/lmdb-go/lmdb` |
| 191 | |
| 192 | On FreeBSD 10, you must explicitly set `CC` (otherwise it will fail with a |
| 193 | cryptic error), for example: |
| 194 | |
| 195 | CC=clang go test -v ./... |
| 196 | |
| 197 | Building commands and running tests can be done with `go` or with `make` |
| 198 | |
| 199 | make bin |
| 200 | make test |
| 201 | make check |
| 202 | make all |
| 203 | |
| 204 | On Linux, you can specify the `pwritev` build tag to reduce the number of syscalls |
| 205 | required when committing a transaction. In your own package you can then do |
| 206 | |
| 207 | go build -tags pwritev . |
| 208 | |
| 209 | to enable the optimisation. |
| 210 | |
| 211 | ##Documentation |
| 212 | |
| 213 | ###Go doc |
| 214 | |
| 215 | The `go doc` documentation available on |
| 216 | [godoc.org](https://godoc.org/github.com/bmatsuo/lmdb-go) is the primary source |
| 217 | of developer documentation for lmdb-go. It provides an overview of the API |
| 218 | with a lot of usage examples. Where necessary the documentation points out |
| 219 | differences between the semantics of methods and their C counterparts. |
| 220 | |
| 221 | ###LMDB |
| 222 | |
| 223 | The LMDB [homepage](http://symas.com/mdb/) and mailing list |
| 224 | ([archives](http://www.openldap.org/lists/openldap-technical/)) are the |
| 225 | official source of documentation regarding low-level LMDB operation and |
| 226 | internals. |
| 227 | |
| 228 | Along with an API reference LMDB provides a high-level |
| 229 | [summary](http://symas.com/mdb/doc/starting.html) of the library. While |
| 230 | lmdb-go abstracts many of the thread and transaction details by default the |
| 231 | rest of the guide is still useful to compare with `go doc`. |
| 232 | |
| 233 | ###Versioning and Stability |
| 234 | |
| 235 | The lmdb-go project makes regular releases with IDs `X.Y.Z`. All packages |
| 236 | outside of the `exp/` directory are considered stable and adhere to the |
| 237 | guidelines of [semantic versioning](http://semver.org/). |
| 238 | |
| 239 | Experimental packages (those packages in `exp/`) are not required to adhere to |
| 240 | semantic versioning. However packages specifically declared to merely be |
| 241 | "unstable" can be relied on more for long term use with less concern. |
| 242 | |
| 243 | The API of an unstable package may change in subtle ways between minor release |
| 244 | versions. But deprecations will be indicated at least one release in advance |
| 245 | and all functionality will remain available through some method. |
| 246 | |
| 247 | ##License |
| 248 | |
| 249 | Except where otherwise noted files in the lmdb-go project are licensed under |
| 250 | the BSD 3-clause open source license. |
| 251 | |
| 252 | The LMDB C source is licensed under the OpenLDAP Public License. |
| 253 | |
| 254 | ##Links |
| 255 | |
| 256 | ####[github.com/bmatsuo/raft-mdb](https://github.com/bmatsuo/raft-mdb) ([godoc](https://godoc.org/github.com/bmatsuo/raft-mdb)) |
| 257 | |
| 258 | An experimental backend for |
| 259 | [github.com/hashicorp/raft](https://github.com/hashicorp/raft) forked from |
| 260 | [github.com/hashicorp/raft-mdb](https://github.com/hashicorp/raft-mdb). |
| 261 | |
| 262 | ####[github.com/bmatsuo/cayley/graph/lmdb](https://github.com/bmatsuo/cayley/tree/master/graph/lmdb) ([godoc](https://godoc.org/github.com/bmatsuo/cayley/graph/lmdb)) |
| 263 | |
| 264 | Experimental backend quad-store for |
| 265 | [github.com/google/cayley](https://github.com/google/cayley) based off of the |
| 266 | BoltDB |
| 267 | [implementation](https://github.com/google/cayley/tree/master/graph/bolt). |