Debian Codegraph @

src/main/org/h2/util/TempFileDeleter.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util;
7
8 import java.lang.ref.PhantomReference;
9 import java.lang.ref.Reference;
10 import java.lang.ref.ReferenceQueue;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13
14 import org.h2.engine.SysProperties;
15 import org.h2.message.DbException;
16 import org.h2.store.fs.FileUtils;
17
18 /**
19 * This class deletes temporary files when they are not used any longer.
20 */
21 public class TempFileDeleter {
22
23 private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
24 private final HashMap<PhantomReference<?>, Object> refMap = new HashMap<>();
25
26 private TempFileDeleter() {
27 // utility class
28 }
29
30 public static TempFileDeleter getInstance() {
31 return new TempFileDeleter();
32 }
33
34 /**
35 * Add a file or a closeable to the list of temporary objects to delete. The
36 * file is deleted once the file object is garbage collected.
37 *
38 * @param resource the file name or the closeable
39 * @param monitor the object to monitor
40 * @return the reference that can be used to stop deleting the file or closing the closeable
41 */
42 public synchronized Reference<?> addFile(Object resource, Object monitor) {
43 if (!(resource instanceof String) && !(resource instanceof AutoCloseable)) {
44 throw DbException.getUnsupportedException("Unsupported resource " + resource);
45 }
46 IOUtils.trace("TempFileDeleter.addFile",
47 resource instanceof String ? (String) resource : "-", monitor);
48 PhantomReference<?> ref = new PhantomReference<>(monitor, queue);
49 refMap.put(ref, resource);
50 deleteUnused();
51 return ref;
52 }
53
54 /**
55 * Delete the given file or close the closeable now. This will remove the
56 * reference from the list.
57 *
58 * @param ref the reference as returned by addFile
59 * @param resource the file name or closeable
60 */
61 public synchronized void deleteFile(Reference<?> ref, Object resource) {
62 if (ref != null) {
63 Object f2 = refMap.remove(ref);
64 if (f2 != null) {
65 if (SysProperties.CHECK) {
66 if (resource != null && !f2.equals(resource)) {
67 throw DbException.getInternalError("f2:" + f2 + " f:" + resource);
68 }
69 }
70 resource = f2;
71 }
72 }
73 if (resource instanceof String) {
74 String fileName = (String) resource;
75 if (FileUtils.exists(fileName)) {
76 try {
77 IOUtils.trace("TempFileDeleter.deleteFile", fileName, null);
78 FileUtils.tryDelete(fileName);
79 } catch (Exception e) {
80 // TODO log such errors?
81 }
82 }
83 } else if (resource instanceof AutoCloseable) {
84 AutoCloseable closeable = (AutoCloseable) resource;
85 try {
86 IOUtils.trace("TempFileDeleter.deleteCloseable", "-", null);
87 closeable.close();
88 } catch (Exception e) {
89 // TODO log such errors?
90 }
91 }
92 }
93
94 /**
95 * Delete all registered temp resources.
96 */
97 public void deleteAll() {
98 for (Object resource : new ArrayList<>(refMap.values())) {
99 deleteFile(null, resource);
100 }
101 deleteUnused();
102 }
103
104 /**
105 * Delete all unused resources now.
106 */
107 public void deleteUnused() {
108 Reference<?> ref;
109 while ((ref = queue.poll()) != null) {
110 deleteFile(ref, null);
111 }
112 }
113
114 /**
115 * This method is called if a file should no longer be deleted or a resource
116 * should no longer be closed if the object is garbage collected.
117 *
118 * @param ref the reference as returned by addFile
119 * @param resource file name or closeable
120 */
121 public void stopAutoDelete(Reference<?> ref, Object resource) {
122 IOUtils.trace("TempFileDeleter.stopAutoDelete",
123 resource instanceof String ? (String) resource : "-", ref);
124 if (ref != null) {
125 Object f2 = refMap.remove(ref);
126 if (SysProperties.CHECK) {
127 if (f2 == null || !f2.equals(resource)) {
128 throw DbException.getInternalError("f2:" + f2 + ' ' + (f2 == null ? "" : f2) + " f:" + resource);
129 }
130 }
131 }
132 deleteUnused();
133 }
134
135 }