0

我正在运行这个测试:

public class LeveldbTest {

@Test
public void test() throws IOException, InterruptedException {
    DBFactory factory = new Iq80DBFactory();

    Options opts = new Options();
    opts.cacheSize(4096);
    opts.compressionType(CompressionType.SNAPPY);
    opts.createIfMissing(true).writeBufferSize(4096);
    final DB db = factory.open(new File("target/leveldb"), opts);
    final AtomicLong counter = new AtomicLong();

    long start = System.currentTimeMillis();
    new Thread() {
        @Override
        public void run() {
            WriteOptions wo = new WriteOptions();
            wo.sync(true);
            while (true) {
                String key = UUID.randomUUID().toString();
                byte[] bytes = key.getBytes();
                System.out.println("putting: " + key);
                db.put(bytes, bytes, wo);
                counter.incrementAndGet();
            }
        }
    }.start();

    new Thread() {
        @Override
        public void run() {
            WriteOptions wo = new WriteOptions();
            ReadOptions ro = new ReadOptions();
            wo.sync(true);
            while (true) {
                DBIterator iterator = db.iterator(ro);
                if (iterator.hasNext()) {
                    Entry<byte[], byte[]> next = iterator.next();
                    byte[] key = next.getKey();
                    System.out.println("deleting: " + new String(key));
                    db.delete(key, wo);
                    counter.incrementAndGet();
                    try {
                        iterator.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
    }.start();

    Thread.sleep(3000);
    System.out.println((double) counter.longValue() / (System.currentTimeMillis() - start));
}
}

我期望密钥将从队列中删除,但在控制台中似乎只有一个相同的密钥被删除:

putting: a1ad4662-18c3-4085-af9d-a451dc3279c0
deleting: 004ef0c2-b278-42c6-9c85-4769d93d5b30 <-
putting: 6c4ee31d-2910-4870-acc5-962f9c40a573
deleting: 004ef0c2-b278-42c6-9c85-4769d93d5b30 <-
deleting: 004ef0c2-b278-42c6-9c85-4769d93d5b30 <-
putting: 63e2e438-3849-46e3-9ea0-1165f9356efb
deleting: 004ef0c2-b278-42c6-9c85-4769d93d5b30 <-

我是否错误地使用了 API?

4

2 回答 2

1

正如您在使用迭代器时所观察到的,您必须在迭代之前使用 seekToFirst 或 seekToLast 将迭代器指向有效行。

我之前已经发现了这一点(困难的方式),它不会像您预期的那样抛出 ISE,您只会得到不一致的读取。

于 2013-12-08T22:29:33.373 回答
1

事实证明,如果我在删除线程中调用 next() 之前调用 iterator.seekToFirst(),它的行为与我预期的一样。那么问题是如果我不寻找一个位置,迭代器指向哪里。

于 2013-02-26T15:57:23.293 回答