Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use LevelDB in go?

Tags:

go

leveldb

From http://code.google.com/p/leveldb/, it seems not explicit or easy to use LevelDB with Go. But I really want to know how to use it in Go.

Could anyone give me a clue?

like image 627
hardPass Avatar asked May 08 '12 04:05

hardPass


People also ask

When should I use LevelDB?

This is useful if you have data that you want to store that you don't want to encode into a string. LevelDB supports atomic operations. You can run many operations at once in a single uninterruptible call.

What is the LevelDB format?

LevelDB is an on-disk key-value store where the keys and values are both arbitrary blobs of data. Each LevelDB database occupies a folder on the file system. The folder will contain some combination of files named “CURRENT”, “LOCK”, “LOG”, “LOG. old” and files named “MANIFEST-######”, “######.

How does LevelDB store data?

Features. 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. LevelDB is not an SQL database.


2 Answers

Here is a Go implementation of LevelDB https://github.com/syndtr/goleveldb

Here is how to use it:

go get github.com/syndtr/goleveldb/leveldb

Create or open database:

db, err := leveldb.OpenFile("path/to/db", nil)
...
defer db.Close()
...

Read or modify the database content:

data, err := db.Get([]byte("key"), nil)
...
err = db.Put([]byte("key"), []byte("value"), nil)
...
err = db.Delete([]byte("key"), nil)
...
like image 150
Anton Avatar answered Oct 06 '22 15:10

Anton


Use levigo - a Golang wrapper around the C++ version of LevelDB.

The file levigo/leveldb_test.go gives you an example of how to use levigo.

like image 36
Sonia Hamilton Avatar answered Oct 06 '22 17:10

Sonia Hamilton