Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
5de4e30b
提交
5de4e30b
authored
6 年前
作者:
Andrei Tokar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
locking of the append buffer
上级
d6b7a8d9
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
330 行增加
和
167 行删除
+330
-167
MVMap.java
h2/src/main/org/h2/mvstore/MVMap.java
+299
-166
Page.java
h2/src/main/org/h2/mvstore/Page.java
+30
-0
Transaction.java
h2/src/main/org/h2/mvstore/tx/Transaction.java
+1
-1
没有找到文件。
h2/src/main/org/h2/mvstore/MVMap.java
浏览文件 @
5de4e30b
...
@@ -51,6 +51,8 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -51,6 +51,8 @@ public class MVMap<K, V> extends AbstractMap<K, V>
private
final
K
[]
keysBuffer
;
private
final
K
[]
keysBuffer
;
private
final
V
[]
valuesBuffer
;
private
final
V
[]
valuesBuffer
;
private
final
Object
lock
=
new
Object
();
private
volatile
boolean
notificationRequested
;
/**
/**
* Whether the map is closed. Volatile so we don't accidentally write to a
* Whether the map is closed. Volatile so we don't accidentally write to a
...
@@ -425,7 +427,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -425,7 +427,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
Page
emptyRootPage
=
createEmptyLeaf
();
Page
emptyRootPage
=
createEmptyLeaf
();
int
attempt
=
0
;
int
attempt
=
0
;
do
{
do
{
rootReference
=
getRoot
();
rootReference
=
getRoot
Internal
();
}
while
(!
updateRoot
(
rootReference
,
emptyRootPage
,
++
attempt
));
}
while
(!
updateRoot
(
rootReference
,
emptyRootPage
,
++
attempt
));
rootReference
.
root
.
removeAllRecursive
();
rootReference
.
root
.
removeAllRecursive
();
}
}
...
@@ -780,9 +782,15 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -780,9 +782,15 @@ public class MVMap<K, V> extends AbstractMap<K, V>
}
}
public
final
RootReference
getRoot
()
{
public
final
RootReference
getRoot
()
{
return
flushAndGetRoot
();
}
private
RootReference
flushAndGetRoot
()
{
RootReference
rootReference
=
getRootInternal
();
RootReference
rootReference
=
getRootInternal
();
return
singleWriter
&&
rootReference
.
getAppendCounter
()
>
0
?
if
(
singleWriter
&&
rootReference
.
getAppendCounter
()
>
0
)
{
flushAppendBuffer
(
rootReference
)
:
rootReference
;
return
flushAppendBuffer
(
rootReference
,
false
);
}
return
rootReference
;
}
}
private
RootReference
getRootInternal
()
{
private
RootReference
getRootInternal
()
{
...
@@ -836,7 +844,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -836,7 +844,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
}
}
RootReference
updatedRootReference
=
new
RootReference
(
newRootPage
,
newVersion
,
previous
,
updateCounter
,
RootReference
updatedRootReference
=
new
RootReference
(
newRootPage
,
newVersion
,
previous
,
updateCounter
,
attemptUpdateCounter
,
false
);
attemptUpdateCounter
);
boolean
success
=
root
.
compareAndSet
(
currentRoot
,
updatedRootReference
);
boolean
success
=
root
.
compareAndSet
(
currentRoot
,
updatedRootReference
);
return
success
?
updatedRootReference
:
null
;
return
success
?
updatedRootReference
:
null
;
}
}
...
@@ -969,13 +977,13 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -969,13 +977,13 @@ public class MVMap<K, V> extends AbstractMap<K, V>
* @return the number of entries
* @return the number of entries
*/
*/
public
final
long
sizeAsLong
()
{
public
final
long
sizeAsLong
()
{
RootReference
rootReference
=
getRoot
();
RootReference
rootReference
=
getRoot
Internal
();
return
rootReference
.
root
.
getTotalCount
()
+
rootReference
.
getAppendCounter
();
return
rootReference
.
root
.
getTotalCount
()
+
rootReference
.
getAppendCounter
();
}
}
@Override
@Override
public
boolean
isEmpty
()
{
public
boolean
isEmpty
()
{
RootReference
rootReference
=
getRoot
();
RootReference
rootReference
=
getRoot
Internal
();
Page
rootPage
=
rootReference
.
root
;
Page
rootPage
=
rootReference
.
root
;
return
rootPage
.
isLeaf
()
&&
rootPage
.
getKeyCount
()
==
0
&&
rootReference
.
getAppendCounter
()
==
0
;
return
rootPage
.
isLeaf
()
&&
rootPage
.
getKeyCount
()
==
0
&&
rootReference
.
getAppendCounter
()
==
0
;
}
}
...
@@ -1192,35 +1200,76 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1192,35 +1200,76 @@ public class MVMap<K, V> extends AbstractMap<K, V>
* If map was used in append mode, this method will ensure that append buffer
* If map was used in append mode, this method will ensure that append buffer
* is flushed - emptied with all entries inserted into map as a new leaf.
* is flushed - emptied with all entries inserted into map as a new leaf.
* @param rootReference current RootReference
* @param rootReference current RootReference
* @param lockedForUpdate whether rootReference is pre-locked already and
* should stay locked upon return
* @return potentially updated RootReference
* @return potentially updated RootReference
*/
*/
private
RootReference
flushAppendBuffer
(
RootReference
rootReference
)
{
private
RootReference
flushAppendBuffer
(
RootReference
rootReference
,
boolean
lockedForUpdate
)
{
IntValueHolder
unsavedMemoryHolder
=
new
IntValueHolder
();
RootReference
lockedRootReference
=
lockedForUpdate
?
rootReference
:
null
;
try
{
int
attempt
=
0
;
int
attempt
=
0
;
int
keyCount
;
int
keyCount
;
while
((
keyCount
=
rootReference
.
getAppendCounter
())
>
0
)
{
while
((
keyCount
=
rootReference
.
getAppendCounter
())
>
0
)
{
Page
page
=
Page
.
createLeaf
(
this
,
if
(
lockedRootReference
==
null
)
{
Arrays
.
copyOf
(
keysBuffer
,
keyCount
),
lockedRootReference
=
tryLock
(
rootReference
,
++
attempt
);
Arrays
.
copyOf
(
valuesBuffer
,
keyCount
),
rootReference
=
lockedRootReference
==
null
?
getRootInternal
()
:
lockedRootReference
;
0
);
continue
;
CursorPos
pos
=
rootReference
.
root
.
getAppendCursorPos
(
null
);
}
assert
page
.
map
==
this
;
Page
rootPage
=
rootReference
.
root
;
CursorPos
pos
=
rootPage
.
getAppendCursorPos
(
null
);
assert
pos
!=
null
;
assert
pos
!=
null
;
assert
page
.
getKeyCount
()
>
0
;
Object
key
=
page
.
getKey
(
0
);
assert
pos
.
index
<
0
:
pos
.
index
;
assert
pos
.
index
<
0
:
pos
.
index
;
int
index
=
-
pos
.
index
-
1
;
int
index
=
-
pos
.
index
-
1
;
assert
index
==
pos
.
page
.
getKeyCount
()
:
index
+
" != "
+
pos
.
page
.
getKeyCount
();
assert
index
==
pos
.
page
.
getKeyCount
()
:
index
+
" != "
+
pos
.
page
.
getKeyCount
();
Page
p
=
pos
.
page
;
Page
p
=
pos
.
page
;
pos
=
pos
.
parent
;
CursorPos
tip
=
pos
;
CursorPos
tip
=
pos
;
int
unsavedMemory
=
page
.
getMemory
();
pos
=
pos
.
parent
;
int
remainingBuffer
=
0
;
Page
page
=
null
;
int
available
=
store
.
getKeysPerPage
()
-
p
.
getKeyCount
();
if
(
available
>
0
)
{
p
=
p
.
copy
();
if
(
keyCount
<=
available
)
{
p
.
expand
(
keyCount
,
keysBuffer
,
valuesBuffer
);
}
else
{
p
.
expand
(
available
,
keysBuffer
,
valuesBuffer
);
keyCount
-=
available
;
if
(
lockedForUpdate
)
{
System
.
arraycopy
(
keysBuffer
,
available
,
keysBuffer
,
0
,
keyCount
);
System
.
arraycopy
(
valuesBuffer
,
available
,
valuesBuffer
,
0
,
keyCount
);
remainingBuffer
=
keyCount
;
}
else
{
Object
[]
keys
=
new
Object
[
keyCount
];
Object
[]
values
=
new
Object
[
keyCount
];
System
.
arraycopy
(
keysBuffer
,
available
,
keys
,
0
,
keyCount
);
System
.
arraycopy
(
valuesBuffer
,
available
,
values
,
0
,
keyCount
);
page
=
Page
.
createLeaf
(
this
,
keys
,
values
,
0
);
}
}
}
else
{
page
=
Page
.
createLeaf
(
this
,
Arrays
.
copyOf
(
keysBuffer
,
keyCount
),
Arrays
.
copyOf
(
valuesBuffer
,
keyCount
),
0
);
}
unsavedMemoryHolder
.
value
=
0
;
if
(
page
!=
null
)
{
assert
page
.
map
==
this
;
assert
page
.
getKeyCount
()
>
0
;
Object
key
=
page
.
getKey
(
0
);
unsavedMemoryHolder
.
value
+=
page
.
getMemory
();
while
(
true
)
{
while
(
true
)
{
if
(
pos
==
null
)
{
if
(
pos
==
null
)
{
if
(
p
.
getKeyCount
()
==
0
)
{
if
(
p
.
getKeyCount
()
==
0
)
{
p
=
page
;
p
=
page
;
}
else
{
}
else
{
Object
[]
keys
=
new
Object
[]
{
key
};
Object
[]
keys
=
new
Object
[]{
key
};
Page
.
PageReference
[]
children
=
new
Page
.
PageReference
[]
{
Page
.
PageReference
[]
children
=
new
Page
.
PageReference
[]
{
new
Page
.
PageReference
(
p
),
new
Page
.
PageReference
(
p
),
new
Page
.
PageReference
(
page
)};
new
Page
.
PageReference
(
page
)};
p
=
Page
.
createNode
(
this
,
keys
,
children
,
p
.
getTotalCount
()
+
page
.
getTotalCount
(),
0
);
p
=
Page
.
createNode
(
this
,
keys
,
children
,
p
.
getTotalCount
()
+
page
.
getTotalCount
(),
0
);
...
@@ -1241,34 +1290,49 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1241,34 +1290,49 @@ public class MVMap<K, V> extends AbstractMap<K, V>
int
at
=
keyCount
-
2
;
int
at
=
keyCount
-
2
;
key
=
p
.
getKey
(
at
);
key
=
p
.
getKey
(
at
);
page
=
p
.
split
(
at
);
page
=
p
.
split
(
at
);
unsavedMemory
+=
p
.
getMemory
()
+
page
.
getMemory
();
unsavedMemoryHolder
.
value
+=
p
.
getMemory
()
+
page
.
getMemory
();
}
}
unsavedMemory
+=
p
.
getMemory
();
while
(
pos
!=
null
)
{
Page
c
=
p
;
p
=
pos
.
page
;
p
=
p
.
copy
();
p
.
setChild
(
pos
.
index
,
c
);
unsavedMemory
+=
p
.
getMemory
();
pos
=
pos
.
parent
;
}
}
RootReference
updatedRootReference
=
new
RootReference
(
rootReference
,
p
,
++
attempt
);
p
=
replacePage
(
pos
,
p
,
unsavedMemoryHolder
);
if
(
root
.
compareAndSet
(
rootReference
,
updatedRootReference
))
{
RootReference
updatedRootReference
=
new
RootReference
(
rootReference
,
p
,
remainingBuffer
,
lockedForUpdate
);
if
(
root
.
compareAndSet
(
rootReference
,
updatedRootReference
))
{
lockedRootReference
=
null
;
while
(
tip
!=
null
)
{
while
(
tip
!=
null
)
{
tip
.
page
.
removePage
();
tip
.
page
.
removePage
();
tip
=
tip
.
parent
;
tip
=
tip
.
parent
;
}
}
if
(
store
.
getFileStore
()
!=
null
)
{
if
(
store
.
getFileStore
()
!=
null
)
{
store
.
registerUnsavedPage
(
unsavedMemory
);
store
.
registerUnsavedPage
(
unsavedMemoryHolder
.
value
);
}
}
assert
updatedRootReference
.
getAppendCounter
()
==
0
;
assert
lockedForUpdate
||
updatedRootReference
.
getAppendCounter
()
==
0
;
return
updatedRootReference
;
return
updatedRootReference
;
}
}
rootReference
=
getRootInternal
();
rootReference
=
getRootInternal
();
}
}
}
finally
{
if
(
lockedRootReference
!=
null
&&
!
lockedForUpdate
)
{
assert
rootReference
.
root
==
lockedRootReference
.
root
;
// assert rootReference.appendCounter == lockedRootReference.appendCounter : rootReference.appendCounter + " != " + lockedRootReference.appendCounter;
rootReference
=
unlockRoot
(
lockedRootReference
.
root
,
lockedRootReference
.
appendCounter
);
}
}
return
rootReference
;
return
rootReference
;
}
}
private
Page
replacePage
(
CursorPos
path
,
Page
replacement
,
IntValueHolder
unsavedMemoryHolder
)
{
int
unsavedMemory
=
replacement
.
getMemory
();
while
(
path
!=
null
)
{
Page
child
=
replacement
;
replacement
=
path
.
page
.
copy
();
replacement
.
setChild
(
path
.
index
,
child
);
unsavedMemory
+=
replacement
.
getMemory
();
path
=
path
.
parent
;
}
unsavedMemoryHolder
.
value
+=
unsavedMemory
;
return
replacement
;
}
/**
/**
* Appends entry to this map. this method is NOT thread safe and can not be used
* Appends entry to this map. this method is NOT thread safe and can not be used
* neither concurrently, nor in combination with any method that updates this map.
* neither concurrently, nor in combination with any method that updates this map.
...
@@ -1279,22 +1343,28 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1279,22 +1343,28 @@ public class MVMap<K, V> extends AbstractMap<K, V>
*/
*/
public
void
append
(
K
key
,
V
value
)
{
public
void
append
(
K
key
,
V
value
)
{
int
attempt
=
0
;
int
attempt
=
0
;
boolean
success
=
false
;
RootReference
lockedRootReference
=
null
;
while
(!
success
)
{
boolean
success
;
do
{
RootReference
rootReference
=
getRootInternal
();
RootReference
rootReference
=
getRootInternal
();
if
(
lockedRootReference
==
null
)
{
lockedRootReference
=
lockRoot
(
rootReference
,
++
attempt
);
rootReference
=
lockedRootReference
;
}
int
appendCounter
=
rootReference
.
getAppendCounter
();
int
appendCounter
=
rootReference
.
getAppendCounter
();
if
(
appendCounter
>=
keysPerPage
)
{
if
(
appendCounter
>=
keysPerPage
)
{
beforeWrite
();
//
beforeWrite();
rootReference
=
flushAppendBuffer
(
rootReference
);
rootReference
=
flushAppendBuffer
(
rootReference
,
true
);
appendCounter
=
rootReference
.
getAppendCounter
();
appendCounter
=
rootReference
.
getAppendCounter
();
assert
appendCounter
<
keysPerPage
;
assert
appendCounter
<
keysPerPage
;
}
}
keysBuffer
[
appendCounter
]
=
key
;
keysBuffer
[
appendCounter
]
=
key
;
valuesBuffer
[
appendCounter
]
=
value
;
valuesBuffer
[
appendCounter
]
=
value
;
RootReference
updatedRootReference
=
new
RootReference
(
rootReference
,
appendCounter
+
1
,
++
attempt
);
RootReference
updatedRootReference
=
new
RootReference
(
rootReference
,
appendCounter
+
1
,
++
attempt
,
false
);
success
=
root
.
compareAndSet
(
rootReference
,
updatedRootReference
);
success
=
root
.
compareAndSet
(
rootReference
,
updatedRootReference
);
}
}
while
(!
success
);
}
}
/**
/**
...
@@ -1304,14 +1374,25 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1304,14 +1374,25 @@ public class MVMap<K, V> extends AbstractMap<K, V>
*/
*/
public
void
trimLast
()
{
public
void
trimLast
()
{
int
attempt
=
0
;
int
attempt
=
0
;
boolean
success
;
RootReference
lockedRootReference
=
null
;
do
{
RootReference
rootReference
=
getRootInternal
();
RootReference
rootReference
=
getRootInternal
();
boolean
success
=
false
;
while
(!
success
)
{
int
appendCounter
=
rootReference
.
getAppendCounter
();
int
appendCounter
=
rootReference
.
getAppendCounter
();
if
(
appendCounter
>
0
)
{
if
(
appendCounter
>
0
)
{
RootReference
updatedRootReference
=
new
RootReference
(
rootReference
,
appendCounter
-
1
,
++
attempt
);
if
(
lockedRootReference
==
null
)
{
lockedRootReference
=
lockRoot
(
rootReference
,
++
attempt
);
rootReference
=
lockedRootReference
;
continue
;
}
// RootReference updatedRootReference = new RootReference(rootReference, appendCounter - 1, ++attempt, rootReference.lockedForUpdate);
RootReference
updatedRootReference
=
new
RootReference
(
rootReference
,
appendCounter
-
1
,
++
attempt
,
false
);
success
=
root
.
compareAndSet
(
rootReference
,
updatedRootReference
);
success
=
root
.
compareAndSet
(
rootReference
,
updatedRootReference
);
rootReference
=
getRootInternal
();
}
else
{
}
else
{
if
(
lockedRootReference
!=
null
)
{
rootReference
=
unlockRoot
(
lockedRootReference
.
root
,
lockedRootReference
.
appendCounter
);
}
assert
rootReference
.
root
.
getKeyCount
()
>
0
;
assert
rootReference
.
root
.
getKeyCount
()
>
0
;
Page
lastLeaf
=
rootReference
.
root
.
getAppendCursorPos
(
null
).
page
;
Page
lastLeaf
=
rootReference
.
root
.
getAppendCursorPos
(
null
).
page
;
assert
lastLeaf
.
isLeaf
();
assert
lastLeaf
.
isLeaf
();
...
@@ -1320,7 +1401,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1320,7 +1401,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
success
=
remove
(
key
)
!=
null
;
success
=
remove
(
key
)
!=
null
;
assert
success
;
assert
success
;
}
}
}
while
(!
success
);
}
}
}
@Override
@Override
...
@@ -1359,38 +1440,46 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1359,38 +1440,46 @@ public class MVMap<K, V> extends AbstractMap<K, V>
*/
*/
public
final
byte
appendCounter
;
public
final
byte
appendCounter
;
RootReference
(
Page
root
,
long
version
,
RootReference
previous
,
RootReference
(
Page
root
,
long
version
,
RootReference
previous
,
long
updateCounter
,
long
updateAttemptCounter
)
{
long
updateCounter
,
long
updateAttemptCounter
,
boolean
lockedForUpdate
)
{
this
.
root
=
root
;
this
.
root
=
root
;
this
.
version
=
version
;
this
.
version
=
version
;
this
.
previous
=
previous
;
this
.
previous
=
previous
;
this
.
updateCounter
=
updateCounter
;
this
.
updateCounter
=
updateCounter
;
this
.
updateAttemptCounter
=
updateAttemptCounter
;
this
.
updateAttemptCounter
=
updateAttemptCounter
;
this
.
lockedForUpdate
=
lockedForUpdat
e
;
this
.
lockedForUpdate
=
fals
e
;
this
.
appendCounter
=
0
;
this
.
appendCounter
=
0
;
}
}
// This one is used for locking
// This one is used for locking
RootReference
(
RootReference
r
)
{
RootReference
(
RootReference
r
,
int
attempt
)
{
this
.
root
=
r
.
root
;
this
.
root
=
r
.
root
;
this
.
version
=
r
.
version
;
this
.
version
=
r
.
version
;
this
.
previous
=
r
.
previous
;
this
.
previous
=
r
.
previous
;
this
.
updateCounter
=
r
.
updateCounter
;
this
.
updateCounter
=
r
.
updateCounter
+
1
;
this
.
updateAttemptCounter
=
r
.
updateAttemptCounter
;
this
.
updateAttemptCounter
=
r
.
updateAttemptCounter
+
attempt
;
this
.
lockedForUpdate
=
true
;
this
.
lockedForUpdate
=
true
;
this
.
appendCounter
=
0
;
this
.
appendCounter
=
r
.
appendCounter
;
}
}
// This one is used for unlocking
// This one is used for unlocking
RootReference
(
RootReference
r
,
Page
root
,
int
a
ttempt
)
{
RootReference
(
RootReference
r
,
Page
root
,
int
a
ppendCounter
)
{
this
.
root
=
root
;
this
.
root
=
root
;
this
.
version
=
r
.
version
;
this
.
version
=
r
.
version
;
this
.
previous
=
r
.
previous
;
this
.
previous
=
r
.
previous
;
this
.
updateCounter
=
r
.
updateCounter
+
1
;
this
.
updateCounter
=
r
.
updateCounter
;
this
.
updateAttemptCounter
=
r
.
updateAttemptCounter
+
attempt
;
this
.
updateAttemptCounter
=
r
.
updateAttemptCounter
;
this
.
lockedForUpdate
=
false
;
this
.
lockedForUpdate
=
false
;
this
.
appendCounter
=
0
;
this
.
appendCounter
=
(
byte
)
appendCounter
;
}
RootReference
(
RootReference
r
,
Page
root
,
int
appendCounter
,
boolean
lockedForUpdate
)
{
this
.
root
=
root
;
this
.
version
=
r
.
version
;
this
.
previous
=
r
.
previous
;
this
.
updateCounter
=
r
.
updateCounter
;
this
.
updateAttemptCounter
=
r
.
updateAttemptCounter
;
this
.
lockedForUpdate
=
lockedForUpdate
;
this
.
appendCounter
=
(
byte
)
appendCounter
;
}
}
// This one is used for version change
// This one is used for version change
...
@@ -1421,13 +1510,13 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1421,13 +1510,13 @@ public class MVMap<K, V> extends AbstractMap<K, V>
}
}
// This one is used for append buffer maintenance
// This one is used for append buffer maintenance
RootReference
(
RootReference
r
,
int
appendCounter
,
int
attempt
)
{
RootReference
(
RootReference
r
,
int
appendCounter
,
int
attempt
,
boolean
lockedForUpdate
)
{
this
.
root
=
r
.
root
;
this
.
root
=
r
.
root
;
this
.
version
=
r
.
version
;
this
.
version
=
r
.
version
;
this
.
previous
=
r
.
previous
;
this
.
previous
=
r
.
previous
;
this
.
updateCounter
=
r
.
updateCounter
+
1
;
this
.
updateCounter
=
r
.
updateCounter
+
1
;
this
.
updateAttemptCounter
=
r
.
updateAttemptCounter
+
attempt
;
this
.
updateAttemptCounter
=
r
.
updateAttemptCounter
+
attempt
;
this
.
lockedForUpdate
=
r
.
lockedForUpdate
;
this
.
lockedForUpdate
=
lockedForUpdate
;
this
.
appendCounter
=
(
byte
)
appendCounter
;
this
.
appendCounter
=
(
byte
)
appendCounter
;
}
}
...
@@ -1703,34 +1792,43 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1703,34 +1792,43 @@ public class MVMap<K, V> extends AbstractMap<K, V>
public
V
operate
(
K
key
,
V
value
,
DecisionMaker
<?
super
V
>
decisionMaker
)
{
public
V
operate
(
K
key
,
V
value
,
DecisionMaker
<?
super
V
>
decisionMaker
)
{
beforeWrite
();
beforeWrite
();
IntValueHolder
unsavedMemoryHolder
=
new
IntValueHolder
();
int
attempt
=
0
;
int
attempt
=
0
;
RootReference
oldRootReference
=
null
;
//
RootReference oldRootReference = null;
while
(
true
)
{
while
(
true
)
{
RootReference
rootReference
=
getRoot
();
RootReference
rootReference
=
getRoot
();
int
contention
=
0
;
// int contention = 1;
if
(
oldRootReference
!=
null
)
{
// if (oldRootReference != null) {
long
updateAttemptCounter
=
rootReference
.
updateAttemptCounter
-
// long updateAttemptCounter = rootReference.updateAttemptCounter -
oldRootReference
.
updateAttemptCounter
;
// oldRootReference.updateAttemptCounter;
assert
updateAttemptCounter
>=
0
:
updateAttemptCounter
;
// assert updateAttemptCounter >= 0 : updateAttemptCounter;
long
updateCounter
=
rootReference
.
updateCounter
-
oldRootReference
.
updateCounter
;
// long updateCounter = rootReference.updateCounter - oldRootReference.updateCounter;
assert
updateCounter
>=
0
:
updateCounter
;
// assert updateCounter >= 0 : updateCounter;
assert
updateAttemptCounter
>=
updateCounter
:
updateAttemptCounter
+
" >= "
+
updateCounter
;
// assert updateAttemptCounter >= updateCounter : updateAttemptCounter + " >= " + updateCounter;
contention
=
(
int
)((
updateAttemptCounter
+
1
)
/
(
updateCounter
+
1
));
// contention = (int)((updateAttemptCounter+1) / (updateCounter+1));
// }
// oldRootReference = rootReference;
RootReference
lockedRootReference
=
null
;
if
((++
attempt
>
3
||
rootReference
.
lockedForUpdate
))
{
lockedRootReference
=
lockRoot
(
rootReference
,
attempt
);
rootReference
=
lockedRootReference
;
}
}
oldRootReference
=
rootReference
;
Page
rootPage
=
rootReference
.
root
;
++
attempt
;
int
appendCounter
=
rootReference
.
getAppendCounter
();
CursorPos
pos
=
traverseDown
(
rootReference
.
root
,
key
);
CursorPos
tip
;
V
result
;
unsavedMemoryHolder
.
value
=
0
;
try
{
CursorPos
pos
=
traverseDown
(
rootPage
,
key
);
Page
p
=
pos
.
page
;
Page
p
=
pos
.
page
;
int
index
=
pos
.
index
;
int
index
=
pos
.
index
;
CursorPos
tip
=
pos
;
tip
=
pos
;
pos
=
pos
.
parent
;
pos
=
pos
.
parent
;
@SuppressWarnings
(
"unchecked"
)
//noinspection unchecked
V
result
=
index
<
0
?
null
:
(
V
)
p
.
getValue
(
index
);
result
=
index
<
0
?
null
:
(
V
)
p
.
getValue
(
index
);
Decision
decision
=
decisionMaker
.
decide
(
result
,
value
);
Decision
decision
=
decisionMaker
.
decide
(
result
,
value
);
int
unsavedMemory
=
0
;
boolean
needUnlock
=
false
;
try
{
switch
(
decision
)
{
switch
(
decision
)
{
case
REPEAT:
case
REPEAT:
decisionMaker
.
reset
();
decisionMaker
.
reset
();
...
@@ -1749,10 +1847,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1749,10 +1847,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
}
}
return
null
;
return
null
;
}
}
if
(
attempt
>
2
&&
!(
needUnlock
=
lockRoot
(
decisionMaker
,
rootReference
,
attempt
,
contention
)))
{
continue
;
}
if
(
p
.
getTotalCount
()
==
1
&&
pos
!=
null
)
{
if
(
p
.
getTotalCount
()
==
1
&&
pos
!=
null
)
{
p
=
pos
.
page
;
p
=
pos
.
page
;
index
=
pos
.
index
;
index
=
pos
.
index
;
...
@@ -1769,10 +1864,6 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1769,10 +1864,6 @@ public class MVMap<K, V> extends AbstractMap<K, V>
break
;
break
;
}
}
case
PUT:
{
case
PUT:
{
if
(
attempt
>
2
&&
!(
needUnlock
=
lockRoot
(
decisionMaker
,
rootReference
,
attempt
,
contention
)))
{
continue
;
}
value
=
decisionMaker
.
selectValue
(
result
,
value
);
value
=
decisionMaker
.
selectValue
(
result
,
value
);
p
=
p
.
copy
();
p
=
p
.
copy
();
if
(
index
<
0
)
{
if
(
index
<
0
)
{
...
@@ -1785,11 +1876,10 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1785,11 +1876,10 @@ public class MVMap<K, V> extends AbstractMap<K, V>
int
at
=
keyCount
>>
1
;
int
at
=
keyCount
>>
1
;
Object
k
=
p
.
getKey
(
at
);
Object
k
=
p
.
getKey
(
at
);
Page
split
=
p
.
split
(
at
);
Page
split
=
p
.
split
(
at
);
unsavedMemory
+=
p
.
getMemory
();
unsavedMemoryHolder
.
value
+=
p
.
getMemory
()
+
split
.
getMemory
();
unsavedMemory
+=
split
.
getMemory
();
if
(
pos
==
null
)
{
if
(
pos
==
null
)
{
Object
keys
[]
=
{
k
};
Object
[]
keys
=
{
k
};
Page
.
PageReference
children
[]
=
{
Page
.
PageReference
[]
children
=
{
new
Page
.
PageReference
(
p
),
new
Page
.
PageReference
(
p
),
new
Page
.
PageReference
(
split
)
new
Page
.
PageReference
(
split
)
};
};
...
@@ -1810,6 +1900,8 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1810,6 +1900,8 @@ public class MVMap<K, V> extends AbstractMap<K, V>
break
;
break
;
}
}
}
}
p
=
replacePage
(
pos
,
p
,
unsavedMemoryHolder
);
/*
unsavedMemory += p.getMemory();
unsavedMemory += p.getMemory();
while (pos != null) {
while (pos != null) {
Page c = p;
Page c = p;
...
@@ -1819,61 +1911,99 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1819,61 +1911,99 @@ public class MVMap<K, V> extends AbstractMap<K, V>
unsavedMemory += p.getMemory();
unsavedMemory += p.getMemory();
pos = pos.parent;
pos = pos.parent;
}
}
if
(
needUnlock
)
{
*/
unlockRoot
(
p
,
attempt
);
rootPage
=
p
;
needUnlock
=
false
;
if
(
lockedRootReference
==
null
&&
!
updateRoot
(
rootReference
,
p
,
attempt
))
{
}
else
if
(!
updateRoot
(
rootReference
,
p
,
attempt
))
{
decisionMaker
.
reset
();
decisionMaker
.
reset
();
continue
;
continue
;
}
}
}
finally
{
if
(
lockedRootReference
!=
null
)
{
unlockRoot
(
rootPage
,
appendCounter
);
}
}
while
(
tip
!=
null
)
{
while
(
tip
!=
null
)
{
tip
.
page
.
removePage
();
tip
.
page
.
removePage
();
tip
=
tip
.
parent
;
tip
=
tip
.
parent
;
}
}
if
(
store
.
getFileStore
()
!=
null
)
{
if
(
store
.
getFileStore
()
!=
null
)
{
store
.
registerUnsavedPage
(
unsavedMemory
);
store
.
registerUnsavedPage
(
unsavedMemoryHolder
.
value
);
}
}
return
result
;
return
result
;
}
finally
{
if
(
needUnlock
)
{
unlockRoot
(
rootReference
.
root
,
attempt
);
}
}
}
}
private
RootReference
lockRoot
(
RootReference
rootReference
,
int
attempt
)
{
while
(
true
)
{
RootReference
lockedRootReference
=
tryLock
(
rootReference
,
attempt
++);
if
(
lockedRootReference
!=
null
)
{
return
lockedRootReference
;
}
rootReference
=
getRootInternal
();
}
}
}
}
private
boolean
lockRoot
(
DecisionMaker
<?
super
V
>
decisionMaker
,
RootReference
rootReference
,
private
RootReference
tryLock
(
RootReference
rootReference
,
int
attempt
)
{
int
attempt
,
int
contention
)
{
if
(!
rootReference
.
lockedForUpdate
)
{
boolean
success
=
lockRoot
(
rootReference
);
RootReference
lockedRootReference
=
new
RootReference
(
rootReference
,
attempt
);
if
(!
success
)
{
if
(
root
.
compareAndSet
(
rootReference
,
lockedRootReference
))
{
decisionMaker
.
reset
();
return
lockedRootReference
;
}
}
RootReference
oldRootReference
=
rootReference
.
previous
;
int
contention
=
1
;
if
(
oldRootReference
!=
null
)
{
long
updateAttemptCounter
=
rootReference
.
updateAttemptCounter
-
oldRootReference
.
updateAttemptCounter
;
assert
updateAttemptCounter
>=
0
:
updateAttemptCounter
;
long
updateCounter
=
rootReference
.
updateCounter
-
oldRootReference
.
updateCounter
;
assert
updateCounter
>=
0
:
updateCounter
;
assert
updateAttemptCounter
>=
updateCounter
:
updateAttemptCounter
+
" >= "
+
updateCounter
;
contention
+=
(
int
)((
updateAttemptCounter
+
1
)
/
(
updateCounter
+
1
));
}
if
(
attempt
>
4
)
{
if
(
attempt
>
4
)
{
if
(
attempt
<=
24
)
{
if
(
attempt
<=
12
)
{
Thread
.
yield
();
Thread
.
yield
();
}
else
{
}
else
if
(
attempt
<=
64
)
{
try
{
try
{
Thread
.
sleep
(
0
,
100
/
contention
+
50
);
Thread
.
sleep
(
0
,
10
*
contention
+
5
);
}
catch
(
InterruptedException
ex
)
{
}
catch
(
InterruptedException
ex
)
{
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
}
}
}
else
{
synchronized
(
lock
)
{
notificationRequested
=
true
;
try
{
lock
.
wait
(
0
,
100
);
}
catch
(
InterruptedException
ignore
)
{
}
}
}
}
}
}
return
success
;
}
}
return
null
;
private
boolean
lockRoot
(
RootReference
rootReference
)
{
return
!
rootReference
.
lockedForUpdate
&&
root
.
compareAndSet
(
rootReference
,
new
RootReference
(
rootReference
));
}
}
private
void
unlockRoot
(
Page
newRoot
,
int
attempt
)
{
private
RootReference
unlockRoot
(
Page
newRoot
,
int
appendCounter
)
{
RootReference
updatedRootReference
;
boolean
success
;
boolean
success
;
do
{
do
{
RootReference
rootReference
=
getRoot
();
RootReference
rootReference
=
getRootInternal
();
RootReference
updatedRootReference
=
new
RootReference
(
rootReference
,
newRoot
,
attempt
);
// assert rootReference.lockedForUpdate;
if
(!
rootReference
.
lockedForUpdate
)
{
return
rootReference
;
}
updatedRootReference
=
new
RootReference
(
rootReference
,
newRoot
,
appendCounter
);
success
=
root
.
compareAndSet
(
rootReference
,
updatedRootReference
);
success
=
root
.
compareAndSet
(
rootReference
,
updatedRootReference
);
}
while
(!
success
);
}
while
(!
success
);
if
(
notificationRequested
)
{
synchronized
(
lock
)
{
notificationRequested
=
false
;
lock
.
notifyAll
();;
}
}
return
updatedRootReference
;
}
}
private
static
CursorPos
traverseDown
(
Page
p
,
Object
key
)
{
private
static
CursorPos
traverseDown
(
Page
p
,
Object
key
)
{
...
@@ -1923,4 +2053,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
...
@@ -1923,4 +2053,7 @@ public class MVMap<K, V> extends AbstractMap<K, V>
}
}
}
}
private
static
final
class
IntValueHolder
{
int
value
;
}
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/mvstore/Page.java
浏览文件 @
5de4e30b
...
@@ -493,6 +493,16 @@ public abstract class Page implements Cloneable
...
@@ -493,6 +493,16 @@ public abstract class Page implements Cloneable
return
bKeys
;
return
bKeys
;
}
}
abstract
void
expand
(
int
extraKeyCount
,
Object
[]
extraKeys
,
Object
[]
extraValues
);
final
void
expandKeys
(
int
extraKeyCount
,
Object
[]
extraKeys
)
{
int
keyCount
=
getKeyCount
();
Object
[]
newKeys
=
createKeyStorage
(
keyCount
+
extraKeyCount
);
System
.
arraycopy
(
keys
,
0
,
newKeys
,
0
,
keyCount
);
System
.
arraycopy
(
extraKeys
,
0
,
newKeys
,
keyCount
,
extraKeyCount
);
keys
=
newKeys
;
}
/**
/**
* Get the total number of key-value pairs, including child pages.
* Get the total number of key-value pairs, including child pages.
*
*
...
@@ -1012,6 +1022,11 @@ public abstract class Page implements Cloneable
...
@@ -1012,6 +1022,11 @@ public abstract class Page implements Cloneable
return
newPage
;
return
newPage
;
}
}
@Override
public
void
expand
(
int
keyCount
,
Object
[]
extraKys
,
Object
[]
extraValues
)
{
throw
new
UnsupportedOperationException
();
}
@Override
@Override
public
long
getTotalCount
()
{
public
long
getTotalCount
()
{
assert
!
isComplete
()
||
totalCount
==
calculateTotalCount
()
:
assert
!
isComplete
()
||
totalCount
==
calculateTotalCount
()
:
...
@@ -1324,6 +1339,21 @@ public abstract class Page implements Cloneable
...
@@ -1324,6 +1339,21 @@ public abstract class Page implements Cloneable
return
newPage
;
return
newPage
;
}
}
@Override
public
void
expand
(
int
extraKeyCount
,
Object
[]
extraKeys
,
Object
[]
extraValues
)
{
int
keyCount
=
getKeyCount
();
expandKeys
(
extraKeyCount
,
extraKeys
);
if
(
values
!=
null
)
{
Object
[]
newValues
=
createValueStorage
(
keyCount
+
extraKeyCount
);
System
.
arraycopy
(
values
,
0
,
newValues
,
0
,
keyCount
);
System
.
arraycopy
(
extraValues
,
0
,
newValues
,
keyCount
,
extraKeyCount
);
values
=
newValues
;
}
if
(
isPersistent
())
{
recalculateMemory
();
}
}
@Override
@Override
public
long
getTotalCount
()
{
public
long
getTotalCount
()
{
return
getKeyCount
();
return
getKeyCount
();
...
...
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/mvstore/tx/Transaction.java
浏览文件 @
5de4e30b
...
@@ -577,7 +577,7 @@ public class Transaction {
...
@@ -577,7 +577,7 @@ public class Transaction {
}
}
private
static
String
stateToString
(
long
state
)
{
private
static
String
stateToString
(
long
state
)
{
return
STATUS_NAMES
[
getStatus
(
state
)]
+
(
hasRollback
(
state
)
?
"
"
:
"!
"
)
+
" "
+
getLogId
(
state
);
return
STATUS_NAMES
[
getStatus
(
state
)]
+
(
hasRollback
(
state
)
?
"
<"
:
"
"
)
+
" "
+
getLogId
(
state
);
}
}
...
...
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论