Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
4b2f3723
提交
4b2f3723
authored
10月 21, 2008
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improved database and in-memory file system.
上级
fb7999e1
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
67 行增加
和
38 行删除
+67
-38
FileSystemDatabase.java
h2/src/main/org/h2/store/fs/FileSystemDatabase.java
+1
-1
FileSystemMemory.java
h2/src/main/org/h2/store/fs/FileSystemMemory.java
+49
-33
TestFileSystem.java
h2/src/test/org/h2/test/unit/TestFileSystem.java
+17
-4
没有找到文件。
h2/src/main/org/h2/store/fs/FileSystemDatabase.java
浏览文件 @
4b2f3723
...
...
@@ -100,6 +100,7 @@ public class FileSystemDatabase extends FileSystem {
*/
public
void
close
()
{
JdbcUtils
.
closeSilently
(
conn
);
INSTANCES
.
remove
(
url
);
}
private
void
commit
()
{
...
...
@@ -240,7 +241,6 @@ public class FileSystemDatabase extends FileSystem {
}
public
String
createTempFile
(
String
name
,
String
suffix
,
boolean
deleteOnExit
,
boolean
inTempDir
)
throws
IOException
{
name
=
translateFileName
(
name
);
name
+=
"."
;
for
(
int
i
=
0
;;
i
++)
{
String
n
=
name
+
i
+
suffix
;
...
...
h2/src/main/org/h2/store/fs/FileSystemMemory.java
浏览文件 @
4b2f3723
...
...
@@ -11,9 +11,11 @@ import java.io.InputStream;
import
java.io.OutputStream
;
import
java.sql.SQLException
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
org.h2.message.Message
;
import
org.h2.util.IOUtils
;
import
org.h2.util.ObjectArray
;
import
org.h2.util.RandomUtils
;
/**
...
...
@@ -23,7 +25,7 @@ import org.h2.util.RandomUtils;
public
class
FileSystemMemory
extends
FileSystem
{
private
static
final
FileSystemMemory
INSTANCE
=
new
FileSystemMemory
();
private
static
final
HashMap
MEMORY_FILES
=
new
HashMap
();
private
final
HashMap
memoryFiles
=
new
HashMap
();
private
FileSystemMemory
()
{
// don't allow construction
...
...
@@ -40,71 +42,85 @@ public class FileSystemMemory extends FileSystem {
public
void
rename
(
String
oldName
,
String
newName
)
{
oldName
=
normalize
(
oldName
);
newName
=
normalize
(
newName
);
FileObjectMemory
f
=
getMemoryFile
(
oldName
);
f
.
setName
(
new
Name
);
synchronized
(
MEMORY_FILES
)
{
MEMORY_FILES
.
remove
(
oldName
);
MEMORY_FILES
.
put
(
newName
,
f
);
synchronized
(
memoryFiles
)
{
FileObjectMemory
f
=
getMemoryFile
(
old
Name
);
f
.
setName
(
newName
);
memoryFiles
.
remove
(
oldName
);
memoryFiles
.
put
(
newName
,
f
);
}
}
public
boolean
createNewFile
(
String
fileName
)
{
if
(
exists
(
fileName
))
{
return
false
;
synchronized
(
memoryFiles
)
{
if
(
exists
(
fileName
))
{
return
false
;
}
getMemoryFile
(
fileName
);
}
// creates the file (not thread safe)
getMemoryFile
(
fileName
);
return
true
;
}
public
boolean
exists
(
String
fileName
)
{
fileName
=
normalize
(
fileName
);
synchronized
(
MEMORY_FILES
)
{
return
MEMORY_FILES
.
get
(
fileName
)
!=
null
;
synchronized
(
memoryFiles
)
{
return
memoryFiles
.
get
(
fileName
)
!=
null
;
}
}
public
void
delete
(
String
fileName
)
{
fileName
=
normalize
(
fileName
);
synchronized
(
MEMORY_FILES
)
{
MEMORY_FILES
.
remove
(
fileName
);
synchronized
(
memoryFiles
)
{
memoryFiles
.
remove
(
fileName
);
}
}
public
boolean
tryDelete
(
String
fileName
)
{
fileName
=
normalize
(
fileName
);
synchronized
(
MEMORY_FILES
)
{
MEMORY_FILES
.
remove
(
fileName
);
synchronized
(
memoryFiles
)
{
memoryFiles
.
remove
(
fileName
);
}
return
true
;
}
public
String
createTempFile
(
String
name
,
String
suffix
,
boolean
deleteOnExit
,
boolean
inTempDir
)
{
name
+=
"."
;
for
(
int
i
=
0
;;
i
++)
{
String
n
=
name
+
(
RandomUtils
.
getSecureLong
()
>>>
1
)
+
suffix
;
if
(!
exists
(
n
))
{
// creates the file (not thread safe)
getMemoryFile
(
n
);
return
n
;
synchronized
(
memoryFiles
)
{
for
(
int
i
=
0
;;
i
++)
{
String
n
=
name
+
(
RandomUtils
.
getSecureLong
()
>>>
1
)
+
suffix
;
if
(!
exists
(
n
))
{
getMemoryFile
(
n
);
return
n
;
}
}
}
}
public
String
[]
listFiles
(
String
path
)
{
synchronized
(
MEMORY_FILES
)
{
String
[]
list
=
new
String
[
MEMORY_FILES
.
size
()];
FileObjectMemory
[]
l
=
new
FileObjectMemory
[
MEMORY_FILES
.
size
()];
MEMORY_FILES
.
values
().
toArray
(
l
);
for
(
int
i
=
0
;
i
<
list
.
length
;
i
++)
{
list
[
i
]
=
l
[
i
].
getName
();
ObjectArray
list
=
new
ObjectArray
();
synchronized
(
memoryFiles
)
{
for
(
Iterator
it
=
memoryFiles
.
keySet
().
iterator
();
it
.
hasNext
();)
{
String
name
=
(
String
)
it
.
next
();
if
(
name
.
startsWith
(
path
))
{
list
.
add
(
name
);
}
}
return
list
;
String
[]
array
=
new
String
[
list
.
size
()];
list
.
toArray
(
array
);
return
array
;
}
}
public
void
deleteRecursive
(
String
fileName
)
throws
SQLException
{
throw
Message
.
getUnsupportedException
();
fileName
=
normalize
(
fileName
);
synchronized
(
memoryFiles
)
{
Iterator
it
=
memoryFiles
.
keySet
().
iterator
();
while
(
it
.
hasNext
())
{
String
name
=
(
String
)
it
.
next
();
if
(
name
.
startsWith
(
fileName
))
{
it
.
remove
();
}
}
}
}
public
boolean
isReadOnly
(
String
fileName
)
{
...
...
@@ -201,12 +217,12 @@ public class FileSystemMemory extends FileSystem {
private
FileObjectMemory
getMemoryFile
(
String
fileName
)
{
fileName
=
normalize
(
fileName
);
synchronized
(
MEMORY_FILES
)
{
FileObjectMemory
m
=
(
FileObjectMemory
)
MEMORY_FILES
.
get
(
fileName
);
synchronized
(
memoryFiles
)
{
FileObjectMemory
m
=
(
FileObjectMemory
)
memoryFiles
.
get
(
fileName
);
if
(
m
==
null
)
{
boolean
compress
=
fileName
.
startsWith
(
FileSystem
.
PREFIX_MEMORY_LZF
);
m
=
new
FileObjectMemory
(
fileName
,
compress
);
MEMORY_FILES
.
put
(
fileName
,
m
);
memoryFiles
.
put
(
fileName
,
m
);
}
return
m
;
}
...
...
h2/src/test/org/h2/test/unit/TestFileSystem.java
浏览文件 @
4b2f3723
...
...
@@ -27,6 +27,15 @@ import org.h2.test.TestBase;
*/
public
class
TestFileSystem
extends
TestBase
{
/**
* Run just this test.
*
* @param a ignored
*/
public
static
void
main
(
String
[]
a
)
throws
Exception
{
TestBase
.
createCaller
().
init
().
test
();
}
public
void
test
()
throws
Exception
{
testDatabaseInMemFileSys
();
testDatabaseInJar
();
...
...
@@ -40,6 +49,7 @@ public class TestFileSystem extends TestBase {
private
void
testDatabaseInMemFileSys
()
throws
SQLException
{
org
.
h2
.
Driver
.
load
();
deleteDb
(
"fsMem"
);
String
url
=
"jdbc:h2:"
+
baseDir
+
"/fsMem"
;
Connection
conn
=
DriverManager
.
getConnection
(
url
,
"sa"
,
"sa"
);
conn
.
createStatement
().
execute
(
"CREATE TABLE TEST AS SELECT * FROM DUAL"
);
...
...
@@ -145,7 +155,7 @@ public class TestFileSystem extends TestBase {
fs
.
rename
(
fsBase
+
"/test3"
,
fsBase
+
"/test2"
);
assertTrue
(!
fs
.
exists
(
fsBase
+
"/test3"
));
assertTrue
(
fs
.
exists
(
fsBase
+
"/test2"
));
assertEquals
(
fs
.
length
(
fsBase
+
"/test2"
),
10000
);
assertEquals
(
10000
,
fs
.
length
(
fsBase
+
"/test2"
)
);
byte
[]
buffer2
=
new
byte
[
10000
];
InputStream
in
=
fs
.
openFileInputStream
(
fsBase
+
"/test2"
);
int
pos
=
0
;
...
...
@@ -157,8 +167,8 @@ public class TestFileSystem extends TestBase {
pos
+=
l
;
}
in
.
close
();
assertEquals
(
pos
,
10000
);
assertEquals
(
buffer
2
,
buffer
);
assertEquals
(
10000
,
pos
);
assertEquals
(
buffer
,
buffer2
);
assertTrue
(
fs
.
tryDelete
(
fsBase
+
"/test2"
));
fs
.
delete
(
fsBase
+
"/test"
);
...
...
@@ -171,6 +181,7 @@ public class TestFileSystem extends TestBase {
assertTrue
(!
fs
.
exists
(
"/testDir"
));
}
}
fs
.
close
();
}
private
void
testRandomAccess
(
String
fsBase
)
throws
Exception
{
...
...
@@ -245,7 +256,7 @@ public class TestFileSystem extends TestBase {
ra
.
close
();
ra
=
new
RandomAccessFile
(
file
,
"rw"
);
f
=
fs
.
openFileObject
(
s
,
"rw"
);
assertEquals
(
f
.
length
(),
ra
.
length
());
assertEquals
(
ra
.
length
(),
f
.
length
());
break
;
}
default
:
...
...
@@ -253,6 +264,7 @@ public class TestFileSystem extends TestBase {
}
f
.
close
();
ra
.
close
();
fs
.
close
();
}
private
void
testTempFile
(
String
fsBase
)
throws
Exception
{
...
...
@@ -273,6 +285,7 @@ public class TestFileSystem extends TestBase {
assertEquals
(
in
.
read
(),
-
1
);
in
.
close
();
out
.
close
();
fs
.
close
();
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论