Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
32f78e38
提交
32f78e38
authored
9月 28, 2017
作者:
andrei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Replace TreeSet with PriorityQueue in MVSecondaryIndex re-build
上级
d439e6df
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
68 行增加
和
62 行删除
+68
-62
MVSecondaryIndex.java
h2/src/main/org/h2/mvstore/db/MVSecondaryIndex.java
+68
-62
没有找到文件。
h2/src/main/org/h2/mvstore/db/MVSecondaryIndex.java
浏览文件 @
32f78e38
...
...
@@ -10,7 +10,8 @@ import java.util.Collections;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.TreeSet
;
import
java.util.PriorityQueue
;
import
java.util.Queue
;
import
org.h2.api.ErrorCode
;
import
org.h2.engine.Database
;
import
org.h2.engine.Session
;
...
...
@@ -37,16 +38,14 @@ import org.h2.value.ValueNull;
/**
* A table stored in a MVStore.
*/
public
class
MVSecondaryIndex
extends
BaseIndex
implements
MVIndex
{
public
final
class
MVSecondaryIndex
extends
BaseIndex
implements
MVIndex
{
/**
* The multi-value table.
*/
final
MVTable
mvTable
;
private
final
int
keyColumns
;
private
final
String
mapName
;
private
TransactionMap
<
Value
,
Value
>
dataMap
;
private
final
MVTable
mvTable
;
private
final
int
keyColumns
;
private
final
TransactionMap
<
Value
,
Value
>
dataMap
;
public
MVSecondaryIndex
(
Database
db
,
MVTable
table
,
int
id
,
String
indexName
,
IndexColumn
[]
columns
,
IndexType
indexType
)
{
...
...
@@ -58,7 +57,7 @@ public class MVSecondaryIndex extends BaseIndex implements MVIndex {
// always store the row key in the map key,
// even for unique indexes, as some of the index columns could be null
keyColumns
=
columns
.
length
+
1
;
mapName
=
"index."
+
getId
();
String
mapName
=
"index."
+
getId
();
int
[]
sortTypes
=
new
int
[
keyColumns
];
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
sortTypes
[
i
]
=
columns
[
i
].
sortType
;
...
...
@@ -77,84 +76,91 @@ public class MVSecondaryIndex extends BaseIndex implements MVIndex {
@Override
public
void
addRowsToBuffer
(
List
<
Row
>
rows
,
String
bufferName
)
{
MVMap
<
Value
,
Value
>
map
=
openMap
(
bufferName
);
MVMap
<
Value
Array
,
Value
>
map
=
openMap
(
bufferName
);
for
(
Row
row
:
rows
)
{
ValueArray
key
=
convertToKey
(
row
);
map
.
put
(
key
,
ValueNull
.
INSTANCE
);
}
}
@Override
public
void
addBufferedRows
(
List
<
String
>
bufferNames
)
{
ArrayList
<
String
>
mapNames
=
New
.
arrayList
(
bufferNames
);
final
CompareMode
compareMode
=
database
.
getCompareMode
();
/**
* A source of values.
*/
class
Source
implements
Comparable
<
Source
>
{
Value
value
;
Iterator
<
Value
>
next
;
int
sourceId
;
private
static
final
class
Source
{
private
final
Iterator
<
ValueArray
>
iterator
;
private
ValueArray
currentRowData
;
public
Source
(
Iterator
<
ValueArray
>
iterator
)
{
this
.
iterator
=
iterator
;
this
.
currentRowData
=
iterator
.
next
();
}
public
boolean
hasNext
()
{
boolean
result
=
iterator
.
hasNext
();
if
(
result
)
{
currentRowData
=
iterator
.
next
();
}
return
result
;
}
public
ValueArray
next
()
{
return
currentRowData
;
}
public
static
final
class
Comparator
implements
java
.
util
.
Comparator
<
Source
>
{
private
final
CompareMode
compareMode
;
public
Comparator
(
CompareMode
compareMode
)
{
this
.
compareMode
=
compareMode
;
}
@Override
public
int
compareTo
(
Source
o
)
{
int
comp
=
value
.
compareTo
(
o
.
value
,
compareMode
);
if
(
comp
==
0
)
{
comp
=
sourceId
-
o
.
sourceId
;
}
return
comp
;
public
int
compare
(
Source
one
,
Source
two
)
{
return
one
.
currentRowData
.
compareTo
(
two
.
currentRowData
,
compareMode
);
}
}
TreeSet
<
Source
>
sources
=
new
TreeSet
<>();
for
(
int
i
=
0
;
i
<
bufferNames
.
size
();
i
++)
{
MVMap
<
Value
,
Value
>
map
=
openMap
(
bufferNames
.
get
(
i
));
Iterator
<
Value
>
it
=
map
.
keyIterator
(
null
);
if
(
it
.
hasNext
())
{
Source
s
=
new
Source
();
s
.
value
=
it
.
next
();
s
.
next
=
it
;
s
.
sourceId
=
i
;
sources
.
add
(
s
);
}
@Override
public
void
addBufferedRows
(
List
<
String
>
bufferNames
)
{
ArrayList
<
String
>
mapNames
=
New
.
arrayList
(
bufferNames
);
CompareMode
compareMode
=
database
.
getCompareMode
();
int
buffersCount
=
bufferNames
.
size
();
Queue
<
Source
>
queue
=
new
PriorityQueue
<>(
buffersCount
,
new
Source
.
Comparator
(
compareMode
));
for
(
String
bufferName
:
bufferNames
)
{
Iterator
<
ValueArray
>
iter
=
openMap
(
bufferName
).
keyIterator
(
null
);
if
(
iter
.
hasNext
())
{
queue
.
add
(
new
Source
(
iter
));
}
}
try
{
while
(
true
)
{
Source
s
=
sources
.
first
();
Value
v
=
s
.
value
;
while
(
!
queue
.
isEmpty
()
)
{
Source
s
=
queue
.
remove
();
Value
Array
rowData
=
s
.
next
()
;
if
(
indexType
.
isUnique
())
{
Value
[]
array
=
((
ValueArray
)
v
)
.
getList
();
Value
[]
array
=
rowData
.
getList
();
// don't change the original value
array
=
array
.
clone
();
array
[
keyColumns
-
1
]
=
ValueLong
.
get
(
Long
.
MIN_VALUE
);
ValueArray
unique
=
ValueArray
.
get
(
array
);
SearchRow
row
=
convertToSearchRow
(
(
ValueArray
)
v
);
SearchRow
row
=
convertToSearchRow
(
rowData
);
checkUnique
(
row
,
dataMap
,
unique
);
}
dataMap
.
putCommitted
(
v
,
ValueNull
.
INSTANCE
);
dataMap
.
putCommitted
(
rowData
,
ValueNull
.
INSTANCE
);
Iterator
<
Value
>
it
=
s
.
next
;
if
(!
it
.
hasNext
())
{
sources
.
remove
(
s
);
if
(
sources
.
size
()
==
0
)
{
break
;
}
}
else
{
Value
nextValue
=
it
.
next
();
sources
.
remove
(
s
);
s
.
value
=
nextValue
;
sources
.
add
(
s
);
if
(
s
.
hasNext
())
{
queue
.
offer
(
s
);
}
}
}
finally
{
for
(
String
tempMapName
:
mapNames
)
{
MVMap
<
Value
,
Value
>
map
=
openMap
(
tempMapName
);
MVMap
<
Value
Array
,
Value
>
map
=
openMap
(
tempMapName
);
map
.
getStore
().
removeMap
(
map
);
}
}
}
private
MVMap
<
Value
,
Value
>
openMap
(
String
mapName
)
{
private
MVMap
<
Value
Array
,
Value
>
openMap
(
String
mapName
)
{
int
[]
sortTypes
=
new
int
[
keyColumns
];
for
(
int
i
=
0
;
i
<
indexColumns
.
length
;
i
++)
{
sortTypes
[
i
]
=
indexColumns
[
i
].
sortType
;
...
...
@@ -163,9 +169,9 @@ public class MVSecondaryIndex extends BaseIndex implements MVIndex {
ValueDataType
keyType
=
new
ValueDataType
(
database
.
getCompareMode
(),
database
,
sortTypes
);
ValueDataType
valueType
=
new
ValueDataType
(
null
,
null
,
null
);
MVMap
.
Builder
<
Value
,
Value
>
builder
=
new
MVMap
.
Builder
<
Value
,
Value
>().
keyType
(
keyType
).
valueType
(
valueType
);
MVMap
<
Value
,
Value
>
map
=
database
.
getMvStore
().
MVMap
.
Builder
<
Value
Array
,
Value
>
builder
=
new
MVMap
.
Builder
<
Value
Array
,
Value
>().
keyType
(
keyType
).
valueType
(
valueType
);
MVMap
<
Value
Array
,
Value
>
map
=
database
.
getMvStore
().
getStore
().
openMap
(
mapName
,
builder
);
if
(!
keyType
.
equals
(
map
.
getKeyType
()))
{
throw
DbException
.
throwInternalError
(
"Incompatible key type"
);
...
...
@@ -332,7 +338,7 @@ public class MVSecondaryIndex extends BaseIndex implements MVIndex {
* @param key the index key
* @return the row
*/
SearchRow
convertToSearchRow
(
ValueArray
key
)
{
private
SearchRow
convertToSearchRow
(
ValueArray
key
)
{
Value
[]
array
=
key
.
getList
();
SearchRow
searchRow
=
mvTable
.
getTemplateRow
();
searchRow
.
setKey
((
array
[
array
.
length
-
1
]).
getLong
());
...
...
@@ -455,7 +461,7 @@ public class MVSecondaryIndex extends BaseIndex implements MVIndex {
* @param session the session
* @return the map
*/
TransactionMap
<
Value
,
Value
>
getMap
(
Session
session
)
{
private
TransactionMap
<
Value
,
Value
>
getMap
(
Session
session
)
{
if
(
session
==
null
)
{
return
dataMap
;
}
...
...
@@ -466,7 +472,7 @@ public class MVSecondaryIndex extends BaseIndex implements MVIndex {
/**
* A cursor.
*/
class
MVStoreCursor
implements
Cursor
{
final
class
MVStoreCursor
implements
Cursor
{
private
final
Session
session
;
private
final
Iterator
<
Value
>
it
;
...
...
@@ -475,7 +481,7 @@ public class MVSecondaryIndex extends BaseIndex implements MVIndex {
private
SearchRow
searchRow
;
private
Row
row
;
p
ublic
MVStoreCursor
(
Session
session
,
Iterator
<
Value
>
it
,
SearchRow
last
)
{
p
rivate
MVStoreCursor
(
Session
session
,
Iterator
<
Value
>
it
,
SearchRow
last
)
{
this
.
session
=
session
;
this
.
it
=
it
;
this
.
last
=
last
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论