Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
801913db
Unverified
提交
801913db
authored
2月 24, 2018
作者:
Noel Grandin
提交者:
GitHub
2月 24, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #903 from katzyn/function_cursor
Handle first and last arguments of FunctionIndex.find()
上级
cacef49b
7bce6ec7
隐藏空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
178 行增加
和
64 行删除
+178
-64
AbstractFunctionCursor.java
h2/src/main/org/h2/index/AbstractFunctionCursor.java
+95
-0
BaseIndex.java
h2/src/main/org/h2/index/BaseIndex.java
+4
-0
FunctionCursor.java
h2/src/main/org/h2/index/FunctionCursor.java
+4
-31
FunctionCursorResultSet.java
h2/src/main/org/h2/index/FunctionCursorResultSet.java
+4
-29
FunctionIndex.java
h2/src/main/org/h2/index/FunctionIndex.java
+9
-4
Index.java
h2/src/main/org/h2/index/Index.java
+9
-0
IndexCursor.java
h2/src/main/org/h2/index/IndexCursor.java
+5
-0
MultiVersionIndex.java
h2/src/main/org/h2/index/MultiVersionIndex.java
+5
-0
TestIndex.java
h2/src/test/org/h2/test/db/TestIndex.java
+43
-0
没有找到文件。
h2/src/main/org/h2/index/AbstractFunctionCursor.java
0 → 100644
浏览文件 @
801913db
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
index
;
import
org.h2.engine.Session
;
import
org.h2.message.DbException
;
import
org.h2.result.Row
;
import
org.h2.result.SearchRow
;
import
org.h2.value.Value
;
/**
* Abstract function cursor.
*/
abstract
class
AbstractFunctionCursor
implements
Cursor
{
private
final
FunctionIndex
index
;
private
final
SearchRow
first
;
private
final
SearchRow
last
;
final
Session
session
;
Value
[]
values
;
Row
row
;
/**
* @param index
* index
* @param first
* first row
* @param last
* last row
* @param session
* session
*/
AbstractFunctionCursor
(
FunctionIndex
index
,
SearchRow
first
,
SearchRow
last
,
Session
session
)
{
this
.
index
=
index
;
this
.
first
=
first
;
this
.
last
=
last
;
this
.
session
=
session
;
}
@Override
public
Row
get
()
{
if
(
values
==
null
)
{
return
null
;
}
if
(
row
==
null
)
{
row
=
session
.
createRow
(
values
,
1
);
}
return
row
;
}
@Override
public
SearchRow
getSearchRow
()
{
return
get
();
}
@Override
public
boolean
next
()
{
final
SearchRow
first
=
this
.
first
,
last
=
this
.
last
;
if
(
first
==
null
&&
last
==
null
)
{
return
nextImpl
();
}
while
(
nextImpl
())
{
Row
current
=
get
();
if
(
first
!=
null
)
{
int
comp
=
index
.
compareRows
(
current
,
first
);
if
(
comp
<
0
)
{
continue
;
}
}
if
(
last
!=
null
)
{
int
comp
=
index
.
compareRows
(
current
,
last
);
if
(
comp
>
0
)
{
continue
;
}
}
return
true
;
}
return
false
;
}
abstract
boolean
nextImpl
();
@Override
public
boolean
previous
()
{
throw
DbException
.
throwInternalError
(
toString
());
}
}
h2/src/main/org/h2/index/BaseIndex.java
浏览文件 @
801913db
...
...
@@ -120,6 +120,10 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
return
false
;
}
@Override
public
boolean
isFindUsingFullTableScan
()
{
return
false
;
}
@Override
public
Cursor
find
(
TableFilter
filter
,
SearchRow
first
,
SearchRow
last
)
{
...
...
h2/src/main/org/h2/index/FunctionCursor.java
浏览文件 @
801913db
...
...
@@ -6,45 +6,23 @@
package
org
.
h2
.
index
;
import
org.h2.engine.Session
;
import
org.h2.message.DbException
;
import
org.h2.result.ResultInterface
;
import
org.h2.result.Row
;
import
org.h2.result.SearchRow
;
import
org.h2.value.Value
;
/**
* A cursor for a function that returns a result.
*/
public
class
FunctionCursor
implements
Cursor
{
public
class
FunctionCursor
extends
AbstractFunction
Cursor
{
private
final
Session
session
;
private
final
ResultInterface
result
;
private
Value
[]
values
;
private
Row
row
;
FunctionCursor
(
Session
session
,
ResultInterface
result
)
{
this
.
session
=
session
;
FunctionCursor
(
FunctionIndex
index
,
SearchRow
first
,
SearchRow
last
,
Session
session
,
ResultInterface
result
)
{
super
(
index
,
first
,
last
,
session
)
;
this
.
result
=
result
;
}
@Override
public
Row
get
()
{
if
(
values
==
null
)
{
return
null
;
}
if
(
row
==
null
)
{
row
=
session
.
createRow
(
values
,
1
);
}
return
row
;
}
@Override
public
SearchRow
getSearchRow
()
{
return
get
();
}
@Override
public
boolean
next
()
{
boolean
nextImpl
()
{
row
=
null
;
if
(
result
!=
null
&&
result
.
next
())
{
values
=
result
.
currentRow
();
...
...
@@ -54,9 +32,4 @@ public class FunctionCursor implements Cursor {
return
values
!=
null
;
}
@Override
public
boolean
previous
()
{
throw
DbException
.
throwInternalError
(
toString
());
}
}
h2/src/main/org/h2/index/FunctionCursorResultSet.java
浏览文件 @
801913db
...
...
@@ -10,7 +10,6 @@ import java.sql.ResultSetMetaData;
import
java.sql.SQLException
;
import
org.h2.engine.Session
;
import
org.h2.message.DbException
;
import
org.h2.result.Row
;
import
org.h2.result.SearchRow
;
import
org.h2.value.DataType
;
import
org.h2.value.Value
;
...
...
@@ -18,16 +17,13 @@ import org.h2.value.Value;
/**
* A cursor for a function that returns a JDBC result set.
*/
public
class
FunctionCursorResultSet
implements
Cursor
{
public
class
FunctionCursorResultSet
extends
AbstractFunction
Cursor
{
private
final
Session
session
;
private
final
ResultSet
result
;
private
final
ResultSetMetaData
meta
;
private
Value
[]
values
;
private
Row
row
;
FunctionCursorResultSet
(
Session
session
,
ResultSet
result
)
{
this
.
session
=
session
;
FunctionCursorResultSet
(
FunctionIndex
index
,
SearchRow
first
,
SearchRow
last
,
Session
session
,
ResultSet
result
)
{
super
(
index
,
first
,
last
,
session
)
;
this
.
result
=
result
;
try
{
this
.
meta
=
result
.
getMetaData
();
...
...
@@ -37,23 +33,7 @@ public class FunctionCursorResultSet implements Cursor {
}
@Override
public
Row
get
()
{
if
(
values
==
null
)
{
return
null
;
}
if
(
row
==
null
)
{
row
=
session
.
createRow
(
values
,
1
);
}
return
row
;
}
@Override
public
SearchRow
getSearchRow
()
{
return
get
();
}
@Override
public
boolean
next
()
{
boolean
nextImpl
()
{
row
=
null
;
try
{
if
(
result
!=
null
&&
result
.
next
())
{
...
...
@@ -72,9 +52,4 @@ public class FunctionCursorResultSet implements Cursor {
return
values
!=
null
;
}
@Override
public
boolean
previous
()
{
throw
DbException
.
throwInternalError
(
toString
());
}
}
\ No newline at end of file
h2/src/main/org/h2/index/FunctionIndex.java
浏览文件 @
801913db
...
...
@@ -17,8 +17,8 @@ import org.h2.table.IndexColumn;
import
org.h2.table.TableFilter
;
/**
* An index for a function that returns a result set.
This index can only scan
*
through all rows, search is not support
ed.
* An index for a function that returns a result set.
Search in this index
*
performs scan over all rows and should be avoid
ed.
*/
public
class
FunctionIndex
extends
BaseIndex
{
...
...
@@ -44,12 +44,17 @@ public class FunctionIndex extends BaseIndex {
throw
DbException
.
getUnsupportedException
(
"ALIAS"
);
}
@Override
public
boolean
isFindUsingFullTableScan
()
{
return
true
;
}
@Override
public
Cursor
find
(
Session
session
,
SearchRow
first
,
SearchRow
last
)
{
if
(
functionTable
.
isBufferResultSetToLocalTemp
())
{
return
new
FunctionCursor
(
session
,
functionTable
.
getResult
(
session
));
return
new
FunctionCursor
(
this
,
first
,
last
,
session
,
functionTable
.
getResult
(
session
));
}
return
new
FunctionCursorResultSet
(
session
,
return
new
FunctionCursorResultSet
(
this
,
first
,
last
,
session
,
functionTable
.
getResultSet
(
session
));
}
...
...
h2/src/main/org/h2/index/Index.java
浏览文件 @
801913db
...
...
@@ -51,6 +51,15 @@ public interface Index extends SchemaObject {
*/
void
remove
(
Session
session
,
Row
row
);
/**
* Returns {@code true} if {@code find()} implementation performs scan over all
* index, {@code false} if {@code find()} performs the fast lookup.
*
* @return {@code true} if {@code find()} implementation performs scan over all
* index, {@code false} if {@code find()} performs the fast lookup
*/
boolean
isFindUsingFullTableScan
();
/**
* Find a row or a list of rows and create a cursor to iterate over the
* result.
...
...
h2/src/main/org/h2/index/IndexCursor.java
浏览文件 @
801913db
...
...
@@ -89,6 +89,11 @@ public class IndexCursor implements Cursor {
alwaysFalse
=
true
;
break
;
}
// If index can perform only full table scan do not try to use it for regular
// lookups, each such lookup will perform an own table scan.
if
(
index
.
isFindUsingFullTableScan
())
{
continue
;
}
Column
column
=
condition
.
getColumn
();
if
(
condition
.
getCompareType
()
==
Comparison
.
IN_LIST
)
{
if
(
start
==
null
&&
end
==
null
)
{
...
...
h2/src/main/org/h2/index/MultiVersionIndex.java
浏览文件 @
801913db
...
...
@@ -72,6 +72,11 @@ public class MultiVersionIndex implements Index {
}
}
@Override
public
boolean
isFindUsingFullTableScan
()
{
return
base
.
isFindUsingFullTableScan
();
}
@Override
public
Cursor
find
(
TableFilter
filter
,
SearchRow
first
,
SearchRow
last
)
{
synchronized
(
sync
)
{
...
...
h2/src/test/org/h2/test/db/TestIndex.java
浏览文件 @
801913db
...
...
@@ -10,19 +10,25 @@ import java.sql.PreparedStatement;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.sql.Types
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Random
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
org.h2.api.ErrorCode
;
import
org.h2.command.dml.Select
;
import
org.h2.result.SortOrder
;
import
org.h2.test.TestBase
;
import
org.h2.tools.SimpleResultSet
;
import
org.h2.value.ValueInt
;
/**
* Index tests.
*/
public
class
TestIndex
extends
TestBase
{
private
static
int
testFunctionIndexCounter
;
private
Connection
conn
;
private
Statement
stat
;
private
final
Random
random
=
new
Random
();
...
...
@@ -89,6 +95,8 @@ public class TestIndex extends TestBase {
testMultiColumnHashIndex
();
testFunctionIndex
();
conn
.
close
();
deleteDb
(
"index"
);
}
...
...
@@ -702,4 +710,39 @@ public class TestIndex extends TestBase {
trace
(
"---done---"
);
}
public
static
ResultSet
testFunctionIndexFunction
()
{
// There are additional callers like JdbcConnection.prepareCommand() and
// CommandContainer.recompileIfReqired()
for
(
StackTraceElement
element
:
Thread
.
currentThread
().
getStackTrace
())
{
if
(
element
.
getClassName
().
startsWith
(
Select
.
class
.
getName
()))
{
testFunctionIndexCounter
++;
break
;
}
}
SimpleResultSet
rs
=
new
SimpleResultSet
();
rs
.
addColumn
(
"ID"
,
Types
.
INTEGER
,
ValueInt
.
PRECISION
,
0
);
rs
.
addColumn
(
"VALUE"
,
Types
.
INTEGER
,
ValueInt
.
PRECISION
,
0
);
rs
.
addRow
(
1
,
10
);
rs
.
addRow
(
2
,
20
);
rs
.
addRow
(
3
,
30
);
return
rs
;
}
private
void
testFunctionIndex
()
throws
SQLException
{
testFunctionIndexCounter
=
0
;
stat
.
execute
(
"CREATE ALIAS TEST_INDEX FOR \""
+
TestIndex
.
class
.
getName
()
+
".testFunctionIndexFunction\""
);
try
(
ResultSet
rs
=
stat
.
executeQuery
(
"SELECT * FROM TEST_INDEX() WHERE ID = 1 OR ID = 3"
))
{
assertTrue
(
rs
.
next
());
assertEquals
(
1
,
rs
.
getInt
(
1
));
assertEquals
(
10
,
rs
.
getInt
(
2
));
assertTrue
(
rs
.
next
());
assertEquals
(
3
,
rs
.
getInt
(
1
));
assertEquals
(
30
,
rs
.
getInt
(
2
));
assertFalse
(
rs
.
next
());
}
finally
{
stat
.
execute
(
"DROP ALIAS TEST_INDEX"
);
}
assertEquals
(
1
,
testFunctionIndexCounter
);
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论