Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
237b1a55
提交
237b1a55
authored
4月 11, 2013
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MVStore table engine - now almost as fast as the page store (WIP)
上级
c9e973a3
显示空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
187 行增加
和
201 行删除
+187
-201
Session.java
h2/src/main/org/h2/engine/Session.java
+6
-8
MVDelegateIndex.java
h2/src/main/org/h2/mvstore/db/MVDelegateIndex.java
+2
-0
MVPrimaryIndex.java
h2/src/main/org/h2/mvstore/db/MVPrimaryIndex.java
+27
-31
MVSecondaryIndex.java
h2/src/main/org/h2/mvstore/db/MVSecondaryIndex.java
+20
-19
MVTable.java
h2/src/main/org/h2/mvstore/db/MVTable.java
+6
-5
MVTableEngine.java
h2/src/main/org/h2/mvstore/db/MVTableEngine.java
+3
-3
TransactionStore2.java
h2/src/main/org/h2/mvstore/db/TransactionStore2.java
+97
-111
Row.java
h2/src/main/org/h2/result/Row.java
+4
-0
TestMVTableEngine.java
h2/src/test/org/h2/test/store/TestMVTableEngine.java
+13
-10
TestTransactionStore.java
h2/src/test/org/h2/test/store/TestTransactionStore.java
+9
-14
没有找到文件。
h2/src/main/org/h2/engine/Session.java
浏览文件 @
237b1a55
...
...
@@ -23,8 +23,8 @@ import org.h2.jdbc.JdbcConnection;
import
org.h2.message.DbException
;
import
org.h2.message.Trace
;
import
org.h2.message.TraceSystem
;
import
org.h2.mvstore.db.TransactionStore
;
import
org.h2.mvstore.db.TransactionStore.Transaction
;
import
org.h2.mvstore.db.TransactionStore
2
;
import
org.h2.mvstore.db.TransactionStore
2
.Transaction
;
import
org.h2.result.ResultInterface
;
import
org.h2.result.Row
;
import
org.h2.schema.Schema
;
...
...
@@ -106,7 +106,6 @@ public class Session extends SessionWithState {
private
SmallLRUCache
<
String
,
Command
>
queryCache
;
private
Transaction
transaction
;
private
long
startStatement
=
-
1
;
private
long
statementVersion
;
public
Session
(
Database
database
,
User
user
,
int
id
)
{
this
.
database
=
database
;
...
...
@@ -1258,20 +1257,19 @@ public class Session extends SessionWithState {
* @param store the store
* @return the transaction
*/
public
Transaction
getTransaction
(
TransactionStore
store
)
{
public
Transaction
getTransaction
(
TransactionStore
2
store
)
{
if
(
transaction
==
null
)
{
transaction
=
store
.
begin
();
sta
tementVersion
=
-
1
;
sta
rtStatement
=
-
1
;
}
return
transaction
;
}
public
long
getStatement
Version
()
{
public
long
getStatement
Savepoint
()
{
if
(
startStatement
==
-
1
)
{
startStatement
=
transaction
.
setSavepoint
();
statementVersion
=
transaction
.
getCurrentVersion
();
}
return
sta
tementVersion
;
return
sta
rtStatement
;
}
}
h2/src/main/org/h2/mvstore/db/MVDelegateIndex.java
浏览文件 @
237b1a55
...
...
@@ -35,7 +35,9 @@ public class MVDelegateIndex extends BaseIndex {
}
}
static
int
count
;
public
void
add
(
Session
session
,
Row
row
)
{
count
++;
// nothing to do
}
...
...
h2/src/main/org/h2/mvstore/db/MVPrimaryIndex.java
浏览文件 @
237b1a55
...
...
@@ -18,8 +18,8 @@ import org.h2.index.Cursor;
import
org.h2.index.IndexType
;
import
org.h2.message.DbException
;
import
org.h2.mvstore.MVMap
;
import
org.h2.mvstore.db.TransactionStore.Transaction
;
import
org.h2.mvstore.db.TransactionStore.TransactionMap
;
import
org.h2.mvstore.db.TransactionStore
2
.Transaction
;
import
org.h2.mvstore.db.TransactionStore
2
.TransactionMap
;
import
org.h2.result.Row
;
import
org.h2.result.SearchRow
;
import
org.h2.result.SortOrder
;
...
...
@@ -37,7 +37,7 @@ public class MVPrimaryIndex extends BaseIndex {
private
final
MVTable
mvTable
;
private
String
mapName
;
private
MVMap
.
Builder
<
Value
,
Value
>
mapBuilder
;
private
TransactionMap
<
Value
,
Value
>
dataMap
;
private
long
lastKey
;
private
int
mainIndexColumn
=
-
1
;
...
...
@@ -54,12 +54,12 @@ public class MVPrimaryIndex extends BaseIndex {
ValueDataType
valueType
=
new
ValueDataType
(
db
.
getCompareMode
(),
db
,
sortTypes
);
mapName
=
getName
()
+
"_"
+
getId
();
mapBuilder
=
new
MVMap
.
Builder
<
Value
,
Value
>().
MVMap
.
Builder
<
Value
,
Value
>
mapBuilder
=
new
MVMap
.
Builder
<
Value
,
Value
>().
keyType
(
keyType
).
valueType
(
valueType
);
TransactionMap
<
Value
,
Value
>
map
=
getMap
(
null
);
Value
k
=
m
ap
.
lastKey
();
m
ap
.
getTransaction
().
commit
();
dataMap
=
mvTable
.
getTransaction
(
null
).
openMap
(
mapName
,
mapBuilder
);
Value
k
=
dataM
ap
.
lastKey
();
dataM
ap
.
getTransaction
().
commit
();
lastKey
=
k
==
null
?
0
:
k
.
getLong
();
}
...
...
@@ -108,10 +108,6 @@ public class MVPrimaryIndex extends BaseIndex {
Long
c
=
row
.
getValue
(
mainIndexColumn
).
getLong
();
row
.
setKey
(
c
);
}
Value
[]
array
=
new
Value
[
columns
.
length
];
for
(
int
i
=
0
;
i
<
array
.
length
;
i
++)
{
array
[
i
]
=
row
.
getValue
(
i
);
}
TransactionMap
<
Value
,
Value
>
map
=
getMap
(
session
);
if
(
map
.
containsKey
(
ValueLong
.
get
(
row
.
getKey
())))
{
String
sql
=
"PRIMARY KEY ON "
+
table
.
getSQL
();
...
...
@@ -122,7 +118,7 @@ public class MVPrimaryIndex extends BaseIndex {
e
.
setSource
(
this
);
throw
e
;
}
map
.
put
(
ValueLong
.
get
(
row
.
getKey
()),
ValueArray
.
get
(
array
));
map
.
put
(
ValueLong
.
get
(
row
.
getKey
()),
ValueArray
.
get
(
row
.
getValueList
()
));
lastKey
=
Math
.
max
(
lastKey
,
row
.
getKey
());
}
...
...
@@ -287,6 +283,25 @@ public class MVPrimaryIndex extends BaseIndex {
return
new
MVStoreCursor
(
session
,
map
.
keyIterator
(
ValueLong
.
get
(
first
)),
last
);
}
public
boolean
isRowIdIndex
()
{
return
true
;
}
/**
* Get the map to store the data.
*
* @param session the session
* @return the map
*/
TransactionMap
<
Value
,
Value
>
getMap
(
Session
session
)
{
if
(
session
==
null
)
{
return
dataMap
;
}
Transaction
t
=
mvTable
.
getTransaction
(
session
);
long
savepoint
=
session
.
getStatementSavepoint
();
return
dataMap
.
getInstance
(
t
,
savepoint
);
}
/**
* A cursor.
*/
...
...
@@ -337,23 +352,4 @@ public class MVPrimaryIndex extends BaseIndex {
}
public
boolean
isRowIdIndex
()
{
return
true
;
}
/**
* Get the map to store the data.
*
* @param session the session
* @return the map
*/
TransactionMap
<
Value
,
Value
>
getMap
(
Session
session
)
{
if
(
session
==
null
)
{
return
mvTable
.
getTransaction
(
null
).
openMap
(
mapName
,
-
1
,
mapBuilder
);
}
Transaction
t
=
mvTable
.
getTransaction
(
session
);
long
version
=
session
.
getStatementVersion
();
return
t
.
openMap
(
mapName
,
version
,
mapBuilder
);
}
}
h2/src/main/org/h2/mvstore/db/MVSecondaryIndex.java
浏览文件 @
237b1a55
...
...
@@ -17,8 +17,8 @@ import org.h2.index.Cursor;
import
org.h2.index.IndexType
;
import
org.h2.message.DbException
;
import
org.h2.mvstore.MVMap
;
import
org.h2.mvstore.db.TransactionStore.Transaction
;
import
org.h2.mvstore.db.TransactionStore.TransactionMap
;
import
org.h2.mvstore.db.TransactionStore
2
.Transaction
;
import
org.h2.mvstore.db.TransactionStore
2
.TransactionMap
;
import
org.h2.result.Row
;
import
org.h2.result.SearchRow
;
import
org.h2.result.SortOrder
;
...
...
@@ -42,7 +42,7 @@ public class MVSecondaryIndex extends BaseIndex {
private
final
int
keyColumns
;
private
String
mapName
;
private
MVMap
.
Builder
<
Value
,
Value
>
mapBuilder
;
private
TransactionMap
<
Value
,
Value
>
dataMap
;
public
MVSecondaryIndex
(
Database
db
,
MVTable
table
,
int
id
,
String
indexName
,
IndexColumn
[]
columns
,
IndexType
indexType
)
{
...
...
@@ -63,9 +63,10 @@ public class MVSecondaryIndex extends BaseIndex {
ValueDataType
keyType
=
new
ValueDataType
(
db
.
getCompareMode
(),
db
,
sortTypes
);
ValueDataType
valueType
=
new
ValueDataType
(
null
,
null
,
null
);
mapBuilder
=
new
MVMap
.
Builder
<
Value
,
Value
>().
MVMap
.
Builder
<
Value
,
Value
>
mapBuilder
=
new
MVMap
.
Builder
<
Value
,
Value
>().
keyType
(
keyType
).
valueType
(
valueType
);
dataMap
=
mvTable
.
getTransaction
(
null
).
openMap
(
mapName
,
mapBuilder
);
}
private
static
void
checkIndexColumnTypes
(
IndexColumn
[]
columns
)
{
...
...
@@ -247,6 +248,21 @@ public class MVSecondaryIndex extends BaseIndex {
// ok
}
/**
* Get the map to store the data.
*
* @param session the session
* @return the map
*/
TransactionMap
<
Value
,
Value
>
getMap
(
Session
session
)
{
if
(
session
==
null
)
{
return
dataMap
;
}
Transaction
t
=
mvTable
.
getTransaction
(
session
);
long
savepoint
=
session
.
getStatementSavepoint
();
return
dataMap
.
getInstance
(
t
,
savepoint
);
}
/**
* A cursor.
*/
...
...
@@ -308,19 +324,4 @@ public class MVSecondaryIndex extends BaseIndex {
}
/**
* Get the map to store the data.
*
* @param session the session
* @return the map
*/
TransactionMap
<
Value
,
Value
>
getMap
(
Session
session
)
{
if
(
session
==
null
)
{
return
mvTable
.
getTransaction
(
null
).
openMap
(
mapName
,
-
1
,
mapBuilder
);
}
Transaction
t
=
mvTable
.
getTransaction
(
session
);
long
version
=
session
.
getStatementVersion
();
return
t
.
openMap
(
mapName
,
version
,
mapBuilder
);
}
}
h2/src/main/org/h2/mvstore/db/MVTable.java
浏览文件 @
237b1a55
...
...
@@ -28,7 +28,7 @@ import org.h2.index.IndexType;
import
org.h2.index.MultiVersionIndex
;
import
org.h2.message.DbException
;
import
org.h2.message.Trace
;
import
org.h2.mvstore.db.TransactionStore.Transaction
;
import
org.h2.mvstore.db.TransactionStore
2
.Transaction
;
import
org.h2.result.Row
;
import
org.h2.result.SortOrder
;
import
org.h2.schema.SchemaObject
;
...
...
@@ -48,7 +48,7 @@ import org.h2.value.Value;
public
class
MVTable
extends
TableBase
{
private
final
String
storeName
;
private
final
TransactionStore
store
;
private
final
TransactionStore
2
store
;
private
MVPrimaryIndex
primaryIndex
;
private
ArrayList
<
Index
>
indexes
=
New
.
arrayList
();
private
long
lastModificationId
;
...
...
@@ -68,7 +68,7 @@ public class MVTable extends TableBase {
*/
private
boolean
waitForLock
;
public
MVTable
(
CreateTableData
data
,
String
storeName
,
TransactionStore
store
)
{
public
MVTable
(
CreateTableData
data
,
String
storeName
,
TransactionStore
2
store
)
{
super
(
data
);
nextAnalyze
=
database
.
getSettings
().
analyzeAuto
;
this
.
storeName
=
storeName
;
...
...
@@ -571,7 +571,8 @@ public class MVTable extends TableBase {
nextAnalyze
=
n
;
}
int
rows
=
session
.
getDatabase
().
getSettings
().
analyzeSample
;
Analyze
.
analyzeTable
(
session
,
this
,
rows
,
false
);
int
test
;
// Analyze.analyzeTable(session, this, rows, false);
}
@Override
...
...
@@ -686,7 +687,7 @@ public class MVTable extends TableBase {
return
session
.
getTransaction
(
store
);
}
public
TransactionStore
getStore
()
{
public
TransactionStore
2
getStore
()
{
return
store
;
}
...
...
h2/src/main/org/h2/mvstore/db/MVTableEngine.java
浏览文件 @
237b1a55
...
...
@@ -142,12 +142,12 @@ public class MVTableEngine implements TableEngine {
/**
* The transaction store.
*/
private
final
TransactionStore
transactionStore
;
private
final
TransactionStore
2
transactionStore
;
public
Store
(
Database
db
,
MVStore
store
)
{
this
.
db
=
db
;
this
.
store
=
store
;
this
.
transactionStore
=
new
TransactionStore
(
store
,
this
.
transactionStore
=
new
TransactionStore
2
(
store
,
new
ValueDataType
(
null
,
null
,
null
));
}
...
...
@@ -155,7 +155,7 @@ public class MVTableEngine implements TableEngine {
return
store
;
}
public
TransactionStore
getTransactionStore
()
{
public
TransactionStore
2
getTransactionStore
()
{
return
transactionStore
;
}
...
...
h2/src/main/org/h2/mvstore/db/TransactionStore2.java
浏览文件 @
237b1a55
...
...
@@ -49,7 +49,7 @@ public class TransactionStore2 {
/**
* The undo log.
* Key: [ transactionId, logId ], value: [
baseVersion
, mapId, key, oldValue ].
* Key: [ transactionId, logId ], value: [
opType
, mapId, key, oldValue ].
*/
final
MVMap
<
long
[],
Object
[]>
undoLog
;
...
...
@@ -90,7 +90,8 @@ public class TransactionStore2 {
// commit could be faster if we have one undo log per transaction,
// or a range delete operation for maps
ArrayType
valueType
=
new
ArrayType
(
new
DataType
[]{
new
ObjectDataType
(),
new
ObjectDataType
(),
keyType
new
ObjectDataType
(),
new
ObjectDataType
(),
keyType
,
new
ObjectDataType
()
});
MVMapConcurrent
.
Builder
<
long
[],
Object
[]>
builder
=
new
MVMapConcurrent
.
Builder
<
long
[],
Object
[]>().
...
...
@@ -153,7 +154,6 @@ public class TransactionStore2 {
* @return the transaction
*/
public
synchronized
Transaction
begin
()
{
store
.
incrementVersion
();
long
transactionId
=
lastTransactionId
++;
int
status
=
Transaction
.
STATUS_OPEN
;
return
new
Transaction
(
this
,
transactionId
,
status
,
null
,
0
);
...
...
@@ -215,7 +215,6 @@ public class TransactionStore2 {
Object
[]
old
=
openTransactions
.
get
(
t
.
getId
());
Object
[]
v
=
{
old
[
0
],
name
};
openTransactions
.
put
(
t
.
getId
(),
v
);
store
.
commit
();
}
/**
...
...
@@ -225,7 +224,6 @@ public class TransactionStore2 {
* @param maxLogId the last log id
*/
void
commit
(
Transaction
t
,
long
maxLogId
)
{
store
.
incrementVersion
();
for
(
long
logId
=
0
;
logId
<
maxLogId
;
logId
++)
{
long
[]
undoKey
=
new
long
[]
{
t
.
getId
(),
logId
};
...
...
@@ -250,15 +248,6 @@ public class TransactionStore2 {
}
openTransactions
.
remove
(
t
.
getId
());
openTransactionMap
.
remove
(
t
.
getId
());
store
.
commit
();
long
oldestVersion
=
store
.
getCurrentVersion
();
for
(
Transaction
u
:
openTransactionMap
.
values
())
{
long
v
=
u
.
startVersion
;
if
(
v
<
oldestVersion
)
{
oldestVersion
=
v
;
}
}
store
.
setRetainVersion
(
oldestVersion
);
}
/**
...
...
@@ -271,7 +260,6 @@ public class TransactionStore2 {
rollbackTo
(
t
,
maxLogId
,
0
);
openTransactions
.
remove
(
t
.
getId
());
openTransactionMap
.
remove
(
t
.
getId
());
store
.
commit
();
}
/**
...
...
@@ -282,7 +270,6 @@ public class TransactionStore2 {
* @param toLogId the log id to roll back to
*/
void
rollbackTo
(
Transaction
t
,
long
maxLogId
,
long
toLogId
)
{
store
.
incrementVersion
();
for
(
long
logId
=
maxLogId
-
1
;
logId
>=
toLogId
;
logId
--)
{
Object
[]
op
=
undoLog
.
get
(
new
long
[]
{
t
.
getId
(),
logId
});
...
...
@@ -293,8 +280,6 @@ public class TransactionStore2 {
MVMap
<
Object
,
Object
[]>
map
=
store
.
openMap
(
mapName
);
Object
key
=
op
[
2
];
Object
[]
oldValue
=
(
Object
[])
op
[
3
];
Object
[]
value
=
map
.
get
(
key
);
if
(
value
!=
null
)
{
if
(
oldValue
==
null
)
{
// this transaction added the value
map
.
remove
(
key
);
...
...
@@ -302,10 +287,8 @@ public class TransactionStore2 {
// this transaction updated the value
map
.
put
(
key
,
oldValue
);
}
undoLog
.
remove
(
op
);
}
undoLog
.
remove
(
logId
);
}
store
.
commit
();
}
/**
...
...
@@ -346,14 +329,14 @@ public class TransactionStore2 {
/**
* The transaction id.
*/
final
long
transactionId
;
final
Long
transactionId
;
long
logId
;
private
int
status
;
private
String
name
;
private
long
logId
;
Transaction
(
TransactionStore2
store
,
long
transactionId
,
int
status
,
String
name
,
long
logId
)
{
this
.
store
=
store
;
this
.
startVersion
=
store
.
store
.
getCurrentVersion
();
...
...
@@ -368,7 +351,7 @@ public class TransactionStore2 {
*
* @return the transaction id
*/
public
l
ong
getId
()
{
public
L
ong
getId
()
{
return
transactionId
;
}
...
...
@@ -408,7 +391,6 @@ public class TransactionStore2 {
*/
public
long
setSavepoint
()
{
checkOpen
();
store
.
store
.
incrementVersion
();
return
logId
;
}
...
...
@@ -425,7 +407,7 @@ public class TransactionStore2 {
}
/**
* Open a data map
where reads are always up to date
.
* Open a data map.
*
* @param <K> the key type
* @param <V> the value type
...
...
@@ -433,22 +415,9 @@ public class TransactionStore2 {
* @return the transaction map
*/
public
<
K
,
V
>
TransactionMap
<
K
,
V
>
openMap
(
String
name
)
{
return
openMap
(
name
,
-
1
);
}
/**
* Open a data map where reads are based on the specified version / savepoint.
*
* @param <K> the key type
* @param <V> the value type
* @param name the name of the map
* @param readVersion the version used for reading
* @return the transaction map
*/
public
<
K
,
V
>
TransactionMap
<
K
,
V
>
openMap
(
String
name
,
long
readVersion
)
{
checkOpen
();
return
new
TransactionMap
<
K
,
V
>(
this
,
name
,
new
ObjectDataType
(),
new
ObjectDataType
()
,
readVersion
);
new
ObjectDataType
());
}
/**
...
...
@@ -457,11 +426,10 @@ public class TransactionStore2 {
* @param <K> the key type
* @param <V> the value type
* @param name the name of the map
* @param readVersion the version used for reading
* @param builder the builder
* @return the transaction map
*/
public
<
K
,
V
>
TransactionMap
<
K
,
V
>
openMap
(
String
name
,
long
readVersion
,
Builder
<
K
,
V
>
builder
)
{
public
<
K
,
V
>
TransactionMap
<
K
,
V
>
openMap
(
String
name
,
Builder
<
K
,
V
>
builder
)
{
checkOpen
();
DataType
keyType
=
builder
.
getKeyType
();
if
(
keyType
==
null
)
{
...
...
@@ -471,7 +439,7 @@ public class TransactionStore2 {
if
(
valueType
==
null
)
{
valueType
=
new
ObjectDataType
();
}
return
new
TransactionMap
<
K
,
V
>(
this
,
name
,
keyType
,
valueType
,
readVersion
);
return
new
TransactionMap
<
K
,
V
>(
this
,
name
,
keyType
,
valueType
);
}
/**
...
...
@@ -549,34 +517,55 @@ public class TransactionStore2 {
* Key: key the key of the data.
* Value: { transactionId, oldVersion, value }
*/
private
final
MVMap
<
K
,
Object
[]>
map
Write
;
private
final
MVMap
<
K
,
Object
[]>
map
;
/**
* The map used for reading (possibly an older version). Reading is done
* on an older version so that changes are not immediately visible, to
* support statement processing (for example
* "update test set id = id + 1").
* <p>
* Key: key the key of the data.
* Value: { transactionId, oldVersion, value }
* If a record was read that was updated by this transaction, and the
* update occurred before this log id, the older version is read. This
* is so that changes are not immediately visible, to support statement
* processing (for example "update test set id = id + 1").
*/
private
final
MVMap
<
K
,
Object
[]>
mapRead
;
private
long
readLogId
=
Long
.
MAX_VALUE
;
TransactionMap
(
Transaction
transaction
,
String
name
,
DataType
keyType
,
DataType
valueType
,
long
readVersion
)
{
DataType
valueType
)
{
this
.
transaction
=
transaction
;
ArrayType
arrayType
=
new
ArrayType
(
new
DataType
[]
{
new
ObjectDataType
(),
new
ObjectDataType
(),
valueType
});
MVMap
.
Builder
<
K
,
Object
[]>
builder
=
new
MVMap
.
Builder
<
K
,
Object
[]>()
.
keyType
(
keyType
).
valueType
(
arrayType
);
mapWrite
=
transaction
.
store
.
store
.
openMap
(
name
,
builder
);
mapId
=
mapWrite
.
getId
();
if
(
readVersion
>=
0
)
{
mapRead
=
mapWrite
.
openVersion
(
readVersion
);
}
else
{
mapRead
=
mapWrite
;
map
=
transaction
.
store
.
store
.
openMap
(
name
,
builder
);
mapId
=
map
.
getId
();
}
private
TransactionMap
(
Transaction
transaction
,
MVMap
<
K
,
Object
[]>
map
,
int
mapId
)
{
this
.
transaction
=
transaction
;
this
.
map
=
map
;
this
.
mapId
=
mapId
;
}
/**
* Set the savepoint. Afterwards, reads are based on the specified
* savepoint.
*
* @param savepoint the savepoint
*/
public
void
setSavepoint
(
long
savepoint
)
{
this
.
readLogId
=
savepoint
;
}
/**
* Get a clone of this map for the given transaction.
*
* @param transaction the transaction
* @param savepoint the savepoint
* @return the map
*/
public
TransactionMap
<
K
,
V
>
getInstance
(
Transaction
transaction
,
long
savepoint
)
{
TransactionMap
<
K
,
V
>
m
=
new
TransactionMap
<
K
,
V
>(
transaction
,
map
,
mapId
);
m
.
setSavepoint
(
savepoint
);
return
m
;
}
/**
...
...
@@ -587,7 +576,7 @@ public class TransactionStore2 {
public
long
getSize
()
{
// TODO this method is very slow
long
size
=
0
;
Cursor
<
K
>
cursor
=
map
Read
.
keyIterator
(
null
);
Cursor
<
K
>
cursor
=
map
.
keyIterator
(
null
);
while
(
cursor
.
hasNext
())
{
K
key
=
cursor
.
next
();
if
(
get
(
key
)
!=
null
)
{
...
...
@@ -701,11 +690,10 @@ public class TransactionStore2 {
* @return true if the value was set
*/
public
boolean
trySet
(
K
key
,
V
value
,
boolean
onlyIfUnchanged
)
{
MVMap
<
K
,
Object
[]>
m
=
mapRead
;
Object
[]
current
=
mapWrite
.
get
(
key
);
Object
[]
current
=
map
.
get
(
key
);
if
(
onlyIfUnchanged
)
{
Object
[]
old
=
m
.
get
(
key
);
if
(!
map
Write
.
areValuesEqual
(
old
,
current
))
{
Object
[]
old
=
getArray
(
key
,
readLogId
);
if
(!
map
.
areValuesEqual
(
old
,
current
))
{
long
tx
=
(
Long
)
current
[
0
];
if
(
tx
==
transaction
.
transactionId
)
{
if
(
value
==
null
)
{
...
...
@@ -724,7 +712,6 @@ public class TransactionStore2 {
}
}
}
long
oldVersion
=
transaction
.
store
.
store
.
getCurrentVersion
()
-
1
;
int
opType
;
if
(
current
==
null
||
current
[
2
]
==
null
)
{
if
(
value
==
null
)
{
...
...
@@ -740,11 +727,12 @@ public class TransactionStore2 {
opType
=
Transaction
.
OP_SET
;
}
}
Object
[]
newValue
=
{
transaction
.
transactionId
,
oldVersion
,
value
};
Object
[]
newValue
=
{
transaction
.
transactionId
,
transaction
.
logId
,
value
};
if
(
current
==
null
)
{
// a new value
newValue
[
1
]
=
null
;
Object
[]
old
=
mapWrite
.
putIfAbsent
(
key
,
newValue
);
Object
[]
old
=
map
.
putIfAbsent
(
key
,
newValue
);
if
(
old
==
null
)
{
transaction
.
log
(
opType
,
mapId
,
key
,
current
);
return
true
;
...
...
@@ -754,15 +742,8 @@ public class TransactionStore2 {
long
tx
=
(
Long
)
current
[
0
];
if
(
tx
==
transaction
.
transactionId
)
{
// added or updated by this transaction
if
(
mapWrite
.
replace
(
key
,
current
,
newValue
))
{
if
(
current
[
1
]
==
null
)
{
if
(
map
.
replace
(
key
,
current
,
newValue
))
{
transaction
.
log
(
opType
,
mapId
,
key
,
current
);
}
else
{
long
c
=
(
Long
)
current
[
1
];
if
(
c
!=
oldVersion
)
{
transaction
.
log
(
opType
,
mapId
,
key
,
current
);
}
}
return
true
;
}
// strange, somebody overwrite the value
...
...
@@ -774,7 +755,7 @@ public class TransactionStore2 {
if
(!
open
)
{
// the transaction is committed:
// overwrite the value
if
(
map
Write
.
replace
(
key
,
current
,
newValue
))
{
if
(
map
.
replace
(
key
,
current
,
newValue
))
{
transaction
.
log
(
opType
,
mapId
,
key
,
current
);
return
true
;
}
...
...
@@ -792,7 +773,7 @@ public class TransactionStore2 {
* @return the value or null
*/
public
V
get
(
K
key
)
{
return
get
(
key
,
mapRea
d
);
return
get
(
key
,
readLogI
d
);
}
/**
...
...
@@ -802,7 +783,7 @@ public class TransactionStore2 {
* @return the value or null
*/
public
V
getLatest
(
K
key
)
{
return
get
(
key
,
mapWrite
);
return
get
(
key
,
Long
.
MAX_VALUE
);
}
/**
...
...
@@ -819,41 +800,46 @@ public class TransactionStore2 {
* Get the value for the given key.
*
* @param key the key
* @param m
the map
* @param m
axLogId the maximum log id
* @return the value or null
*/
@SuppressWarnings
(
"unchecked"
)
public
V
get
(
K
key
,
MVMap
<
K
,
Object
[]>
m
)
{
public
V
get
(
K
key
,
long
maxLogId
)
{
checkOpen
();
Object
[]
data
=
getArray
(
key
,
maxLogId
);
return
data
==
null
?
null
:
(
V
)
data
[
2
];
}
private
Object
[]
getArray
(
K
key
,
long
maxLog
)
{
Object
[]
data
=
map
.
get
(
key
);
while
(
true
)
{
Object
[]
data
=
m
.
get
(
key
);
long
tx
;
if
(
data
==
null
)
{
// doesn't exist or deleted by a committed transaction
return
null
;
}
tx
=
(
Long
)
data
[
0
];
long
logId
=
(
Long
)
data
[
1
];
if
(
tx
==
transaction
.
transactionId
)
{
// added by this transaction
return
(
V
)
data
[
2
];
if
(
logId
<
maxLog
)
{
return
data
;
}
}
// added or updated by another transaction
boolean
open
=
transaction
.
store
.
openTransactions
.
containsKey
(
tx
);
if
(!
open
)
{
// it is committed
return
(
V
)
data
[
2
]
;
return
data
;
}
tx
=
(
Long
)
data
[
0
];
// get the value before the uncommitted transaction
if
(
data
[
1
]
==
null
)
{
// a new entry
return
null
;
}
long
oldVersion
=
(
Long
)
data
[
1
];
m
=
mapWrite
.
openVersion
(
oldVersion
);
long
[]
x
=
new
long
[]
{
tx
,
logId
};
data
=
transaction
.
store
.
undoLog
.
get
(
x
);
data
=
(
Object
[])
data
[
3
];
}
}
/**
* Rename the map.
*
...
...
@@ -861,7 +847,7 @@ public class TransactionStore2 {
*/
public
void
renameMap
(
String
newMapName
)
{
// TODO rename maps transactionally
map
Write
.
renameMap
(
newMapName
);
map
.
renameMap
(
newMapName
);
}
/**
...
...
@@ -870,7 +856,7 @@ public class TransactionStore2 {
* @return true if closed
*/
public
boolean
isClosed
()
{
return
map
Write
.
isClosed
();
return
map
.
isClosed
();
}
/**
...
...
@@ -878,7 +864,7 @@ public class TransactionStore2 {
*/
public
void
removeMap
()
{
// TODO remove in a transaction
map
Write
.
removeMap
();
map
.
removeMap
();
}
/**
...
...
@@ -886,7 +872,7 @@ public class TransactionStore2 {
*/
public
void
clear
()
{
// TODO truncate transactionally
map
Write
.
clear
();
map
.
clear
();
}
/**
...
...
@@ -896,7 +882,7 @@ public class TransactionStore2 {
*/
public
K
firstKey
()
{
// TODO transactional firstKey
return
map
Read
.
firstKey
();
return
map
.
firstKey
();
}
/**
...
...
@@ -906,7 +892,7 @@ public class TransactionStore2 {
*/
public
K
lastKey
()
{
// TODO transactional lastKey
return
map
Read
.
lastKey
();
return
map
.
lastKey
();
}
/**
...
...
@@ -917,7 +903,7 @@ public class TransactionStore2 {
*/
public
Iterator
<
K
>
keyIterator
(
K
from
)
{
// TODO transactional keyIterator
return
map
Read
.
keyIterator
(
from
);
return
map
.
keyIterator
(
from
);
}
/**
...
...
@@ -928,7 +914,7 @@ public class TransactionStore2 {
*/
public
K
ceilingKey
(
K
key
)
{
// TODO transactional ceilingKey
return
map
Read
.
ceilingKey
(
key
);
return
map
.
ceilingKey
(
key
);
}
/**
...
...
@@ -940,7 +926,7 @@ public class TransactionStore2 {
*/
public
K
higherKey
(
K
key
)
{
// TODO transactional higherKey
return
map
Read
.
higherKey
(
key
);
return
map
.
higherKey
(
key
);
}
/**
...
...
@@ -952,7 +938,7 @@ public class TransactionStore2 {
*/
public
K
lowerKey
(
K
key
)
{
// TODO Auto-generated method stub
return
map
Read
.
lowerKey
(
key
);
return
map
.
lowerKey
(
key
);
}
public
Transaction
getTransaction
()
{
...
...
h2/src/main/org/h2/result/Row.java
浏览文件 @
237b1a55
...
...
@@ -164,4 +164,8 @@ public class Row implements SearchRow {
return
deleted
;
}
public
Value
[]
getValueList
()
{
return
data
;
}
}
h2/src/test/org/h2/test/store/TestMVTableEngine.java
浏览文件 @
237b1a55
...
...
@@ -17,6 +17,7 @@ import org.h2.engine.Constants;
import
org.h2.mvstore.db.MVTableEngine
;
import
org.h2.store.fs.FileUtils
;
import
org.h2.test.TestBase
;
import
org.h2.util.Profiler
;
import
org.h2.util.Task
;
/**
...
...
@@ -38,23 +39,26 @@ public class TestMVTableEngine extends TestBase {
testEncryption
();
testReadOnly
();
testReuseDiskSpace
();
testDataTypes
();
//
testDataTypes();
testLocking
();
testSimple
();
//
testSimple();
}
private
void
testSpeed
()
throws
Exception
{
String
dbName
;
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
dbName
=
"mvstore"
;
dbName
+=
";LOCK_MODE=0"
;
//
dbName += ";LOCK_MODE=0";
testSpeed
(
dbName
);
// Profiler prof = new Profiler().startCollecting();
int
tes
;
//Profiler prof = new Profiler().startCollecting();
dbName
=
"mvstore"
+
";DEFAULT_TABLE_ENGINE=org.h2.mvstore.db.MVTableEngine"
;
// dbName += ";LOCK_MODE=0";
testSpeed
(
dbName
);
//
System.out.println(prof.getTop(10));
//
System.out.println(prof.getTop(10));
}
FileUtils
.
deleteRecursive
(
getBaseDir
(),
true
);
}
private
void
testSpeed
(
String
dbName
)
throws
Exception
{
...
...
@@ -69,15 +73,14 @@ public class TestMVTableEngine extends TestBase {
stat
.
execute
(
"create table test(id int primary key, name varchar(255))"
);
PreparedStatement
prep
=
conn
.
prepareStatement
(
"insert into test values(?, ?)"
);
prep
.
setString
(
2
,
"Hello World"
);
prep
.
setString
(
2
,
"Hello World
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
"
);
long
time
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
1
00000
;
i
++)
{
for
(
int
i
=
0
;
i
<
2
00000
;
i
++)
{
prep
.
setInt
(
1
,
i
);
prep
.
execute
();
}
System
.
out
.
println
((
System
.
currentTimeMillis
()
-
time
)
+
" "
+
dbName
);
conn
.
close
();
FileUtils
.
deleteRecursive
(
getBaseDir
(),
true
);
}
private
void
testEncryption
()
throws
Exception
{
...
...
@@ -375,7 +378,7 @@ public class TestMVTableEngine extends TestBase {
stat
.
execute
(
"insert into test(id, name) values(10, 'Hello')"
);
fail
();
}
catch
(
SQLException
e
)
{
assertEquals
(
ErrorCode
.
DUPLICATE_KEY_1
,
e
.
getErrorCode
());
assertEquals
(
e
.
toString
(),
ErrorCode
.
DUPLICATE_KEY_1
,
e
.
getErrorCode
());
}
rs
=
stat
.
executeQuery
(
"select min(id), max(id), min(name), max(name) from test"
);
...
...
h2/src/test/org/h2/test/store/TestTransactionStore.java
浏览文件 @
237b1a55
...
...
@@ -64,21 +64,19 @@ public class TestTransactionStore extends TestBase {
Transaction
tx
;
TransactionMap
<
String
,
String
>
m
;
long
startUpdate
;
long
version
;
tx
=
ts
.
begin
();
// start of statement
// create table test
startUpdate
=
tx
.
setSavepoint
();
version
=
s
.
getCurrentVersion
(
);
tx
.
openMap
(
"test"
,
version
);
m
=
tx
.
openMap
(
"test"
);
m
.
setSavepoint
(
startUpdate
);
// start of statement
// insert into test(id, name) values(1, 'Hello'), (2, 'World')
startUpdate
=
tx
.
setSavepoint
();
version
=
s
.
getCurrentVersion
();
m
=
tx
.
openMap
(
"test"
,
version
);
m
.
setSavepoint
(
startUpdate
);
assertTrue
(
m
.
trySet
(
"1"
,
"Hello"
,
true
));
assertTrue
(
m
.
trySet
(
"2"
,
"World"
,
true
));
// not seen yet (within the same statement)
...
...
@@ -87,13 +85,12 @@ public class TestTransactionStore extends TestBase {
// start of statement
startUpdate
=
tx
.
setSavepoint
();
version
=
s
.
getCurrentVersion
();
// now we see the newest version
m
=
tx
.
openMap
(
"test"
,
version
);
m
.
setSavepoint
(
startUpdate
);
assertEquals
(
"Hello"
,
m
.
get
(
"1"
));
assertEquals
(
"World"
,
m
.
get
(
"2"
));
// update test set primaryKey = primaryKey + 1
// (this is usually a tricky case
s
)
// (this is usually a tricky case)
assertEquals
(
"Hello"
,
m
.
get
(
"1"
));
assertTrue
(
m
.
trySet
(
"1"
,
null
,
true
));
assertTrue
(
m
.
trySet
(
"2"
,
"Hello"
,
true
));
...
...
@@ -110,8 +107,7 @@ public class TestTransactionStore extends TestBase {
// start of statement
startUpdate
=
tx
.
setSavepoint
();
version
=
s
.
getCurrentVersion
();
m
=
tx
.
openMap
(
"test"
,
version
);
m
.
setSavepoint
(
startUpdate
);
// select * from test
assertNull
(
m
.
get
(
"1"
));
assertEquals
(
"Hello"
,
m
.
get
(
"2"
));
...
...
@@ -119,8 +115,7 @@ public class TestTransactionStore extends TestBase {
// start of statement
startUpdate
=
tx
.
setSavepoint
();
version
=
s
.
getCurrentVersion
();
m
=
tx
.
openMap
(
"test"
,
version
);
m
.
setSavepoint
(
startUpdate
);
// update test set id = 1
// should fail: duplicate key
assertTrue
(
m
.
trySet
(
"2"
,
null
,
true
));
...
...
@@ -129,8 +124,8 @@ public class TestTransactionStore extends TestBase {
assertFalse
(
m
.
trySet
(
"1"
,
"World"
,
true
));
tx
.
rollbackToSavepoint
(
startUpdate
);
version
=
s
.
getCurrentVersion
();
m
=
tx
.
openMap
(
"test"
,
version
);
startUpdate
=
tx
.
setSavepoint
();
m
.
setSavepoint
(
startUpdate
);
assertNull
(
m
.
get
(
"1"
));
assertEquals
(
"Hello"
,
m
.
get
(
"2"
));
assertEquals
(
"World"
,
m
.
get
(
"3"
));
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论