strata-storage 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,287 @@
1
+ package com.strata.storage;
2
+
3
+ import android.content.Context;
4
+ import android.util.Log;
5
+ import com.getcapacitor.JSObject;
6
+ import java.io.File;
7
+ import java.io.FileOutputStream;
8
+ import java.io.IOException;
9
+ import java.nio.charset.StandardCharsets;
10
+ import java.net.URLDecoder;
11
+ import java.net.URLEncoder;
12
+ import java.util.ArrayList;
13
+ import java.util.List;
14
+ import java.util.UUID;
15
+ import org.json.JSONException;
16
+
17
+ /**
18
+ * Filesystem-backed storage. One file per key lives under
19
+ * {@code <filesDir>/strata_storage}. The file name is a reversible
20
+ * URL-encoding of the key (no path separators survive encoding) and the file
21
+ * contents are the JSON-serialized FULL wrapper object ({@code value, created,
22
+ * updated, expires?, tags?, metadata?}), mirroring the SQLite value-shape
23
+ * contract.
24
+ *
25
+ * <p>Writes are atomic: a temp file is written then renamed over the target.
26
+ * All access is synchronized on the instance so overlapping bridge calls are
27
+ * safe.
28
+ */
29
+ public class FilesystemStorage {
30
+ private static final String DIR_NAME = "strata_storage";
31
+ private static final String ENCODING = "UTF-8";
32
+ /**
33
+ * Reserved subdirectory (inside the storage dir) that holds in-flight temp
34
+ * files for atomic writes. Because it is a directory, the file-only
35
+ * enumeration in keys()/size()/clear() skips it automatically, and temp
36
+ * names therefore can never collide with an encoded key file in the storage
37
+ * dir (e.g. a real key {@code "backup.tmp"}).
38
+ */
39
+ private static final String STAGING_DIR_NAME = ".strata-staging";
40
+
41
+ private final File baseDir;
42
+ private final File stagingDir;
43
+
44
+ public FilesystemStorage(Context context) {
45
+ this.baseDir = new File(context.getFilesDir(), DIR_NAME);
46
+ this.stagingDir = new File(baseDir, STAGING_DIR_NAME);
47
+ ensureDir();
48
+ }
49
+
50
+ private synchronized void ensureDir() {
51
+ if (!baseDir.exists()) {
52
+ // mkdirs() returns false if it already exists due to a race; the
53
+ // subsequent exists() check below is the real guard.
54
+ baseDir.mkdirs();
55
+ }
56
+ if (!stagingDir.exists()) {
57
+ stagingDir.mkdirs();
58
+ }
59
+ }
60
+
61
+ /** Reversibly encode a key into a path-separator-free file name. */
62
+ private static String encodeKey(String key) {
63
+ try {
64
+ return URLEncoder.encode(key, ENCODING);
65
+ } catch (Exception e) {
66
+ // UTF-8 is always supported; fall back defensively.
67
+ return key.replaceAll("[^A-Za-z0-9_-]", "_");
68
+ }
69
+ }
70
+
71
+ /** Inverse of {@link #encodeKey(String)}. */
72
+ private static String decodeKey(String fileName) {
73
+ try {
74
+ return URLDecoder.decode(fileName, ENCODING);
75
+ } catch (Exception e) {
76
+ return fileName;
77
+ }
78
+ }
79
+
80
+ private File fileForKey(String key) {
81
+ return new File(baseDir, encodeKey(key));
82
+ }
83
+
84
+ /**
85
+ * Read + parse the full wrapper for {@code key}. Missing file → {@code null}.
86
+ * Unparseable contents → {@code null} (treated as a miss; never throws).
87
+ */
88
+ public synchronized JSObject get(String key) {
89
+ File file = fileForKey(key);
90
+ if (!file.exists() || !file.isFile()) {
91
+ return null;
92
+ }
93
+ try {
94
+ String json = readFile(file);
95
+ return new JSObject(json);
96
+ } catch (JSONException parseError) {
97
+ Log.w("StrataStorage", "Unparseable filesystem value for key " + key);
98
+ return null;
99
+ } catch (IOException ioError) {
100
+ Log.e("StrataStorage", "Failed to read filesystem value for key " + key, ioError);
101
+ return null;
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Atomically persist the full wrapper for {@code key}: write a temp file
107
+ * then rename it over the destination.
108
+ */
109
+ public synchronized boolean set(String key, JSObject wrapper) {
110
+ if (wrapper == null) {
111
+ return false;
112
+ }
113
+ ensureDir();
114
+ File target = fileForKey(key);
115
+ // Temp file lives in the staging subdir (same filesystem → atomic
116
+ // rename) with a UUID name, so it can never collide with a key file.
117
+ File tmp = new File(stagingDir, UUID.randomUUID().toString() + ".tmp");
118
+ FileOutputStream fos = null;
119
+ try {
120
+ fos = new FileOutputStream(tmp);
121
+ fos.write(wrapper.toString().getBytes(StandardCharsets.UTF_8));
122
+ fos.flush();
123
+ fos.getFD().sync();
124
+ fos.close();
125
+ fos = null;
126
+
127
+ // Rename is atomic on the same filesystem. Remove a stale target
128
+ // first since File.renameTo can fail when the destination exists.
129
+ if (target.exists() && !target.delete()) {
130
+ Log.w("StrataStorage", "Could not delete existing file before rename: " + key);
131
+ }
132
+ if (!tmp.renameTo(target)) {
133
+ tmp.delete();
134
+ return false;
135
+ }
136
+ return true;
137
+ } catch (IOException e) {
138
+ Log.e("StrataStorage", "Failed to set filesystem value for key " + key, e);
139
+ if (tmp.exists()) {
140
+ tmp.delete();
141
+ }
142
+ return false;
143
+ } finally {
144
+ if (fos != null) {
145
+ try {
146
+ fos.close();
147
+ } catch (IOException ignored) {
148
+ // best-effort close
149
+ }
150
+ }
151
+ }
152
+ }
153
+
154
+ public synchronized boolean remove(String key) {
155
+ File file = fileForKey(key);
156
+ if (!file.exists()) {
157
+ return false;
158
+ }
159
+ return file.delete();
160
+ }
161
+
162
+ /**
163
+ * Delete all stored entries. {@code prefix}, when non-null, restricts the
164
+ * delete to keys whose (decoded) name starts with the prefix.
165
+ */
166
+ public synchronized boolean clear(String prefix) {
167
+ File[] files = baseDir.listFiles();
168
+ if (files == null) {
169
+ return true;
170
+ }
171
+ boolean ok = true;
172
+ for (File file : files) {
173
+ // Skips the staging subdirectory (and any other non-file entry).
174
+ if (!file.isFile()) {
175
+ continue;
176
+ }
177
+ if (prefix == null || decodeKey(file.getName()).startsWith(prefix)) {
178
+ if (!file.delete()) {
179
+ ok = false;
180
+ }
181
+ }
182
+ }
183
+ // On a full clear, also drop any orphaned in-flight temp files.
184
+ if (prefix == null) {
185
+ File[] temps = stagingDir.listFiles();
186
+ if (temps != null) {
187
+ for (File t : temps) {
188
+ t.delete();
189
+ }
190
+ }
191
+ }
192
+ return ok;
193
+ }
194
+
195
+ public synchronized List<String> keys(String pattern) {
196
+ List<String> keys = new ArrayList<>();
197
+ File[] files = baseDir.listFiles();
198
+ if (files == null) {
199
+ return keys;
200
+ }
201
+ for (File file : files) {
202
+ // Skips the staging subdirectory (and any other non-file entry).
203
+ if (!file.isFile()) {
204
+ continue;
205
+ }
206
+ String key = decodeKey(file.getName());
207
+ if (pattern == null || key.contains(pattern)) {
208
+ keys.add(key);
209
+ }
210
+ }
211
+ return keys;
212
+ }
213
+
214
+ public synchronized boolean has(String key) {
215
+ File file = fileForKey(key);
216
+ return file.exists() && file.isFile();
217
+ }
218
+
219
+ /**
220
+ * Size information. {@code total} = {@code keys} + {@code values} (matching
221
+ * the web adapters' convention), {@code values} = Σ file byte sizes,
222
+ * {@code keys} = Σ decoded-key byte lengths, {@code metadata} = Σ length of
223
+ * the parsed {@code metadata} field (0 when absent or unparseable),
224
+ * {@code count} = number of stored files. The detailed breakdown is only
225
+ * surfaced to JS when {@code detailed} is true.
226
+ */
227
+ public synchronized SQLiteStorage.SizeInfo size(boolean detailed) {
228
+ File[] files = baseDir.listFiles();
229
+ long valuesBytes = 0;
230
+ long keysBytes = 0;
231
+ long metadataBytes = 0;
232
+ int count = 0;
233
+
234
+ if (files != null) {
235
+ for (File file : files) {
236
+ // Skips the staging subdirectory (and any other non-file entry).
237
+ if (!file.isFile()) {
238
+ continue;
239
+ }
240
+ count++;
241
+ valuesBytes += file.length();
242
+ keysBytes += decodeKey(file.getName()).getBytes(StandardCharsets.UTF_8).length;
243
+ if (detailed) {
244
+ metadataBytes += metadataByteLength(file);
245
+ }
246
+ }
247
+ }
248
+
249
+ if (!detailed) {
250
+ return new SQLiteStorage.SizeInfo(keysBytes + valuesBytes, count);
251
+ }
252
+ return new SQLiteStorage.SizeInfo(keysBytes + valuesBytes, count, keysBytes, valuesBytes, metadataBytes);
253
+ }
254
+
255
+ /** Parse the file and return the byte length of its {@code metadata} field. */
256
+ private long metadataByteLength(File file) {
257
+ try {
258
+ JSObject wrapper = new JSObject(readFile(file));
259
+ Object metadata = wrapper.opt("metadata");
260
+ if (metadata != null && metadata != org.json.JSONObject.NULL) {
261
+ return metadata.toString().getBytes(StandardCharsets.UTF_8).length;
262
+ }
263
+ } catch (Exception ignored) {
264
+ // Unparseable → contributes 0 metadata bytes.
265
+ }
266
+ return 0;
267
+ }
268
+
269
+ private String readFile(File file) throws IOException {
270
+ byte[] data = new byte[(int) file.length()];
271
+ java.io.FileInputStream fis = null;
272
+ try {
273
+ fis = new java.io.FileInputStream(file);
274
+ int offset = 0;
275
+ int read;
276
+ while (offset < data.length
277
+ && (read = fis.read(data, offset, data.length - offset)) != -1) {
278
+ offset += read;
279
+ }
280
+ } finally {
281
+ if (fis != null) {
282
+ fis.close();
283
+ }
284
+ }
285
+ return new String(data, StandardCharsets.UTF_8);
286
+ }
287
+ }