SwiftLMDB

2.1.6

Simple & fast key value store in Swift
agisboye/SwiftLMDB

What's New

2024-01-29T19:34:26Z
  • Updates LMDB to 0.9.29
  • Bump Swift tools version to 5.7

SwiftLMDB

SwiftLMDB is an opinionated wrapper around the LMDB key/value database.

The wrapper abstracts away the C API and some of its concepts. Instead you get a clean Swift API that makes it easy to store arbitrary values in a fast and embedded database.

The only requirement is that keys and values can be converted to Data. To assert this, keys and values must conform to the DataConvertible protocol. Fundamental Swift value types are supported out of the box.

Features

  • Small and lightweight
  • Fast
  • Unit tested
  • Xcode documentation
  • Cross platform

SwiftLMDB has been tested on iOS and macOS, however it should also run on Linux. Feel free to test it out.

Requirements

  • iOS 8.0+ or macOS 10.10+
  • Swift 5.7+

Installation

Swift Package Manager

Add SwiftLMDB as a dependency in your Package.swift file.

dependencies: [
.package(url: "https://github.com/agisboye/SwiftLMDB.git", from: "2.0.0")
]

Carthage

To integrate LMDB into your Xcode project using Carthage, specify it in your Cartfile:

github "agisboye/SwiftLMDB"

Run carthage update to build the framework and drag SwiftLMDB.framework into your Xcode project.

Usage

Start by importing the module.

import SwiftLMDB

Creating a database

Databases are contained within an environment. An environment may contain multiple databases, each identified by their name.

let environment: Environment
let database: Database

do {
    // The folder in which the environment is opened must already exist.
    try FileManager.default.createDirectory(at: envURL, withIntermediateDirectories: true, attributes: nil)

    environment = try Environment(path: envURL.path, flags: [], maxDBs: 32)
    database = try environment.openDatabase(named: "db1", flags: [.create])

} catch {
    print(error)
}

Put a value

Any value conforming to DataConvertible can be inserted with any key conforming to DataConvertible.

try database.put(value: "Hello world!", forKey: "key1")

Get a value

When you need to get back a value from the database, you specify the expected type (the type of the value you put in) and the key. This returns an optional of the type that you specify.

if let value = try database.get(type: String.self, forKey: "key1") { // String?
    // String
}

Delete a value

try database.deleteValue(forKey: "key1")

Check if a key exists

try database.exists(key: "key1") // Bool

Empty

Removes all entries from the database.

try database.empty()

database.count == 0 // true

Drop

Removes all entries from the database and deletes the database from the environment. The database can no longer be used after calling this method and should be discarded.

try database.drop()

Cursor

Database conforms to the Sequence protocol, so you can iterate over all (key, value) pairs in the database.

for (k, v) in database {
    let key = String(data: k)!
    let value = String(data: v)!
}

Contributing

Contributions are very welcome. Open an issue or submit a pull request.

License

SwiftLMDB is available under the MIT license. See the LICENSE file for more info. LMDB is licensen under the OpenLDAP Public License.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Wed Nov 20 2024 22:56:27 GMT-1000 (Hawaii-Aleutian Standard Time)