Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
411255cd
提交
411255cd
authored
2月 15, 2010
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Hash indexes now are only used for single column indexes.
上级
62c5f801
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
81 行增加
和
143 行删除
+81
-143
BaseHashIndex.java
h2/src/main/org/h2/index/BaseHashIndex.java
+0
-85
HashIndex.java
h2/src/main/org/h2/index/HashIndex.java
+64
-43
IndexType.java
h2/src/main/org/h2/index/IndexType.java
+1
-0
NonUniqueHashCursor.java
h2/src/main/org/h2/index/NonUniqueHashCursor.java
+3
-3
NonUniqueHashIndex.java
h2/src/main/org/h2/index/NonUniqueHashIndex.java
+12
-11
TableData.java
h2/src/main/org/h2/table/TableData.java
+1
-1
没有找到文件。
h2/src/main/org/h2/index/BaseHashIndex.java
deleted
100644 → 0
浏览文件 @
62c5f801
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 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.SearchRow
;
import
org.h2.table.Column
;
import
org.h2.table.IndexColumn
;
import
org.h2.table.TableData
;
import
org.h2.value.Value
;
import
org.h2.value.ValueArray
;
/**
* Base of hash indexes.
*/
public
abstract
class
BaseHashIndex
extends
BaseIndex
{
public
BaseHashIndex
(
TableData
table
,
int
id
,
String
indexName
,
IndexColumn
[]
columns
,
IndexType
indexType
)
{
initBaseIndex
(
table
,
id
,
indexName
,
columns
,
indexType
);
}
public
void
close
(
Session
session
)
{
// nothing to do
}
public
void
remove
(
Session
session
)
{
// nothing to do
}
/**
* Generate the search key from a row. Single column indexes are mapped to
* the value, multi-column indexes are mapped to an value array.
*
* @param row the row
* @return the value
*/
protected
Value
getKey
(
SearchRow
row
)
{
if
(
columns
.
length
==
1
)
{
Column
column
=
columns
[
0
];
int
index
=
column
.
getColumnId
();
Value
v
=
row
.
getValue
(
index
);
return
v
;
}
Value
[]
list
=
new
Value
[
columns
.
length
];
for
(
int
i
=
0
;
i
<
columns
.
length
;
i
++)
{
Column
column
=
columns
[
i
];
int
index
=
column
.
getColumnId
();
list
[
i
]
=
row
.
getValue
(
index
);
}
return
ValueArray
.
get
(
list
);
}
public
double
getCost
(
Session
session
,
int
[]
masks
)
{
for
(
Column
column
:
columns
)
{
int
index
=
column
.
getColumnId
();
int
mask
=
masks
[
index
];
if
((
mask
&
IndexCondition
.
EQUALITY
)
!=
IndexCondition
.
EQUALITY
)
{
return
Long
.
MAX_VALUE
;
}
}
return
2
;
}
public
void
checkRename
()
{
// ok
}
public
boolean
needRebuild
()
{
return
true
;
}
public
boolean
canGetFirstOrLast
()
{
return
false
;
}
public
Cursor
findFirstOrLast
(
Session
session
,
boolean
first
)
{
throw
DbException
.
getUnsupportedException
(
"HASH"
);
}
}
h2/src/main/org/h2/index/HashIndex.java
浏览文件 @
411255cd
...
...
@@ -10,33 +10,30 @@ import org.h2.engine.Session;
import
org.h2.message.DbException
;
import
org.h2.result.Row
;
import
org.h2.result.SearchRow
;
import
org.h2.table.Column
;
import
org.h2.table.IndexColumn
;
import
org.h2.table.TableData
;
import
org.h2.util.IntIntHashMap
;
import
org.h2.util.ValueHashMap
;
import
org.h2.value.Value
;
/**
* An unique index based on an in-memory hash map.
*/
public
class
HashIndex
extends
Base
Hash
Index
{
public
class
HashIndex
extends
BaseIndex
{
pr
ivate
ValueHashMap
<
Integer
>
rows
;
private
IntIntHashMap
intMap
;
private
TableData
tableData
;
pr
otected
final
int
indexColumn
;
private
final
TableData
tableData
;
private
ValueHashMap
<
Long
>
rows
;
public
HashIndex
(
TableData
table
,
int
id
,
String
indexName
,
IndexColumn
[]
columns
,
IndexType
indexType
)
{
super
(
table
,
id
,
indexName
,
columns
,
indexType
);
initBaseIndex
(
table
,
id
,
indexName
,
columns
,
indexType
);
this
.
indexColumn
=
columns
[
0
].
column
.
getColumnId
();
this
.
tableData
=
table
;
reset
();
}
private
void
reset
()
{
if
(
columns
.
length
==
1
&&
columns
[
0
].
getType
()
==
Value
.
INT
)
{
intMap
=
new
IntIntHashMap
();
}
else
{
rows
=
ValueHashMap
.
newInstance
(
table
.
getDatabase
());
}
rows
=
ValueHashMap
.
newInstance
(
table
.
getDatabase
());
}
public
void
truncate
(
Session
session
)
{
...
...
@@ -44,27 +41,17 @@ public class HashIndex extends BaseHashIndex {
}
public
void
add
(
Session
session
,
Row
row
)
{
if
(
intMap
!=
null
)
{
int
key
=
row
.
getValue
(
columns
[
0
].
getColumnId
()).
getInt
();
intMap
.
put
(
key
,
(
int
)
row
.
getKey
());
}
else
{
Value
key
=
getKey
(
row
);
Object
old
=
rows
.
get
(
key
);
if
(
old
!=
null
)
{
// TODO index duplicate key for hash indexes: is this allowed?
throw
getDuplicateKeyException
();
}
rows
.
put
(
getKey
(
row
),
(
int
)
row
.
getKey
());
Value
key
=
row
.
getValue
(
indexColumn
);
Object
old
=
rows
.
get
(
key
);
if
(
old
!=
null
)
{
// TODO index duplicate key for hash indexes: is this allowed?
throw
getDuplicateKeyException
();
}
rows
.
put
(
key
,
row
.
getKey
());
}
public
void
remove
(
Session
session
,
Row
row
)
{
if
(
intMap
!=
null
)
{
int
key
=
row
.
getValue
(
columns
[
0
].
getColumnId
()).
getInt
();
intMap
.
remove
(
key
);
}
else
{
rows
.
remove
(
getKey
(
row
));
}
rows
.
remove
(
row
.
getValue
(
indexColumn
));
}
public
Cursor
find
(
Session
session
,
SearchRow
first
,
SearchRow
last
)
{
...
...
@@ -73,21 +60,11 @@ public class HashIndex extends BaseHashIndex {
throw
DbException
.
throwInternalError
();
}
Row
result
;
if
(
intMap
!=
null
)
{
int
key
=
first
.
getValue
(
columns
[
0
].
getColumnId
()).
getInt
();
int
pos
=
intMap
.
get
(
key
);
if
(
pos
!=
IntIntHashMap
.
NOT_FOUND
)
{
result
=
tableData
.
getRow
(
session
,
pos
);
}
else
{
result
=
null
;
}
Long
pos
=
rows
.
get
(
first
.
getValue
(
indexColumn
));
if
(
pos
==
null
)
{
result
=
null
;
}
else
{
Integer
pos
=
rows
.
get
(
getKey
(
first
));
if
(
pos
==
null
)
{
result
=
null
;
}
else
{
result
=
tableData
.
getRow
(
session
,
pos
.
intValue
());
}
result
=
tableData
.
getRow
(
session
,
pos
.
intValue
());
}
return
new
HashCursor
(
result
);
}
...
...
@@ -97,7 +74,51 @@ public class HashIndex extends BaseHashIndex {
}
public
long
getRowCountApproximation
()
{
return
intMap
!=
null
?
intMap
.
size
()
:
rows
.
size
();
return
rows
.
size
();
}
public
void
close
(
Session
session
)
{
// nothing to do
}
public
void
remove
(
Session
session
)
{
// nothing to do
}
/**
* Generate the search key from a row. Only single column indexes are
* supported (multi-column indexes could be mapped to an value array, but
* that is much slower than using a tree based index).
*
* @param row the row
* @return the value
*/
public
double
getCost
(
Session
session
,
int
[]
masks
)
{
for
(
Column
column
:
columns
)
{
int
index
=
column
.
getColumnId
();
int
mask
=
masks
[
index
];
if
((
mask
&
IndexCondition
.
EQUALITY
)
!=
IndexCondition
.
EQUALITY
)
{
return
Long
.
MAX_VALUE
;
}
}
return
2
;
}
public
void
checkRename
()
{
// ok
}
public
boolean
needRebuild
()
{
return
true
;
}
public
boolean
canGetFirstOrLast
()
{
return
false
;
}
public
Cursor
findFirstOrLast
(
Session
session
,
boolean
first
)
{
throw
DbException
.
getUnsupportedException
(
"HASH"
);
}
}
h2/src/main/org/h2/index/IndexType.java
浏览文件 @
411255cd
...
...
@@ -10,6 +10,7 @@ package org.h2.index;
* Represents information about the properties of an index
*/
public
class
IndexType
{
private
boolean
primaryKey
,
persistent
,
unique
,
hash
,
scan
;
private
boolean
belongsToConstraint
;
...
...
h2/src/main/org/h2/index/NonUniqueHashCursor.java
浏览文件 @
411255cd
...
...
@@ -6,11 +6,11 @@
*/
package
org
.
h2
.
index
;
import
java.util.ArrayList
;
import
org.h2.engine.Session
;
import
org.h2.result.Row
;
import
org.h2.result.SearchRow
;
import
org.h2.table.TableData
;
import
org.h2.util.IntArray
;
/**
* Cursor implementation for non-unique hash index
...
...
@@ -20,12 +20,12 @@ import org.h2.util.IntArray;
public
class
NonUniqueHashCursor
implements
Cursor
{
private
final
Session
session
;
private
final
IntArray
positions
;
private
final
ArrayList
<
Long
>
positions
;
private
final
TableData
tableData
;
private
int
index
=
-
1
;
public
NonUniqueHashCursor
(
Session
session
,
TableData
tableData
,
IntArray
positions
)
{
public
NonUniqueHashCursor
(
Session
session
,
TableData
tableData
,
ArrayList
<
Long
>
positions
)
{
this
.
session
=
session
;
this
.
tableData
=
tableData
;
this
.
positions
=
positions
;
...
...
h2/src/main/org/h2/index/NonUniqueHashIndex.java
浏览文件 @
411255cd
...
...
@@ -6,13 +6,14 @@
*/
package
org
.
h2
.
index
;
import
java.util.ArrayList
;
import
org.h2.engine.Session
;
import
org.h2.message.DbException
;
import
org.h2.result.Row
;
import
org.h2.result.SearchRow
;
import
org.h2.table.IndexColumn
;
import
org.h2.table.TableData
;
import
org.h2.util.
IntArray
;
import
org.h2.util.
New
;
import
org.h2.util.ValueHashMap
;
import
org.h2.value.Value
;
...
...
@@ -21,9 +22,9 @@ import org.h2.value.Value;
*
* @author Sergi Vladykin
*/
public
class
NonUniqueHashIndex
extends
Base
HashIndex
{
public
class
NonUniqueHashIndex
extends
HashIndex
{
private
ValueHashMap
<
IntArray
>
rows
;
private
ValueHashMap
<
ArrayList
<
Long
>
>
rows
;
private
TableData
tableData
;
private
long
rowCount
;
...
...
@@ -43,13 +44,13 @@ public class NonUniqueHashIndex extends BaseHashIndex {
}
public
void
add
(
Session
session
,
Row
row
)
{
Value
key
=
getKey
(
row
);
IntArray
positions
=
rows
.
get
(
key
);
Value
key
=
row
.
getValue
(
indexColumn
);
ArrayList
<
Long
>
positions
=
rows
.
get
(
key
);
if
(
positions
==
null
)
{
positions
=
new
IntArray
(
1
);
positions
=
New
.
arrayList
(
);
rows
.
put
(
key
,
positions
);
}
positions
.
add
(
(
int
)
row
.
getKey
());
positions
.
add
(
row
.
getKey
());
rowCount
++;
}
...
...
@@ -58,13 +59,13 @@ public class NonUniqueHashIndex extends BaseHashIndex {
// last row in table
reset
();
}
else
{
Value
key
=
getKey
(
row
);
IntArray
positions
=
rows
.
get
(
key
);
Value
key
=
row
.
getValue
(
indexColumn
);
ArrayList
<
Long
>
positions
=
rows
.
get
(
key
);
if
(
positions
.
size
()
==
1
)
{
// last row with such key
rows
.
remove
(
key
);
}
else
{
positions
.
remove
Value
((
int
)
row
.
getKey
());
positions
.
remove
(
row
.
getKey
());
}
rowCount
--;
}
...
...
@@ -79,7 +80,7 @@ public class NonUniqueHashIndex extends BaseHashIndex {
throw
DbException
.
throwInternalError
();
}
}
IntArray
positions
=
rows
.
get
(
getKey
(
first
));
ArrayList
<
Long
>
positions
=
rows
.
get
(
first
.
getValue
(
indexColumn
));
return
new
NonUniqueHashCursor
(
session
,
tableData
,
positions
);
}
...
...
h2/src/main/org/h2/table/TableData.java
浏览文件 @
411255cd
...
...
@@ -196,7 +196,7 @@ public class TableData extends Table {
index
=
new
PageBtreeIndex
(
this
,
indexId
,
indexName
,
cols
,
indexType
,
create
,
session
);
}
}
else
{
if
(
indexType
.
isHash
())
{
if
(
indexType
.
isHash
()
&&
cols
.
length
<=
1
)
{
if
(
indexType
.
isUnique
())
{
index
=
new
HashIndex
(
this
,
indexId
,
indexName
,
cols
,
indexType
);
}
else
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论