Git Product home page Git Product logo

clj-leveldb's Introduction

This is a self-contained wrapper around LevelDB, which provides all the necessary binaries via leveldbjni.

basic usage

[factual/clj-leveldb "0.1.2"]

To create or access a database, use clj-leveldb/create-db:

clj-leveldb> (def db (create-db "/tmp/leveldb" {}))
#'clj-leveldb/db

This database object can now be used with clj-leveldb/get, put, delete, batch, and iterator.

clj-leveldb> (put db "a" "b")
nil
clj-leveldb> (get db "a")
#<byte[] [B@5e0a614c>

Notice that the value returned is a byte-array. This is because byte arrays are the native storage format for LevelDB, and we haven't defined custom encoders and decoders. This can be done in create-db:

clj-leveldb> (def db (create-db "/tmp/leveldb" 
                       {:key-decoder byte-streams/to-string 
                        :val-decoder byte-streams/to-string}))
#'clj-leveldb/db
clj-leveldb> (get db "a")
"b"

Notice that we haven't defined key-encoder or val-encoder; this is because there's a default transformation between strings and byte-arrays, which assumes a utf-8 encoding. If we wanted to support keywords, or use a different encoding, we'd have to explicitly specify encoders.

Both put and delete can take multiple values, which will be written in batch:

clj-leveldb> (put db "a" "b" "c" "d" "e" "f")
nil
clj-leveldb> (delete db "a" "c" "e")
nil

If you need to batch a collection of puts and deletes, use batch:

clj-leveldb> (batch db {:put ["a" "b" "c" "d"] :delete ["j" "k" "l"]})

We can also get a sequence of all key/value pairs, either in the entire database or within a given range using iterator:

clj-leveldb> (put db "a" "b" "c" "d" "e" "f")
nil
clj-leveldb> (iterator db)
(["a" "b"] ["c" "d"] ["e" "f"])
clj-leveldb> (iterator db "c" nil)
(["c" "d"] ["e" "f"])
clj-leveldb> (iterator db nil "c")
(["a" "b"] ["c" "d"])

Syncing writes to disk can be forced via sync, and compaction can be forced via compact.

Full documentation can be found here.

license

Copyright © 2013 Factual, Inc.

Distributed under the Eclipse Public License, the same as Clojure.

clj-leveldb's People

Contributors

elliot42 avatar maxcountryman avatar worace avatar ztellman 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.