Debian Codegraph @

src/main/org/h2/store/fs/rec/FilePathRec.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.rec;
7
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.nio.channels.FileChannel;
11 import org.h2.store.fs.FilePath;
12 import org.h2.store.fs.FilePathWrapper;
13 import org.h2.store.fs.Recorder;
14
15 /**
16 * A file system that records all write operations and can re-play them.
17 */
18 public class FilePathRec extends FilePathWrapper {
19
20 private static final FilePathRec INSTANCE = new FilePathRec();
21
22 private static Recorder recorder;
23
24 private boolean trace;
25
26 /**
27 * Register the file system.
28 */
29 public static void register() {
30 FilePath.register(INSTANCE);
31 }
32
33 /**
34 * Set the recorder class.
35 *
36 * @param recorder the recorder
37 */
38 public static void setRecorder(Recorder recorder) {
39 FilePathRec.recorder = recorder;
40 }
41
42 @Override
43 public boolean createFile() {
44 log(Recorder.CREATE_NEW_FILE, name);
45 return super.createFile();
46 }
47
48 @Override
49 public FilePath createTempFile(String suffix, boolean inTempDir) throws IOException {
50 log(Recorder.CREATE_TEMP_FILE, unwrap(name) + ":" + suffix + ":" + inTempDir);
51 return super.createTempFile(suffix, inTempDir);
52 }
53
54 @Override
55 public void delete() {
56 log(Recorder.DELETE, name);
57 super.delete();
58 }
59
60 @Override
61 public FileChannel open(String mode) throws IOException {
62 return new FileRec(this, super.open(mode), name);
63 }
64
65 @Override
66 public OutputStream newOutputStream(boolean append) throws IOException {
67 log(Recorder.OPEN_OUTPUT_STREAM, name);
68 return super.newOutputStream(append);
69 }
70
71 @Override
72 public void moveTo(FilePath newPath, boolean atomicReplace) {
73 log(Recorder.RENAME, unwrap(name) + ":" + unwrap(newPath.name));
74 super.moveTo(newPath, atomicReplace);
75 }
76
77 public boolean isTrace() {
78 return trace;
79 }
80
81 public void setTrace(boolean trace) {
82 this.trace = trace;
83 }
84
85 /**
86 * Log the operation.
87 *
88 * @param op the operation
89 * @param fileName the file name(s)
90 */
91 void log(int op, String fileName) {
92 log(op, fileName, null, 0);
93 }
94
95 /**
96 * Log the operation.
97 *
98 * @param op the operation
99 * @param fileName the file name
100 * @param data the data or null
101 * @param x the value or 0
102 */
103 void log(int op, String fileName, byte[] data, long x) {
104 if (recorder != null) {
105 recorder.log(op, fileName, data, x);
106 }
107 }
108
109 /**
110 * Get the prefix for this file system.
111 *
112 * @return the prefix
113 */
114 @Override
115 public String getScheme() {
116 return "rec";
117 }
118
119 }