Giter Site home page Giter Site logo

fern-flower-lab / leveldb-clj Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 12 KB

The LevelDB driver for Clojure

License: MIT License

Clojure 100.00%
binary-storage clojure clojure-library inmemory-db kv-store ledger leveldb tiny-library transactional

leveldb-clj's Introduction

leveldb-clj

A Clojure tiny and low-level library to handle key-value store at LevelDB for blockchain projects.

GitHub leveldb clj GitHub tag (latest by date) GitHub last commit GitHub release (latest by date)

Usage

leiningen project.clj
...
 :dependencies [[org.clojure/clojure "1.11.1"]
...
                [ai.z7/leveldb-clj "0.0.4"]]
...

and then

(ns my.application.ns
  (:require [leveldb-clj.core :as leveldb]
            [leveldb-clj.kv :as kv]
            [leveldb-clj.tx :as tx]))

;; regular interface designed for generic kv/* methods
(defonce ^:private store
  (leveldb/map->LevelDBStore {:path "mystore.level.db"}))

;; atomic interface designed for transactional tx/* methods
(defonce ^:private atomic-store
  (tx/->LevelDB-TX store (atom {})))

That is - the store is ready!

Generic interface

test=> (def store (leveldb/map->LevelDBStore {:path "mystore.level.db"}))

test=> (kv/insert store (.getBytes "eee") (.getBytes "fff"))
; #leveldb_clj.core.LevelDBStore{:db #object[org.iq80.leveldb.impl.DbImpl 0x69957a8 "org.iq80.leveldb.impl.DbImpl@69957a8"], :codec nil}

test=> (kv/list-keys store)
; (#object["[B" 0x72b795cf "[B@72b795cf"])

test=> (map #(String. %) (kv/list-keys store))
; ("eee")

test=> (kv/retrieve store (.getBytes "eee"))
; #object["[B" 0x453ad576 "[B@453ad576"]

test=> (String. (kv/retrieve store (.getBytes "eee")))
; "fff"

test=> (kv/delete store (.getBytes "eee"))
; #leveldb_clj.core.LevelDBStore{:db #object[org.iq80.leveldb.impl.DbImpl 0x69957a8 "org.iq80.leveldb.impl.DbImpl@69957a8"], :codec nil}

test=> (map #(String. %) (kv/list-keys store))
; ()

test=> (String. (kv/retrieve store (.getBytes "eee")))
; Execution error (NullPointerException) at java.lang.String/<init> (String.java:1460).
; Cannot invoke "java.lang.StringBuffer.toString()" because "buffer" is null

Transactional interface

test=> (def atomic-store (tx/->LevelDB-TX store (atom {})))

test=> (map #(String. %) (kv/list-keys store))
; ()
test=> (tx/add atomic-store (.getBytes "eee") (.getBytes "fff"))
; #leveldb_clj.tx.LevelDB-TX{:store #leveldb_clj.core.LevelDBStore{:db #object[org.iq80.leveldb.impl.DbImpl 0x69957a8 "org.iq80.leveldb.impl.DbImpl@69957a8"], :codec nil}, :state #object[clojure.lang.Atom 0x3e85d3ce {:status :ready, :val {#object["[B" 0x38381caf "[B@38381caf"] #object["[B" 0x4b1aeb91 "[B@4b1aeb91"]}}]}

test=> (map #(String. %) (kv/list-keys store))
; ()

test=> (tx/commit atomic-store)
; #leveldb_clj.tx.LevelDB-TX{:store #leveldb_clj.core.LevelDBStore{:db #object[org.iq80.leveldb.impl.DbImpl 0x69957a8 "org.iq80.leveldb.impl.DbImpl@69957a8"], :codec nil}, :state #object[clojure.lang.Atom 0x3e85d3ce {:status :ready, :val {}}]}
test=> (map #(String. %) (kv/list-keys store))
; ("eee")

(tx/add atomic-store (.getBytes "eee") (.getBytes "HHH"))
; #leveldb_clj.tx.LevelDB-TX{:store #leveldb_clj.core.LevelDBStore{:db #object[org.iq80.leveldb.impl.DbImpl 0xb2e2921 "org.iq80.leveldb.impl.DbImpl@b2e2921"], :codec nil}, :state #object[clojure.lang.Atom 0x7d995254 {:status :ready, :val {#object["[B" 0x266702a9 "[B@266702a9"] #object["[B" 0x77cfe94e "[B@77cfe94e"]}}]}

test=> (map #(String. %) (kv/list-keys store))
; ("eee")

test=> (String. (kv/retrieve store (.getBytes "eee")))
; "fff"

test=> (tx/rollback atomic-store)
; #leveldb_clj.tx.LevelDB-TX{:store #leveldb_clj.core.LevelDBStore{:db #object[org.iq80.leveldb.impl.DbImpl 0xb2e2921 "org.iq80.leveldb.impl.DbImpl@b2e2921"], :codec nil}, :state #object[clojure.lang.Atom 0x7d995254 {:status :ready, :val {}}]}

test=> (String. (kv/retrieve store (.getBytes "eee")))
; "fff"

test=> (tx/add atomic-store (.getBytes "eee") (.getBytes "HHH"))
; #leveldb_clj.tx.LevelDB-TX{:store #leveldb_clj.core.LevelDBStore{:db #object[org.iq80.leveldb.impl.DbImpl 0xb2e2921 "org.iq80.leveldb.impl.DbImpl@b2e2921"], :codec nil}, :state #object[clojure.lang.Atom 0x7d995254 {:status :ready, :val {#object["[B" 0xe0570d "[B@e0570d"] #object["[B" 0x159a9133 "[B@159a9133"]}}]}

test=> (String. (kv/retrieve store (.getBytes "eee")))
"fff"

test=> (tx/commit atomic-store)
; #leveldb_clj.tx.LevelDB-TX{:store #leveldb_clj.core.LevelDBStore{:db #object[org.iq80.leveldb.impl.DbImpl 0xb2e2921 "org.iq80.leveldb.impl.DbImpl@b2e2921"], :codec nil}, :state #object[clojure.lang.Atom 0x7d995254 {:status :ready, :val {}}]}

test=> (String. (kv/retrieve store (.getBytes "eee")))
; "HHH"

License

Distributed under the MIT license.

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.