Giter VIP home page Giter VIP logo

lmdb-zig's Introduction

LMDB Zig bindings

This repository creates an idiomatic Zig API on top of LMDB. At the moment the bindings are incomplete, use at your own risk.

Using with zigmod

Add the following to dependencies in your zig.mod:

- type: git
  path: https://github.com/leroycep/lmdb-zig.git

Then run:

zigmod fetch

Check out the zigmod repository for more detail.

Getting started

Make sure to check out the official LMDB Getting Started documentation too!

First, you need to create a environment.

const std = @import("std");
const lmdb = @import("lmdb");

pub fn main() !void {
    var env = try lmdb.Environment.open("hellodb", .{ .mode = 0o664 });
    defer env.close();
}

"hellodb" in this case is a path to a directory that will be used to store the environment. This directory needs to exists before it is opened as a LMDB environment. Here we will make sure it exists by creating it manually. mode is the file mode the UNIX permissions to set on created files.

From there we have to open a transaction to get access to a database:

    var db_txn = try env.transaction(.{});
    var db = try db_txn.database(null, .{ .create = true });
    try db_txn.commit();

Here we pass in the create flag to create the database if it doesn't exist in the environment.

After all that, we can get and put keys and values into the database:

    var txn = try env.transaction(.{});
    try txn.put(db, "hello", "world", .{});

    const value = txn.get(db, "hello");
    std.log.info("hello => {}", .{value}); // prints "hello => world"

    try txn.commit();

You can see the full source in examples/simple.zig. To run it we need to create the environment directory:

$ cd lmdb-zig
$ zigmod fetch
$ mkdir hellodb
$ zig build run-example-simple
info: hello => world

If you want to go further than this, please check out the official LMDB Getting Started documentation.

Raw Bindings

Because lmdb-zig is new, not all the LMDB API is covered. If you want to access the raw bindings, use lmdb.sys or @cInclude("lmdb.h").

lmdb-zig's People

Contributors

leroycep avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  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.