Giter VIP home page Giter VIP logo

swiftlmdb's Introduction

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.

swiftlmdb's People

Contributors

agisboye avatar eriktier avatar regexident avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.