• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

LevelDB for Ruby (embedded)

Leveldb

LevelDB is a database library (C++, 350 kB) written at Google. It is an embedded database. LevelDB is a persistent ordered map.

LevelDB stores keys and values in arbitrary byte arrays, and data is sorted by key. It supports batching writes, forward and backward iteration, and compression of the data via Google's Snappy compression library. Still, LevelDB is not a SQL database. (Wikipedia)

Features

  • Keys and values are arbitrary byte arrays.
  • Data is stored sorted by key.
  • Callers can provide a (soon) custom comparison function to override the sort order.
  • The basic operations are Put(key,value), Get(key), Delete(key).
  • Multiple changes can be made in one atomic batch.
  • Users can create a transient snapshot to get a consistent view of data.
  • Forward and backward iteration is supported over the data.
  • Data is automatically compressed using the Snappy compression library.
  • External activity (file system operations etc.) is relayed through a virtual interface so users can customize the operating system interactions.
  • Detailed documentation about how to use the library is included with the source code.

Reading

Installation

Development

$ brew install snappy
$ git clone git://github.com/DAddYE/leveldb.git
$ cd leveldb
$ bundle install
$ bundle exec rake compile
$ bundle exec rake console

Standard

$ brew install snappy
$ gem install leveldb
$ irb -r leveldb

Usage

Here a basic usage:

db = LevelDB::DB.new '/tmp/foo'

# Writing
db.put('hello', 'world')
db['hello'] = 'world'

# Reading
db.get('hello') # => world
db['hello'] # => world
db.exists?('hello') # => true

# Reading/Writing
db.fetch('hello', 'hello world') # => will write 'hello world' if there is no key 'hello'
db.fetch('hello'){ |key| 'hello world' } # => same as above

# Deleting
db.delete('hello')

# Iterating
db.each { |key, val| puts "Key: #{key}, Val: #{val}" }
db.reverse_each { |key, val| puts "Key: #{key}, Val: #{val}" }
db.keys
db.values
db.map { |k,v| do_some_with(k, v) }
db.reduce([]) { |memo, (k, v)| memo << k + v; memo }
db.each # => enumerator
db.reverse_each # => enumerator

# Ranges
db.range('c', 'd') { |k,v| do_some_with_only_keys_in_range }
db.reverse_range('c', 'd') # => same as above but results are in reverse order
db.range(...) # => enumerable

# Batches
db.batch do |b|
  b.put 'a', 1
  b.put 'b', 2
  b.delete 'c'
end

b = db.batch
b.put 'a', 1
b.put 'b', 2
b.delete 'c'
b.write!

# Snapshots
db.put 'a', 1
db.put 'b', 2
db.put 'c', 3

snap = db.snapshot

db.delete 'a'
db.get 'a' # => nil

snap.set!

db.get('a') # => 1

snap.reset!

db.get('a') # => nil

snap.set!

db.get('a') # => 1

# Properties
db.read_property('leveldb.stats')

# Level  Files Size(MB) Time(sec) Read(MB) Write(MB)
# --------------------------------------------------
#   0        1        0         0        0         0
#   1        1        0         0        0         0

# same of:
db.stats

Benchmarks

Preface: those are only for general purpose, I know that zedshaw will kill me for this, but ... on my mac:

Model Identifier:	MacBookPro10,1
Processor Name:	Intel Core i7
Processor Speed:	2.3 GHz
Number of Processors:	1
Total Number of Cores:	4
L2 Cache (per Core):	256 KB
L3 Cache:	6 MB
Memory:	8 GB

The benchmark code is in benchmark/leveldb.rb

Writing/Reading 100mb of very random data of 10kb each:

Without compression:

      user     system      total        real
put  0.530000   0.310000   0.840000 (  1.420387)
get  0.800000   0.460000   1.260000 (  2.626631)

Level  Files Size(MB) Time(sec) Read(MB) Write(MB)
--------------------------------------------------
  0        1        0         0        0         0
  2       50       98         0        0         0
  3        1        2         0        0         0

With compression:

      user     system      total        real
put  0.850000   0.320000   1.170000 (  1.721609)
get  1.160000   0.480000   1.640000 (  2.703543)

Level  Files Size(MB) Time(sec) Read(MB) Write(MB)
--------------------------------------------------
  0        1        0         0        0         0
  1        5       10         0        0         0
  2       45       90         0        0         0

NOTE: as you can see snappy can't compress that kind of very very random data, but I was not interested to bench snappy (as a compressor) but only to see how (eventually) much slower will be using it. As you can see, only a few and on normal data the db size will be much much better!

With batch:

      user     system      total        real
put  0.260000   0.170000   0.430000 (  0.433407)

Level  Files Size(MB) Time(sec) Read(MB) Write(MB)
--------------------------------------------------
  0        1      100         1        0       100

Difference between a c++ pure ruby impl?

This, again, only for general purpose, but I want to compare the c++ implementation of leveldb-ruby with this that use ffi.

I'm aware that this lib is 1 year older, but for those who cares, the basic bench:

      user     system      total        real
put  0.440000   0.300000   0.740000 (  1.363188)
get  0.440000   0.440000   1.460000 (  2.407274)

Todo

  1. Add pluggable serializers
  2. Custom comparators

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

More Repositories

1

foreverb

Small daemon framework for ruby, with logging, error handler, scheduling and much more.
Ruby
520
star
2

vips

Go Bindings for Vips (a super fast image processor)
Go
450
star
3

mini_record

ActiveRecord meets DataMapper, with MiniRecord you are be able to write schema inside your models.
Ruby
265
star
4

do

DO - IT! is a thin framework useful to manage remote servers through ssh.
Ruby
166
star
5

igo

Intermediate GoLang source representation
Go
142
star
6

lipsiadmin

Lipsiadmin is a new revolutionary admin for your projects. Lipsiadmin is based on Ext Js 3+. framework (with prototype adapter) and is ready for Rails 2.+. This admin is for newbie developper but also for experts, is not entirely written in javascript because the aim of developper wose build in a agile way web/site apps so we use extjs in a new intelligent way a mixin of “old” html and new ajax functions, for example ext manage the layout of page, grids, tree and errors, but form are in html code.
Ruby
105
star
7

trez

A fast image resizer
Go
102
star
8

githubwatcher

Github Growl Watcher, watch any project and get instant growl notifications for: updates, new watchers, forks and issues
Ruby
88
star
9

lightbox

LightBox Helper is a small but beautifull helper that automatize the process for include LightBox on our pages.
JavaScript
39
star
10

ruby_nsq

Ruby client for the NSQ realtime message processing system
Ruby
28
star
11

gruby

Go lang to Ruby transpiler
Go
26
star
12

soda.vim

Reinterpreted Soda Theme for Vim Users
Vim Script
21
star
13

lipsiablog

Lightweight, simple, fast, easy blogging engine written in rails and Lipsiadmin
JavaScript
17
star
14

exception-notifier

Exception Notifier Plugin for Rails, that send email notification and generate error pages that you can use with your controller Layout.
Ruby
13
star
15

google_tasks

This gem provides access to google tasks apis
Ruby
10
star
16

model

Naive sql to struct for go
Go
8
star
17

node.nim

Work in progress node pattern on the awesome Nimrod
Nim
7
star
18

goroutine

Simple goroutine model for Ruby.
Ruby
6
star
19

fiddler

Wrapper for Fiddler (Ruby 2.0 FFI impl)
Ruby
6
star
20

ruvy

FFI Bindings to Libuv (experimental)
Ruby
6
star
21

eva

Effortless event driven micro framework that runs on Ruby in a different syntax
Ruby
6
star
22

golia

WebSite Link and Speed Checker
Ruby
5
star
23

documents

Document nubble FS
Ruby
4
star
24

tomorrow.vim

Dark light version of new Tomorrow Theme
Vim Script
4
star
25

.vim

My vim editor, plugins, syntax and more.
Vim Script
3
star
26

forever

MOVED HERE: https://github.com/DAddYE/foreverb
3
star
27

wintersmith-perian

Browserify, Coffee, Stylus, UglifyJS and CleanCSS
CoffeeScript
3
star
28

gist

Repo as a gist
Nim
3
star
29

loop

A tiny Ruby program used to periodically execute a command.
Ruby
2
star
30

snippets

Snipmate custom snippets
2
star
31

.do

My Servo recipes
Ruby
1
star
32

yarv

RubyVM (yarv) compiler/decompiler, written in go.
Go
1
star
33

rosetta

One language to rule them all.
Ruby
1
star
34

homebrew-alt

My homebrews
Ruby
1
star
35

mrb

FFI bindings for mruby
Ruby
1
star
36

uv

Ruby bindings for libuv
Ruby
1
star
37

lipsiadmin-I18n

Repository for collecting Locale data for Lipsiadmin
1
star
38

igo.vim

Vim syntax for igo
Vim Script
1
star
39

coding-style

Coding style
1
star