Coder Social home page Coder Social logo

zio-lmdb's Introduction

Lightning Memory Database (LMDB) for ZIO

Why ZIO-lmdb ? Because I wanted a very simple embedded (in the same process) ACID database for small applications while keeping deployment, maintenance, upgrades as simple as possible.

ZIO-lmdb is based on the powerful lmdb-java library and bring a higher level API in order to enhance the developer experience.

So ZIO-lmdb is an embedded key/value database, with an easy to use opinionated API, choices have been made to make the developer experience as simple as possible :

  • JSON based storage using zio-json,
  • safe update by using a lambda which will be called with the previous value if it exists and returns the new value,
  • identifiers are managed by the developer, just use UUID or ULID.

API is designed to not lie, all functions signatures describe precisely what you must expect from them, thanks to ZIO and Scala3.

Definitions

For a better understanding, this library use a slightly different vocabulary from LMDB original one :

  • Database : (LMDB talk about Environment)
    • The place where the database file is stored on your file system
    • A set of configuration for this database (expected maximum size, expected collection number)
  • Collection : (LMDB talk about Database)
    • A sorted map where to store your data
    • One database contains multiple collection
  • Transaction : (the same for LMDB)
    • for global coherency within the same database
    • only one simultaneous write access is possible within the same database

Usages example

test("basic usage")(
  for {
    collection        <- LMDB.collectionCreate[Record]("example")
    record             = Record("John Doe", 42)
    recordId          <- Random.nextUUID.map(_.toString)
    updatedState      <- collection.upsert(recordId, previousRecord => record)
    gotten            <- collection.fetch(recordId).some
    deletedRecord     <- collection.delete(recordId)
    gotNothing        <- collection.fetch(recordId)
  } yield assertTrue(
    updateStated.previous.isEmpty,
    updateStated.current == record,
    gotten == record,
    deletedRecord.contains(record),
    gotNothing.isEmpty
  )
)

Code snippets, runnable using scala-cli :

Operating lmdb databases

LMDB standard tools can be used to manage the databases content : sudo apt-get install lmdb-utils

  • to get some database statistics : mdb_stat -a database_directory_path/
  • to dump the content of a database : mdb_dump -a -p database_directory_path/
  • to dump the content of a database collection : mdb_dump -s collectionName -p database_directory_path/
  • to restore some collection or the entire database use the command named mdb_load which uses the same format as for mdb_dump

As zio-lmdb is using json format, dumps are just text, which can be edited and then loaded back. So simple data migration is straightforward.

Requirements

When LVMDB is used as persistence store with recent JVM it requires some JVM options :

--add-opens java.base/java.nio=ALL-UNNAMED
--add-opens java.base/sun.nio.ch=ALL-UNNAMED

zio-lmdb's People

Contributors

dacr 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.