Debian Codegraph @

src/main/org/h2/mvstore/db/MVTempResult.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.mvstore.db;
7
8 import java.io.IOException;
9 import java.lang.ref.Reference;
10 import java.util.Collection;
11
12 import org.h2.engine.Constants;
13 import org.h2.engine.Database;
14 import org.h2.expression.Expression;
15 import org.h2.message.DbException;
16 import org.h2.mvstore.FileStore;
17 import org.h2.mvstore.MVStore;
18 import org.h2.result.ResultExternal;
19 import org.h2.result.SortOrder;
20 import org.h2.store.fs.FileUtils;
21 import org.h2.util.TempFileDeleter;
22 import org.h2.value.Value;
23
24 /**
25 * Temporary result.
26 *
27 * <p>
28 * A separate MVStore in a temporary file is used for each result. The file is
29 * removed when this result and all its copies are closed.
30 * {@link TempFileDeleter} is also used to delete this file if results are not
31 * closed properly.
32 * </p>
33 */
34 public abstract class MVTempResult implements ResultExternal {
35
36 private static final class CloseImpl implements AutoCloseable {
37 /**
38 * MVStore.
39 */
40 private final MVStore store;
41
42 /**
43 * File name.
44 */
45 private final String fileName;
46
47 CloseImpl(MVStore store, String fileName) {
48 this.store = store;
49 this.fileName = fileName;
50 }
51
52 @Override
53 public void close() throws Exception {
54 store.closeImmediately();
55 FileUtils.tryDelete(fileName);
56 }
57
58 }
59
60 /**
61 * Creates MVStore-based temporary result.
62 *
63 * @param database
64 * database
65 * @param expressions
66 * expressions
67 * @param distinct
68 * is output distinct
69 * @param distinctIndexes
70 * indexes of distinct columns for DISTINCT ON results
71 * @param visibleColumnCount
72 * count of visible columns
73 * @param resultColumnCount
74 * the number of columns including visible columns and additional
75 * virtual columns for ORDER BY and DISTINCT ON clauses
76 * @param sort
77 * sort order, or {@code null}
78 * @return temporary result
79 */
80 public static ResultExternal of(Database database, Expression[] expressions, boolean distinct,
81 int[] distinctIndexes, int visibleColumnCount, int resultColumnCount, SortOrder sort) {
82 return distinct || distinctIndexes != null || sort != null
83 ? new MVSortedTempResult(database, expressions, distinct, distinctIndexes, visibleColumnCount,
84 resultColumnCount, sort)
85 : new MVPlainTempResult(database, expressions, visibleColumnCount, resultColumnCount);
86 }
87
88 private final Database database;
89
90 /**
91 * MVStore.
92 */
93 final MVStore store;
94
95 /**
96 * Column expressions.
97 */
98 final Expression[] expressions;
99
100 /**
101 * Count of visible columns.
102 */
103 final int visibleColumnCount;
104
105 /**
106 * Total count of columns.
107 */
108 final int resultColumnCount;
109
110 /**
111 * Count of rows. Used only in a root results, copies always have 0 value.
112 */
113 int rowCount;
114
115 /**
116 * Parent store for copies. If {@code null} this result is a root result.
117 */
118 final MVTempResult parent;
119
120 /**
121 * Count of child results.
122 */
123 int childCount;
124
125 /**
126 * Whether this result is closed.
127 */
128 boolean closed;
129
130 /**
131 * Temporary file deleter.
132 */
133 private final TempFileDeleter tempFileDeleter;
134
135 /**
136 * Closeable to close the storage.
137 */
138 private final CloseImpl closeable;
139
140 /**
141 * Reference to the record in the temporary file deleter.
142 */
143 private final Reference<?> fileRef;
144
145 /**
146 * Creates a shallow copy of the result.
147 *
148 * @param parent
149 * parent result
150 */
151 MVTempResult(MVTempResult parent) {
152 this.parent = parent;
153 this.database = parent.database;
154 this.store = parent.store;
155 this.expressions = parent.expressions;
156 this.visibleColumnCount = parent.visibleColumnCount;
157 this.resultColumnCount = parent.resultColumnCount;
158 this.tempFileDeleter = null;
159 this.closeable = null;
160 this.fileRef = null;
161 }
162
163 /**
164 * Creates a new temporary result.
165 *
166 * @param database
167 * database
168 * @param expressions
169 * column expressions
170 * @param visibleColumnCount
171 * count of visible columns
172 * @param resultColumnCount
173 * total count of columns
174 */
175 MVTempResult(Database database, Expression[] expressions, int visibleColumnCount, int resultColumnCount) {
176 this.database = database;
177 try {
178 String fileName = FileUtils.createTempFile("h2tmp", Constants.SUFFIX_TEMP_FILE, true);
179
180 FileStore<?> fileStore = database.getStore().getMvStore().getFileStore().open(fileName, false);
181 MVStore.Builder builder = new MVStore.Builder().adoptFileStore(fileStore).cacheSize(0)
182 .autoCommitDisabled();
183 store = builder.open();
184 this.expressions = expressions;
185 this.visibleColumnCount = visibleColumnCount;
186 this.resultColumnCount = resultColumnCount;
187 tempFileDeleter = database.getTempFileDeleter();
188 closeable = new CloseImpl(store, fileName);
189 fileRef = tempFileDeleter.addFile(closeable, this);
190 } catch (IOException e) {
191 throw DbException.convert(e);
192 }
193 parent = null;
194 }
195
196 @Override
197 public int addRows(Collection<Value[]> rows) {
198 for (Value[] row : rows) {
199 addRow(row);
200 }
201 return rowCount;
202 }
203
204 @Override
205 public synchronized void close() {
206 if (closed) {
207 return;
208 }
209 closed = true;
210 if (parent != null) {
211 parent.closeChild();
212 } else {
213 if (childCount == 0) {
214 delete();
215 }
216 }
217 }
218
219 private synchronized void closeChild() {
220 if (--childCount == 0 && closed) {
221 delete();
222 }
223 }
224
225 private void delete() {
226 tempFileDeleter.deleteFile(fileRef, closeable);
227 }
228
229 }