• Stars
    star
    38
  • Rank 683,585 (Top 14 %)
  • Language
    Crystal
  • License
    GNU Lesser Genera...
  • Created almost 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Crystal binding for LevelDB

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

More Repositories

1

icr

Interactive console for Crystal programming language
Crystal
500
star
2

crystal-patterns

📖 Examples of GOF patterns written in Crystal
Crystal
291
star
3

jwt

JWT implementation in Crystal
Crystal
204
star
4

crystal-libraries-needed

A list of libraries that are needed or wanted for the Crystal-Language
141
star
5

msgpack-crystal

MessagePack implementation in Crystal msgpack.org[Crystal]
Crystal
133
star
6

cossack

Simple and flexible HTTP client for Crystal with middleware and test support.
Crystal
105
star
7

hardware

Get CPU, Memory and Network informations of the running OS and its processes
Crystal
71
star
8

kiwi

A unified Crystal interface for key-value stores.
Crystal
61
star
9

toml.cr

TOML parser for Crystal
Crystal
58
star
10

zeromq-crystal

Crystal
46
star
11

crystal-ann

Web site to announce new Crystal projects, blog posts, updates and other work activities
CSS
45
star
12

future.cr

Crystal
34
star
13

bloom_filter

Bloom filter implementation in Crystal lang
Crystal
34
star
14

timecop.cr

A testing library that allows "time travel," "freezing time," and "time acceleration". Inspired by the ruby-timecop library.
Crystal
19
star
15

crystal-kcov

Crystal
16
star
16

autolink.cr

🔗 Auto link for Crystal
Crystal
16
star
17

crystal-notifications

A library for notifications, this started as a port from ActiveSupport::Notifications
Crystal
16
star
18

bluetooth

Bluetooth Bluez binding in Crystal
Crystal
10
star
19

community

The crystal community
10
star
20

cr-config

An all-in-one configuration library to handle any possible configuration need
Crystal
9
star
21

snappy-crystal

Snappy bindings for Crystal
Crystal
6
star
22

cr-i18n

Crystal
3
star
23

kilt-components

Crystal
2
star