LevelDB 编译安装(Debian 8)
博客专区 > norxiva 的博客 > 博客详情
LevelDB 编译安装(Debian 8)
norxiva 发表于2年前
LevelDB 编译安装(Debian 8)
  • 发表于 2年前
  • 阅读 46
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

环境信息

虚拟机节点(192.168.100.171<debian171>)

Debian jessie 8.5 

LevelDB 1.18

安装leveldb

sudo apt-get install libsnappy-dev
sudo tar xvf leveldb-1.18.tar.gz
cd leveldb-1.18/
sudo make
sudo cp libleveldb.so.1.18 /usr/local/lib
sudo cp libleveldb.a /usr/local/lib
sudo cp -R include/leveldb /usr/local/include
cd /usr/local/lib
sudo ln -s libleveldb.so.1.18 libleveldb.so
sudo ln -s libleveldb.so.1.18 libleveldb.so.1
sudo ldconfig

java代码

package surfin.example.leveldb.simpledemo;

import static org.iq80.leveldb.impl.Iq80DBFactory.asString;
import static org.iq80.leveldb.impl.Iq80DBFactory.bytes;
import static org.iq80.leveldb.impl.Iq80DBFactory.factory;

import java.io.File;
import java.io.IOException;

import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBComparator;
import org.iq80.leveldb.DBIterator;
import org.iq80.leveldb.Options;
import org.iq80.leveldb.ReadOptions;
import org.iq80.leveldb.WriteBatch;
import org.iq80.leveldb.WriteOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleDemoBootstrap {
	private static final Logger log = LoggerFactory.getLogger(SimpleDemoBootstrap.class);

	public static void main(String[] args) {

		Options options = new Options();
		options.createIfMissing(true);
		DB db = null;
		try {
			db = factory.open(new File("D:/leveldb-example/simpledemo"), options);
			
			//put get delete
			db.put(bytes("Tampa"), bytes("rocks"));
			byte[] tampa_b = db.get(bytes("Tampa"));
			log.info("Tampa: {}", asString(tampa_b));
			WriteOptions wo = new WriteOptions().sync(true);
			db.delete(bytes("Tampa"), wo);
			tampa_b = db.get(bytes("Tampa"));
			log.info("Tampa: {}", asString(tampa_b));

			//write batch
			WriteBatch wb = db.createWriteBatch();
			try{
				
				wb.put(bytes("Tampa"), bytes("green"));
				wb.put(bytes("London"), bytes("red"));
				db.write(wb);
			}finally{
				wb.close();
			}
			
			//Iterating key/values.
			DBIterator iter = db.iterator();
			try{
				for(iter.seekToFirst();iter.hasNext();iter.next()){
					String key = asString(iter.peekNext().getKey());
					String val = asString(iter.peekNext().getValue());
					log.info("iter {}:{}", key, val);
				}
				
			}finally{
				iter.close();
			}
			
			
			//Working against a Snapshot view of the Database.
			ReadOptions ro = new ReadOptions();
			ro.snapshot(db.getSnapshot());
			try {
				iter = db.iterator(ro);
				try{
					for(iter.seekToFirst();iter.hasNext();iter.next()){
						String key = asString(iter.peekNext().getKey());
						String val = asString(iter.peekNext().getValue());
						log.info("snapshot iter {}:{}", key, val);
					}
					
					log.info("snapshot Tampa: {}",asString(db.get(bytes("Tampa"), ro)));
					
				}finally{
					iter.close();
				}

			} finally {
			  // Make sure you close the snapshot to avoid resource leaks.
			  ro.snapshot().close();
			}
			
			//OUsing a custom Comparator.

			DBComparator comparator = new DBComparator(){
			    public int compare(byte[] key1, byte[] key2) {
			        return new String(key1).compareTo(new String(key2));
			    }
			    public String name() {
			        return "simple";
			    }
			    public byte[] findShortestSeparator(byte[] start, byte[] limit) {
			        return start;
			    }
			    public byte[] findShortSuccessor(byte[] key) {
			        return key;
			    }
			};
			
//			log.info(String.valueOf(System.currentTimeMillis()));
			

		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}finally{
			if(db != null){
				try {
					db.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}
}

pom依赖

<dependency>
	<groupId>org.iq80.leveldb</groupId>
	<artifactId>leveldb</artifactId>
</dependency>

参考资料

https://gist.github.com/dustismo/6203329

https://techoverflow.net/blog/2012/12/14/compiling-installing-leveldb-on-linux/

标签: leveldb
共有 人打赏支持
粉丝 0
博文 11
码字总数 5308
×
norxiva
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: