Skip to content

Commit 0fb37f4

Browse files
committed
File dump working
1 parent 2418237 commit 0fb37f4

2 files changed

Lines changed: 102 additions & 6 deletions

File tree

prompt.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,96 @@ work.ppt
170170
Every 30 seconds the system will update the session database (if there is a change), and check for all of the other sessions in the database folder to see if there are updates, and if there are merge them with Yjs.
171171

172172
How does that sound?
173+
174+
--- day #3 start ---
175+
176+
# OneFolder Technical Documentation – Session and Backup System
177+
178+
This document summarizes the key technical changes implemented in OneFolder. These updates cover session management (via a unique UUID stored in a `session.json` file), IPC-based retrieval of the session ID, and an enhanced backup scheduler with dual backup mechanisms.
179+
180+
## 1. Session Management
181+
182+
### Session UUID Generation and Storage
183+
184+
- **UUID Generation:**
185+
- On first run, OneFolder generates a unique session ID using `crypto.randomUUID()` if available; otherwise, it falls back to Node's crypto module.
186+
- **Persistent Storage:**
187+
- The generated UUID is stored in a dedicated file (`session.json`) located in the application's `userData` directory.
188+
- **Purpose:**
189+
- This ensures each OneFolder installation has a unique identifier, isolating its backups from others.
190+
191+
### IPC Retrieval
192+
193+
- **IPC Handler:**
194+
- An IPC handler (`GET_SESSION_ID`) is registered in the main process.
195+
- **Usage:**
196+
- The renderer process retrieves the session ID using `RendererMessenger.getSessionId()`, ensuring that all backup operations (folder structure, dumps, etc.) are tied to the correct session.
197+
198+
## 2. Backup Folder Structure
199+
200+
- **New Structure:**
201+
- Backups are stored in:
202+
`<baseBackupDirectory>/database/<sessionId>/`
203+
- **Benefit:**
204+
- This isolates backups per session and supports syncing through cloud storage without conflicts.
205+
206+
## 3. Backup Scheduler Enhancements
207+
208+
### Dual Backup Mechanisms
209+
210+
#### Auto Backup (Snapshot) Functionality
211+
212+
- **Trigger:**
213+
- Activated via the `schedule()` method using a debounced function.
214+
- **Operation:**
215+
- Creates snapshot files named `auto-backup-<index>.json`.
216+
- Additionally, it makes daily and weekly copies to `daily.json` and `weekly.json`.
217+
218+
#### Auto Dump Functionality
219+
220+
- **Operation:**
221+
- Independently dumps the entire Y.Doc state to a file.
222+
- The dump file is named `database.yjs.db` (instead of `database.db`).
223+
- This dump is performed every `SYNC_INTERVAL` milliseconds.
224+
- **Configurability:**
225+
- The interval is configurable via the constant `SYNC_INTERVAL` (default set to 10,000 ms).
226+
227+
### Directory Updates
228+
229+
- **updateBackupDirectory():**
230+
- Updates the backup directory to a new base path while preserving the structure `<newDir>/database/<sessionId>`.
231+
- Restarts the auto dump functionality so that future dumps are written to the new location.
232+
233+
## 4. Restoration Process
234+
235+
- **restoreFromFile():**
236+
- Clears the persistent y-indexeddb storage.
237+
- Destroys the current Y.Doc and creates a new one.
238+
- Applies the backup update from the file.
239+
- Reinitializes the y-indexeddb provider and reloads the application once syncing is complete.
240+
241+
## 5. Configurability and Constants
242+
243+
- **SYNC_INTERVAL:**
244+
- A constant defined at the top of the backup scheduler file that controls the auto dump interval.
245+
- This allows for easy adjustment of the dump frequency.
246+
247+
## Summary
248+
249+
- **Unique Session Identification:**
250+
- Each installation is uniquely identified using a UUID stored in `session.json` and retrieved via IPC.
251+
- **Session-Specific Backup Storage:**
252+
- Backups are organized under `<baseBackupDirectory>/database/<sessionId>/`, ensuring isolation and safe syncing.
253+
- **Dual Backup Mechanisms:**
254+
- The scheduler provides both periodic JSON snapshot backups (with daily/weekly copies) and continuous Y.Doc dumps (to `database.yjs.db`), with the dump interval configurable by `SYNC_INTERVAL`.
255+
- **Robust Restoration:**
256+
- The restoration process clears existing storage, reapplies the backup, and reinitializes the application state seamlessly.
257+
258+
These enhancements provide a robust, flexible, and session-specific backup system that underpins OneFolder’s local-first, cloud-synced design.
259+
260+
--- day #3 end ---
261+
262+
Notes for day 4:
263+
We are getting very close, now we just need to sync with the other sessions.
264+
265+
In the same loop as the dump of the db, we now have to first check the other `uuid`'s databases, and merge them with ours.

src/backend/backup-scheduler.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* `auto-backup-<index>.json` and makes daily/weekly copies.
99
*
1010
* 2. **Auto Dump Functionality:**
11-
* - Independently, every 10 seconds the entire Y.Doc is dumped (exported) to a single file named
12-
* `database.db` inside the backup folder. This file is overwritten on each dump.
11+
* - Independently, every SYNC_INTERVAL milliseconds the entire Y.Doc is dumped (exported) to a single file named
12+
* `database.yjs.db` inside the backup folder. This file is overwritten on each dump.
1313
*
1414
* The folder structure is:
1515
* <baseBackupDirectory>/database/<sessionId>/
@@ -26,6 +26,9 @@ import { debounce } from '../../common/timeout';
2626
import { DataBackup } from '../api/data-backup';
2727
import { AUTO_BACKUP_TIMEOUT, NUM_AUTO_BACKUPS } from './config';
2828

29+
// Configurable dump interval (in milliseconds)
30+
export const SYNC_INTERVAL = 10000; // Change this constant to adjust the auto dump interval
31+
2932
/** Returns the date at 00:00 today */
3033
function getToday(): Date {
3134
const today = new Date();
@@ -178,19 +181,19 @@ export default class BackupScheduler implements DataBackup {
178181

179182
/**
180183
* **Auto Dump Functionality:**
181-
* Starts an interval that dumps the entire Y.Doc state to a file named "database.db"
182-
* every 10 seconds. The file is overwritten on each dump.
184+
* Starts an interval that dumps the entire Y.Doc state to a file named "database.yjs.db"
185+
* every SYNC_INTERVAL milliseconds. The file is overwritten on each dump.
183186
*/
184187
#startAutoDump(): void {
185-
const dumpFilePath = path.join(this.#backupDirectory, 'database.db');
188+
const dumpFilePath = path.join(this.#backupDirectory, 'database.yjs.db');
186189
this.#dumpInterval = setInterval(async () => {
187190
try {
188191
await this.backupToFile(dumpFilePath);
189192
console.log('Auto-dumped database to', dumpFilePath);
190193
} catch (error) {
191194
console.error('Error during auto dump:', error);
192195
}
193-
}, 10000); // every 10 seconds
196+
}, SYNC_INTERVAL);
194197
}
195198

196199
/**

0 commit comments

Comments
 (0)