-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIRManager.java
More file actions
367 lines (333 loc) · 13 KB
/
Copy pathIRManager.java
File metadata and controls
367 lines (333 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package cod.ir;
import cod.ast.node.Type;
import cod.ptac.Artifact;
import cod.ptac.Compiler;
import cod.ptac.Unit;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.CRC32;
public class IRManager {
private static final String BIN_DIR = "bin";
private static final String IR_EXT = ".codb";
private static final String CONTAINER_EXT = ".codc";
private static final int BUFFER_SIZE = 8192;
private static final Map<String, Object> CONTAINER_LOCKS = new ConcurrentHashMap<String, Object>();
private final String projectRoot;
private final IRWriter writer;
private final IRReader reader;
private final Map<String, Map<String, Type>> cache;
private final Map<String, Map<String, Artifact>> artifactCache;
private final Compiler compiler;
public IRManager(String projectRoot) {
this.projectRoot = projectRoot;
this.writer = new IRWriter();
this.reader = new IRReader();
this.cache = new HashMap<String, Map<String, Type>>();
this.artifactCache = new HashMap<String, Map<String, Artifact>>();
this.compiler = new Compiler();
}
public Type load(String unit, String className) {
if (unit == null || className == null) {
return null;
}
Map<String, Type> unitCache = cache.get(unit);
if (unitCache != null) {
Type cached = unitCache.get(className);
if (cached != null) {
return cached;
}
}
try {
Artifact artifact = readArtifactFromContainer(unit, className);
if (artifact == null) {
// Standalone .codb files are a permanent supported format.
// .codc containers are additive grouping, not a replacement.
File file = getIRFile(unit, className);
if (!file.exists()) {
return null;
}
artifact = reader.readArtifact(file);
}
if (artifact != null) {
putArtifactCache(unit, className, artifact);
Type type = artifact.typeSnapshot;
if (type != null) {
putCache(unit, className, type);
}
return type;
}
return null;
} catch (IOException e) {
return null;
}
}
public void save(String unit, Type type) {
if (type == null || unit == null || type.name == null) {
return;
}
try {
Artifact artifact = compiler.compile(unit, type);
writeArtifactToContainer(unit, artifact.className, artifact);
putCache(unit, type.name, type);
putArtifactCache(unit, type.name, artifact);
} catch (IOException ignored) {}
}
public Artifact loadArtifact(String unit, String className) {
if (unit == null || className == null) {
return null;
}
Map<String, Artifact> unitCache = artifactCache.get(unit);
if (unitCache != null && unitCache.containsKey(className)) {
return unitCache.get(className);
}
try {
Artifact artifact = readArtifactFromContainer(unit, className);
if (artifact == null) {
// Standalone .codb files are a permanent supported format.
// .codc containers are additive grouping, not a replacement.
File file = getIRFile(unit, className);
if (!file.exists()) {
return null;
}
artifact = reader.readArtifact(file);
}
if (artifact != null) {
putArtifactCache(unit, className, artifact);
if (artifact.typeSnapshot != null) {
putCache(unit, className, artifact.typeSnapshot);
}
}
return artifact;
} catch (IOException e) {
return null;
}
}
public Unit loadCodPTACUnit(String unit, String className) {
Artifact artifact = loadArtifact(unit, className);
return artifact != null ? artifact.unit : null;
}
public void saveArtifact(String unit, Artifact artifact) {
if (artifact == null || unit == null || artifact.className == null) return;
try {
writeArtifactToContainer(unit, artifact.className, artifact);
putArtifactCache(unit, artifact.className, artifact);
if (artifact.typeSnapshot != null) {
putCache(unit, artifact.className, artifact.typeSnapshot);
}
} catch (IOException ignored) {}
}
public void clearCache() {
cache.clear();
artifactCache.clear();
}
public Map<String, Object> getCacheStats() {
Map<String, Object> stats = new HashMap<String, Object>();
int total = 0;
for (Map<String, Type> unitCache : cache.values()) {
total += unitCache.size();
}
stats.put("units", cache.size());
stats.put("classes", total);
int artifacts = 0;
for (Map<String, Artifact> unitArtifacts : artifactCache.values()) {
artifacts += unitArtifacts.size();
}
stats.put("artifacts", artifacts);
return stats;
}
private void putCache(String unit, String className, Type type) {
Map<String, Type> unitCache = cache.get(unit);
if (unitCache == null) {
unitCache = new HashMap<String, Type>();
cache.put(unit, unitCache);
}
unitCache.put(className, type);
}
private void putArtifactCache(String unit, String className, Artifact artifact) {
Map<String, Artifact> unitCache = artifactCache.get(unit);
if (unitCache == null) {
unitCache = new HashMap<String, Artifact>();
artifactCache.put(unit, unitCache);
}
unitCache.put(className, artifact);
}
private File getIRFile(String unit, String className) {
String path = projectRoot + "/src/" + BIN_DIR + "/" + unit + "/" + className + IR_EXT;
return new File(path);
}
private File getContainerFile(String unit) {
String rootUnit = getRootUnit(unit);
String path = projectRoot + "/src/" + BIN_DIR + "/" + rootUnit + CONTAINER_EXT;
return new File(path);
}
private String getRootUnit(String unit) {
if (unit == null || unit.isEmpty()) return "default";
int dot = unit.indexOf('.');
if (dot < 0) return unit;
if (dot == 0) return "default";
return unit.substring(0, dot);
}
private String getContainerEntryName(String unit, String className) {
return unit + "/" + className + IR_EXT;
}
private Artifact readArtifactFromContainer(String unit, String className) throws IOException {
File container = getContainerFile(unit);
if (!container.exists() || !container.isFile()) {
return null;
}
String targetEntry = getContainerEntryName(unit, className);
ZipInputStream in = null;
try {
in = new ZipInputStream(new BufferedInputStream(new FileInputStream(container)));
ZipEntry entry;
while ((entry = in.getNextEntry()) != null) {
if (!entry.isDirectory() && targetEntry.equals(entry.getName())) {
byte[] data = readAllBytes(in);
return readArtifactFromBytes(data);
}
}
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
}
private void writeArtifactToContainer(String unit, String className, Artifact artifact) throws IOException {
if (unit == null || className == null || artifact == null) return;
File container = getContainerFile(unit);
File parent = container.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
throw new IOException("Failed to create IR container directory: " + parent.getAbsolutePath());
}
Object containerLock = getContainerLock(container);
synchronized (containerLock) {
Map<String, byte[]> entries = readContainerEntries(container);
entries.put(getContainerEntryName(unit, className), writeArtifactToBytes(artifact));
File temp = new File(container.getAbsolutePath() + ".tmp");
ZipOutputStream out = null;
boolean moved = false;
try {
out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(temp)));
out.setLevel(0);
for (Map.Entry<String, byte[]> e : entries.entrySet()) {
byte[] value = e.getValue();
CRC32 crc = new CRC32();
crc.update(value);
ZipEntry zipEntry = new ZipEntry(e.getKey());
// .codc is intentionally an uncompressed zip container (level 0, STORED entries).
zipEntry.setMethod(ZipEntry.STORED);
zipEntry.setSize(value.length);
zipEntry.setCompressedSize(value.length);
zipEntry.setCrc(crc.getValue());
out.putNextEntry(zipEntry);
out.write(value);
out.closeEntry();
}
out.finish();
Files.move(temp.toPath(), container.toPath(), StandardCopyOption.REPLACE_EXISTING);
moved = true;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ignored) {}
}
if (!moved && temp.exists()) {
try {
Files.delete(temp.toPath());
} catch (IOException ignored) {}
}
}
}
}
private Object getContainerLock(File container) {
String key = container.getAbsolutePath();
Object lock = CONTAINER_LOCKS.get(key);
if (lock != null) return lock;
Object created = new Object();
Object existing = CONTAINER_LOCKS.putIfAbsent(key, created);
return existing != null ? existing : created;
}
private Map<String, byte[]> readContainerEntries(File container) throws IOException {
Map<String, byte[]> entries = new LinkedHashMap<>();
if (container == null || !container.exists() || !container.isFile()) {
return entries;
}
ZipInputStream in = null;
try {
in = new ZipInputStream(new BufferedInputStream(new FileInputStream(container)));
ZipEntry entry;
while ((entry = in.getNextEntry()) != null) {
if (entry.isDirectory()) continue;
entries.put(entry.getName(), readAllBytes(in));
}
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
return entries;
}
private byte[] writeArtifactToBytes(Artifact artifact) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = null;
try {
out = new DataOutputStream(baos);
IRArtifactCodec.writeArtifact(out, artifact);
out.flush();
return baos.toByteArray();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ignored) {}
}
}
}
private Artifact readArtifactFromBytes(byte[] data) throws IOException {
if (data == null) return null;
DataInputStream in = null;
try {
in = new DataInputStream(new ByteArrayInputStream(data));
return IRArtifactCodec.readArtifact(in);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
}
private byte[] readAllBytes(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while ((read = in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
}
return out.toByteArray();
}
}