]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/AbstractRocksIterator.java
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
8 import java
.nio
.ByteBuffer
;
11 * Base class implementation for Rocks Iterators
14 * <p>Multiple threads can invoke const methods on an RocksIterator without
15 * external synchronization, but if any of the threads may call a
16 * non-const method, all threads accessing the same RocksIterator must use
17 * external synchronization.</p>
19 * @param <P> The type of the Parent Object from which the Rocks Iterator was
20 * created. This is used by disposeInternal to avoid double-free
21 * issues with the underlying C++ object.
22 * @see org.rocksdb.RocksObject
24 public abstract class AbstractRocksIterator
<P
extends RocksObject
>
25 extends RocksObject
implements RocksIteratorInterface
{
28 protected AbstractRocksIterator(final P parent
,
29 final long nativeHandle
) {
31 // parent must point to a valid RocksDB instance.
32 assert (parent
!= null);
33 // RocksIterator must hold a reference to the related parent instance
34 // to guarantee that while a GC cycle starts RocksIterator instances
35 // are freed prior to parent instances.
40 public boolean isValid() {
41 assert (isOwningHandle());
42 return isValid0(nativeHandle_
);
46 public void seekToFirst() {
47 assert (isOwningHandle());
48 seekToFirst0(nativeHandle_
);
52 public void seekToLast() {
53 assert (isOwningHandle());
54 seekToLast0(nativeHandle_
);
58 public void seek(byte[] target
) {
59 assert (isOwningHandle());
60 seek0(nativeHandle_
, target
, target
.length
);
64 public void seekForPrev(byte[] target
) {
65 assert (isOwningHandle());
66 seekForPrev0(nativeHandle_
, target
, target
.length
);
70 public void seek(ByteBuffer target
) {
71 assert (isOwningHandle() && target
.isDirect());
72 seekDirect0(nativeHandle_
, target
, target
.position(), target
.remaining());
73 target
.position(target
.limit());
77 public void seekForPrev(ByteBuffer target
) {
78 assert (isOwningHandle() && target
.isDirect());
79 seekForPrevDirect0(nativeHandle_
, target
, target
.position(), target
.remaining());
80 target
.position(target
.limit());
85 assert (isOwningHandle());
91 assert (isOwningHandle());
96 public void status() throws RocksDBException
{
97 assert (isOwningHandle());
98 status0(nativeHandle_
);
102 * <p>Deletes underlying C++ iterator pointer.</p>
104 * <p>Note: the underlying handle can only be safely deleted if the parent
105 * instance related to a certain RocksIterator is still valid and initialized.
106 * Therefore {@code disposeInternal()} checks if the parent is initialized
107 * before freeing the native handle.</p>
110 protected void disposeInternal() {
111 if (parent_
.isOwningHandle()) {
112 disposeInternal(nativeHandle_
);
116 abstract boolean isValid0(long handle
);
117 abstract void seekToFirst0(long handle
);
118 abstract void seekToLast0(long handle
);
119 abstract void next0(long handle
);
120 abstract void prev0(long handle
);
121 abstract void seek0(long handle
, byte[] target
, int targetLen
);
122 abstract void seekForPrev0(long handle
, byte[] target
, int targetLen
);
123 abstract void seekDirect0(long handle
, ByteBuffer target
, int targetOffset
, int targetLen
);
124 abstract void seekForPrevDirect0(long handle
, ByteBuffer target
, int targetOffset
, int targetLen
);
125 abstract void status0(long handle
) throws RocksDBException
;