Debian Codegraph @

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