Debian Codegraph @

src/main/org/h2/store/fs/mem/FilePathMem.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.store.fs.mem;
7
8 import java.nio.channels.FileChannel;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.TreeMap;
12 import org.h2.api.ErrorCode;
13 import org.h2.message.DbException;
14 import org.h2.store.fs.FilePath;
15
16 /**
17 * This file system keeps files fully in memory. There is an option to compress
18 * file blocks to save memory.
19 */
20 public class FilePathMem extends FilePath {
21
22 private static final TreeMap<String, FileMemData> MEMORY_FILES =
23 new TreeMap<>();
24 private static final FileMemData DIRECTORY = new FileMemData("", false);
25
26 @Override
27 public FilePathMem getPath(String path) {
28 FilePathMem p = new FilePathMem();
29 p.name = getCanonicalPath(path);
30 return p;
31 }
32
33 @Override
34 public long size() {
35 return getMemoryFile().length();
36 }
37
38 @Override
39 public void moveTo(FilePath newName, boolean atomicReplace) {
40 synchronized (MEMORY_FILES) {
41 if (!atomicReplace && !newName.name.equals(name) &&
42 MEMORY_FILES.containsKey(newName.name)) {
43 throw DbException.get(ErrorCode.FILE_RENAME_FAILED_2, name, newName + " (exists)");
44 }
45 FileMemData f = getMemoryFile();
46 f.setName(newName.name);
47 MEMORY_FILES.remove(name);
48 MEMORY_FILES.put(newName.name, f);
49 }
50 }
51
52 @Override
53 public boolean createFile() {
54 synchronized (MEMORY_FILES) {
55 if (exists()) {
56 return false;
57 }
58 getMemoryFile();
59 }
60 return true;
61 }
62
63 @Override
64 public boolean exists() {
65 if (isRoot()) {
66 return true;
67 }
68 synchronized (MEMORY_FILES) {
69 return MEMORY_FILES.get(name) != null;
70 }
71 }
72
73 @Override
74 public void delete() {
75 if (isRoot()) {
76 return;
77 }
78 synchronized (MEMORY_FILES) {
79 FileMemData old = MEMORY_FILES.remove(name);
80 if (old != null) {
81 old.truncate(0);
82 }
83 }
84 }
85
86 @Override
87 public List<FilePath> newDirectoryStream() {
88 ArrayList<FilePath> list = new ArrayList<>();
89 synchronized (MEMORY_FILES) {
90 for (String n : MEMORY_FILES.tailMap(name).keySet()) {
91 if (n.startsWith(name)) {
92 if (!n.equals(name) && n.indexOf('/', name.length() + 1) < 0) {
93 list.add(getPath(n));
94 }
95 } else {
96 break;
97 }
98 }
99 return list;
100 }
101 }
102
103 @Override
104 public boolean setReadOnly() {
105 return getMemoryFile().setReadOnly();
106 }
107
108 @Override
109 public boolean canWrite() {
110 return getMemoryFile().canWrite();
111 }
112
113 @Override
114 public FilePathMem getParent() {
115 int idx = name.lastIndexOf('/');
116 return idx < 0 ? null : getPath(name.substring(0, idx));
117 }
118
119 @Override
120 public boolean isDirectory() {
121 if (isRoot()) {
122 return true;
123 }
124 synchronized (MEMORY_FILES) {
125 FileMemData d = MEMORY_FILES.get(name);
126 return d == DIRECTORY;
127 }
128 }
129
130 @Override
131 public boolean isRegularFile() {
132 if (isRoot()) {
133 return false;
134 }
135 synchronized (MEMORY_FILES) {
136 FileMemData d = MEMORY_FILES.get(name);
137 return d != null && d != DIRECTORY;
138 }
139 }
140
141 @Override
142 public boolean isAbsolute() {
143 // TODO relative files are not supported
144 return true;
145 }
146
147 @Override
148 public FilePathMem toRealPath() {
149 return this;
150 }
151
152 @Override
153 public long lastModified() {
154 return getMemoryFile().getLastModified();
155 }
156
157 @Override
158 public void createDirectory() {
159 if (exists()) {
160 throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1,
161 name + " (a file with this name already exists)");
162 }
163 synchronized (MEMORY_FILES) {
164 MEMORY_FILES.put(name, DIRECTORY);
165 }
166 }
167
168 @Override
169 public FileChannel open(String mode) {
170 FileMemData obj = getMemoryFile();
171 return new FileMem(obj, "r".equals(mode));
172 }
173
174 private FileMemData getMemoryFile() {
175 synchronized (MEMORY_FILES) {
176 FileMemData m = MEMORY_FILES.get(name);
177 if (m == DIRECTORY) {
178 throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1,
179 name + " (a directory with this name already exists)");
180 }
181 if (m == null) {
182 m = new FileMemData(name, compressed());
183 MEMORY_FILES.put(name, m);
184 }
185 return m;
186 }
187 }
188
189 private boolean isRoot() {
190 return name.equals(getScheme() + ":");
191 }
192
193 /**
194 * Get the canonical path for this file name.
195 *
196 * @param fileName the file name
197 * @return the canonical path
198 */
199 protected static String getCanonicalPath(String fileName) {
200 fileName = fileName.replace('\\', '/');
201 int idx = fileName.indexOf(':') + 1;
202 if (fileName.length() > idx && fileName.charAt(idx) != '/') {
203 fileName = fileName.substring(0, idx) + "/" + fileName.substring(idx);
204 }
205 return fileName;
206 }
207
208 @Override
209 public String getScheme() {
210 return "memFS";
211 }
212
213 /**
214 * Whether the file should be compressed.
215 *
216 * @return if it should be compressed.
217 */
218 boolean compressed() {
219 return false;
220 }
221
222 }
223
224