Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
kvdb
rocksdb
提交
a76d9286
R
rocksdb
项目概览
kvdb
/
rocksdb
12 个月 前同步成功
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rocksdb
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a76d9286
编写于
1月 23, 2015
作者:
Y
Yueh-Hsuan Chiang
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #466 from fyrz/RocksJava-Support-ReadOptions-Iterator
[RocksJava] Allow using ReadOptions with iterators
上级
3b494a61
bef7821f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
263 addition
and
32 deletion
+263
-32
java/org/rocksdb/RocksDB.java
java/org/rocksdb/RocksDB.java
+82
-15
java/org/rocksdb/test/SnapshotTest.java
java/org/rocksdb/test/SnapshotTest.java
+124
-5
java/rocksjni/rocksjni.cc
java/rocksjni/rocksjni.cc
+57
-12
未找到文件。
java/org/rocksdb/RocksDB.java
浏览文件 @
a76d9286
...
...
@@ -1107,21 +1107,40 @@ public class RocksDB extends RocksObject {
}
/**
* Return a heap-allocated iterator over the contents of the database.
* The result of newIterator() is initially invalid (caller must
* call one of the Seek methods on the iterator before using it).
* <p>Return a heap-allocated iterator over the contents of the
* database. The result of newIterator() is initially invalid
* (caller must call one of the Seek methods on the iterator
* before using it).</p>
*
* Caller should close the iterator when it is no longer needed.
*
<p>
Caller should close the iterator when it is no longer needed.
* The returned iterator should be closed before this db is closed.
* </p>
*
* @return instance of iterator object.
*/
public
RocksIterator
newIterator
()
{
return
new
RocksIterator
(
this
,
iterator
0
(
nativeHandle_
));
return
new
RocksIterator
(
this
,
iterator
(
nativeHandle_
));
}
/**
* <p>Return a heap-allocated iterator over the contents of the
* database. The result of newIterator() is initially invalid
* (caller must call one of the Seek methods on the iterator
* before using it).</p>
*
* <p>Caller should close the iterator when it is no longer needed.
* The returned iterator should be closed before this db is closed.
* </p>
*
* @param readOptions {@link ReadOptions} instance.
* @return instance of iterator object.
*/
public
RocksIterator
newIterator
(
ReadOptions
readOptions
)
{
return
new
RocksIterator
(
this
,
iterator
(
nativeHandle_
,
readOptions
.
nativeHandle_
));
}
/**
* <p>Return a handle to the current DB state. Iterators created with
* this handle will all observe a stable snapshot of the current DB
* state. The caller must call ReleaseSnapshot(result) when the
...
...
@@ -1153,22 +1172,45 @@ public class RocksDB extends RocksObject {
}
/**
* Return a heap-allocated iterator over the contents of the database.
* The result of newIterator() is initially invalid (caller must
* call one of the Seek methods on the iterator before using it).
* <p>Return a heap-allocated iterator over the contents of the
* database. The result of newIterator() is initially invalid
* (caller must call one of the Seek methods on the iterator
* before using it).</p>
*
* Caller should close the iterator when it is no longer needed.
*
<p>
Caller should close the iterator when it is no longer needed.
* The returned iterator should be closed before this db is closed.
* </p>
*
* @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
* instance
* @return instance of iterator object.
*/
public
RocksIterator
newIterator
(
ColumnFamilyHandle
columnFamilyHandle
)
{
return
new
RocksIterator
(
this
,
iterator
0
(
nativeHandle_
,
return
new
RocksIterator
(
this
,
iterator
CF
(
nativeHandle_
,
columnFamilyHandle
.
nativeHandle_
));
}
/**
* <p>Return a heap-allocated iterator over the contents of the
* database. The result of newIterator() is initially invalid
* (caller must call one of the Seek methods on the iterator
* before using it).</p>
*
* <p>Caller should close the iterator when it is no longer needed.
* The returned iterator should be closed before this db is closed.
* </p>
*
* @param columnFamilyHandle {@link org.rocksdb.ColumnFamilyHandle}
* instance
* @param readOptions {@link ReadOptions} instance.
* @return instance of iterator object.
*/
public
RocksIterator
newIterator
(
ColumnFamilyHandle
columnFamilyHandle
,
ReadOptions
readOptions
)
{
return
new
RocksIterator
(
this
,
iteratorCF
(
nativeHandle_
,
columnFamilyHandle
.
nativeHandle_
,
readOptions
.
nativeHandle_
));
}
/**
* Returns iterators from a consistent database state across multiple
* column families. Iterators are heap allocated and need to be deleted
...
...
@@ -1184,10 +1226,31 @@ public class RocksDB extends RocksObject {
*/
public
List
<
RocksIterator
>
newIterators
(
List
<
ColumnFamilyHandle
>
columnFamilyHandleList
)
throws
RocksDBException
{
return
newIterators
(
columnFamilyHandleList
,
new
ReadOptions
());
}
/**
* Returns iterators from a consistent database state across multiple
* column families. Iterators are heap allocated and need to be deleted
* before the db is deleted
*
* @param columnFamilyHandleList {@link java.util.List} containing
* {@link org.rocksdb.ColumnFamilyHandle} instances.
* @param readOptions {@link ReadOptions} instance.
* @return {@link java.util.List} containing {@link org.rocksdb.RocksIterator}
* instances
*
* @throws RocksDBException thrown if error happens in underlying
* native library.
*/
public
List
<
RocksIterator
>
newIterators
(
List
<
ColumnFamilyHandle
>
columnFamilyHandleList
,
ReadOptions
readOptions
)
throws
RocksDBException
{
List
<
RocksIterator
>
iterators
=
new
ArrayList
<>(
columnFamilyHandleList
.
size
());
long
[]
iteratorRefs
=
iterators
(
nativeHandle_
,
columnFamilyHandleList
);
long
[]
iteratorRefs
=
iterators
(
nativeHandle_
,
columnFamilyHandleList
,
readOptions
.
nativeHandle_
);
for
(
int
i
=
0
;
i
<
columnFamilyHandleList
.
size
();
i
++){
iterators
.
add
(
new
RocksIterator
(
this
,
iteratorRefs
[
i
]));
}
...
...
@@ -1637,10 +1700,14 @@ public class RocksDB extends RocksObject {
String
property
,
int
propertyLength
)
throws
RocksDBException
;
protected
native
long
getLongProperty
(
long
nativeHandle
,
long
cfHandle
,
String
property
,
int
propertyLength
)
throws
RocksDBException
;
protected
native
long
iterator0
(
long
handle
);
protected
native
long
iterator0
(
long
handle
,
long
cfHandle
);
protected
native
long
iterator
(
long
handle
);
protected
native
long
iterator
(
long
handle
,
long
readOptHandle
);
protected
native
long
iteratorCF
(
long
handle
,
long
cfHandle
);
protected
native
long
iteratorCF
(
long
handle
,
long
cfHandle
,
long
readOptHandle
);
protected
native
long
[]
iterators
(
long
handle
,
List
<
ColumnFamilyHandle
>
columnFamilyNames
)
throws
RocksDBException
;
List
<
ColumnFamilyHandle
>
columnFamilyNames
,
long
readOptHandle
)
throws
RocksDBException
;
protected
native
long
getSnapshot
(
long
nativeHandle
);
protected
native
void
releaseSnapshot
(
long
nativeHandle
,
long
snapshotHandle
);
...
...
java/org/rocksdb/test/SnapshotTest.java
浏览文件 @
a76d9286
...
...
@@ -8,11 +8,7 @@ import org.junit.ClassRule;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.TemporaryFolder
;
import
org.rocksdb.Options
;
import
org.rocksdb.ReadOptions
;
import
org.rocksdb.RocksDB
;
import
org.rocksdb.RocksDBException
;
import
org.rocksdb.Snapshot
;
import
org.rocksdb.*
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -94,4 +90,127 @@ public class SnapshotTest {
}
}
}
@Test
public
void
iteratorWithSnapshot
()
throws
RocksDBException
{
RocksDB
db
=
null
;
Options
options
=
null
;
ReadOptions
readOptions
=
null
;
RocksIterator
iterator
=
null
;
RocksIterator
snapshotIterator
=
null
;
try
{
options
=
new
Options
();
options
.
setCreateIfMissing
(
true
);
db
=
RocksDB
.
open
(
options
,
dbFolder
.
getRoot
().
getAbsolutePath
());
db
.
put
(
"key"
.
getBytes
(),
"value"
.
getBytes
());
// Get new Snapshot of database
Snapshot
snapshot
=
db
.
getSnapshot
();
readOptions
=
new
ReadOptions
();
// set snapshot in ReadOptions
readOptions
.
setSnapshot
(
snapshot
);
db
.
put
(
"key2"
.
getBytes
(),
"value2"
.
getBytes
());
// iterate over current state of db
iterator
=
db
.
newIterator
();
iterator
.
seekToFirst
();
assertThat
(
iterator
.
isValid
()).
isTrue
();
assertThat
(
iterator
.
key
()).
isEqualTo
(
"key"
.
getBytes
());
iterator
.
next
();
assertThat
(
iterator
.
isValid
()).
isTrue
();
assertThat
(
iterator
.
key
()).
isEqualTo
(
"key2"
.
getBytes
());
iterator
.
next
();
assertThat
(
iterator
.
isValid
()).
isFalse
();
// iterate using a snapshot
snapshotIterator
=
db
.
newIterator
(
readOptions
);
snapshotIterator
.
seekToFirst
();
assertThat
(
snapshotIterator
.
isValid
()).
isTrue
();
assertThat
(
snapshotIterator
.
key
()).
isEqualTo
(
"key"
.
getBytes
());
snapshotIterator
.
next
();
assertThat
(
snapshotIterator
.
isValid
()).
isFalse
();
// release Snapshot
db
.
releaseSnapshot
(
snapshot
);
}
finally
{
if
(
iterator
!=
null
)
{
iterator
.
dispose
();
}
if
(
snapshotIterator
!=
null
)
{
snapshotIterator
.
dispose
();
}
if
(
db
!=
null
)
{
db
.
close
();
}
if
(
options
!=
null
)
{
options
.
dispose
();
}
if
(
readOptions
!=
null
)
{
readOptions
.
dispose
();
}
}
}
@Test
public
void
iteratorWithSnapshotOnColumnFamily
()
throws
RocksDBException
{
RocksDB
db
=
null
;
Options
options
=
null
;
ReadOptions
readOptions
=
null
;
RocksIterator
iterator
=
null
;
RocksIterator
snapshotIterator
=
null
;
try
{
options
=
new
Options
();
options
.
setCreateIfMissing
(
true
);
db
=
RocksDB
.
open
(
options
,
dbFolder
.
getRoot
().
getAbsolutePath
());
db
.
put
(
"key"
.
getBytes
(),
"value"
.
getBytes
());
// Get new Snapshot of database
Snapshot
snapshot
=
db
.
getSnapshot
();
readOptions
=
new
ReadOptions
();
// set snapshot in ReadOptions
readOptions
.
setSnapshot
(
snapshot
);
db
.
put
(
"key2"
.
getBytes
(),
"value2"
.
getBytes
());
// iterate over current state of column family
iterator
=
db
.
newIterator
(
db
.
getDefaultColumnFamily
());
iterator
.
seekToFirst
();
assertThat
(
iterator
.
isValid
()).
isTrue
();
assertThat
(
iterator
.
key
()).
isEqualTo
(
"key"
.
getBytes
());
iterator
.
next
();
assertThat
(
iterator
.
isValid
()).
isTrue
();
assertThat
(
iterator
.
key
()).
isEqualTo
(
"key2"
.
getBytes
());
iterator
.
next
();
assertThat
(
iterator
.
isValid
()).
isFalse
();
// iterate using a snapshot on default column family
snapshotIterator
=
db
.
newIterator
(
db
.
getDefaultColumnFamily
(),
readOptions
);
snapshotIterator
.
seekToFirst
();
assertThat
(
snapshotIterator
.
isValid
()).
isTrue
();
assertThat
(
snapshotIterator
.
key
()).
isEqualTo
(
"key"
.
getBytes
());
snapshotIterator
.
next
();
assertThat
(
snapshotIterator
.
isValid
()).
isFalse
();
// release Snapshot
db
.
releaseSnapshot
(
snapshot
);
}
finally
{
if
(
iterator
!=
null
)
{
iterator
.
dispose
();
}
if
(
snapshotIterator
!=
null
)
{
snapshotIterator
.
dispose
();
}
if
(
db
!=
null
)
{
db
.
close
();
}
if
(
options
!=
null
)
{
options
.
dispose
();
}
if
(
readOptions
!=
null
)
{
readOptions
.
dispose
();
}
}
}
}
java/rocksjni/rocksjni.cc
浏览文件 @
a76d9286
...
...
@@ -1142,40 +1142,85 @@ void Java_org_rocksdb_RocksDB_disposeInternal(
delete
reinterpret_cast
<
rocksdb
::
DB
*>
(
jhandle
);
}
jlong
rocksdb_iterator_helper
(
rocksdb
::
DB
*
db
,
rocksdb
::
ReadOptions
read_options
,
rocksdb
::
ColumnFamilyHandle
*
cf_handle
)
{
rocksdb
::
Iterator
*
iterator
=
nullptr
;
if
(
cf_handle
!=
nullptr
)
{
iterator
=
db
->
NewIterator
(
read_options
,
cf_handle
);
}
else
{
iterator
=
db
->
NewIterator
(
read_options
);
}
return
reinterpret_cast
<
jlong
>
(
iterator
);
}
/*
* Class: org_rocksdb_RocksDB
* Method: iterator
0
* Method: iterator
* Signature: (J)J
*/
jlong
Java_org_rocksdb_RocksDB_iterator
0
__J
(
jlong
Java_org_rocksdb_RocksDB_iterator__J
(
JNIEnv
*
env
,
jobject
jdb
,
jlong
db_handle
)
{
auto
db
=
reinterpret_cast
<
rocksdb
::
DB
*>
(
db_handle
);
rocksdb
::
Iterator
*
iterator
=
db
->
NewIterator
(
rocksdb
::
ReadOptions
());
return
reinterpret_cast
<
jlong
>
(
iterator
);
return
rocksdb_iterator_helper
(
db
,
rocksdb
::
ReadOptions
(),
nullptr
);
}
/*
* Class: org_rocksdb_RocksDB
* Method: iterator
* Signature: (JJ)J
*/
jlong
Java_org_rocksdb_RocksDB_iterator__JJ
(
JNIEnv
*
env
,
jobject
jdb
,
jlong
db_handle
,
jlong
jread_options_handle
)
{
auto
db
=
reinterpret_cast
<
rocksdb
::
DB
*>
(
db_handle
);
auto
&
read_options
=
*
reinterpret_cast
<
rocksdb
::
ReadOptions
*>
(
jread_options_handle
);
return
rocksdb_iterator_helper
(
db
,
read_options
,
nullptr
);
}
/*
* Class: org_rocksdb_RocksDB
* Method: iterator
0
* Method: iterator
CF
* Signature: (JJ)J
*/
jlong
Java_org_rocksdb_RocksDB_iterator
0
__JJ
(
jlong
Java_org_rocksdb_RocksDB_iterator
CF
__JJ
(
JNIEnv
*
env
,
jobject
jdb
,
jlong
db_handle
,
jlong
jcf_handle
)
{
auto
db
=
reinterpret_cast
<
rocksdb
::
DB
*>
(
db_handle
);
auto
cf_handle
=
reinterpret_cast
<
rocksdb
::
ColumnFamilyHandle
*>
(
jcf_handle
);
rocksdb
::
Iterator
*
iterator
=
db
->
NewIterator
(
rocksdb
::
ReadOptions
(),
cf_handle
);
return
reinterpret_cast
<
jlong
>
(
iterator
);
return
rocksdb_iterator_helper
(
db
,
rocksdb
::
ReadOptions
(),
cf_handle
);
}
/*
* Class: org_rocksdb_RocksDB
* Method: iteratorCF
* Signature: (JJJ)J
*/
jlong
Java_org_rocksdb_RocksDB_iteratorCF__JJJ
(
JNIEnv
*
env
,
jobject
jdb
,
jlong
db_handle
,
jlong
jcf_handle
,
jlong
jread_options_handle
)
{
auto
db
=
reinterpret_cast
<
rocksdb
::
DB
*>
(
db_handle
);
auto
cf_handle
=
reinterpret_cast
<
rocksdb
::
ColumnFamilyHandle
*>
(
jcf_handle
);
auto
&
read_options
=
*
reinterpret_cast
<
rocksdb
::
ReadOptions
*>
(
jread_options_handle
);
return
rocksdb_iterator_helper
(
db
,
read_options
,
cf_handle
);
}
/*
* Class: org_rocksdb_RocksDB
* Method: iterators
* Signature: (JLjava/util/List;)[J
* Signature: (JLjava/util/List;
J
)[J
*/
jlongArray
Java_org_rocksdb_RocksDB_iterators
(
JNIEnv
*
env
,
jobject
jdb
,
jlong
db_handle
,
jobject
jcfhandle_list
)
{
JNIEnv
*
env
,
jobject
jdb
,
jlong
db_handle
,
jobject
jcfhandle_list
,
jlong
jread_options_handle
)
{
auto
db
=
reinterpret_cast
<
rocksdb
::
DB
*>
(
db_handle
);
auto
&
read_options
=
*
reinterpret_cast
<
rocksdb
::
ReadOptions
*>
(
jread_options_handle
);
std
::
vector
<
rocksdb
::
ColumnFamilyHandle
*>
cf_handles
;
std
::
vector
<
rocksdb
::
Iterator
*>
iterators
;
...
...
@@ -1195,7 +1240,7 @@ jlongArray Java_org_rocksdb_RocksDB_iterators(
}
}
rocksdb
::
Status
s
=
db
->
NewIterators
(
r
ocksdb
::
ReadOptions
()
,
rocksdb
::
Status
s
=
db
->
NewIterators
(
r
ead_options
,
cf_handles
,
&
iterators
);
if
(
s
.
ok
())
{
jlongArray
jLongArray
=
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录