strata-storage 2.5.0 → 2.6.1
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.
- package/AI-INTEGRATION-GUIDE.md +12 -3
- package/README.md +31 -9
- package/android/AGENTS.md +24 -7
- package/android/CLAUDE.md +42 -4
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/strata/storage/FilesystemStorage.java +287 -0
- package/android/src/main/java/com/strata/storage/SQLiteStorage.java +243 -221
- package/android/src/main/java/com/stratastorage/StrataStoragePlugin.java +202 -78
- package/dist/README.md +31 -9
- package/dist/android/AGENTS.md +24 -7
- package/dist/android/CLAUDE.md +42 -4
- package/dist/android/build.gradle +1 -1
- package/dist/android/src/main/java/com/strata/storage/FilesystemStorage.java +287 -0
- package/dist/android/src/main/java/com/strata/storage/SQLiteStorage.java +243 -221
- package/dist/android/src/main/java/com/stratastorage/StrataStoragePlugin.java +202 -78
- package/dist/ios/AGENTS.md +19 -4
- package/dist/ios/CLAUDE.md +39 -4
- package/dist/ios/Plugin/FilesystemStorage.swift +218 -0
- package/dist/ios/Plugin/SQLiteStorage.swift +265 -173
- package/dist/ios/Plugin/StrataStoragePlugin.swift +100 -42
- package/dist/package.json +1 -1
- package/ios/AGENTS.md +19 -4
- package/ios/CLAUDE.md +39 -4
- package/ios/Plugin/FilesystemStorage.swift +218 -0
- package/ios/Plugin/SQLiteStorage.swift +265 -173
- package/ios/Plugin/StrataStoragePlugin.swift +100 -42
- package/package.json +6 -6
|
@@ -1,24 +1,44 @@
|
|
|
1
1
|
package com.strata.storage;
|
|
2
2
|
|
|
3
3
|
import android.content.Context;
|
|
4
|
+
import android.content.ContentValues;
|
|
4
5
|
import android.database.Cursor;
|
|
5
6
|
import android.database.sqlite.SQLiteDatabase;
|
|
6
7
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
7
|
-
import android.content.ContentValues;
|
|
8
8
|
import android.util.Log;
|
|
9
|
+
import com.getcapacitor.JSObject;
|
|
10
|
+
import java.nio.charset.StandardCharsets;
|
|
9
11
|
import java.util.ArrayList;
|
|
10
|
-
import java.util.List;
|
|
11
12
|
import java.util.HashMap;
|
|
13
|
+
import java.util.HashSet;
|
|
14
|
+
import java.util.List;
|
|
12
15
|
import java.util.Map;
|
|
16
|
+
import java.util.Set;
|
|
17
|
+
import org.json.JSONException;
|
|
13
18
|
import org.json.JSONObject;
|
|
14
|
-
import org.json.JSONArray;
|
|
15
|
-
import java.nio.charset.StandardCharsets;
|
|
16
19
|
|
|
20
|
+
/**
|
|
21
|
+
* SQLite-backed storage. One {@link SQLiteStorage} instance maps to a single
|
|
22
|
+
* database file (one {@link SQLiteOpenHelper}). A single instance can serve
|
|
23
|
+
* multiple logical tables: every public method takes a sanitized {@code table}
|
|
24
|
+
* name and the table is created on first use via {@code CREATE TABLE IF NOT
|
|
25
|
+
* EXISTS}.
|
|
26
|
+
*
|
|
27
|
+
* <p>Value-shape contract (matches the TS {@code SqliteAdapter}): {@code set}
|
|
28
|
+
* receives the FULL wrapper object ({@code value, created, updated, expires?,
|
|
29
|
+
* tags?, metadata?}). The entire wrapper is JSON-serialized into the
|
|
30
|
+
* {@code value} column for round-trip fidelity, while {@code created},
|
|
31
|
+
* {@code updated}, {@code expires}, {@code tags} and {@code metadata} are also
|
|
32
|
+
* extracted into their own columns for TTL + query support. {@code get} parses
|
|
33
|
+
* the {@code value} column back into a {@link JSObject} wrapper.
|
|
34
|
+
*
|
|
35
|
+
* <p>All database access is synchronized on the instance so overlapping bridge
|
|
36
|
+
* calls cannot corrupt the connection.
|
|
37
|
+
*/
|
|
17
38
|
public class SQLiteStorage extends SQLiteOpenHelper {
|
|
18
39
|
private static final int DATABASE_VERSION = 1;
|
|
19
|
-
private static final String
|
|
20
|
-
|
|
21
|
-
|
|
40
|
+
private static final String DEFAULT_TABLE = "storage";
|
|
41
|
+
|
|
22
42
|
private static final String KEY_ID = "key";
|
|
23
43
|
private static final String KEY_VALUE = "value";
|
|
24
44
|
private static final String KEY_CREATED = "created";
|
|
@@ -26,178 +46,181 @@ public class SQLiteStorage extends SQLiteOpenHelper {
|
|
|
26
46
|
private static final String KEY_EXPIRES = "expires";
|
|
27
47
|
private static final String KEY_TAGS = "tags";
|
|
28
48
|
private static final String KEY_METADATA = "metadata";
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
49
|
+
|
|
50
|
+
/** Tables already created in this DB during the process lifetime. */
|
|
51
|
+
private final Set<String> ensuredTables = new HashSet<>();
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param context Android context
|
|
55
|
+
* @param dbName fully-resolved database file name (already sanitized +
|
|
56
|
+
* suffixed with ".db" by the plugin)
|
|
57
|
+
*/
|
|
34
58
|
public SQLiteStorage(Context context, String dbName) {
|
|
35
|
-
super(context, dbName
|
|
59
|
+
super(context, dbName, null, DATABASE_VERSION);
|
|
36
60
|
}
|
|
37
|
-
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sanitize a table name to a safe SQL identifier ({@code ^[A-Za-z0-9_]+$}).
|
|
64
|
+
* Table names cannot be passed as bound parameters in SQLite, so they MUST
|
|
65
|
+
* be validated before being interpolated into DDL/DML.
|
|
66
|
+
*/
|
|
67
|
+
public static String sanitizeTable(String table) {
|
|
68
|
+
if (table == null || table.isEmpty()) {
|
|
69
|
+
return DEFAULT_TABLE;
|
|
70
|
+
}
|
|
71
|
+
String cleaned = table.replaceAll("[^A-Za-z0-9_]", "");
|
|
72
|
+
return cleaned.isEmpty() ? DEFAULT_TABLE : cleaned;
|
|
73
|
+
}
|
|
74
|
+
|
|
38
75
|
@Override
|
|
39
76
|
public void onCreate(SQLiteDatabase db) {
|
|
40
|
-
|
|
77
|
+
// Tables are created lazily per (database, table) via ensureTable().
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@Override
|
|
81
|
+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
82
|
+
// No destructive migrations: tables are created on demand and persist.
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create the table for {@code safeTable} if it does not yet exist. Caller
|
|
87
|
+
* MUST pass an already-sanitized identifier.
|
|
88
|
+
*/
|
|
89
|
+
private void ensureTable(SQLiteDatabase db, String safeTable) {
|
|
90
|
+
if (ensuredTables.contains(safeTable)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
String createTable = "CREATE TABLE IF NOT EXISTS " + safeTable + "("
|
|
41
94
|
+ KEY_ID + " TEXT PRIMARY KEY,"
|
|
42
|
-
+ KEY_VALUE + "
|
|
95
|
+
+ KEY_VALUE + " TEXT NOT NULL,"
|
|
43
96
|
+ KEY_CREATED + " INTEGER NOT NULL,"
|
|
44
97
|
+ KEY_UPDATED + " INTEGER NOT NULL,"
|
|
45
98
|
+ KEY_EXPIRES + " INTEGER,"
|
|
46
99
|
+ KEY_TAGS + " TEXT,"
|
|
47
100
|
+ KEY_METADATA + " TEXT" + ")";
|
|
48
|
-
db.execSQL(
|
|
101
|
+
db.execSQL(createTable);
|
|
102
|
+
ensuredTables.add(safeTable);
|
|
49
103
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (value instanceof byte[]) {
|
|
62
|
-
valueBytes = (byte[]) value;
|
|
63
|
-
} else if (value instanceof String) {
|
|
64
|
-
valueBytes = ((String) value).getBytes(StandardCharsets.UTF_8);
|
|
65
|
-
} else {
|
|
66
|
-
// Convert complex objects to JSON then to bytes
|
|
67
|
-
String json;
|
|
68
|
-
if (value instanceof JSONObject) {
|
|
69
|
-
json = ((JSONObject) value).toString();
|
|
70
|
-
} else {
|
|
71
|
-
// Use helper method to convert object to JSON string
|
|
72
|
-
try {
|
|
73
|
-
json = objectToJsonString(value);
|
|
74
|
-
} catch (Exception jsonEx) {
|
|
75
|
-
// Fallback to string representation
|
|
76
|
-
json = value.toString();
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
valueBytes = json.getBytes(StandardCharsets.UTF_8);
|
|
80
|
-
}
|
|
81
|
-
} catch (Exception e) {
|
|
82
|
-
Log.e("StrataStorage", "Failed to set value in SQLite", e);
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Persist the full wrapper for {@code key}. The wrapper is stored verbatim
|
|
107
|
+
* as JSON in the {@code value} column; {@code created}/{@code updated}/
|
|
108
|
+
* {@code expires}/{@code tags}/{@code metadata} are mirrored into columns
|
|
109
|
+
* for TTL + query.
|
|
110
|
+
*
|
|
111
|
+
* @param wrapper the full {@link StorageValue} wrapper as a {@link JSObject}
|
|
112
|
+
*/
|
|
113
|
+
public synchronized boolean set(String table, String key, JSObject wrapper) {
|
|
114
|
+
if (wrapper == null) {
|
|
83
115
|
return false;
|
|
84
116
|
}
|
|
85
|
-
|
|
86
|
-
ContentValues values = new ContentValues();
|
|
87
|
-
|
|
117
|
+
String safeTable = sanitizeTable(table);
|
|
88
118
|
long now = System.currentTimeMillis();
|
|
119
|
+
|
|
120
|
+
// Column extraction for TTL + query. Defaults keep legacy fidelity.
|
|
121
|
+
long created = wrapper.optLong(KEY_CREATED, now);
|
|
122
|
+
long updated = wrapper.optLong(KEY_UPDATED, now);
|
|
123
|
+
|
|
124
|
+
ContentValues values = new ContentValues();
|
|
89
125
|
values.put(KEY_ID, key);
|
|
90
|
-
values.put(KEY_VALUE,
|
|
91
|
-
values.put(KEY_CREATED,
|
|
92
|
-
values.put(KEY_UPDATED,
|
|
93
|
-
|
|
94
|
-
if (
|
|
95
|
-
values.put(KEY_EXPIRES,
|
|
126
|
+
values.put(KEY_VALUE, wrapper.toString());
|
|
127
|
+
values.put(KEY_CREATED, created);
|
|
128
|
+
values.put(KEY_UPDATED, updated);
|
|
129
|
+
|
|
130
|
+
if (wrapper.has(KEY_EXPIRES) && !wrapper.isNull(KEY_EXPIRES)) {
|
|
131
|
+
values.put(KEY_EXPIRES, wrapper.optLong(KEY_EXPIRES));
|
|
96
132
|
}
|
|
97
|
-
|
|
98
|
-
|
|
133
|
+
// tags / metadata are stored as their JSON text for query use.
|
|
134
|
+
Object tags = wrapper.opt(KEY_TAGS);
|
|
135
|
+
if (tags != null && tags != JSONObject.NULL) {
|
|
136
|
+
values.put(KEY_TAGS, tags.toString());
|
|
99
137
|
}
|
|
100
|
-
|
|
101
|
-
|
|
138
|
+
Object metadata = wrapper.opt(KEY_METADATA);
|
|
139
|
+
if (metadata != null && metadata != JSONObject.NULL) {
|
|
140
|
+
values.put(KEY_METADATA, metadata.toString());
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
SQLiteDatabase db = getWritableDatabase();
|
|
145
|
+
ensureTable(db, safeTable);
|
|
146
|
+
long result = db.insertWithOnConflict(
|
|
147
|
+
safeTable, null, values, SQLiteDatabase.CONFLICT_REPLACE);
|
|
148
|
+
return result != -1;
|
|
149
|
+
} catch (Exception e) {
|
|
150
|
+
Log.e("StrataStorage", "Failed to set value in SQLite", e);
|
|
151
|
+
return false;
|
|
102
152
|
}
|
|
103
|
-
|
|
104
|
-
long result = db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
|
|
105
|
-
db.close();
|
|
106
|
-
return result != -1;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Convenience method for simple Object values
|
|
110
|
-
public boolean set(String key, Object value) {
|
|
111
|
-
return set(key, value, null, null, null);
|
|
112
153
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Read the full wrapper for {@code key}, parsed back from the JSON stored in
|
|
157
|
+
* the {@code value} column. Missing row → {@code null}. Unparseable legacy
|
|
158
|
+
* bytes → {@code null} (treated as a miss; never throws).
|
|
159
|
+
*/
|
|
160
|
+
public synchronized JSObject get(String table, String key) {
|
|
161
|
+
String safeTable = sanitizeTable(table);
|
|
162
|
+
SQLiteDatabase db = getReadableDatabase();
|
|
163
|
+
ensureTable(db, safeTable);
|
|
116
164
|
Cursor cursor = null;
|
|
117
165
|
try {
|
|
118
|
-
cursor = db.query(
|
|
166
|
+
cursor = db.query(safeTable, new String[]{KEY_VALUE}, KEY_ID + "=?",
|
|
119
167
|
new String[]{key}, null, null, null, null);
|
|
120
|
-
|
|
121
|
-
Map<String, Object> result = null;
|
|
122
168
|
if (cursor != null && cursor.moveToFirst()) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
result.put("created", cursor.getLong(cursor.getColumnIndex(KEY_CREATED)));
|
|
127
|
-
result.put("updated", cursor.getLong(cursor.getColumnIndex(KEY_UPDATED)));
|
|
128
|
-
|
|
129
|
-
int expiresIndex = cursor.getColumnIndex(KEY_EXPIRES);
|
|
130
|
-
if (!cursor.isNull(expiresIndex)) {
|
|
131
|
-
result.put("expires", cursor.getLong(expiresIndex));
|
|
169
|
+
String stored = cursor.getString(0);
|
|
170
|
+
if (stored == null) {
|
|
171
|
+
return null;
|
|
132
172
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
int metadataIndex = cursor.getColumnIndex(KEY_METADATA);
|
|
140
|
-
if (!cursor.isNull(metadataIndex)) {
|
|
141
|
-
result.put("metadata", cursor.getString(metadataIndex));
|
|
173
|
+
try {
|
|
174
|
+
return new JSObject(stored);
|
|
175
|
+
} catch (JSONException parseError) {
|
|
176
|
+
// Legacy / corrupted payload: treat as a miss rather than throw.
|
|
177
|
+
Log.w("StrataStorage", "Unparseable SQLite value for key " + key);
|
|
178
|
+
return null;
|
|
142
179
|
}
|
|
143
180
|
}
|
|
144
|
-
return
|
|
181
|
+
return null;
|
|
145
182
|
} finally {
|
|
146
183
|
if (cursor != null) {
|
|
147
184
|
cursor.close();
|
|
148
185
|
}
|
|
149
|
-
db.close();
|
|
150
186
|
}
|
|
151
187
|
}
|
|
152
|
-
|
|
153
|
-
public boolean remove(String key) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
db
|
|
188
|
+
|
|
189
|
+
public synchronized boolean remove(String table, String key) {
|
|
190
|
+
String safeTable = sanitizeTable(table);
|
|
191
|
+
SQLiteDatabase db = getWritableDatabase();
|
|
192
|
+
ensureTable(db, safeTable);
|
|
193
|
+
int result = db.delete(safeTable, KEY_ID + " = ?", new String[]{key});
|
|
157
194
|
return result > 0;
|
|
158
195
|
}
|
|
159
|
-
|
|
160
|
-
public boolean clear() {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
public boolean clear(String prefix) {
|
|
165
|
-
SQLiteDatabase db = this.getWritableDatabase();
|
|
196
|
+
|
|
197
|
+
public synchronized boolean clear(String table, String prefix) {
|
|
198
|
+
String safeTable = sanitizeTable(table);
|
|
199
|
+
SQLiteDatabase db = getWritableDatabase();
|
|
200
|
+
ensureTable(db, safeTable);
|
|
166
201
|
int result;
|
|
167
|
-
|
|
168
202
|
if (prefix != null) {
|
|
169
|
-
|
|
170
|
-
result = db.delete(TABLE_NAME, KEY_ID + " LIKE ?", new String[]{prefix + "%"});
|
|
203
|
+
result = db.delete(safeTable, KEY_ID + " LIKE ?", new String[]{prefix + "%"});
|
|
171
204
|
} else {
|
|
172
|
-
|
|
173
|
-
result = db.delete(TABLE_NAME, null, null);
|
|
205
|
+
result = db.delete(safeTable, null, null);
|
|
174
206
|
}
|
|
175
|
-
|
|
176
|
-
db.close();
|
|
177
207
|
return result >= 0;
|
|
178
208
|
}
|
|
179
|
-
|
|
180
|
-
public List<String> keys() {
|
|
181
|
-
return keys(null);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
public List<String> keys(String pattern) {
|
|
185
|
-
List<String> keys = new ArrayList<>();
|
|
186
|
-
String selectQuery;
|
|
187
|
-
String[] selectionArgs = null;
|
|
188
|
-
|
|
189
|
-
if (pattern != null) {
|
|
190
|
-
selectQuery = "SELECT " + KEY_ID + " FROM " + TABLE_NAME + " WHERE " + KEY_ID + " LIKE ?";
|
|
191
|
-
selectionArgs = new String[]{"%" + pattern + "%"};
|
|
192
|
-
} else {
|
|
193
|
-
selectQuery = "SELECT " + KEY_ID + " FROM " + TABLE_NAME;
|
|
194
|
-
}
|
|
195
209
|
|
|
196
|
-
|
|
210
|
+
public synchronized List<String> keys(String table, String pattern) {
|
|
211
|
+
String safeTable = sanitizeTable(table);
|
|
212
|
+
List<String> keys = new ArrayList<>();
|
|
213
|
+
SQLiteDatabase db = getReadableDatabase();
|
|
214
|
+
ensureTable(db, safeTable);
|
|
197
215
|
Cursor cursor = null;
|
|
198
216
|
try {
|
|
199
|
-
|
|
200
|
-
|
|
217
|
+
if (pattern != null) {
|
|
218
|
+
cursor = db.query(safeTable, new String[]{KEY_ID}, KEY_ID + " LIKE ?",
|
|
219
|
+
new String[]{"%" + pattern + "%"}, null, null, null, null);
|
|
220
|
+
} else {
|
|
221
|
+
cursor = db.query(safeTable, new String[]{KEY_ID},
|
|
222
|
+
null, null, null, null, null);
|
|
223
|
+
}
|
|
201
224
|
if (cursor != null && cursor.moveToFirst()) {
|
|
202
225
|
do {
|
|
203
226
|
keys.add(cursor.getString(0));
|
|
@@ -208,49 +231,43 @@ public class SQLiteStorage extends SQLiteOpenHelper {
|
|
|
208
231
|
if (cursor != null) {
|
|
209
232
|
cursor.close();
|
|
210
233
|
}
|
|
211
|
-
db.close();
|
|
212
234
|
}
|
|
213
235
|
}
|
|
214
|
-
|
|
215
|
-
public boolean has(String key) {
|
|
216
|
-
|
|
236
|
+
|
|
237
|
+
public synchronized boolean has(String table, String key) {
|
|
238
|
+
String safeTable = sanitizeTable(table);
|
|
239
|
+
SQLiteDatabase db = getReadableDatabase();
|
|
240
|
+
ensureTable(db, safeTable);
|
|
217
241
|
Cursor cursor = null;
|
|
218
242
|
try {
|
|
219
|
-
cursor = db.query(
|
|
243
|
+
cursor = db.query(safeTable, new String[]{KEY_ID}, KEY_ID + "=?",
|
|
220
244
|
new String[]{key}, null, null, null, null);
|
|
221
|
-
|
|
222
|
-
return exists;
|
|
245
|
+
return cursor != null && cursor.getCount() > 0;
|
|
223
246
|
} finally {
|
|
224
247
|
if (cursor != null) {
|
|
225
248
|
cursor.close();
|
|
226
249
|
}
|
|
227
|
-
db.close();
|
|
228
250
|
}
|
|
229
251
|
}
|
|
230
252
|
|
|
231
253
|
/**
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
* needs to enumerate candidate rows.
|
|
254
|
+
* Enumerate every key in the table. The TS {@code SqliteAdapter} applies the
|
|
255
|
+
* real query condition by re-fetching/filtering each key, so this only needs
|
|
256
|
+
* to surface candidate keys.
|
|
236
257
|
*/
|
|
237
|
-
public List<Map<String, Object>> query() {
|
|
258
|
+
public synchronized List<Map<String, Object>> query(String table) {
|
|
259
|
+
String safeTable = sanitizeTable(table);
|
|
238
260
|
List<Map<String, Object>> rows = new ArrayList<>();
|
|
239
|
-
SQLiteDatabase db =
|
|
261
|
+
SQLiteDatabase db = getReadableDatabase();
|
|
262
|
+
ensureTable(db, safeTable);
|
|
240
263
|
Cursor cursor = null;
|
|
241
264
|
try {
|
|
242
|
-
cursor = db.query(
|
|
265
|
+
cursor = db.query(safeTable, new String[]{KEY_ID},
|
|
243
266
|
null, null, null, null, null);
|
|
244
267
|
if (cursor != null && cursor.moveToFirst()) {
|
|
245
268
|
do {
|
|
246
269
|
Map<String, Object> row = new HashMap<>();
|
|
247
270
|
row.put("key", cursor.getString(0));
|
|
248
|
-
byte[] valueBytes = cursor.getBlob(1);
|
|
249
|
-
if (valueBytes != null) {
|
|
250
|
-
row.put("value", new String(valueBytes, StandardCharsets.UTF_8));
|
|
251
|
-
} else {
|
|
252
|
-
row.put("value", null);
|
|
253
|
-
}
|
|
254
271
|
rows.add(row);
|
|
255
272
|
} while (cursor.moveToNext());
|
|
256
273
|
}
|
|
@@ -259,91 +276,96 @@ public class SQLiteStorage extends SQLiteOpenHelper {
|
|
|
259
276
|
if (cursor != null) {
|
|
260
277
|
cursor.close();
|
|
261
278
|
}
|
|
262
|
-
db.close();
|
|
263
279
|
}
|
|
264
280
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Size information. {@code total} = Σ(length(key) + length(value)), matching
|
|
284
|
+
* the web adapters' convention (key bytes are included). When {@code
|
|
285
|
+
* detailed} is true the byte breakdown is also populated: {@code keys} = Σ
|
|
286
|
+
* length(key), {@code values} = Σ length(value), {@code metadata} = Σ
|
|
287
|
+
* length(metadata) (0 for null); {@code count} is the row count.
|
|
288
|
+
*/
|
|
289
|
+
public synchronized SizeInfo size(String table, boolean detailed) {
|
|
290
|
+
String safeTable = sanitizeTable(table);
|
|
291
|
+
SQLiteDatabase db = getReadableDatabase();
|
|
292
|
+
ensureTable(db, safeTable);
|
|
268
293
|
Cursor cursor = null;
|
|
269
294
|
try {
|
|
270
|
-
|
|
295
|
+
if (!detailed) {
|
|
296
|
+
cursor = db.rawQuery("SELECT COUNT(*), SUM(LENGTH(" + KEY_ID + ") + LENGTH("
|
|
297
|
+
+ KEY_VALUE + ")) FROM " + safeTable, null);
|
|
298
|
+
long totalSize = 0;
|
|
299
|
+
int count = 0;
|
|
300
|
+
if (cursor != null && cursor.moveToFirst()) {
|
|
301
|
+
count = cursor.getInt(0);
|
|
302
|
+
if (!cursor.isNull(1)) {
|
|
303
|
+
totalSize = cursor.getLong(1);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return new SizeInfo(totalSize, count);
|
|
307
|
+
}
|
|
271
308
|
|
|
272
|
-
|
|
309
|
+
cursor = db.query(safeTable,
|
|
310
|
+
new String[]{KEY_ID, KEY_VALUE, KEY_METADATA},
|
|
311
|
+
null, null, null, null, null);
|
|
312
|
+
long keysBytes = 0;
|
|
313
|
+
long valuesBytes = 0;
|
|
314
|
+
long metadataBytes = 0;
|
|
273
315
|
int count = 0;
|
|
274
|
-
|
|
275
316
|
if (cursor != null && cursor.moveToFirst()) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
317
|
+
do {
|
|
318
|
+
count++;
|
|
319
|
+
String key = cursor.getString(0);
|
|
320
|
+
String value = cursor.getString(1);
|
|
321
|
+
String metadata = cursor.getString(2);
|
|
322
|
+
if (key != null) {
|
|
323
|
+
keysBytes += key.getBytes(StandardCharsets.UTF_8).length;
|
|
324
|
+
}
|
|
325
|
+
if (value != null) {
|
|
326
|
+
valuesBytes += value.getBytes(StandardCharsets.UTF_8).length;
|
|
327
|
+
}
|
|
328
|
+
if (metadata != null) {
|
|
329
|
+
metadataBytes += metadata.getBytes(StandardCharsets.UTF_8).length;
|
|
330
|
+
}
|
|
331
|
+
} while (cursor.moveToNext());
|
|
281
332
|
}
|
|
282
|
-
|
|
283
|
-
return new SizeInfo(totalSize, count);
|
|
333
|
+
return new SizeInfo(keysBytes + valuesBytes, count, keysBytes, valuesBytes, metadataBytes);
|
|
284
334
|
} finally {
|
|
285
335
|
if (cursor != null) {
|
|
286
336
|
cursor.close();
|
|
287
337
|
}
|
|
288
|
-
db.close();
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Convert an object to JSON string using reflection
|
|
294
|
-
*/
|
|
295
|
-
private String objectToJsonString(Object obj) throws Exception {
|
|
296
|
-
if (obj == null) {
|
|
297
|
-
return "null";
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
if (obj instanceof String || obj instanceof Number || obj instanceof Boolean) {
|
|
301
|
-
return obj.toString();
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
if (obj instanceof Map) {
|
|
305
|
-
JSONObject jsonObj = new JSONObject();
|
|
306
|
-
Map<?, ?> map = (Map<?, ?>) obj;
|
|
307
|
-
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
|
308
|
-
String key = entry.getKey().toString();
|
|
309
|
-
jsonObj.put(key, entry.getValue());
|
|
310
|
-
}
|
|
311
|
-
return jsonObj.toString();
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
if (obj instanceof List || obj.getClass().isArray()) {
|
|
315
|
-
JSONArray jsonArray = new JSONArray();
|
|
316
|
-
if (obj instanceof List) {
|
|
317
|
-
List<?> list = (List<?>) obj;
|
|
318
|
-
for (Object item : list) {
|
|
319
|
-
jsonArray.put(item);
|
|
320
|
-
}
|
|
321
|
-
} else {
|
|
322
|
-
Object[] array = (Object[]) obj;
|
|
323
|
-
for (Object item : array) {
|
|
324
|
-
jsonArray.put(item);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
return jsonArray.toString();
|
|
328
338
|
}
|
|
329
|
-
|
|
330
|
-
// For other objects, create a simple JSON object with their string representation
|
|
331
|
-
JSONObject jsonObj = new JSONObject();
|
|
332
|
-
jsonObj.put("value", obj.toString());
|
|
333
|
-
jsonObj.put("type", obj.getClass().getSimpleName());
|
|
334
|
-
return jsonObj.toString();
|
|
335
339
|
}
|
|
336
|
-
|
|
340
|
+
|
|
337
341
|
/**
|
|
338
|
-
* Size information
|
|
342
|
+
* Size information. The detailed byte breakdown is only meaningful when
|
|
343
|
+
* {@link #detailed} is true.
|
|
339
344
|
*/
|
|
340
345
|
public static class SizeInfo {
|
|
341
346
|
public final long total;
|
|
342
347
|
public final int count;
|
|
343
|
-
|
|
348
|
+
public final boolean detailed;
|
|
349
|
+
public final long keysBytes;
|
|
350
|
+
public final long valuesBytes;
|
|
351
|
+
public final long metadataBytes;
|
|
352
|
+
|
|
344
353
|
public SizeInfo(long total, int count) {
|
|
345
354
|
this.total = total;
|
|
346
355
|
this.count = count;
|
|
356
|
+
this.detailed = false;
|
|
357
|
+
this.keysBytes = 0;
|
|
358
|
+
this.valuesBytes = 0;
|
|
359
|
+
this.metadataBytes = 0;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
public SizeInfo(long total, int count, long keysBytes, long valuesBytes, long metadataBytes) {
|
|
363
|
+
this.total = total;
|
|
364
|
+
this.count = count;
|
|
365
|
+
this.detailed = true;
|
|
366
|
+
this.keysBytes = keysBytes;
|
|
367
|
+
this.valuesBytes = valuesBytes;
|
|
368
|
+
this.metadataBytes = metadataBytes;
|
|
347
369
|
}
|
|
348
370
|
}
|
|
349
|
-
}
|
|
371
|
+
}
|