Book Image

Getting Started with LevelDB

By : Andy Dent
Book Image

Getting Started with LevelDB

By: Andy Dent

Overview of this book

<p>Mobile and standalone desktop apps often need to store and rapidly retrieve data. LevelDB scales from a few simple values up to trillions of records and is flexible enough to store any kind of data.<br /><br />Getting Started with LevelDB progresses through the sophisticated ways to use databases. It starts by explaining how to install and build LevelDB from the basics, and moves on to explain the different Objective-C layers for LevelDB. It can be used as an introduction to database-backed iOS apps, as well as provides clarification to key-value stores and helps you understand how they relate to SQL.<br /><br />LevelDB is a flexible data solution that uses key-value stores to allow for the development of highly complex, efficient data storage systems. From the basics of data storage and retrieval to complex cases involving data storage for apps, this book covers a wide range of LevelDB topics<br /><br />Even relatively simple apps may have the need to search their data using different terms. Getting Started with LevelDB shows how to design keys to retrieve and store data efficiently. You will learn how its “levelled” nature delivers speedy writes and how to tune its settings and design for performance. You will also see a few techniques for debugging and tuning.</p> <p><br />Getting Started with LevelDB will leave you as an accomplished LevelDB programmer, confident you can build a complex database-backed app with high performance on iOS or OS/X.</p>
Table of Contents (18 chapters)
Getting Started with LevelDB
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Downloading LevelDB and Building with OS X
Index

Using LevelDB from Ruby


The most commonly recommended Ruby wrapper for LevelDB is from https://github.com/wmorgan/leveldb-ruby and can be installed, as seen in the log file included in the code samples for this chapter, with a gem command:

sudo gem install leveldb-ruby

Note that it has been stalled since 2011 and is minimal, not even including batch support. However, it supports the basics with code similar to the previous Python code:

require 'leveldb'
# open a data store
db = LevelDB::DB.new("/tmp/testleveldb11_ruby.db")
# a simple Put operation
db.put('name', 'William Bloggs')
db.put('spouse', 'Josephine Bloggs')
db.put('dog', 'Kilroy')
db.put('occupation', 'Dev')
db.close()

Unlike the Python code, reading back is idiomatically Ruby code where you can just treat the database as a dictionary and apply a block to it:

db.each do |k,v|
  puts "Key=#{k}, Value=#{v}"
end

A much more useful and complete wrapper is https://github.com/DAddYE/leveldb which includes nicer iterators and batches but has a more complex install and needs Ruby 2.0. It adds batch support:

db.batch do |b|
  b.put 'spouse', 'Josephine Bloggs'
  b.put 'dog', 'Kilroy'
  b.delete 'name'
end

This sample as a Ruby style of code blocks uses the idiom that the block contains all the logic to apply to the batch so implies a write at the end of the block.