Using LEVELDB in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

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 would prefer, such as this, but the library document is not at all, and no one on the internet to use the actual combat environment, whether it can be used in the production environment using the question mark, insurance, I decided not to use.

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 a cgo version, that is, Levigo.

Use Levigo, need to compile the installation leveldb, if need compression support also need to compile snappy, in order to be simple, I wrote a widget script, as follows:

#!/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 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 && \ Make 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.m K ">> 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\" >> build_config.mk ">> build_detect_platform && make Snappy=1 && \ Make &&am P \ mkdir-p $LEVELDB _dir/include/leveldb && \ Install include/leveldb/*.h $LEVELDB _dir/include/leveldb && \ mkdir-p $LEVELDB _dir/l IB && \ Cp-af libleveldb.* $LEVELDB _dir/lib) Else echo "Skip install LEVELDB" Fifunction Add_path () {# $ 1 Path variable # $ 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 good package, but one thing I'm not used to, in Leveldb, for read and write operation, all need to pass in an option, this thing is a default option object most of the time, There's no need to create a delete every time you go inside. So I encapsulated it, provided the following interface, so that the default option is used.

For iterator, I refer to the C + + model, which provides the iterator and reverse_iterator two modes, as follows:

The specific code is here.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

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.