Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
33a080ef
提交
33a080ef
authored
8月 08, 2018
作者:
Andrei Tokar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
CacheLongKeyLIRS: some re-factoring,
make high water mark for non-resident queue size configurable
上级
dc6b54b1
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
75 行增加
和
99 行删除
+75
-99
CacheLongKeyLIRS.java
h2/src/main/org/h2/mvstore/cache/CacheLongKeyLIRS.java
+75
-99
没有找到文件。
h2/src/main/org/h2/mvstore/cache/CacheLongKeyLIRS.java
浏览文件 @
33a080ef
...
...
@@ -57,6 +57,7 @@ public class CacheLongKeyLIRS<V> {
private
final
int
segmentMask
;
private
final
int
stackMoveDistance
;
private
final
int
nonResidentQueueSize
;
private
final
int
nonResidentQueueSizeHigh
;
/**
* Create a new cache with the given memory size.
...
...
@@ -67,6 +68,7 @@ public class CacheLongKeyLIRS<V> {
public
CacheLongKeyLIRS
(
Config
config
)
{
setMaxMemory
(
config
.
maxMemory
);
this
.
nonResidentQueueSize
=
config
.
nonResidentQueueSize
;
this
.
nonResidentQueueSizeHigh
=
config
.
nonResidentQueueSizeHigh
;
DataUtils
.
checkArgument
(
Integer
.
bitCount
(
config
.
segmentCount
)
==
1
,
"The segment count must be a power of 2, is {0}"
,
config
.
segmentCount
);
...
...
@@ -85,8 +87,8 @@ public class CacheLongKeyLIRS<V> {
public
void
clear
()
{
long
max
=
getMaxItemSize
();
for
(
int
i
=
0
;
i
<
segmentCount
;
i
++)
{
segments
[
i
]
=
new
Segment
<>(
max
,
stackMoveDistance
,
8
,
nonResidentQueueSize
);
segments
[
i
]
=
new
Segment
<>(
max
,
stackMoveDistance
,
8
,
nonResidentQueueSize
,
nonResidentQueueSizeHigh
);
}
}
...
...
@@ -111,8 +113,8 @@ public class CacheLongKeyLIRS<V> {
* @return true if there is a resident entry
*/
public
boolean
containsKey
(
long
key
)
{
int
hash
=
getHash
(
key
);
return
getSegment
(
hash
).
containsKey
(
key
,
hash
)
;
Entry
<
V
>
e
=
find
(
key
);
return
e
!=
null
&&
e
.
value
!=
null
;
}
/**
...
...
@@ -149,6 +151,10 @@ public class CacheLongKeyLIRS<V> {
* @return the old value, or null if there was no resident entry
*/
public
V
put
(
long
key
,
V
value
,
int
memory
)
{
if
(
value
==
null
)
{
throw
DataUtils
.
newIllegalArgumentException
(
"The value may not be null"
);
}
int
hash
=
getHash
(
key
);
int
segmentIndex
=
getSegmentIndex
(
hash
);
Segment
<
V
>
s
=
segments
[
segmentIndex
];
...
...
@@ -215,8 +221,8 @@ public class CacheLongKeyLIRS<V> {
* @return the memory, or 0 if there is no resident entry
*/
public
int
getMemory
(
long
key
)
{
int
hash
=
getHash
(
key
);
return
getSegment
(
hash
).
getMemory
(
key
,
hash
);
Entry
<
V
>
e
=
find
(
key
);
return
e
==
null
?
0
:
e
.
getMemory
(
);
}
/**
...
...
@@ -229,7 +235,9 @@ public class CacheLongKeyLIRS<V> {
*/
public
V
get
(
long
key
)
{
int
hash
=
getHash
(
key
);
return
getSegment
(
hash
).
get
(
key
,
hash
);
Segment
<
V
>
s
=
getSegment
(
hash
);
Entry
<
V
>
e
=
s
.
find
(
key
,
hash
);
return
s
.
get
(
e
);
}
private
Segment
<
V
>
getSegment
(
int
hash
)
{
...
...
@@ -553,11 +561,17 @@ public class CacheLongKeyLIRS<V> {
private
final
int
mask
;
/**
*
The number of entries in the non-resident queue, as a factor of the
* number of entries in the map.
*
Low watermark for the number of entries in the non-resident queue,
*
as a factor of the
number of entries in the map.
*/
private
final
int
nonResidentQueueSize
;
/**
* High watermark for the number of entries in the non-resident queue,
* as a factor of the number of entries in the map.
*/
private
final
int
nonResidentQueueSizeHigh
;
/**
* The stack of recently referenced elements. This includes all hot
* entries, and the recently referenced cold entries. Resident cold
...
...
@@ -594,18 +608,19 @@ public class CacheLongKeyLIRS<V> {
/**
* Create a new cache segment.
*
* @param maxMemory the maximum memory to use
* @param maxMemory the maximum memory to use
* @param stackMoveDistance the number of other entries to be moved to
* the top of the stack before moving an entry to the top
* @param len the number of hash table buckets (must be a power of 2)
* @param nonResidentQueueSize the non-resident queue size factor
* @param nonResidentQueueSize the non-resident queue size low watermark factor
* @param nonResidentQueueSizeHigh the non-resident queue size high watermark factor
*/
Segment
(
long
maxMemory
,
int
stackMoveDistance
,
int
len
,
int
nonResidentQueueSize
)
{
int
nonResidentQueueSize
,
int
nonResidentQueueSizeHigh
)
{
setMaxMemory
(
maxMemory
);
this
.
stackMoveDistance
=
stackMoveDistance
;
this
.
nonResidentQueueSize
=
nonResidentQueueSize
;
this
.
nonResidentQueueSizeHigh
=
nonResidentQueueSizeHigh
;
// the bit mask has all bits set
mask
=
len
-
1
;
...
...
@@ -632,12 +647,13 @@ public class CacheLongKeyLIRS<V> {
* @param len the number of hash table buckets (must be a power of 2)
*/
Segment
(
Segment
<
V
>
old
,
int
len
)
{
this
(
old
.
maxMemory
,
old
.
stackMoveDistance
,
len
,
old
.
nonResidentQueueSize
);
this
(
old
.
maxMemory
,
old
.
stackMoveDistance
,
len
,
old
.
nonResidentQueueSize
,
old
.
nonResidentQueueSizeHigh
);
hits
=
old
.
hits
;
misses
=
old
.
misses
;
Entry
<
V
>
s
=
old
.
stack
.
stackPrev
;
while
(
s
!=
old
.
stack
)
{
Entry
<
V
>
e
=
copy
(
s
);
Entry
<
V
>
e
=
new
Entry
<>
(
s
);
addToMap
(
e
);
addToStack
(
e
);
s
=
s
.
stackPrev
;
...
...
@@ -646,7 +662,7 @@ public class CacheLongKeyLIRS<V> {
while
(
s
!=
old
.
queue
)
{
Entry
<
V
>
e
=
find
(
s
.
key
,
getHash
(
s
.
key
));
if
(
e
==
null
)
{
e
=
copy
(
s
);
e
=
new
Entry
<>
(
s
);
addToMap
(
e
);
}
addToQueue
(
queue
,
e
);
...
...
@@ -656,7 +672,7 @@ public class CacheLongKeyLIRS<V> {
while
(
s
!=
old
.
queue2
)
{
Entry
<
V
>
e
=
find
(
s
.
key
,
getHash
(
s
.
key
));
if
(
e
==
null
)
{
e
=
copy
(
s
);
e
=
new
Entry
<>
(
s
);
addToMap
(
e
);
}
addToQueue
(
queue2
,
e
);
...
...
@@ -690,54 +706,25 @@ public class CacheLongKeyLIRS<V> {
mapSize
++;
}
private
static
<
V
>
Entry
<
V
>
copy
(
Entry
<
V
>
old
)
{
Entry
<
V
>
e
=
new
Entry
<>(
old
.
memory
);
e
.
key
=
old
.
key
;
e
.
value
=
old
.
value
;
e
.
reference
=
old
.
reference
;
e
.
topMove
=
old
.
topMove
;
return
e
;
}
/**
* Get the memory used for the given key.
* Get the value from the given entry.
* This method adjusts the internal state of the cache sometimes,
* to ensure commonly used entries stay in the cache.
*
* @param key the key (may not be null)
* @param hash the hash
* @return the memory, or 0 if there is no resident entry
*/
int
getMemory
(
long
key
,
int
hash
)
{
Entry
<
V
>
e
=
find
(
key
,
hash
);
return
e
==
null
?
0
:
e
.
getMemory
();
}
/**
* Get the value for the given key if the entry is cached. This method
* adjusts the internal state of the cache sometimes, to ensure commonly
* used entries stay in the cache.
*
* @param key the key (may not be null)
* @param hash the hash
* @param e the entry
* @return the value, or null if there is no resident entry
*/
V
get
(
long
key
,
int
hash
)
{
Entry
<
V
>
e
=
find
(
key
,
hash
);
synchronized
(
this
)
{
if
(
e
==
null
)
{
// the entry was not found
misses
++;
return
null
;
}
V
value
=
e
.
getValue
();
if
(
value
==
null
)
{
// it was a non-resident entry
misses
++;
return
null
;
}
synchronized
V
get
(
Entry
<
V
>
e
)
{
V
value
=
e
==
null
?
null
:
e
.
getValue
();
if
(
value
==
null
)
{
// the entry was not found
// or it was a non-resident entry
misses
++;
}
else
{
access
(
e
);
hits
++;
return
value
;
}
return
value
;
}
/**
...
...
@@ -749,8 +736,7 @@ public class CacheLongKeyLIRS<V> {
private
void
access
(
Entry
<
V
>
e
)
{
if
(
e
.
isHot
())
{
if
(
e
!=
stack
.
stackNext
&&
e
.
stackNext
!=
null
)
{
if
(
stackMoveDistance
==
0
||
stackMoveCounter
-
e
.
topMove
>
stackMoveDistance
)
{
if
(
stackMoveCounter
-
e
.
topMove
>
stackMoveDistance
)
{
// move a hot entry to the top of the stack
// unless it is already there
boolean
wasEnd
=
e
==
stack
.
stackPrev
;
...
...
@@ -806,10 +792,6 @@ public class CacheLongKeyLIRS<V> {
* @return the old value, or null if there was no resident entry
*/
synchronized
V
put
(
long
key
,
int
hash
,
V
value
,
int
memory
)
{
if
(
value
==
null
)
{
throw
DataUtils
.
newIllegalArgumentException
(
"The value may not be null"
);
}
Entry
<
V
>
e
=
find
(
key
,
hash
);
boolean
existed
=
e
!=
null
;
V
old
=
null
;
...
...
@@ -821,9 +803,7 @@ public class CacheLongKeyLIRS<V> {
// the new entry is too big to fit
return
old
;
}
e
=
new
Entry
<>(
memory
);
e
.
key
=
key
;
e
.
value
=
value
;
e
=
new
Entry
<>(
key
,
value
,
memory
);
int
index
=
hash
&
mask
;
e
.
mapNext
=
entries
[
index
];
entries
[
index
]
=
e
;
...
...
@@ -930,19 +910,16 @@ public class CacheLongKeyLIRS<V> {
}
void
trimNonResidentQueue
()
{
Entry
<
V
>
e
;
int
maxQueue2Size
=
4
*
nonResidentQueueSize
*
(
mapSize
-
queue2Size
);
int
residentCount
=
mapSize
-
queue2Size
;
int
maxQueue2SizeHigh
=
nonResidentQueueSizeHigh
*
residentCount
;
int
maxQueue2Size
=
nonResidentQueueSize
*
residentCount
;
while
(
queue2Size
>
maxQueue2Size
)
{
e
=
queue2
.
queuePrev
;
int
hash
=
getHash
(
e
.
key
);
remove
(
e
.
key
,
hash
);
}
maxQueue2Size
>>=
2
;
while
(
queue2Size
>
maxQueue2Size
)
{
e
=
queue2
.
queuePrev
;
WeakReference
<
V
>
reference
=
e
.
reference
;
if
(
reference
!=
null
&&
reference
.
get
()
!=
null
)
{
break
;
// stop trimming if entry holds a value
Entry
<
V
>
e
=
queue2
.
queuePrev
;
if
(
queue2Size
<=
maxQueue2SizeHigh
)
{
WeakReference
<
V
>
reference
=
e
.
reference
;
if
(
reference
!=
null
&&
reference
.
get
()
!=
null
)
{
break
;
// stop trimming if entry holds a value
}
}
int
hash
=
getHash
(
e
.
key
);
remove
(
e
.
key
,
hash
);
...
...
@@ -1075,19 +1052,6 @@ public class CacheLongKeyLIRS<V> {
return
keys
;
}
/**
* Check whether there is a resident entry for the given key. This
* method does not adjust the internal state of the cache.
*
* @param key the key (may not be null)
* @param hash the hash
* @return true if there is a resident entry
*/
boolean
containsKey
(
long
key
,
int
hash
)
{
Entry
<
V
>
e
=
find
(
key
,
hash
);
return
e
!=
null
&&
e
.
value
!=
null
;
}
/**
* Get the set of keys for resident entries.
*
...
...
@@ -1131,7 +1095,7 @@ public class CacheLongKeyLIRS<V> {
/**
* The key.
*/
long
key
;
final
long
key
;
/**
* The value. Set to null for non-resident-cold entries.
...
...
@@ -1180,12 +1144,20 @@ public class CacheLongKeyLIRS<V> {
Entry
<
V
>
mapNext
;
public
Entry
()
{
this
(
0
);
Entry
()
{
this
(
0
L
,
null
,
0
);
}
public
Entry
(
int
memory
)
{
Entry
(
long
key
,
V
value
,
int
memory
)
{
this
.
key
=
key
;
this
.
memory
=
memory
;
this
.
value
=
value
;
}
Entry
(
Entry
<
V
>
old
)
{
this
(
old
.
key
,
old
.
value
,
old
.
memory
);
this
.
reference
=
old
.
reference
;
this
.
topMove
=
old
.
topMove
;
}
/**
...
...
@@ -1228,11 +1200,15 @@ public class CacheLongKeyLIRS<V> {
public
int
stackMoveDistance
=
32
;
/**
*
The number of entries in the non-resident queue, as a factor of the
* number of all other entries in the map.
*
Low water mark for the number of entries in the non-resident queue,
*
as a factor of the
number of all other entries in the map.
*/
public
final
int
nonResidentQueueSize
=
3
;
/**
* High watermark for the number of entries in the non-resident queue,
* as a factor of the number of all other entries in the map
*/
public
final
int
nonResidentQueueSizeHigh
=
12
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论