Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
c53f1270
提交
c53f1270
authored
6月 03, 2014
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Large result sets now always create temporary tables instead of temporary files.
上级
864b1958
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
107 行增加
和
48 行删除
+107
-48
LocalResult.java
h2/src/main/org/h2/result/LocalResult.java
+3
-3
ResultTempTable.java
h2/src/main/org/h2/result/ResultTempTable.java
+104
-45
没有找到文件。
h2/src/main/org/h2/result/LocalResult.java
浏览文件 @
c53f1270
...
@@ -269,7 +269,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
...
@@ -269,7 +269,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
distinctRows
.
put
(
array
,
values
);
distinctRows
.
put
(
array
,
values
);
rowCount
=
distinctRows
.
size
();
rowCount
=
distinctRows
.
size
();
if
(
rowCount
>
maxMemoryRows
)
{
if
(
rowCount
>
maxMemoryRows
)
{
external
=
new
ResultTempTable
(
session
,
true
,
sort
);
external
=
new
ResultTempTable
(
session
,
expressions
,
true
,
sort
);
rowCount
=
external
.
addRows
(
distinctRows
.
values
());
rowCount
=
external
.
addRows
(
distinctRows
.
values
());
distinctRows
=
null
;
distinctRows
=
null
;
}
}
...
@@ -282,7 +282,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
...
@@ -282,7 +282,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
rowCount
++;
rowCount
++;
if
(
rows
.
size
()
>
maxMemoryRows
)
{
if
(
rows
.
size
()
>
maxMemoryRows
)
{
if
(
external
==
null
)
{
if
(
external
==
null
)
{
external
=
new
ResultTempTable
(
session
,
false
,
sort
);
external
=
new
ResultTempTable
(
session
,
expressions
,
false
,
sort
);
}
}
addRowsToDisk
();
addRowsToDisk
();
}
}
...
@@ -319,7 +319,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
...
@@ -319,7 +319,7 @@ public class LocalResult implements ResultInterface, ResultTarget {
break
;
break
;
}
}
if
(
external
==
null
)
{
if
(
external
==
null
)
{
external
=
new
ResultTempTable
(
session
,
true
,
sort
);
external
=
new
ResultTempTable
(
session
,
expressions
,
true
,
sort
);
}
}
rows
.
add
(
list
);
rows
.
add
(
list
);
if
(
rows
.
size
()
>
maxMemoryRows
)
{
if
(
rows
.
size
()
>
maxMemoryRows
)
{
...
...
h2/src/main/org/h2/result/ResultTempTable.java
浏览文件 @
c53f1270
...
@@ -7,10 +7,13 @@
...
@@ -7,10 +7,13 @@
package
org
.
h2
.
result
;
package
org
.
h2
.
result
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
org.h2.command.ddl.CreateTableData
;
import
org.h2.command.ddl.CreateTableData
;
import
org.h2.engine.Constants
;
import
org.h2.engine.Constants
;
import
org.h2.engine.Database
;
import
org.h2.engine.Database
;
import
org.h2.engine.Session
;
import
org.h2.engine.Session
;
import
org.h2.expression.Expression
;
import
org.h2.index.Cursor
;
import
org.h2.index.Cursor
;
import
org.h2.index.Index
;
import
org.h2.index.Index
;
import
org.h2.index.IndexType
;
import
org.h2.index.IndexType
;
...
@@ -21,7 +24,7 @@ import org.h2.table.IndexColumn;
...
@@ -21,7 +24,7 @@ import org.h2.table.IndexColumn;
import
org.h2.table.RegularTable
;
import
org.h2.table.RegularTable
;
import
org.h2.table.Table
;
import
org.h2.table.Table
;
import
org.h2.value.Value
;
import
org.h2.value.Value
;
import
org.h2.value.Value
Array
;
import
org.h2.value.Value
Null
;
/**
/**
* This class implements the temp table buffer for the LocalResult class.
* This class implements the temp table buffer for the LocalResult class.
...
@@ -31,25 +34,34 @@ public class ResultTempTable implements ResultExternal {
...
@@ -31,25 +34,34 @@ public class ResultTempTable implements ResultExternal {
private
static
final
String
COLUMN_NAME
=
"DATA"
;
private
static
final
String
COLUMN_NAME
=
"DATA"
;
private
final
boolean
distinct
;
private
final
boolean
distinct
;
private
final
SortOrder
sort
;
private
final
SortOrder
sort
;
private
final
Index
index
;
private
Index
index
;
private
Session
session
;
private
Session
session
;
private
Table
table
;
private
Table
table
;
private
Cursor
resultCursor
;
private
Cursor
resultCursor
;
private
int
rowCount
;
private
int
rowCount
;
private
int
columnCount
;
private
final
ResultTempTable
parent
;
private
final
ResultTempTable
parent
;
private
boolean
closed
;
private
boolean
closed
;
private
int
childCount
;
private
int
childCount
;
private
boolean
containsLob
;
ResultTempTable
(
Session
session
,
boolean
distinct
,
SortOrder
sort
)
{
ResultTempTable
(
Session
session
,
Expression
[]
expressions
,
boolean
distinct
,
SortOrder
sort
)
{
this
.
session
=
session
;
this
.
session
=
session
;
this
.
distinct
=
distinct
;
this
.
distinct
=
distinct
;
this
.
sort
=
sort
;
this
.
sort
=
sort
;
this
.
columnCount
=
expressions
.
length
;
Schema
schema
=
session
.
getDatabase
().
getSchema
(
Constants
.
SCHEMA_MAIN
);
Schema
schema
=
session
.
getDatabase
().
getSchema
(
Constants
.
SCHEMA_MAIN
);
Column
column
=
new
Column
(
COLUMN_NAME
,
Value
.
ARRAY
);
column
.
setNullable
(
false
);
CreateTableData
data
=
new
CreateTableData
();
CreateTableData
data
=
new
CreateTableData
();
data
.
columns
.
add
(
column
);
for
(
int
i
=
0
;
i
<
expressions
.
length
;
i
++)
{
int
type
=
expressions
[
i
].
getType
();
Column
col
=
new
Column
(
COLUMN_NAME
+
i
,
type
);
if
(
type
==
Value
.
CLOB
||
type
==
Value
.
BLOB
)
{
containsLob
=
true
;
}
data
.
columns
.
add
(
col
);
}
data
.
id
=
session
.
getDatabase
().
allocateObjectId
();
data
.
id
=
session
.
getDatabase
().
allocateObjectId
();
data
.
tableName
=
"TEMP_RESULT_SET_"
+
data
.
id
;
data
.
tableName
=
"TEMP_RESULT_SET_"
+
data
.
id
;
data
.
temporary
=
true
;
data
.
temporary
=
true
;
...
@@ -58,39 +70,62 @@ public class ResultTempTable implements ResultExternal {
...
@@ -58,39 +70,62 @@ public class ResultTempTable implements ResultExternal {
data
.
create
=
true
;
data
.
create
=
true
;
data
.
session
=
session
;
data
.
session
=
session
;
table
=
schema
.
createTable
(
data
);
table
=
schema
.
createTable
(
data
);
int
indexId
=
session
.
getDatabase
().
allocateObjectId
();
if
(
sort
!=
null
||
distinct
)
{
IndexColumn
indexColumn
=
new
IndexColumn
();
createIndex
();
indexColumn
.
column
=
column
;
indexColumn
.
columnName
=
COLUMN_NAME
;
IndexType
indexType
;
IndexColumn
[]
indexCols
=
{
indexColumn
};
if
(
session
.
getDatabase
().
getMvStore
()
!=
null
)
{
indexType
=
IndexType
.
createNonUnique
(
true
);
index
=
table
.
addIndex
(
session
,
data
.
tableName
,
indexId
,
indexCols
,
indexType
,
true
,
null
);
index
.
setTemporary
(
true
);
}
else
{
indexType
=
IndexType
.
createPrimaryKey
(
true
,
false
);
index
=
new
PageBtreeIndex
((
RegularTable
)
table
,
indexId
,
data
.
tableName
,
indexCols
,
indexType
,
true
,
session
);
index
.
setTemporary
(
true
);
table
.
getIndexes
().
add
(
index
);
}
}
parent
=
null
;
parent
=
null
;
}
}
private
ResultTempTable
(
ResultTempTable
parent
)
{
private
ResultTempTable
(
ResultTempTable
parent
)
{
this
.
parent
=
parent
;
this
.
parent
=
parent
;
this
.
columnCount
=
parent
.
columnCount
;
this
.
distinct
=
parent
.
distinct
;
this
.
distinct
=
parent
.
distinct
;
this
.
session
=
parent
.
session
;
this
.
session
=
parent
.
session
;
this
.
table
=
parent
.
table
;
this
.
table
=
parent
.
table
;
this
.
index
=
parent
.
index
;
this
.
index
=
parent
.
index
;
this
.
rowCount
=
parent
.
rowCount
;
this
.
rowCount
=
parent
.
rowCount
;
// sort is only used when adding rows
this
.
sort
=
parent
.
sort
;
this
.
sort
=
null
;
this
.
containsLob
=
parent
.
containsLob
;
reset
();
reset
();
}
}
private
void
createIndex
()
{
IndexColumn
[]
indexCols
=
null
;
if
(
sort
!=
null
)
{
int
[]
colInd
=
sort
.
getQueryColumnIndexes
();
indexCols
=
new
IndexColumn
[
colInd
.
length
];
for
(
int
i
=
0
;
i
<
colInd
.
length
;
i
++)
{
IndexColumn
indexColumn
=
new
IndexColumn
();
indexColumn
.
column
=
table
.
getColumn
(
colInd
[
i
]);
indexColumn
.
sortType
=
sort
.
getSortTypes
()[
i
];
indexColumn
.
columnName
=
COLUMN_NAME
+
i
;
indexCols
[
i
]
=
indexColumn
;
}
}
else
{
indexCols
=
new
IndexColumn
[
columnCount
];
for
(
int
i
=
0
;
i
<
columnCount
;
i
++)
{
IndexColumn
indexColumn
=
new
IndexColumn
();
indexColumn
.
column
=
table
.
getColumn
(
i
);
indexColumn
.
columnName
=
COLUMN_NAME
+
i
;
indexCols
[
i
]
=
indexColumn
;
}
}
String
indexName
=
table
.
getSchema
().
getUniqueIndexName
(
session
,
table
,
Constants
.
PREFIX_INDEX
);
int
indexId
=
session
.
getDatabase
().
allocateObjectId
();
IndexType
indexType
=
IndexType
.
createNonUnique
(
true
);
if
(
session
.
getDatabase
().
getMvStore
()
!=
null
)
{
index
=
table
.
addIndex
(
session
,
indexName
,
indexId
,
indexCols
,
indexType
,
true
,
null
);
index
.
setTemporary
(
true
);
}
else
{
index
=
new
PageBtreeIndex
((
RegularTable
)
table
,
indexId
,
indexName
,
indexCols
,
indexType
,
true
,
session
);
index
.
setTemporary
(
true
);
table
.
getIndexes
().
add
(
index
);
}
}
@Override
@Override
public
synchronized
ResultExternal
createShallowCopy
()
{
public
synchronized
ResultExternal
createShallowCopy
()
{
if
(
parent
!=
null
)
{
if
(
parent
!=
null
)
{
...
@@ -138,6 +173,7 @@ public class ResultTempTable implements ResultExternal {
...
@@ -138,6 +173,7 @@ public class ResultTempTable implements ResultExternal {
@Override
@Override
public
int
addRows
(
ArrayList
<
Value
[]>
rows
)
{
public
int
addRows
(
ArrayList
<
Value
[]>
rows
)
{
// speeds up inserting, but not really needed:
if
(
sort
!=
null
)
{
if
(
sort
!=
null
)
{
sort
.
sort
(
rows
);
sort
.
sort
(
rows
);
}
}
...
@@ -172,6 +208,11 @@ public class ResultTempTable implements ResultExternal {
...
@@ -172,6 +208,11 @@ public class ResultTempTable implements ResultExternal {
if
(
table
==
null
)
{
if
(
table
==
null
)
{
return
;
return
;
}
}
if
(
containsLob
)
{
// contains BLOB or CLOB: can not truncate now,
// otherwise the BLOB and CLOB entries are removed
return
;
}
try
{
try
{
Database
database
=
session
.
getDatabase
();
Database
database
=
session
.
getDatabase
();
// Need to lock because not all of the code-paths
// Need to lock because not all of the code-paths
...
@@ -189,7 +230,9 @@ public class ResultTempTable implements ResultExternal {
...
@@ -189,7 +230,9 @@ public class ResultTempTable implements ResultExternal {
// time. (the table is truncated, so this is just one record)
// time. (the table is truncated, so this is just one record)
if
(!
database
.
isSysTableLocked
())
{
if
(!
database
.
isSysTableLocked
())
{
Session
sysSession
=
database
.
getSystemSession
();
Session
sysSession
=
database
.
getSystemSession
();
if
(
index
!=
null
)
{
index
.
removeChildrenAndResources
(
sysSession
);
index
.
removeChildrenAndResources
(
sysSession
);
}
table
.
removeChildrenAndResources
(
sysSession
);
table
.
removeChildrenAndResources
(
sysSession
);
// the transaction must be committed immediately
// the transaction must be committed immediately
sysSession
.
commit
(
false
);
sysSession
.
commit
(
false
);
...
@@ -207,13 +250,13 @@ public class ResultTempTable implements ResultExternal {
...
@@ -207,13 +250,13 @@ public class ResultTempTable implements ResultExternal {
@Override
@Override
public
Value
[]
next
()
{
public
Value
[]
next
()
{
if
(
resultCursor
==
null
)
{
if
(
resultCursor
==
null
)
{
if
(
session
.
getDatabase
().
getMvStore
()
!=
null
)
{
Index
idx
;
Index
idx
;
if
(
distinct
||
sort
!=
null
)
{
if
(
distinct
||
sort
!=
null
)
{
idx
=
index
;
idx
=
index
;
}
else
{
}
else
{
idx
=
table
.
getScanIndex
(
session
);
idx
=
table
.
getScanIndex
(
session
);
}
}
if
(
session
.
getDatabase
().
getMvStore
()
!=
null
)
{
// sometimes the transaction is already committed,
// sometimes the transaction is already committed,
// in which case we can't use the session
// in which case we can't use the session
if
(
idx
.
getRowCount
(
session
)
==
0
&&
rowCount
>
0
)
{
if
(
idx
.
getRowCount
(
session
)
==
0
&&
rowCount
>
0
)
{
...
@@ -224,15 +267,14 @@ public class ResultTempTable implements ResultExternal {
...
@@ -224,15 +267,14 @@ public class ResultTempTable implements ResultExternal {
resultCursor
=
idx
.
find
(
session
,
null
,
null
);
resultCursor
=
idx
.
find
(
session
,
null
,
null
);
}
}
}
else
{
}
else
{
resultCursor
=
i
nde
x
.
find
(
session
,
null
,
null
);
resultCursor
=
i
d
x
.
find
(
session
,
null
,
null
);
}
}
}
}
if
(!
resultCursor
.
next
())
{
if
(!
resultCursor
.
next
())
{
return
null
;
return
null
;
}
}
Row
row
=
resultCursor
.
get
();
Row
row
=
resultCursor
.
get
();
ValueArray
data
=
(
ValueArray
)
row
.
getValue
(
0
);
return
row
.
getValueList
();
return
data
.
getList
();
}
}
@Override
@Override
...
@@ -240,19 +282,36 @@ public class ResultTempTable implements ResultExternal {
...
@@ -240,19 +282,36 @@ public class ResultTempTable implements ResultExternal {
resultCursor
=
null
;
resultCursor
=
null
;
}
}
private
static
Row
convertToRow
(
Value
[]
values
)
{
private
Row
convertToRow
(
Value
[]
values
)
{
ValueArray
data
=
ValueArray
.
get
(
values
);
if
(
values
.
length
<
columnCount
)
{
return
new
Row
(
new
Value
[]{
data
},
Row
.
MEMORY_CALCULATE
);
Value
[]
v2
=
Arrays
.
copyOf
(
values
,
columnCount
);
for
(
int
i
=
values
.
length
;
i
<
columnCount
;
i
++)
{
v2
[
i
]
=
ValueNull
.
INSTANCE
;
}
values
=
v2
;
}
return
new
Row
(
values
,
Row
.
MEMORY_CALCULATE
);
}
}
private
Cursor
find
(
Row
row
)
{
private
Cursor
find
(
Row
row
)
{
if
(
index
==
null
)
{
// for the case "in(select ...)", the query might
// use an optimization and not create the index
// up front
createIndex
();
}
Cursor
cursor
=
index
.
find
(
session
,
row
,
row
);
Cursor
cursor
=
index
.
find
(
session
,
row
,
row
);
Value
a
=
row
.
getValue
(
0
);
Database
db
=
session
.
getDatabase
(
);
while
(
cursor
.
next
())
{
while
(
cursor
.
next
())
{
SearchRow
found
;
SearchRow
found
=
cursor
.
getSearchRow
();
found
=
cursor
.
getSearchRow
();
boolean
ok
=
true
;
Value
b
=
found
.
getValue
(
0
);
for
(
int
i
=
0
;
i
<
row
.
getColumnCount
();
i
++)
{
if
(
session
.
getDatabase
().
areEqual
(
a
,
b
))
{
if
(!
db
.
areEqual
(
row
.
getValue
(
i
),
found
.
getValue
(
i
)))
{
ok
=
false
;
break
;
}
}
if
(
ok
)
{
return
cursor
;
return
cursor
;
}
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论