Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
582ca70b
提交
582ca70b
authored
8月 10, 2012
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
A persistent tree map (work in progress).
上级
cc743041
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
435 行增加
和
170 行删除
+435
-170
BtreeMap.java
h2/src/tools/org/h2/dev/store/btree/BtreeMap.java
+120
-61
BtreeMapStore.java
h2/src/tools/org/h2/dev/store/btree/BtreeMapStore.java
+315
-109
没有找到文件。
h2/src/tools/org/h2/dev/store/btree/BtreeMap.java
浏览文件 @
582ca70b
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
package
org
.
h2
.
dev
.
store
.
btree
;
package
org
.
h2
.
dev
.
store
.
btree
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
java.util.TreeMap
;
/**
/**
* A stored map.
* A stored map.
...
@@ -16,35 +17,23 @@ import java.util.Iterator;
...
@@ -16,35 +17,23 @@ import java.util.Iterator;
*/
*/
public
class
BtreeMap
<
K
,
V
>
{
public
class
BtreeMap
<
K
,
V
>
{
private
final
BtreeMapStore
store
;
private
final
int
id
;
private
final
int
id
;
private
final
String
name
;
private
final
String
name
;
private
final
DataType
keyType
;
private
final
DataType
keyType
;
private
final
DataType
valueType
;
private
final
DataType
valueType
;
private
final
long
createVersion
;
private
final
TreeMap
<
Long
,
Page
>
oldRoots
=
new
TreeMap
<
Long
,
Page
>();
private
BtreeMapStore
store
;
private
Page
root
;
private
Page
root
;
private
boolean
readOnly
;
private
BtreeMap
(
BtreeMapStore
store
,
int
id
,
String
name
,
DataType
keyType
,
DataType
valueType
)
{
BtreeMap
(
BtreeMapStore
store
,
int
id
,
String
name
,
DataType
keyType
,
DataType
valueType
,
long
createVersion
)
{
this
.
store
=
store
;
this
.
store
=
store
;
this
.
id
=
id
;
this
.
id
=
id
;
this
.
name
=
name
;
this
.
name
=
name
;
this
.
keyType
=
keyType
;
this
.
keyType
=
keyType
;
this
.
valueType
=
valueType
;
this
.
valueType
=
valueType
;
}
this
.
createVersion
=
createVersion
;
/**
* Open a map.
*
* @param <K> the key type
* @param <V> the value type
* @param store the tree store
* @param id the map id
* @param name the name of the map
* @param keyClass the key class
* @param valueClass the value class
* @return the map
*/
static
<
K
,
V
>
BtreeMap
<
K
,
V
>
open
(
BtreeMapStore
store
,
int
id
,
String
name
,
DataType
keyType
,
DataType
valueType
)
{
return
new
BtreeMap
<
K
,
V
>(
store
,
id
,
name
,
keyType
,
valueType
);
}
}
/**
/**
...
@@ -54,8 +43,10 @@ public class BtreeMap<K, V> {
...
@@ -54,8 +43,10 @@ public class BtreeMap<K, V> {
* @param data the value
* @param data the value
*/
*/
public
void
put
(
K
key
,
V
data
)
{
public
void
put
(
K
key
,
V
data
)
{
markChanged
();
checkWrite
();
root
=
Page
.
put
(
this
,
root
,
key
,
data
);
Page
oldRoot
=
root
;
root
=
Page
.
put
(
this
,
root
,
store
.
getCurrentVersion
(),
key
,
data
);
markChanged
(
oldRoot
);
}
}
/**
/**
...
@@ -66,12 +57,17 @@ public class BtreeMap<K, V> {
...
@@ -66,12 +57,17 @@ public class BtreeMap<K, V> {
*/
*/
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
public
V
get
(
K
key
)
{
public
V
get
(
K
key
)
{
checkOpen
();
if
(
root
==
null
)
{
if
(
root
==
null
)
{
return
null
;
return
null
;
}
}
return
(
V
)
root
.
find
(
key
);
return
(
V
)
root
.
find
(
key
);
}
}
public
boolean
containsKey
(
K
key
)
{
return
get
(
key
)
!=
null
;
}
/**
/**
* Get the page for the given value.
* Get the page for the given value.
*
*
...
@@ -89,19 +85,32 @@ public class BtreeMap<K, V> {
...
@@ -89,19 +85,32 @@ public class BtreeMap<K, V> {
* Remove all entries.
* Remove all entries.
*/
*/
public
void
clear
()
{
public
void
clear
()
{
checkWrite
();
if
(
root
!=
null
)
{
if
(
root
!=
null
)
{
markChanged
()
;
Page
oldRoot
=
root
;
root
.
removeAllRecursive
();
root
.
removeAllRecursive
();
root
=
null
;
root
=
null
;
markChanged
(
oldRoot
);
}
}
}
}
/**
/**
* Remove all entries, and remove the map.
* Remove all entries, and remove the map.
The map becomes invalid.
*/
*/
public
void
remove
()
{
public
void
remove
()
{
clear
();
checkWrite
();
if
(
root
!=
null
)
{
root
.
removeAllRecursive
();
}
store
.
removeMap
(
id
);
store
.
removeMap
(
id
);
oldRoots
.
clear
();
root
=
null
;
store
=
null
;
readOnly
=
true
;
}
public
boolean
isClosed
()
{
return
store
==
null
;
}
}
/**
/**
...
@@ -110,25 +119,23 @@ public class BtreeMap<K, V> {
...
@@ -110,25 +119,23 @@ public class BtreeMap<K, V> {
* @param key the key
* @param key the key
*/
*/
public
void
remove
(
K
key
)
{
public
void
remove
(
K
key
)
{
checkWrite
();
if
(
root
!=
null
)
{
if
(
root
!=
null
)
{
markChanged
();
Page
oldRoot
=
root
;
root
=
Page
.
remove
(
root
,
key
);
root
=
Page
.
remove
(
root
,
store
.
getCurrentVersion
(),
key
);
markChanged
(
oldRoot
);
}
}
}
}
/**
private
void
markChanged
(
Page
oldRoot
)
{
* Was this map changed.
if
(
oldRoot
!=
root
)
{
*
oldRoots
.
put
(
store
.
getCurrentVersion
(),
oldRoot
);
* @return true if yes
store
.
markChanged
(
this
);
*/
}
boolean
isChanged
()
{
return
root
!=
null
&&
root
.
getPos
()
<
0
;
}
}
private
void
markChanged
()
{
public
boolean
hasUnsavedChanges
()
{
if
(!
isChanged
())
{
return
oldRoots
.
size
()
>
0
;
store
.
markChanged
(
name
,
this
);
}
}
}
/**
/**
...
@@ -160,20 +167,6 @@ public class BtreeMap<K, V> {
...
@@ -160,20 +167,6 @@ public class BtreeMap<K, V> {
return
valueType
;
return
valueType
;
}
}
long
getTransaction
()
{
return
store
.
getTransaction
();
}
/**
* Register a page and get the next temporary page id.
*
* @param p the new page
* @return the page id
*/
long
registerTempPage
(
Page
p
)
{
return
store
.
registerTempPage
(
p
);
}
/**
/**
* Read a page.
* Read a page.
*
*
...
@@ -184,22 +177,13 @@ public class BtreeMap<K, V> {
...
@@ -184,22 +177,13 @@ public class BtreeMap<K, V> {
return
store
.
readPage
(
this
,
pos
);
return
store
.
readPage
(
this
,
pos
);
}
}
/**
* Remove a page.
*
* @param pos the position of the page
*/
void
removePage
(
long
pos
)
{
store
.
removePage
(
pos
);
}
/**
/**
* Set the position of the root page.
* Set the position of the root page.
*
*
* @param rootPos the position
* @param rootPos the position
, 0 for empty
*/
*/
void
setRootPos
(
long
rootPos
)
{
void
setRootPos
(
long
rootPos
)
{
root
=
readPage
(
rootPos
);
root
=
r
ootPos
==
0
?
null
:
r
eadPage
(
rootPos
);
}
}
/**
/**
...
@@ -209,6 +193,7 @@ public class BtreeMap<K, V> {
...
@@ -209,6 +193,7 @@ public class BtreeMap<K, V> {
* @return the iterator
* @return the iterator
*/
*/
public
Iterator
<
K
>
keyIterator
(
K
from
)
{
public
Iterator
<
K
>
keyIterator
(
K
from
)
{
checkOpen
();
return
new
Cursor
<
K
>(
root
,
from
);
return
new
Cursor
<
K
>(
root
,
from
);
}
}
...
@@ -238,4 +223,78 @@ public class BtreeMap<K, V> {
...
@@ -238,4 +223,78 @@ public class BtreeMap<K, V> {
return
id
;
return
id
;
}
}
void
rollbackTo
(
long
version
)
{
checkWrite
();
if
(
version
<=
createVersion
)
{
remove
();
}
else
{
// iterating in ascending order, and pick the last version -
// this is not terribly efficient if there are many versions
// but it is a simple algorithm
Long
newestOldVersion
=
null
;
for
(
Iterator
<
Long
>
it
=
oldRoots
.
keySet
().
iterator
();
it
.
hasNext
();)
{
Long
x
=
it
.
next
();
if
(
x
>=
version
)
{
if
(
newestOldVersion
==
null
)
{
newestOldVersion
=
x
;
root
=
oldRoots
.
get
(
x
);
}
it
.
remove
();
}
}
}
}
public
void
setReadOnly
(
boolean
readOnly
)
{
this
.
readOnly
=
readOnly
;
}
public
boolean
isReadOnly
()
{
return
readOnly
;
}
private
void
checkOpen
()
{
if
(
store
==
null
)
{
throw
new
IllegalStateException
(
"This map is closed"
);
}
}
private
void
checkWrite
()
{
if
(
readOnly
)
{
checkOpen
();
throw
new
IllegalStateException
(
"This map is read-only"
);
}
}
public
String
toString
()
{
StringBuilder
buff
=
new
StringBuilder
();
buff
.
append
(
"map:"
).
append
(
name
);
if
(
readOnly
)
{
buff
.
append
(
" readOnly"
);
}
if
(
store
==
null
)
{
buff
.
append
(
" closed"
);
}
return
buff
.
toString
();
}
public
void
close
()
{
readOnly
=
true
;
store
=
null
;
oldRoots
.
clear
();
root
=
null
;
}
public
int
hashCode
()
{
return
id
;
}
public
boolean
equals
(
Object
o
)
{
return
this
==
o
;
}
long
getCreatedVersion
()
{
return
createVersion
;
}
}
}
h2/src/tools/org/h2/dev/store/btree/BtreeMapStore.java
浏览文件 @
582ca70b
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论