Code Monkey home page Code Monkey logo

leveldb's Introduction

LevelDB crystal levedb

Crystal binding for LevelDB.

Build Status

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.

Installation

Prerequisites

Debian:

sudo apt-get install libleveldb-dev libleveldb1v5 libsnappy1v5

shard.yml

dependencies:
  leveldb:
    github: "crystal-community/leveldb"
    version: "~> 0.2.0"

Usage

Basic usage

require "leveldb"

# Create DB (if does not exist yet) and open
db = LevelDB::DB.new("./db")

# Put, get, delete
db.put("name", "Sergey")
db.get("name")  # => "Sergey"
db.delete("name")
db.get("name")  # => nil

# [], []= methods work the same
db["city"] = "Berlin"
db["city"]  # => "Berlin"

# Iterate through all the keys
db.each do |key, val|
  puts "#{key} = #{val}"
end

# Close database
db.close
db.closed? # => true
db.opened? # => false

# Close the database and remove all the data
db.destroy

# Remove all the keys, keep the database open
db.clear

Batches

Apply a atomic batch of of operation to the key-value store.

require "leveldb"

db = LevelDB::DB.new("./db")
begin
  batch = LevelDB::Batch.new

  batch.put("name","Martin")
  batch.put("age","25")
  batch.put("location","Bariloche")
  batch.delete("age")

  # write batch to the db in atomic way
  db.write(batch)

  puts db.get("name")
  puts db.get("age") # nil
  puts db.get("location")
ensure
  # free memory 
  batch.destroy 
  # close the database
  db.close
end

Snapshots

Snapshots provide consistent read-only views over the entire state of the key-value store.

db = LevelDB::DB.new("./db")

db.put("a", "1")
db.get("a")  # => "1"

snapshot = db.create_snapshot
db.delete("a")
db.get("a")  # => nil

db.set_snapshot(snapshot)
db.get("a")  # => "1"

db.unset_snapshot
db.get("a")  # => nil

Performance

There is performance comparison of LevelDB and other stores from Kiwi project.

LevelDB VS other storages

Contributors

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.