Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
35c2d677
提交
35c2d677
authored
9月 28, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement Map in TransactionMap
上级
8f3a8e36
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
68 行增加
和
9 行删除
+68
-9
MVMap.java
h2/src/main/org/h2/mvstore/MVMap.java
+3
-2
TransactionMap.java
h2/src/main/org/h2/mvstore/tx/TransactionMap.java
+53
-7
TestTransactionStore.java
h2/src/test/org/h2/test/store/TestTransactionStore.java
+12
-0
没有找到文件。
h2/src/main/org/h2/mvstore/MVMap.java
浏览文件 @
35c2d677
...
...
@@ -950,10 +950,11 @@ public class MVMap<K, V> extends AbstractMap<K, V>
}
/**
* Get the number of entries, as a integer.
Integer.MAX_VALUE is returned if
* there are more than this entries.
* Get the number of entries, as a integer.
{@link Integer#MAX_VALUE} is
*
returned if
there are more than this entries.
*
* @return the number of entries, as an integer
* @see #sizeAsLong()
*/
@Override
public
final
int
size
()
{
...
...
h2/src/main/org/h2/mvstore/tx/TransactionMap.java
浏览文件 @
35c2d677
...
...
@@ -12,10 +12,12 @@ import org.h2.mvstore.Page;
import
org.h2.mvstore.type.DataType
;
import
java.util.AbstractMap
;
import
java.util.AbstractSet
;
import
java.util.BitSet
;
import
java.util.Iterator
;
import
java.util.Map
;
import
java.util.NoSuchElementException
;
import
java.util.Set
;
/**
* A map that supports transactions.
...
...
@@ -23,7 +25,7 @@ import java.util.NoSuchElementException;
* @param <K> the key type
* @param <V> the value type
*/
public
class
TransactionMap
<
K
,
V
>
{
public
class
TransactionMap
<
K
,
V
>
extends
AbstractMap
<
K
,
V
>
{
/**
* The map used for writing (the latest version).
...
...
@@ -53,6 +55,19 @@ public class TransactionMap<K, V> {
return
new
TransactionMap
<>(
transaction
,
map
);
}
/**
* Get the number of entries, as a integer. {@link Integer#MAX_VALUE} is
* returned if there are more than this entries.
*
* @return the number of entries, as an integer
* @see #sizeAsLong()
*/
@Override
public
final
int
size
()
{
long
size
=
sizeAsLong
();
return
size
>
Integer
.
MAX_VALUE
?
Integer
.
MAX_VALUE
:
(
int
)
size
;
}
/**
* Get the size of the raw map. This includes uncommitted entries, and
* transiently removed entries, so it is the maximum number of entries.
...
...
@@ -178,7 +193,8 @@ public class TransactionMap<K, V> {
* @param key the key
* @throws IllegalStateException if a lock timeout occurs
*/
public
V
remove
(
K
key
)
{
@Override
public
V
remove
(
Object
key
)
{
return
set
(
key
,
(
V
)
null
);
}
...
...
@@ -193,6 +209,7 @@ public class TransactionMap<K, V> {
* @return the old value
* @throws IllegalStateException if a lock timeout occurs
*/
@Override
public
V
put
(
K
key
,
V
value
)
{
DataUtils
.
checkArgument
(
value
!=
null
,
"The value may not be null"
);
return
set
(
key
,
value
);
...
...
@@ -207,6 +224,7 @@ public class TransactionMap<K, V> {
* @param value the new value (not null)
* @return the old value
*/
// Do not add @Override, code should be compatible with Java 7
public
V
putIfAbsent
(
K
key
,
V
value
)
{
DataUtils
.
checkArgument
(
value
!=
null
,
"The value may not be null"
);
TxDecisionMaker
decisionMaker
=
new
TxDecisionMaker
.
PutIfAbsentDecisionMaker
(
map
.
getId
(),
key
,
value
,
...
...
@@ -245,12 +263,12 @@ public class TransactionMap<K, V> {
return
result
;
}
private
V
set
(
K
key
,
V
value
)
{
private
V
set
(
Object
key
,
V
value
)
{
TxDecisionMaker
decisionMaker
=
new
TxDecisionMaker
.
PutDecisionMaker
(
map
.
getId
(),
key
,
value
,
transaction
);
return
set
(
key
,
decisionMaker
);
}
private
V
set
(
K
key
,
TxDecisionMaker
decisionMaker
)
{
private
V
set
(
Object
key
,
TxDecisionMaker
decisionMaker
)
{
TransactionStore
store
=
transaction
.
store
;
Transaction
blockingTransaction
;
long
sequenceNumWhenStarted
;
...
...
@@ -262,7 +280,9 @@ public class TransactionMap<K, V> {
// since TxDecisionMaker has it embedded,
// MVRTreeMap has weird traversal logic based on it,
// and any non-null value will do
result
=
map
.
put
(
key
,
VersionedValue
.
DUMMY
,
decisionMaker
);
@SuppressWarnings
(
"unchecked"
)
K
k
=
(
K
)
key
;
result
=
map
.
put
(
k
,
VersionedValue
.
DUMMY
,
decisionMaker
);
MVMap
.
Decision
decision
=
decisionMaker
.
getDecision
();
assert
decision
!=
null
;
...
...
@@ -343,8 +363,9 @@ public class TransactionMap<K, V> {
* @param key the key
* @return the value or null
*/
@Override
@SuppressWarnings
(
"unchecked"
)
public
V
get
(
K
key
)
{
public
V
get
(
Object
key
)
{
VersionedValue
data
=
map
.
get
(
key
);
if
(
data
==
null
)
{
// doesn't exist or deleted by a committed transaction
...
...
@@ -370,7 +391,8 @@ public class TransactionMap<K, V> {
* @param key the key
* @return true if the map contains an entry for this key
*/
public
boolean
containsKey
(
K
key
)
{
@Override
public
boolean
containsKey
(
Object
key
)
{
return
get
(
key
)
!=
null
;
}
...
...
@@ -403,11 +425,34 @@ public class TransactionMap<K, V> {
/**
* Clear the map.
*/
@Override
public
void
clear
()
{
// TODO truncate transactionally?
map
.
clear
();
}
@Override
public
Set
<
Entry
<
K
,
V
>>
entrySet
()
{
return
new
AbstractSet
<
Entry
<
K
,
V
>>()
{
@Override
public
Iterator
<
Entry
<
K
,
V
>>
iterator
()
{
return
entryIterator
(
null
,
null
);
}
@Override
public
int
size
()
{
return
TransactionMap
.
this
.
size
();
}
@Override
public
boolean
contains
(
Object
o
)
{
return
TransactionMap
.
this
.
containsKey
(
o
);
}
};
}
/**
* Get the first key.
*
...
...
@@ -693,4 +738,5 @@ public class TransactionMap<K, V> {
"Removal is not supported"
);
}
}
}
h2/src/test/org/h2/test/store/TestTransactionStore.java
浏览文件 @
35c2d677
...
...
@@ -13,6 +13,7 @@ import java.sql.Statement;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map.Entry
;
import
java.util.Random
;
import
java.util.concurrent.atomic.AtomicInteger
;
...
...
@@ -235,6 +236,7 @@ public class TestTransactionStore extends TestBase {
Random
r
=
new
Random
(
1
);
for
(
int
i
=
0
;
i
<
size
*
3
;
i
++)
{
assertEquals
(
"op: "
+
i
,
size
,
map1
.
size
());
assertEquals
(
"op: "
+
i
,
size
,
(
int
)
map1
.
sizeAsLong
());
// keep the first 10%, and add 10%
int
k
=
size
/
10
+
r
.
nextInt
(
size
);
...
...
@@ -507,6 +509,7 @@ public class TestTransactionStore extends TestBase {
Transaction
tx
,
tx2
;
TransactionMap
<
String
,
String
>
m
,
m2
;
Iterator
<
String
>
it
,
it2
;
Iterator
<
Entry
<
String
,
String
>>
entryIt
;
tx
=
ts
.
begin
();
m
=
tx
.
openMap
(
"test"
);
...
...
@@ -532,6 +535,15 @@ public class TestTransactionStore extends TestBase {
assertEquals
(
"3"
,
it
.
next
());
assertFalse
(
it
.
hasNext
());
entryIt
=
m
.
entrySet
().
iterator
();
assertTrue
(
entryIt
.
hasNext
());
assertEquals
(
"1"
,
entryIt
.
next
().
getKey
());
assertTrue
(
entryIt
.
hasNext
());
assertEquals
(
"2"
,
entryIt
.
next
().
getKey
());
assertTrue
(
entryIt
.
hasNext
());
assertEquals
(
"3"
,
entryIt
.
next
().
getKey
());
assertFalse
(
entryIt
.
hasNext
());
it2
=
m2
.
keyIterator
(
null
);
assertTrue
(
it2
.
hasNext
());
assertEquals
(
"1"
,
it2
.
next
());
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论