Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
8a5b3ca8
提交
8a5b3ca8
authored
6 年前
作者:
Andrei Tokar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use weak references in LIRS cache
上级
0494963b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
135 行增加
和
105 行删除
+135
-105
CacheLongKeyLIRS.java
h2/src/main/org/h2/mvstore/cache/CacheLongKeyLIRS.java
+93
-71
TestCacheLongKeyLIRS.java
h2/src/test/org/h2/test/store/TestCacheLongKeyLIRS.java
+42
-34
没有找到文件。
h2/src/main/org/h2/mvstore/cache/CacheLongKeyLIRS.java
浏览文件 @
8a5b3ca8
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
*/
*/
package
org
.
h2
.
mvstore
.
cache
;
package
org
.
h2
.
mvstore
.
cache
;
import
java.lang.ref.WeakReference
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.HashSet
;
...
@@ -123,7 +124,7 @@ public class CacheLongKeyLIRS<V> {
...
@@ -123,7 +124,7 @@ public class CacheLongKeyLIRS<V> {
*/
*/
public
V
peek
(
long
key
)
{
public
V
peek
(
long
key
)
{
Entry
<
V
>
e
=
find
(
key
);
Entry
<
V
>
e
=
find
(
key
);
return
e
==
null
?
null
:
e
.
value
;
return
e
==
null
?
null
:
e
.
getValue
()
;
}
}
/**
/**
...
@@ -246,7 +247,7 @@ public class CacheLongKeyLIRS<V> {
...
@@ -246,7 +247,7 @@ public class CacheLongKeyLIRS<V> {
* @param key the key
* @param key the key
* @return the hash code
* @return the hash code
*/
*/
static
int
getHash
(
long
key
)
{
private
static
int
getHash
(
long
key
)
{
int
hash
=
(
int
)
((
key
>>>
32
)
^
key
);
int
hash
=
(
int
)
((
key
>>>
32
)
^
key
);
// a supplemental secondary hash function
// a supplemental secondary hash function
// to protect against hash codes that don't differ much
// to protect against hash codes that don't differ much
...
@@ -306,7 +307,10 @@ public class CacheLongKeyLIRS<V> {
...
@@ -306,7 +307,10 @@ public class CacheLongKeyLIRS<V> {
public
synchronized
Set
<
Map
.
Entry
<
Long
,
V
>>
entrySet
()
{
public
synchronized
Set
<
Map
.
Entry
<
Long
,
V
>>
entrySet
()
{
HashMap
<
Long
,
V
>
map
=
new
HashMap
<>();
HashMap
<
Long
,
V
>
map
=
new
HashMap
<>();
for
(
long
k
:
keySet
())
{
for
(
long
k
:
keySet
())
{
map
.
put
(
k
,
find
(
k
).
value
);
V
value
=
peek
(
k
);
if
(
value
!=
null
)
{
map
.
put
(
k
,
value
);
}
}
}
return
map
.
entrySet
();
return
map
.
entrySet
();
}
}
...
@@ -426,7 +430,7 @@ public class CacheLongKeyLIRS<V> {
...
@@ -426,7 +430,7 @@ public class CacheLongKeyLIRS<V> {
public
List
<
V
>
values
()
{
public
List
<
V
>
values
()
{
ArrayList
<
V
>
list
=
new
ArrayList
<>();
ArrayList
<
V
>
list
=
new
ArrayList
<>();
for
(
long
k
:
keySet
())
{
for
(
long
k
:
keySet
())
{
V
value
=
find
(
k
).
value
;
V
value
=
peek
(
k
)
;
if
(
value
!=
null
)
{
if
(
value
!=
null
)
{
list
.
add
(
value
);
list
.
add
(
value
);
}
}
...
@@ -449,7 +453,7 @@ public class CacheLongKeyLIRS<V> {
...
@@ -449,7 +453,7 @@ public class CacheLongKeyLIRS<V> {
* @param value the value
* @param value the value
* @return true if it is stored
* @return true if it is stored
*/
*/
public
boolean
containsValue
(
Object
value
)
{
public
boolean
containsValue
(
V
value
)
{
return
getMap
().
containsValue
(
value
);
return
getMap
().
containsValue
(
value
);
}
}
...
@@ -461,7 +465,7 @@ public class CacheLongKeyLIRS<V> {
...
@@ -461,7 +465,7 @@ public class CacheLongKeyLIRS<V> {
public
Map
<
Long
,
V
>
getMap
()
{
public
Map
<
Long
,
V
>
getMap
()
{
HashMap
<
Long
,
V
>
map
=
new
HashMap
<>();
HashMap
<
Long
,
V
>
map
=
new
HashMap
<>();
for
(
long
k
:
keySet
())
{
for
(
long
k
:
keySet
())
{
V
x
=
find
(
k
).
value
;
V
x
=
peek
(
k
)
;
if
(
x
!=
null
)
{
if
(
x
!=
null
)
{
map
.
put
(
k
,
x
);
map
.
put
(
k
,
x
);
}
}
...
@@ -674,15 +678,15 @@ public class CacheLongKeyLIRS<V> {
...
@@ -674,15 +678,15 @@ public class CacheLongKeyLIRS<V> {
int
index
=
getHash
(
e
.
key
)
&
mask
;
int
index
=
getHash
(
e
.
key
)
&
mask
;
e
.
mapNext
=
entries
[
index
];
e
.
mapNext
=
entries
[
index
];
entries
[
index
]
=
e
;
entries
[
index
]
=
e
;
usedMemory
+=
e
.
memory
;
usedMemory
+=
e
.
getMemory
()
;
mapSize
++;
mapSize
++;
}
}
private
static
<
V
>
Entry
<
V
>
copy
(
Entry
<
V
>
old
)
{
private
static
<
V
>
Entry
<
V
>
copy
(
Entry
<
V
>
old
)
{
Entry
<
V
>
e
=
new
Entry
<>();
Entry
<
V
>
e
=
new
Entry
<>(
old
.
memory
);
e
.
key
=
old
.
key
;
e
.
key
=
old
.
key
;
e
.
value
=
old
.
value
;
e
.
value
=
old
.
value
;
e
.
memory
=
old
.
memory
;
e
.
reference
=
old
.
reference
;
e
.
topMove
=
old
.
topMove
;
e
.
topMove
=
old
.
topMove
;
return
e
;
return
e
;
}
}
...
@@ -696,7 +700,7 @@ public class CacheLongKeyLIRS<V> {
...
@@ -696,7 +700,7 @@ public class CacheLongKeyLIRS<V> {
*/
*/
int
getMemory
(
long
key
,
int
hash
)
{
int
getMemory
(
long
key
,
int
hash
)
{
Entry
<
V
>
e
=
find
(
key
,
hash
);
Entry
<
V
>
e
=
find
(
key
,
hash
);
return
e
==
null
?
0
:
e
.
memory
;
return
e
==
null
?
0
:
e
.
getMemory
()
;
}
}
/**
/**
...
@@ -710,44 +714,33 @@ public class CacheLongKeyLIRS<V> {
...
@@ -710,44 +714,33 @@ public class CacheLongKeyLIRS<V> {
*/
*/
V
get
(
long
key
,
int
hash
)
{
V
get
(
long
key
,
int
hash
)
{
Entry
<
V
>
e
=
find
(
key
,
hash
);
Entry
<
V
>
e
=
find
(
key
,
hash
);
if
(
e
==
null
)
{
synchronized
(
this
)
{
// the entry was not found
if
(
e
==
null
)
{
misses
++;
// the entry was not found
return
null
;
misses
++;
}
return
null
;
V
value
=
e
.
value
;
if
(
value
==
null
)
{
// it was a non-resident entry
misses
++;
return
null
;
}
if
(
e
.
isHot
())
{
if
(
e
!=
stack
.
stackNext
)
{
if
(
stackMoveDistance
==
0
||
stackMoveCounter
-
e
.
topMove
>
stackMoveDistance
)
{
access
(
key
,
hash
);
}
}
}
}
else
{
V
value
=
e
.
getValue
();
access
(
key
,
hash
);
if
(
value
==
null
)
{
// it was a non-resident entry
misses
++;
return
null
;
}
access
(
e
);
hits
++;
return
value
;
}
}
hits
++;
return
value
;
}
}
/**
/**
* Access an item, moving the entry to the top of the stack or front of
* Access an item, moving the entry to the top of the stack or front of
* the queue if found.
* the queue if found.
*
*
* @param
key the key
* @param
e entry to record access for
*/
*/
private
synchronized
void
access
(
long
key
,
int
hash
)
{
private
void
access
(
Entry
<
V
>
e
)
{
Entry
<
V
>
e
=
find
(
key
,
hash
);
if
(
e
==
null
||
e
.
value
==
null
)
{
return
;
}
if
(
e
.
isHot
())
{
if
(
e
.
isHot
())
{
if
(
e
!=
stack
.
stackNext
)
{
if
(
e
!=
stack
.
stackNext
&&
e
.
stackNext
!=
null
)
{
if
(
stackMoveDistance
==
0
||
if
(
stackMoveDistance
==
0
||
stackMoveCounter
-
e
.
topMove
>
stackMoveDistance
)
{
stackMoveCounter
-
e
.
topMove
>
stackMoveDistance
)
{
// move a hot entry to the top of the stack
// move a hot entry to the top of the stack
...
@@ -763,22 +756,33 @@ public class CacheLongKeyLIRS<V> {
...
@@ -763,22 +756,33 @@ public class CacheLongKeyLIRS<V> {
}
}
}
}
}
else
{
}
else
{
removeFromQueue
(
e
);
V
v
=
e
.
getValue
();
if
(
e
.
stackNext
!=
null
)
{
if
(
v
!=
null
)
{
// resident cold entries become hot
removeFromQueue
(
e
);
// if they are on the stack
if
(
e
.
reference
!=
null
)
{
removeFromStack
(
e
);
e
.
value
=
v
;
// which means a hot entry needs to become cold
e
.
reference
=
null
;
// (this entry is cold, that means there is at least one
usedMemory
+=
e
.
memory
;
// more entry in the stack, which must be hot)
}
convertOldestHotToCold
();
if
(
e
.
stackNext
!=
null
)
{
}
else
{
// resident, or even non-resident (weak value reference),
// cold entries that are not on the stack
// cold entries become hot if they are on the stack
// move to the front of the queue
removeFromStack
(
e
);
addToQueue
(
queue
,
e
);
// which means a hot entry needs to become cold
// (this entry is cold, that means there is at least one
// more entry in the stack, which must be hot)
convertOldestHotToCold
();
}
else
{
// cold entries that are not on the stack
// move to the front of the queue
addToQueue
(
queue
,
e
);
}
// in any case, the cold entry is moved to the top of the stack
addToStack
(
e
);
// but if newly promoted cold/non-resident is the only entry on a stack now
// that means last one is cold, need to prune
pruneStack
();
}
}
// in any case, the cold entry is moved to the top of the stack
addToStack
(
e
);
}
}
}
}
...
@@ -798,25 +802,20 @@ public class CacheLongKeyLIRS<V> {
...
@@ -798,25 +802,20 @@ public class CacheLongKeyLIRS<V> {
throw
DataUtils
.
newIllegalArgumentException
(
throw
DataUtils
.
newIllegalArgumentException
(
"The value may not be null"
);
"The value may not be null"
);
}
}
V
old
;
Entry
<
V
>
e
=
find
(
key
,
hash
);
Entry
<
V
>
e
=
find
(
key
,
hash
);
boolean
existed
;
boolean
existed
=
e
!=
null
;
if
(
e
==
null
)
{
V
old
=
null
;
existed
=
false
;
if
(
existed
)
{
old
=
null
;
old
=
e
.
getValue
();
}
else
{
existed
=
true
;
old
=
e
.
value
;
remove
(
key
,
hash
);
remove
(
key
,
hash
);
}
}
if
(
memory
>
maxMemory
)
{
if
(
memory
>
maxMemory
)
{
// the new entry is too big to fit
// the new entry is too big to fit
return
old
;
return
old
;
}
}
e
=
new
Entry
<>();
e
=
new
Entry
<>(
memory
);
e
.
key
=
key
;
e
.
key
=
key
;
e
.
value
=
value
;
e
.
value
=
value
;
e
.
memory
=
memory
;
int
index
=
hash
&
mask
;
int
index
=
hash
&
mask
;
e
.
mapNext
=
entries
[
index
];
e
.
mapNext
=
entries
[
index
];
entries
[
index
]
=
e
;
entries
[
index
]
=
e
;
...
@@ -836,7 +835,7 @@ public class CacheLongKeyLIRS<V> {
...
@@ -836,7 +835,7 @@ public class CacheLongKeyLIRS<V> {
addToStack
(
e
);
addToStack
(
e
);
if
(
existed
)
{
if
(
existed
)
{
// if it was there before (even non-resident), it becomes hot
// if it was there before (even non-resident), it becomes hot
access
(
key
,
hash
);
access
(
e
);
}
}
return
old
;
return
old
;
}
}
...
@@ -855,9 +854,7 @@ public class CacheLongKeyLIRS<V> {
...
@@ -855,9 +854,7 @@ public class CacheLongKeyLIRS<V> {
if
(
e
==
null
)
{
if
(
e
==
null
)
{
return
null
;
return
null
;
}
}
V
old
;
if
(
e
.
key
==
key
)
{
if
(
e
.
key
==
key
)
{
old
=
e
.
value
;
entries
[
index
]
=
e
.
mapNext
;
entries
[
index
]
=
e
.
mapNext
;
}
else
{
}
else
{
Entry
<
V
>
last
;
Entry
<
V
>
last
;
...
@@ -868,11 +865,11 @@ public class CacheLongKeyLIRS<V> {
...
@@ -868,11 +865,11 @@ public class CacheLongKeyLIRS<V> {
return
null
;
return
null
;
}
}
}
while
(
e
.
key
!=
key
);
}
while
(
e
.
key
!=
key
);
old
=
e
.
value
;
last
.
mapNext
=
e
.
mapNext
;
last
.
mapNext
=
e
.
mapNext
;
}
}
V
old
=
e
.
getValue
();
mapSize
--;
mapSize
--;
usedMemory
-=
e
.
memory
;
usedMemory
-=
e
.
getMemory
()
;
if
(
e
.
stackNext
!=
null
)
{
if
(
e
.
stackNext
!=
null
)
{
removeFromStack
(
e
);
removeFromStack
(
e
);
}
}
...
@@ -886,10 +883,10 @@ public class CacheLongKeyLIRS<V> {
...
@@ -886,10 +883,10 @@ public class CacheLongKeyLIRS<V> {
addToStackBottom
(
e
);
addToStackBottom
(
e
);
}
}
}
}
pruneStack
();
}
else
{
}
else
{
removeFromQueue
(
e
);
removeFromQueue
(
e
);
}
}
pruneStack
();
return
old
;
return
old
;
}
}
...
@@ -916,14 +913,18 @@ public class CacheLongKeyLIRS<V> {
...
@@ -916,14 +913,18 @@ public class CacheLongKeyLIRS<V> {
Entry
<
V
>
e
=
queue
.
queuePrev
;
Entry
<
V
>
e
=
queue
.
queuePrev
;
usedMemory
-=
e
.
memory
;
usedMemory
-=
e
.
memory
;
removeFromQueue
(
e
);
removeFromQueue
(
e
);
e
.
reference
=
new
WeakReference
<>(
e
.
value
);
e
.
value
=
null
;
e
.
value
=
null
;
e
.
memory
=
0
;
addToQueue
(
queue2
,
e
);
addToQueue
(
queue2
,
e
);
// the size of the non-resident-cold entries needs to be limited
// the size of the non-resident-cold entries needs to be limited
int
maxQueue2Size
=
nonResidentQueueSize
*
(
mapSize
-
queue2Size
);
int
maxQueue2Size
=
nonResidentQueueSize
*
(
mapSize
-
queue2Size
);
if
(
maxQueue2Size
>=
0
)
{
if
(
maxQueue2Size
>=
0
)
{
while
(
queue2Size
>
maxQueue2Size
)
{
while
(
queue2Size
>
maxQueue2Size
)
{
e
=
queue2
.
queuePrev
;
e
=
queue2
.
queuePrev
;
WeakReference
<
V
>
reference
=
e
.
reference
;
if
(
reference
!=
null
&&
reference
.
get
()
!=
null
)
{
break
;
// stop trimming if entry holds a value
}
int
hash
=
getHash
(
e
.
key
);
int
hash
=
getHash
(
e
.
key
);
remove
(
e
.
key
,
hash
);
remove
(
e
.
key
,
hash
);
}
}
...
@@ -1120,10 +1121,15 @@ public class CacheLongKeyLIRS<V> {
...
@@ -1120,10 +1121,15 @@ public class CacheLongKeyLIRS<V> {
*/
*/
V
value
;
V
value
;
/**
* Weak reference to the value. Set to null for resident entries.
*/
WeakReference
<
V
>
reference
;
/**
/**
* The estimated memory used.
* The estimated memory used.
*/
*/
int
memory
;
final
int
memory
;
/**
/**
* When the item was last moved to the top of the stack.
* When the item was last moved to the top of the stack.
...
@@ -1156,6 +1162,15 @@ public class CacheLongKeyLIRS<V> {
...
@@ -1156,6 +1162,15 @@ public class CacheLongKeyLIRS<V> {
*/
*/
Entry
<
V
>
mapNext
;
Entry
<
V
>
mapNext
;
public
Entry
()
{
this
(
0
);
}
public
Entry
(
int
memory
)
{
this
.
memory
=
memory
;
}
/**
/**
* Whether this entry is hot. Cold entries are in one of the two queues.
* Whether this entry is hot. Cold entries are in one of the two queues.
*
*
...
@@ -1165,6 +1180,13 @@ public class CacheLongKeyLIRS<V> {
...
@@ -1165,6 +1180,13 @@ public class CacheLongKeyLIRS<V> {
return
queueNext
==
null
;
return
queueNext
==
null
;
}
}
V
getValue
()
{
return
value
==
null
?
reference
.
get
()
:
value
;
}
int
getMemory
()
{
return
value
==
null
?
0
:
memory
;
}
}
}
/**
/**
...
...
This diff is collapsed.
Click to expand it.
h2/src/test/org/h2/test/store/TestCacheLongKeyLIRS.java
浏览文件 @
8a5b3ca8
...
@@ -46,28 +46,37 @@ public class TestCacheLongKeyLIRS extends TestBase {
...
@@ -46,28 +46,37 @@ public class TestCacheLongKeyLIRS extends TestBase {
testRandomOperations
();
testRandomOperations
();
}
}
private
static
void
testRandomSmallCache
()
{
private
void
testRandomSmallCache
()
{
Random
r
=
new
Random
(
1
);
Random
r
=
new
Random
(
1
);
for
(
int
i
=
0
;
i
<
10000
;
i
++)
{
for
(
int
i
=
0
;
i
<
10000
;
i
++)
{
int
j
=
0
;
int
j
=
0
;
StringBuilder
buff
=
new
StringBuilder
();
StringBuilder
buff
=
new
StringBuilder
();
CacheLongKeyLIRS
<
Integer
>
test
=
createCache
(
1
+
r
.
nextInt
(
10
));
int
maxSize
=
1
+
r
.
nextInt
(
10
);
buff
.
append
(
"size:"
).
append
(
maxSize
).
append
(
'\n'
);
CacheLongKeyLIRS
<
Integer
>
test
=
createCache
(
maxSize
);
for
(;
j
<
30
;
j
++)
{
for
(;
j
<
30
;
j
++)
{
int
key
=
r
.
nextInt
(
5
);
String
lastState
=
toString
(
test
);
switch
(
r
.
nextInt
(
3
))
{
try
{
case
0
:
int
key
=
r
.
nextInt
(
5
);
int
memory
=
r
.
nextInt
(
5
)
+
1
;
switch
(
r
.
nextInt
(
3
))
{
buff
.
append
(
"add "
).
append
(
key
).
append
(
' '
).
case
0
:
append
(
memory
).
append
(
'\n'
);
int
memory
=
r
.
nextInt
(
5
)
+
1
;
test
.
put
(
key
,
j
,
memory
);
buff
.
append
(
"add "
).
append
(
key
).
append
(
' '
).
break
;
append
(
memory
).
append
(
'\n'
);
case
1
:
test
.
put
(
key
,
j
,
memory
);
buff
.
append
(
"remove "
).
append
(
key
).
append
(
'\n'
);
break
;
test
.
remove
(
key
);
case
1
:
break
;
buff
.
append
(
"remove "
).
append
(
key
).
append
(
'\n'
);
case
2
:
test
.
remove
(
key
);
buff
.
append
(
"get "
).
append
(
key
).
append
(
'\n'
);
break
;
test
.
get
(
key
);
case
2
:
buff
.
append
(
"get "
).
append
(
key
).
append
(
'\n'
);
test
.
get
(
key
);
}
verify
(
test
,
null
);
}
catch
(
Throwable
ex
)
{
println
(
i
+
"\n"
+
buff
+
"\n"
+
lastState
+
"\n"
+
toString
(
test
));
throw
ex
;
}
}
}
}
}
}
...
@@ -164,22 +173,22 @@ public class TestCacheLongKeyLIRS extends TestBase {
...
@@ -164,22 +173,22 @@ public class TestCacheLongKeyLIRS extends TestBase {
assertEquals
(
1
,
test
.
getMemory
(
5
));
assertEquals
(
1
,
test
.
getMemory
(
5
));
assertEquals
(
0
,
test
.
getMemory
(
4
));
assertEquals
(
0
,
test
.
getMemory
(
4
));
assertEquals
(
0
,
test
.
getMemory
(
100
));
assertEquals
(
0
,
test
.
getMemory
(
100
));
assertNull
(
test
.
peek
(
4
));
assertN
otN
ull
(
test
.
peek
(
4
));
assertNull
(
test
.
get
(
4
));
assertN
otN
ull
(
test
.
get
(
4
));
assertEquals
(
10
,
test
.
get
(
1
).
intValue
());
assertEquals
(
10
,
test
.
get
(
1
).
intValue
());
assertEquals
(
20
,
test
.
get
(
2
).
intValue
());
assertEquals
(
20
,
test
.
get
(
2
).
intValue
());
assertEquals
(
30
,
test
.
get
(
3
).
intValue
());
assertEquals
(
30
,
test
.
get
(
3
).
intValue
());
verify
(
test
,
"mem:
4 stack: 3 2 1 cold: 5 non-resident: 4
"
);
verify
(
test
,
"mem:
5 stack: 3 2 1 cold: 4 5 non-resident:
"
);
assertEquals
(
50
,
test
.
get
(
5
).
intValue
());
assertEquals
(
50
,
test
.
get
(
5
).
intValue
());
verify
(
test
,
"mem:
4 stack: 5 3 2 1 cold: 5 non-resident: 4
"
);
verify
(
test
,
"mem:
5 stack: 5 3 2 1 cold: 5 4 non-resident:
"
);
assertEquals
(
50
,
test
.
get
(
5
).
intValue
());
assertEquals
(
50
,
test
.
get
(
5
).
intValue
());
verify
(
test
,
"mem:
4 stack: 5 3 2 cold: 1 non-resident: 4
"
);
verify
(
test
,
"mem:
5 stack: 5 3 2 cold: 1 4 non-resident:
"
);
// remove
// remove
assertEquals
(
50
,
test
.
remove
(
5
).
intValue
());
assertEquals
(
50
,
test
.
remove
(
5
).
intValue
());
assertNull
(
test
.
remove
(
5
));
assertNull
(
test
.
remove
(
5
));
verify
(
test
,
"mem:
3 stack: 3 2 1 cold: non-resident: 4
"
);
verify
(
test
,
"mem:
4 stack: 3 2 1 cold: 4 non-resident:
"
);
assertNull
(
test
.
remove
(
4
));
assertN
otN
ull
(
test
.
remove
(
4
));
verify
(
test
,
"mem: 3 stack: 3 2 1 cold: non-resident:"
);
verify
(
test
,
"mem: 3 stack: 3 2 1 cold: non-resident:"
);
assertNull
(
test
.
remove
(
4
));
assertNull
(
test
.
remove
(
4
));
verify
(
test
,
"mem: 3 stack: 3 2 1 cold: non-resident:"
);
verify
(
test
,
"mem: 3 stack: 3 2 1 cold: non-resident:"
);
...
@@ -195,7 +204,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
...
@@ -195,7 +204,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
verify
(
test
,
"mem: 3 stack: 4 3 2 cold: non-resident: 1"
);
verify
(
test
,
"mem: 3 stack: 4 3 2 cold: non-resident: 1"
);
assertEquals
(
20
,
test
.
remove
(
2
).
intValue
());
assertEquals
(
20
,
test
.
remove
(
2
).
intValue
());
assertFalse
(
test
.
containsKey
(
1
));
assertFalse
(
test
.
containsKey
(
1
));
assert
Null
(
test
.
remove
(
1
));
assert
Equals
(
10
,
test
.
remove
(
1
).
intValue
(
));
assertFalse
(
test
.
containsKey
(
1
));
assertFalse
(
test
.
containsKey
(
1
));
verify
(
test
,
"mem: 2 stack: 4 3 cold: non-resident:"
);
verify
(
test
,
"mem: 2 stack: 4 3 cold: non-resident:"
);
test
.
put
(
1
,
10
);
test
.
put
(
1
,
10
);
...
@@ -226,7 +235,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
...
@@ -226,7 +235,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
// 1 was non-resident, so this should make it hot
// 1 was non-resident, so this should make it hot
test
.
put
(
1
,
10
);
test
.
put
(
1
,
10
);
verify
(
test
,
"mem: 4 stack: 1 5 4 3 cold: 2 non-resident: 5"
);
verify
(
test
,
"mem: 4 stack: 1 5 4 3 cold: 2 non-resident: 5"
);
assert
Fals
e
(
test
.
containsValue
(
50
));
assert
Tru
e
(
test
.
containsValue
(
50
));
test
.
remove
(
2
);
test
.
remove
(
2
);
test
.
remove
(
3
);
test
.
remove
(
3
);
test
.
remove
(
4
);
test
.
remove
(
4
);
...
@@ -332,8 +341,8 @@ public class TestCacheLongKeyLIRS extends TestBase {
...
@@ -332,8 +341,8 @@ public class TestCacheLongKeyLIRS extends TestBase {
for
(
int
i
=
0
;
i
<
20
;
i
++)
{
for
(
int
i
=
0
;
i
<
20
;
i
++)
{
test
.
put
(
i
,
10
*
i
);
test
.
put
(
i
,
10
*
i
);
}
}
verify
(
test
,
"mem: 4 stack: 19 18 17 16 15 14 13 12 11 10 3 2 1 "
+
verify
(
test
,
"mem: 4 stack: 19 18 17 16 15 14 13 12 11 10
9 8 7 6 5 4
3 2 1 "
+
"cold: 19 non-resident: 18 17 16 15 14 13 12 11 1
0"
);
"cold: 19 non-resident: 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4
0"
);
}
}
private
void
testLimitMemory
()
{
private
void
testLimitMemory
()
{
...
@@ -344,13 +353,13 @@ public class TestCacheLongKeyLIRS extends TestBase {
...
@@ -344,13 +353,13 @@ public class TestCacheLongKeyLIRS extends TestBase {
verify
(
test
,
"mem: 4 stack: 4 3 2 1 cold: 4 non-resident: 0"
);
verify
(
test
,
"mem: 4 stack: 4 3 2 1 cold: 4 non-resident: 0"
);
assertTrue
(
""
+
test
.
getUsedMemory
(),
test
.
getUsedMemory
()
<=
4
);
assertTrue
(
""
+
test
.
getUsedMemory
(),
test
.
getUsedMemory
()
<=
4
);
test
.
put
(
6
,
60
,
3
);
test
.
put
(
6
,
60
,
3
);
verify
(
test
,
"mem: 4 stack: 6 4 3 cold: 6 non-resident: 2 1 4"
);
verify
(
test
,
"mem: 4 stack: 6 4 3 cold: 6 non-resident: 2 1 4
0
"
);
assertTrue
(
""
+
test
.
getUsedMemory
(),
test
.
getUsedMemory
()
<=
4
);
assertTrue
(
""
+
test
.
getUsedMemory
(),
test
.
getUsedMemory
()
<=
4
);
test
.
put
(
7
,
70
,
3
);
test
.
put
(
7
,
70
,
3
);
verify
(
test
,
"mem: 4 stack: 7 6
3 cold: 7 non-resident: 6 2 1
"
);
verify
(
test
,
"mem: 4 stack: 7 6
4 3 cold: 7 non-resident: 6 2 1 4 0
"
);
assertTrue
(
""
+
test
.
getUsedMemory
(),
test
.
getUsedMemory
()
<=
4
);
assertTrue
(
""
+
test
.
getUsedMemory
(),
test
.
getUsedMemory
()
<=
4
);
test
.
put
(
8
,
80
,
4
);
test
.
put
(
8
,
80
,
4
);
verify
(
test
,
"mem: 4 stack: 8 cold: non-resident:"
);
verify
(
test
,
"mem: 4 stack: 8 cold: non-resident:
3 7 6 2 1 4 0
"
);
assertTrue
(
""
+
test
.
getUsedMemory
(),
test
.
getUsedMemory
()
<=
4
);
assertTrue
(
""
+
test
.
getUsedMemory
(),
test
.
getUsedMemory
()
<=
4
);
}
}
...
@@ -369,7 +378,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
...
@@ -369,7 +378,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
test
.
put
(
i
,
i
*
10
);
test
.
put
(
i
,
i
*
10
);
test
.
get
(
i
);
test
.
get
(
i
);
if
(
log
)
{
if
(
log
)
{
System
.
out
.
println
(
"get "
+
i
+
" -> "
+
test
);
println
(
"get "
+
i
+
" -> "
+
test
);
}
}
}
}
verify
(
test
,
null
);
verify
(
test
,
null
);
...
@@ -394,14 +403,13 @@ public class TestCacheLongKeyLIRS extends TestBase {
...
@@ -394,14 +403,13 @@ public class TestCacheLongKeyLIRS extends TestBase {
}
}
verify
(
test
,
null
);
verify
(
test
,
null
);
}
}
// ensure 0..9 are hot, 10..17 are not resident, 18..19 are cold
// ensure 0..9 are hot, 10..17 are not resident, 18..19 are cold
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
Integer
x
=
test
.
get
(
i
);
Integer
x
=
test
.
get
(
i
);
if
(
i
<
size
/
2
||
i
==
size
-
1
||
i
==
size
-
2
)
{
if
(
i
<
size
/
2
||
i
==
size
-
1
||
i
==
size
-
2
)
{
assertNotNull
(
"i: "
+
i
,
x
);
assertNotNull
(
"i: "
+
i
,
x
);
assertEquals
(
i
*
10
,
x
.
intValue
());
assertEquals
(
i
*
10
,
x
.
intValue
());
}
else
{
assertNull
(
x
);
}
}
verify
(
test
,
null
);
verify
(
test
,
null
);
}
}
...
...
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论