Using LEVELDB in Golang

Source: Internet
Author: User

Leveldb is a very powerful KV database, naturally, I also want to be able to use in go.

If there is an official go leveldb implementation, then I will give priority. For example, this. However, the library documentation is not available, and no one is found on the Internet for the actual combat environment. I decided not to use it as a question mark on whether it could be used in a production environment or to be on the safe side.

Because LEVELDB has C interface, so through the CGO can be very convenient integration, so I decided to adopt this way, fortunately, someone has done the CGO version number, that is, Levigo.

use Levigo. Need to compile and install LEVELDB, assuming that compression support is required to compile snappy, for simplicity. I wrote a widget script, such as the following:

#!/bin/bash#refer https://github.com/norton/lets/blob/master/c_src/build_deps.sh# You must set up the actual snappy and LEVELDB source code address here Snappy_src=./snappyleveldb_src=./leveldbsnappy_dir=/usr/local/snappyleveldb_dir =/USR/LOCAL/LEVELDBIF [!-f $SNAPPY _dir/lib/libsnappy.a]; Then (CDs $SNAPPY _SRC &&/configure--prefix= $SNAPPY _dir && make && Mak E install) Else echo "Skip install snappy" Fiif [!-f $LEVELDB _DIR/LIB/LIBLEVELDB.A]; Then (CD $LEVELDB _src && echo "echo \" Platform_cflags+=-i$snappy_dir/include\ ">> build_config.mk ">> Build_detect_platform && echo" echo \ "Platform_cxxflags+=-i$snappy_dir/include\" >> build_ Config.mk ">> build_detect_platform && echo" echo \ "Platform_ldflags+=-l $SNAPPY _dir/lib-lsnappy\" &          Gt;> build_config.mk ">> build_detect_platform && make Snappy=1 && make && Mkdir-p $LEVELDB _dir/include/leveldb && Install include/leveldb/*.h $LEVELDB _dir/include/leveldb && mkdir-p $LEVELDB _dir/lib &amp ;& cp-af libleveldb.* $LEVELDB _dir/lib) Else echo "Skip install LEVELDB" Fifunction Add_path () {# path V Ariable # $ A path to add if [-D "$ $"] && [[": $:"! = * ": $:" *]]; Then echo "$1:$2" Else echo "$" fi}export cgo_cflags= "-i$leveldb_dir/include-i$snappy_dir/include" Export cgo_ldf lags= "-l$leveldb_dir/lib-l$snappy_dir/lib-lsnappy" Export ld_library_path=$ (add_path $LD _library_path $SNAPPY _dir/  LIB) Export ld_library_path=$ (Add_path $LD _library_path $LEVELDB _dir/lib) go get Github.com/jmhodges/levigo

For the use of leveldb in Go, Levigo did a very good package, but one thing I'm not used to. In the LEVELDB. For both read and write operations, you need to pass in an option. Most of the time this thing is a default option object, and it is not necessary to create deletes every time you go inside.

So I encapsulated it. An interface such as the following is provided, so that the default option is used.

At the same time for iterator. I took the C + + model and provided two modes of iterator and reverse_iterator. For example, the following:

The detailed code is here.

Using LEVELDB in Golang

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.