Matthias Nott
8 days ago a4f64fec47b8cafb638d6643df2b56b9e152d3d3
lib/services/message_store.dart
....@@ -64,33 +64,18 @@
6464 final logFile = File(_logPath(dir));
6565 final indexFile = File(_indexPath(dir));
6666
67
- // Try loading saved index first (fast path).
68
- if (indexFile.existsSync()) {
69
- try {
70
- final raw = indexFile.readAsStringSync();
71
- final decoded = jsonDecode(raw) as Map<String, dynamic>;
72
- for (final entry in decoded.entries) {
73
- _index[entry.key] =
74
- (entry.value as List<dynamic>).map((e) => e as int).toList();
75
- }
76
- } catch (_) {
77
- _index.clear();
78
- }
79
- }
80
-
81
- // Count actual lines in log to set _lineCount.
67
+ // Always rebuild index from log (the saved index.json may be stale
68
+ // if the app was killed before a flush).
8269 if (logFile.existsSync()) {
8370 final content = logFile.readAsStringSync();
8471 _lineCount = content.isEmpty
8572 ? 0
8673 : content.trimRight().split('\n').length;
74
+ if (_lineCount > 0) {
75
+ await _rebuildIndex(logFile);
76
+ }
8777 } else {
8878 _lineCount = 0;
89
- }
90
-
91
- // If the index was missing or corrupt, rebuild from log.
92
- if (_index.isEmpty && _lineCount > 0) {
93
- await _rebuildIndex(logFile);
9479 }
9580
9681 TraceService.instance.addTrace(
....@@ -143,15 +128,15 @@
143128 logFile.writeAsStringSync(line, mode: FileMode.append);
144129
145130 // Update in-memory index.
146
- _index.putIfAbsent(sessionId, () => []).add(_lineCount);
131
+ final lineNum = _lineCount;
132
+ _index.putIfAbsent(sessionId, () => []).add(lineNum);
147133 _lineCount++;
148134
149
- // Periodically flush index to disk.
150
- _appendsSinceFlush++;
151
- if (_appendsSinceFlush >= _indexFlushInterval) {
152
- _flushIndex(dir);
153
- _appendsSinceFlush = 0;
154
- }
135
+ TraceService.instance.addTrace('MsgStoreV2 APPEND',
136
+ '${sessionId.substring(0, 8)} line=$lineNum total=$_lineCount idx=${_index[sessionId]?.length ?? 0}');
137
+
138
+ // Flush index after every append to prevent data loss on app kill.
139
+ _flushIndex(dir);
155140 } catch (e) {
156141 TraceService.instance.addTrace('MsgStoreV2 APPEND ERROR', '$e');
157142 }
....@@ -173,6 +158,8 @@
173158
174159 // Read all lines at once then pick the ones we need.
175160 final allLines = logFile.readAsLinesSync();
161
+ TraceService.instance.addTrace('MsgStoreV2 LOAD detail',
162
+ '${sessionId.substring(0, 8)}: fileLines=${allLines.length} indexEntries=${lineNumbers.length} lineCount=$_lineCount');
176163 final messages = <Message>[];
177164
178165 for (final n in lineNumbers) {