Last active
April 7, 2025 06:55
-
-
Save dacr/790df1705c7ec19ae2fe4098dad8d762 to your computer and use it in GitHub Desktop.
ZIO LMDB simple example using configuration provider / published by https://github.com/dacr/code-examples-manager #ab2e9b2c-bdcc-4eab-ba9b-3ab3953429c7/58aed1d7f49deb7c1f7fc71141766bc68ac6c067
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO LMDB simple example using configuration provider | |
// keywords : scala, zio, lmdb, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : ab2e9b2c-bdcc-4eab-ba9b-3ab3953429c7 | |
// created-on : 2023-04-29T22:48:17+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala 3.6.4 | |
//> using dep fr.janalyse::zio-lmdb:2.0.0 | |
//> using dep dev.zio::zio-config:4.0.4 | |
//> using dep dev.zio::zio-config-typesafe:4.0.4 | |
//> using javaOpt --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED | |
// --------------------- | |
import zio.* | |
import zio.lmdb.*, zio.lmdb.json.* | |
import zio.json.* | |
import zio.config.typesafe.* | |
import java.io.File | |
import java.util.UUID | |
import java.time.OffsetDateTime | |
case class Record( | |
uuid: UUID, | |
name: String, | |
age: Int, | |
addedOn: OffsetDateTime | |
) derives LMDBCodecJson | |
object SimpleExample extends ZIOAppDefault { | |
// Simulating a configuration, or use .fromResourcePath to automatically load configs from class path | |
val configProvider = | |
ConfigProvider.fromHoconString( | |
input = s"""lmdb { | |
| name = my-database | |
| home = /tmp | |
| sync = false | |
| maxReaders = 10 | |
|} | |
|""".stripMargin | |
) | |
override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] = Runtime.setConfigProvider(configProvider) | |
override def run = example.provide(LMDB.live, Scope.default) | |
val collectionName = "examples" | |
val example = for { | |
examples <- LMDB.collectionCreate[Record](collectionName, failIfExists = false) | |
recordId <- Random.nextUUID | |
dateTime <- Clock.currentDateTime | |
record = Record(recordId, "John Doe", 42, dateTime) | |
_ <- examples.upsertOverwrite(recordId.toString, record) | |
gotten <- examples.fetch(recordId.toString).some | |
collected <- examples.collect() | |
_ <- Console.printLine(s"collection $collectionName contains ${collected.size} records") | |
_ <- ZIO.foreach(collected)(record => Console.printLine(record)) | |
lmdb <- ZIO.service[LMDB] | |
_ <- Console.printLine("""LMDB standard tools can be used to manage the database content : sudo apt-get install lmdb-utils""") | |
_ <- Console.printLine(s"""To get some statistics : mdb_stat -s $collectionName ${lmdb.databasePath}/""") | |
_ <- Console.printLine(s"""To dump collection content : mdb_dump -p -s $collectionName ${lmdb.databasePath}/""") | |
} yield () | |
} | |
SimpleExample.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment