Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #42

Open
wants to merge 1 commit into
base: zio2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 109 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,116 @@
[![Project stage][Stage]][Stage-Page]
[![Release Artifacts][Badge-SonatypeReleases]][Link-SonatypeReleases]
[//]: # (This file was autogenerated using `zio-sbt-website` plugin via `sbt generateReadme` command.)
[//]: # (So please do not edit it manually. Instead, change "docs/index.md" file or sbt setting keys)
[//]: # (e.g. "readmeDocumentation" and "readmeSupport".)

# zio-rocksdb
# ZIO RocksDB

A ZIO-based interface to RocksDB.
[ZIO RocksDB](https://github.com/zio/zio-rocksdb) is a ZIO-based interface to RocksDB.

## Getting help
Rocksdb is an embeddable persistent key-value store that is optimized for fast storage. ZIO RocksDB provides us a functional ZIO wrapper around its Java API.

Join us on the [ZIO Discord server](https://discord.gg/2ccFBr4).
|Project Stage | CI | Release | Snapshot | Discord | Github |
|--------------|----|---------|----------|---------|--------|
|[![Development](https://img.shields.io/badge/Project%20Stage-Development-green.svg)](https://github.com/zio/zio/wiki/Project-Stages) |![CI Badge](https://github.com/zio/zio-rocksdb/workflows/CI/badge.svg) |[![Sonatype Releases](https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-rocksdb_2.12.svg)](https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-rocksdb_2.12/) |[![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-rocksdb_2.12.svg)](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-rocksdb_2.12/) |[![Chat on Discord!](https://img.shields.io/discord/629491597070827530?logo=discord)](https://discord.gg/2ccFBr4) |[![ZIO RocksDB](https://img.shields.io/github/stars/zio/zio-rocksdb?style=social)](https://github.com/zio/zio-rocksdb) |

## Legal
## Installation

Copyright 2019 Itamar Ravid and the zio-rocksdb contributors. All rights reserved.
In order to use this library, we need to add the following line in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-rocksdb" % "0.4.2"
```

## Example 1

Use the provided `RocksDB` wrapper:

```scala
import java.nio.charset.StandardCharsets.UTF_8
import zio.rocksdb.RocksDB

val key = "key".getBytes(UTF_8)
val value = "value".getBytes(UTF_8)

val database = RocksDB.live("/data/state")
val readWrite = RocksDB.put(key, value) *> RocksDB.get(key)
val result = readWrite.provide(database)
```

## Example 2

An example of writing and reading key/value pairs and also using transactional operations when using RocksDB:

```scala
import zio._
import zio.rocksdb._

import java.nio.charset.StandardCharsets._

object ZIORocksDBExample extends ZIOAppDefault {

private def bytesToString(bytes: Array[Byte]): String = new String(bytes, UTF_8)
private def bytesToInt(bytes: Array[Byte]): Int = bytesToString(bytes).toInt

val job1: ZIO[RocksDB, Throwable, Unit] =
for {
_ <- RocksDB.put(
"Key".getBytes(UTF_8),
"Value".getBytes(UTF_8)
)
result <- RocksDB.get("Key".getBytes(UTF_8))
stringResult = result.map(bytesToString)
_ <- Console.printLine(s"value: $stringResult")
} yield ()

val job2: ZIO[TransactionDB, Throwable, Unit] =
for {
key <- ZIO.succeed("COUNT".getBytes(UTF_8))
_ <- TransactionDB.put(key, 0.toString.getBytes(UTF_8))
_ <- ZIO.foreachParDiscard(0 until 10) { _ =>
TransactionDB.atomically {
Transaction.getForUpdate(key, exclusive = true) flatMap { iCount =>
Transaction.put(key, iCount.map(bytesToInt).map(_ + 1).getOrElse(-1).toString.getBytes(UTF_8))
}
}
}
value <- TransactionDB.get(key)
counterValue = value.map(bytesToInt)
_ <- Console.printLine(s"The value of counter: $counterValue") // Must be 10
} yield ()

[Link-SonatypeReleases]: https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-rocksdb_2.12/ "Sonatype Releases"
[Badge-SonatypeReleases]: https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-rocksdb_2.12.svg "Sonatype Releases"
[Stage]: https://img.shields.io/badge/Project%20Stage-Development-yellowgreen.svg
[Stage-Page]: https://github.com/zio/zio/wiki/Project-Stages
private val transactional_db =
TransactionDB.live(new org.rocksdb.Options().setCreateIfMissing(true), "tr_db")

private val rocks_db =
RocksDB.live(new org.rocksdb.Options().setCreateIfMissing(true), "rocks_db")

def run =
(job1.provide(rocks_db) <*> job2.provide(transactional_db))
.foldCauseZIO(cause => Console.printLine(cause.prettyPrint) *> ZIO.succeed(1), _ => ZIO.succeed(0))
}
```

## Documentation

Learn more on the [ZIO RocksDB homepage](https://zio.dev/zio-rocksdb/)!

## Contributing

For the general guidelines, see ZIO [contributor's guide](https://zio.dev/about/contributing).

## Code of Conduct

See the [Code of Conduct](https://zio.dev/about/code-of-conduct)

## Support

Come chat with us on [![Badge-Discord]][Link-Discord].

[Badge-Discord]: https://img.shields.io/discord/629491597070827530?logo=discord "chat on discord"
[Link-Discord]: https://discord.gg/2ccFBr4 "Discord"

## License

[License](LICENSE)

Copyright 2019 Itamar Ravid and the zio-rocksdb contributors. All rights reserved.