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 & ;& 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