Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
73ef660f
提交
73ef660f
authored
5月 04, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for AutoCloseable objects to TempFileDeleter
上级
bc78d7a3
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
49 行增加
和
32 行删除
+49
-32
TempFileDeleter.java
h2/src/main/org/h2/util/TempFileDeleter.java
+49
-32
没有找到文件。
h2/src/main/org/h2/util/TempFileDeleter.java
浏览文件 @
73ef660f
...
@@ -21,7 +21,7 @@ import org.h2.store.fs.FileUtils;
...
@@ -21,7 +21,7 @@ import org.h2.store.fs.FileUtils;
public
class
TempFileDeleter
{
public
class
TempFileDeleter
{
private
final
ReferenceQueue
<
Object
>
queue
=
new
ReferenceQueue
<>();
private
final
ReferenceQueue
<
Object
>
queue
=
new
ReferenceQueue
<>();
private
final
HashMap
<
PhantomReference
<?>,
String
>
refMap
=
new
HashMap
<>();
private
final
HashMap
<
PhantomReference
<?>,
Object
>
refMap
=
new
HashMap
<>();
private
TempFileDeleter
()
{
private
TempFileDeleter
()
{
// utility class
// utility class
...
@@ -32,43 +32,59 @@ public class TempFileDeleter {
...
@@ -32,43 +32,59 @@ public class TempFileDeleter {
}
}
/**
/**
* Add a file
to the list of temp files to delete. The file is deleted onc
e
* Add a file
or a closeable to the list of temporary objects to delete. Th
e
* the file object is garbage collected.
*
file is deleted once
the file object is garbage collected.
*
*
* @param
fileName the file nam
e
* @param
resource the file name or the closeabl
e
* @param
file
the object to monitor
* @param
monitor
the object to monitor
* @return the reference that can be used to stop deleting the file
* @return the reference that can be used to stop deleting the file
or closing the closeable
*/
*/
public
synchronized
Reference
<?>
addFile
(
String
fileName
,
Object
file
)
{
public
synchronized
Reference
<?>
addFile
(
Object
resource
,
Object
monitor
)
{
IOUtils
.
trace
(
"TempFileDeleter.addFile"
,
fileName
,
file
);
if
(!(
resource
instanceof
String
)
&&
!(
resource
instanceof
AutoCloseable
))
{
PhantomReference
<?>
ref
=
new
PhantomReference
<>(
file
,
queue
);
throw
DbException
.
getUnsupportedException
(
"Unsupported resource "
+
resource
);
refMap
.
put
(
ref
,
fileName
);
}
IOUtils
.
trace
(
"TempFileDeleter.addFile"
,
resource
instanceof
String
?
(
String
)
resource
:
"-"
,
monitor
);
PhantomReference
<?>
ref
=
new
PhantomReference
<>(
monitor
,
queue
);
refMap
.
put
(
ref
,
resource
);
deleteUnused
();
deleteUnused
();
return
ref
;
return
ref
;
}
}
/**
/**
* Delete the given file now. This will remove the reference from the list.
* Delete the given file or close the closeable now. This will remove the
* reference from the list.
*
*
* @param ref the reference as returned by addFile
* @param ref the reference as returned by addFile
* @param
fileName the file nam
e
* @param
resource the file name or closeabl
e
*/
*/
public
synchronized
void
deleteFile
(
Reference
<?>
ref
,
String
fileNam
e
)
{
public
synchronized
void
deleteFile
(
Reference
<?>
ref
,
Object
resourc
e
)
{
if
(
ref
!=
null
)
{
if
(
ref
!=
null
)
{
String
f2
=
refMap
.
remove
(
ref
);
Object
f2
=
refMap
.
remove
(
ref
);
if
(
f2
!=
null
)
{
if
(
f2
!=
null
)
{
if
(
SysProperties
.
CHECK
)
{
if
(
SysProperties
.
CHECK
)
{
if
(
fileName
!=
null
&&
!
f2
.
equals
(
fileNam
e
))
{
if
(
resource
!=
null
&&
!
f2
.
equals
(
resourc
e
))
{
DbException
.
throwInternalError
(
"f2:"
+
f2
+
" f:"
+
fileNam
e
);
DbException
.
throwInternalError
(
"f2:"
+
f2
+
" f:"
+
resourc
e
);
}
}
}
}
fileNam
e
=
f2
;
resourc
e
=
f2
;
}
}
}
}
if
(
fileName
!=
null
&&
FileUtils
.
exists
(
fileName
))
{
if
(
resource
instanceof
String
)
{
String
fileName
=
(
String
)
resource
;
if
(
FileUtils
.
exists
(
fileName
))
{
try
{
IOUtils
.
trace
(
"TempFileDeleter.deleteFile"
,
fileName
,
null
);
FileUtils
.
tryDelete
(
fileName
);
}
catch
(
Exception
e
)
{
// TODO log such errors?
}
}
}
else
if
(
resource
instanceof
AutoCloseable
)
{
AutoCloseable
closeable
=
(
AutoCloseable
)
resource
;
try
{
try
{
IOUtils
.
trace
(
"TempFileDeleter.delete
File"
,
fileName
,
null
);
IOUtils
.
trace
(
"TempFileDeleter.delete
Closeable"
,
"-"
,
null
);
FileUtils
.
tryDelete
(
fileName
);
closeable
.
close
(
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
// TODO log such errors?
// TODO log such errors?
}
}
...
@@ -76,17 +92,17 @@ public class TempFileDeleter {
...
@@ -76,17 +92,17 @@ public class TempFileDeleter {
}
}
/**
/**
* Delete all registered temp
fil
es.
* Delete all registered temp
resourc
es.
*/
*/
public
void
deleteAll
()
{
public
void
deleteAll
()
{
for
(
String
tempFil
e
:
new
ArrayList
<>(
refMap
.
values
()))
{
for
(
Object
resourc
e
:
new
ArrayList
<>(
refMap
.
values
()))
{
deleteFile
(
null
,
tempFil
e
);
deleteFile
(
null
,
resourc
e
);
}
}
deleteUnused
();
deleteUnused
();
}
}
/**
/**
* Delete all unused
fil
es now.
* Delete all unused
resourc
es now.
*/
*/
public
void
deleteUnused
()
{
public
void
deleteUnused
()
{
while
(
queue
!=
null
)
{
while
(
queue
!=
null
)
{
...
@@ -99,20 +115,21 @@ public class TempFileDeleter {
...
@@ -99,20 +115,21 @@ public class TempFileDeleter {
}
}
/**
/**
* This method is called if a file should no longer be deleted
if the object
* This method is called if a file should no longer be deleted
or a resource
* is garbage collected.
*
should no longer be closed if the object
is garbage collected.
*
*
* @param ref the reference as returned by addFile
* @param ref the reference as returned by addFile
* @param
fileName the file nam
e
* @param
resource file name or closeabl
e
*/
*/
public
void
stopAutoDelete
(
Reference
<?>
ref
,
String
fileName
)
{
public
void
stopAutoDelete
(
Reference
<?>
ref
,
Object
resource
)
{
IOUtils
.
trace
(
"TempFileDeleter.stopAutoDelete"
,
fileName
,
ref
);
IOUtils
.
trace
(
"TempFileDeleter.stopAutoDelete"
,
resource
instanceof
String
?
(
String
)
resource
:
"-"
,
ref
);
if
(
ref
!=
null
)
{
if
(
ref
!=
null
)
{
String
f2
=
refMap
.
remove
(
ref
);
Object
f2
=
refMap
.
remove
(
ref
);
if
(
SysProperties
.
CHECK
)
{
if
(
SysProperties
.
CHECK
)
{
if
(
f2
==
null
||
!
f2
.
equals
(
fileNam
e
))
{
if
(
f2
==
null
||
!
f2
.
equals
(
resourc
e
))
{
DbException
.
throwInternalError
(
"f2:"
+
f2
+
DbException
.
throwInternalError
(
"f2:"
+
f2
+
" "
+
(
f2
==
null
?
""
:
f2
)
+
" f:"
+
fileNam
e
);
" "
+
(
f2
==
null
?
""
:
f2
)
+
" f:"
+
resourc
e
);
}
}
}
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论