Skip to content
This repository was archived by the owner on Aug 3, 2026. It is now read-only.

Commit 184a78f

Browse files
committed
ESPJsonDB is now a thin façade over runtime-owned state
1 parent 2ca5c55 commit 184a78f

21 files changed

Lines changed: 2150 additions & 1655 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project are documented in this file.
55
The format follows Keep a Changelog and the project adheres to Semantic Versioning.
66

77
## [Unreleased]
8+
### Changed
9+
- Moved mutable DB ownership behind an internal runtime and moved file upload / path handling behind a real `FileStore` subsystem.
10+
- `Collection` now uses an internal backing store, enforces `maxDecodedViews` / `maxRecordsInMemory`, and applies revision-based conflict checks in update paths.
11+
12+
### Removed
13+
- Compatibility aliases `getDiag()`, `getAllCollectionName()`, and `unRegisterSchema()`.
14+
- Direct file helper methods from `ESPJsonDB`; use `db.files()` only.
815

916
## [2.0.0] - 2026-03-27
1017
### Added
@@ -18,6 +25,7 @@ The format follows Keep a Changelog and the project adheres to Semantic Versioni
1825
### Changed
1926
- Snapshot payloads now include `_meta` alongside `_id` and document fields.
2027
- `getDiagnostics()` replaces `getDiag()`, `listCollectionNames()` replaces `getAllCollectionName()`, and `unregisterSchema()` replaces `unRegisterSchema()`.
28+
- File operations now live only behind `db.files()`, with async state access renamed to `cancelUpload()` / `getUploadState()`.
2129
- Collection persistence now uses `.jdb` files instead of raw `.mp` MessagePack payload files.
2230
- Unique constraints are enforced through in-memory per-field indexes instead of full collection scans.
2331
- The top-level CMake build now uses C++17 to match the library metadata.

examples/AsyncFileUpload/AsyncFileUpload.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void setup() {
7272
opts.chunkSize = 128;
7373
opts.overwrite = true;
7474

75-
auto start = db.writeFileStreamAsync("uploads/telemetry.bin", uploadPull, opts, onUploadDone);
75+
auto start = db.files().writeFileStreamAsync("uploads/telemetry.bin", uploadPull, opts, onUploadDone);
7676
if (!start.status.ok()) {
7777
Serial.printf("writeFileStreamAsync failed: %s\n", start.status.message);
7878
return;
@@ -91,7 +91,7 @@ void loop() {
9191
}
9292

9393
if (!gUploadDone) {
94-
auto state = db.getFileUploadState(gUploadId);
94+
auto state = db.files().getUploadState(gUploadId);
9595
if (state.status.ok()) {
9696
Serial.printf("Upload state: %u\n", static_cast<unsigned>(state.value));
9797
}
@@ -109,7 +109,7 @@ void loop() {
109109
return;
110110
}
111111

112-
auto file = db.readFile("uploads/telemetry.bin");
112+
auto file = db.files().readFile("uploads/telemetry.bin");
113113
if (!file.status.ok()) {
114114
Serial.printf("readFile failed: %s\n", file.status.message);
115115
return;

examples/AsyncLargeFileUpload/AsyncLargeFileUpload.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void setup() {
127127
opts.chunkSize = kChunkSize;
128128
opts.overwrite = true;
129129

130-
auto start = db.writeFileStreamAsync(
130+
auto start = db.files().writeFileStreamAsync(
131131
"uploads/large_payload.bin",
132132
largeUploadPull,
133133
opts,
@@ -153,7 +153,7 @@ void loop() {
153153
}
154154

155155
if (!gUploadDone) {
156-
auto state = db.getFileUploadState(gUploadId);
156+
auto state = db.files().getUploadState(gUploadId);
157157
if (state.status.ok()) {
158158
Serial.printf(
159159
"state=%u progress=%u/%u\n",
@@ -175,7 +175,7 @@ void loop() {
175175
return;
176176
}
177177

178-
auto sizeRes = db.fileSize("uploads/large_payload.bin");
178+
auto sizeRes = db.files().fileSize("uploads/large_payload.bin");
179179
if (!sizeRes.status.ok() || sizeRes.value != kPayloadSize) {
180180
Serial.printf(
181181
"fileSize mismatch: %s size=%u\n",
@@ -186,7 +186,7 @@ void loop() {
186186
}
187187

188188
HashingSink sink;
189-
auto readRes = db.readFileStream("uploads/large_payload.bin", sink, kReadChunkSize);
189+
auto readRes = db.files().readFileStream("uploads/large_payload.bin", sink, kReadChunkSize);
190190
if (!readRes.status.ok()) {
191191
Serial.printf("readFileStream failed: %s\n", readRes.status.message);
192192
return;

examples/FileStreaming/FileStreaming.ino

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,37 @@ void setup() {
3737
}
3838

3939
// 1) Plain text file
40-
st = db.writeTextFile("docs/readme.txt", "Hello from ESPJsonDB file storage.\nLine #2.");
40+
st = db.files().writeTextFile("docs/readme.txt", "Hello from ESPJsonDB file storage.\nLine #2.");
4141
if (!st.ok()) {
4242
Serial.printf("writeTextFile(txt) failed: %s\n", st.message);
4343
return;
4444
}
4545

4646
// 2) JSON file (stored as text payload)
47-
st = db.writeTextFile("configs/app.json", "{\"mode\":\"demo\",\"intervalMs\":1000}");
47+
st = db.files().writeTextFile("configs/app.json", "{\"mode\":\"demo\",\"intervalMs\":1000}");
4848
if (!st.ok()) {
4949
Serial.printf("writeTextFile(json) failed: %s\n", st.message);
5050
return;
5151
}
5252

5353
// 3) CSV file
54-
st = db.writeTextFile("exports/metrics.csv", "ts,temp,humidity\n1,21.4,48\n2,21.7,47\n");
54+
st = db.files().writeTextFile("exports/metrics.csv", "ts,temp,humidity\n1,21.4,48\n2,21.7,47\n");
5555
if (!st.ok()) {
5656
Serial.printf("writeTextFile(csv) failed: %s\n", st.message);
5757
return;
5858
}
5959

6060
// 4) Binary file using direct byte write
6161
const uint8_t binPayload[] = {0x00, 0xA1, 0xFF, 0x10, 0x22, 0x33, 0x44, 0x55};
62-
st = db.writeFile("firmware/chunk.bin", binPayload, sizeof(binPayload));
62+
st = db.files().writeFile("firmware/chunk.bin", binPayload, sizeof(binPayload));
6363
if (!st.ok()) {
6464
Serial.printf("writeFile(bin) failed: %s\n", st.message);
6565
return;
6666
}
6767

6868
// 5) Custom extension file (any file type works)
6969
const uint8_t customPayload[] = {'M', 'O', 'D', 'L', 0x01, 0x02, 0x03, 0x04};
70-
st = db.writeFile("assets/model.dat", customPayload, sizeof(customPayload));
70+
st = db.files().writeFile("assets/model.dat", customPayload, sizeof(customPayload));
7171
if (!st.ok()) {
7272
Serial.printf("writeFile(dat) failed: %s\n", st.message);
7373
return;
@@ -83,7 +83,7 @@ void setup() {
8383
ESPJsonDBFileOptions opts;
8484
opts.overwrite = true;
8585
opts.chunkSize = 128;
86-
st = db.writeFileFromPath("streams/raw_capture.raw", seedPath, opts);
86+
st = db.files().writeFileFromPath("streams/raw_capture.raw", seedPath, opts);
8787
if (!st.ok()) {
8888
Serial.printf("writeFileFromPath failed: %s\n", st.message);
8989
return;
@@ -100,19 +100,19 @@ void setup() {
100100
return;
101101
}
102102

103-
auto streamOut = db.readFileStream("streams/raw_capture.raw", sink, 96);
103+
auto streamOut = db.files().readFileStream("streams/raw_capture.raw", sink, 96);
104104
sink.close();
105105
if (!streamOut.status.ok()) {
106106
Serial.printf("readFileStream failed: %s\n", streamOut.status.message);
107107
return;
108108
}
109109

110110
// 8) Verify with helper reads
111-
auto txt = db.readTextFile("docs/readme.txt");
112-
auto jsn = db.readTextFile("configs/app.json");
113-
auto csv = db.readTextFile("exports/metrics.csv");
114-
auto bin = db.readFile("firmware/chunk.bin");
115-
auto modelSize = db.fileSize("assets/model.dat");
111+
auto txt = db.files().readTextFile("docs/readme.txt");
112+
auto jsn = db.files().readTextFile("configs/app.json");
113+
auto csv = db.files().readTextFile("exports/metrics.csv");
114+
auto bin = db.files().readFile("firmware/chunk.bin");
115+
auto modelSize = db.files().fileSize("assets/model.dat");
116116

117117
if (!txt.status.ok() || !jsn.status.ok() || !csv.status.ok() || !bin.status.ok() ||
118118
!modelSize.status.ok()) {
@@ -128,7 +128,7 @@ void setup() {
128128
Serial.printf("model.dat bytes: %u\n", static_cast<unsigned>(modelSize.value));
129129
Serial.printf("streamed raw bytes out: %u\n", static_cast<unsigned>(streamOut.value));
130130

131-
auto exists = db.fileExists("streams/raw_capture.raw");
131+
auto exists = db.files().fileExists("streams/raw_capture.raw");
132132
Serial.printf(
133133
"streams/raw_capture.raw exists: %s\n",
134134
(exists.status.ok() && exists.value) ? "yes" : "no"

examples/LargeFileStreaming/LargeFileStreaming.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ void setup() {
108108
opts.chunkSize = kWriteChunkSize;
109109

110110
const char *path = "firmware/large_payload.bin";
111-
st = db.writeFileStream(path, pullCb, opts);
111+
st = db.files().writeFileStream(path, pullCb, opts);
112112
if (!st.ok()) {
113113
Serial.printf("writeFileStream failed: %s\n", st.message);
114114
return;
115115
}
116116

117-
auto sizeRes = db.fileSize(path);
117+
auto sizeRes = db.files().fileSize(path);
118118
if (!sizeRes.status.ok() || sizeRes.value != kPayloadSize) {
119119
Serial.printf(
120120
"fileSize mismatch: status=%s size=%u\n",
@@ -125,7 +125,7 @@ void setup() {
125125
}
126126

127127
HashingSink sink;
128-
auto readRes = db.readFileStream(path, sink, kReadChunkSize);
128+
auto readRes = db.files().readFileStream(path, sink, kReadChunkSize);
129129
if (!readRes.status.ok()) {
130130
Serial.printf("readFileStream failed: %s\n", readRes.status.message);
131131
return;

0 commit comments

Comments
 (0)