java entries_Java leveldb遍历特定前缀的entries

maven dependency:

org.fusesource.leveldbjni

leveldbjni-all

1.8

测试代码实现, 很简单就不多阐述了.

package com.qshuai.singleton;

import org.iq80.leveldb.CompressionType;

import org.iq80.leveldb.DB;

import org.iq80.leveldb.DBIterator;

import org.iq80.leveldb.Options;

import java.io.File;

import java.io.IOException;

import static org.fusesource.leveldbjni.JniDBFactory.asString;

import static org.fusesource.leveldbjni.JniDBFactory.factory;

public class DBTest {

public static void main(String[] args) {

Options utxoOptions = new Options();

utxoOptions.createIfMissing(true);

utxoOptions.compressionType(CompressionType.SNAPPY);

utxoOptions.writeBufferSize(800 * 1048576); // 800MB cache for write buffer

utxoOptions.cacheSize(0);

DB ldb = null;

try {

ldb = factory.open(new File("testdb"), utxoOptions);

} catch (IOException e) {

e.printStackTrace();

}

// less value

ldb.put(("120000" + "00").getBytes(), new byte[2]);

String prefix = "120099";

ldb.put((prefix + "01").getBytes(), new byte[2]);

ldb.put((prefix + "02").getBytes(), new byte[2]);

ldb.put((prefix + "03").getBytes(), new byte[2]);

ldb.put((prefix + "04").getBytes(), new byte[2]);

ldb.put((prefix + "05").getBytes(), new byte[2]);

// great value

ldb.put(("999999" + "00").getBytes(), new byte[2]);

// [first target] to [the last]

System.out.println("[first target] to [the last]");

DBIterator iterator = ldb.iterator();

try {

for (iterator.seek(prefix.getBytes()); iterator.hasNext(); iterator.next()) {

String key = asString(iterator.peekNext().getKey());

String value = asString(iterator.peekNext().getValue());

System.out.println(key + " = " + value);

}

} finally {

}

// [first target] to [last target]

System.out.println("\n[first target] to [last target]");

DBIterator it = ldb.iterator();

try {

for (it.seek(prefix.getBytes()); it.hasNext(); it.next()) {

if (!equalPrefix(it.peekNext().getKey(), prefix.getBytes()))

break;

String key = asString(it.peekNext().getKey());

String value = asString(it.peekNext().getValue());

System.out.println(key + " = " + value);

}

} finally {

}

}

public static boolean equalPrefix(byte[] src, byte[] target) {

if (src.length < target.length)

return false;

for (int i = 0; i

if (src[i] != target[i])

return false;

}

return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值