Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
fc19fdc3
提交
fc19fdc3
authored
1月 15, 2010
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
New LOB storage
上级
25e1e0da
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
231 行增加
和
0 行删除
+231
-0
TestLob.java
h2/src/test/org/h2/test/utils/TestLob.java
+231
-0
没有找到文件。
h2/src/test/org/h2/test/utils/TestLob.java
0 → 100644
浏览文件 @
fc19fdc3
/*
* Copyright 2004-2009 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
.
test
.
utils
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
org.h2.message.Message
;
import
org.h2.tools.DeleteDbFiles
;
import
org.h2.util.ByteUtils
;
import
org.h2.util.IOUtils
;
/**
* Implementation and tests of a LOB storage mechanism that splits LOBs in
* blocks.
*/
public
class
TestLob
{
private
static
final
int
BLOCK_LENGTH
=
20000
;
private
Connection
conn
;
private
PreparedStatement
prepInsertBlock
,
prepSelectBlock
,
prepDeleteBlock
;
private
PreparedStatement
prepInsertLob
,
prepSelectLob
,
prepDeleteLob
;
private
long
nextLob
;
private
long
nextBlock
;
/**
* The LOB identifier.
*/
private
static
class
LobId
{
private
long
id
;
private
long
length
;
LobId
(
long
id
,
long
length
)
{
this
.
id
=
id
;
this
.
length
=
length
;
}
LobId
(
byte
[]
key
)
{
id
=
ByteUtils
.
readLong
(
key
,
0
);
length
=
ByteUtils
.
readLong
(
key
,
8
);
}
byte
[]
getKey
()
{
byte
[]
key
=
new
byte
[
16
];
ByteUtils
.
writeLong
(
key
,
0
,
id
);
ByteUtils
.
writeLong
(
key
,
0
,
length
);
return
key
;
}
long
getId
()
{
return
id
;
}
long
getLength
()
{
return
length
;
}
}
/**
* An input stream that reads from a LOB.
*/
static
class
LobInputStream
extends
InputStream
{
private
byte
[]
page
=
new
byte
[
BLOCK_LENGTH
];
private
byte
[]
buff
=
new
byte
[
1
];
private
PreparedStatement
prepSelectBlock
;
private
long
remaining
;
private
long
offset
;
private
long
next
;
LobInputStream
(
PreparedStatement
prepSelectBlock
,
long
first
,
long
length
)
{
this
.
next
=
first
;
this
.
remaining
=
length
;
this
.
prepSelectBlock
=
prepSelectBlock
;
}
public
int
read
()
throws
IOException
{
int
len
=
readFully
(
buff
,
0
,
1
);
if
(
len
==
0
)
{
return
-
1
;
}
return
buff
[
0
]
&
255
;
}
public
int
read
(
byte
[]
buff
)
throws
IOException
{
return
readFully
(
buff
,
0
,
buff
.
length
);
}
public
int
read
(
byte
[]
buff
,
int
off
,
int
length
)
throws
IOException
{
return
readFully
(
buff
,
0
,
buff
.
length
);
}
private
int
readFully
(
byte
[]
buff
,
int
off
,
int
length
)
throws
IOException
{
int
len
=
0
;
while
(
length
>
0
&&
remaining
>
0
)
{
}
return
len
;
}
}
/**
* Run just this test.
*
* @param a ignored
*/
public
static
void
main
(
String
...
a
)
throws
Exception
{
new
TestLob
().
test
();
}
private
void
test
()
throws
SQLException
{
DeleteDbFiles
.
execute
(
"data"
,
"test"
,
true
);
org
.
h2
.
Driver
.
load
();
Connection
c
=
DriverManager
.
getConnection
(
"jdbc:h2:data/test"
);
init
(
c
);
c
=
DriverManager
.
getConnection
(
"jdbc:h2:data/test"
);
int
len
=
128
*
1024
;
byte
[]
buff
=
new
byte
[
len
];
Statement
stat
=
c
.
createStatement
();
stat
.
execute
(
"create table test(id int primary key, data blob)"
);
PreparedStatement
prep
=
conn
.
prepareStatement
(
"insert into test(id, data) values(?, ?)"
);
// Profiler prof = new Profiler();
// prof.setInterval(1);
// prof.startCollecting();
int
x
=
0
;
for
(
int
j
=
0
;
j
<
6
;
j
++)
{
boolean
regular
=
(
j
&
1
)
==
1
;
long
time
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
400
;
i
++)
{
prep
.
setInt
(
1
,
x
++);
if
(
regular
)
{
prep
.
setBinaryStream
(
2
,
new
ByteArrayInputStream
(
buff
),
len
);
prep
.
execute
();
}
else
{
addLob
(
new
ByteArrayInputStream
(
buff
),
-
1
,
-
1
);
}
}
System
.
out
.
println
((
regular
?
"regular: "
:
"block: "
)
+
(
System
.
currentTimeMillis
()
-
time
));
}
// prof.stopCollecting();
// System.out.println(prof.getTop(5));
c
.
close
();
}
private
void
init
(
Connection
newConn
)
throws
SQLException
{
this
.
conn
=
newConn
;
Statement
stat
=
conn
.
createStatement
();
stat
.
execute
(
"set undo_log 0"
);
// stat.execute("set redo_log_binary 0");
// TODO support incremental garbage collection
stat
.
execute
(
"create table if not exists block(id bigint primary key, next bigint, data binary)"
);
stat
.
execute
(
"create table if not exists lob(id bigint primary key, length bigint, block bigint, table int)"
);
ResultSet
rs
;
rs
=
stat
.
executeQuery
(
"select max(id) from block"
);
rs
.
next
();
nextBlock
=
rs
.
getLong
(
1
)
+
1
;
rs
=
stat
.
executeQuery
(
"select max(id) from lob"
);
rs
.
next
();
nextLob
=
rs
.
getLong
(
1
)
+
1
;
prepInsertBlock
=
conn
.
prepareStatement
(
"insert into block values(?, ?, ?)"
);
prepDeleteBlock
=
conn
.
prepareStatement
(
"delete from block where id = ?"
);
prepInsertLob
=
conn
.
prepareStatement
(
"insert into lob values(?, ?, ?, ?)"
);
prepSelectBlock
=
conn
.
prepareStatement
(
"select id, next, data from block where id = ?"
);
}
private
LobId
addLob
(
InputStream
in
,
long
maxLength
,
int
table
)
throws
SQLException
{
byte
[][]
buff
=
new
byte
[
2
][
BLOCK_LENGTH
];
if
(
maxLength
<
0
)
{
maxLength
=
Long
.
MAX_VALUE
;
}
long
length
=
0
;
int
blockId
=
0
;
long
firstBlock
=
nextBlock
;
long
lob
=
nextLob
++;
try
{
for
(;
maxLength
>
0
;
blockId
++)
{
int
len
=
IOUtils
.
readFully
(
in
,
buff
[
blockId
&
1
],
0
,
BLOCK_LENGTH
);
if
(
len
<=
0
)
{
break
;
}
length
+=
len
;
maxLength
-=
len
;
if
(
blockId
>
0
)
{
prepInsertBlock
.
setLong
(
1
,
nextBlock
++);
prepInsertBlock
.
setLong
(
2
,
nextBlock
);
prepInsertBlock
.
setBytes
(
3
,
buff
[(
blockId
-
1
)
&
1
]);
prepInsertBlock
.
execute
();
}
}
prepInsertBlock
.
setLong
(
1
,
nextBlock
++);
prepInsertBlock
.
setLong
(
2
,
firstBlock
);
prepInsertBlock
.
setBytes
(
3
,
buff
[(
blockId
-
1
)
&
1
]);
prepInsertBlock
.
execute
();
prepInsertLob
.
setLong
(
1
,
lob
);
prepInsertLob
.
setLong
(
2
,
length
);
prepInsertLob
.
setLong
(
3
,
firstBlock
);
prepInsertLob
.
setInt
(
4
,
table
);
prepInsertLob
.
execute
();
return
new
LobId
(
lob
,
length
);
}
catch
(
IOException
e
)
{
deleteBlocks
(
firstBlock
,
nextBlock
-
1
);
throw
Message
.
convertIOException
(
e
,
"adding blob"
);
}
}
private
InputStream
getInputStream
(
LobId
lobId
)
{
long
id
=
lobId
.
getId
();
long
length
=
lobId
.
getLength
();
return
new
LobInputStream
(
prepSelectBlock
,
id
,
length
);
}
private
void
deleteBlocks
(
long
first
,
long
last
)
throws
SQLException
{
for
(
long
id
=
first
;
id
<=
last
;
id
++)
{
prepDeleteBlock
.
setLong
(
1
,
id
);
prepDeleteBlock
.
execute
();
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论