diff --git a/lib/storage/drift/blob_store.dart b/lib/storage/drift/blob_store.dart index 5f75f9f7..e6bc8686 100644 --- a/lib/storage/drift/blob_store.dart +++ b/lib/storage/drift/blob_store.dart @@ -243,6 +243,179 @@ class BlobStore { return rows.map((r) => r.read(key)!).toSet(); } + /// Holds the set of store keys whose blob has been moved into rows. + /// + /// A separate marker rather than "this key has rows" because those are not + /// the same statement. A key can have rows and an incomplete migration at + /// the same time, and treating rows as proof of completeness is how a + /// half-migrated history would read as a whole one. + static const String migratedMarkerKey = '__msgrows_migrated'; + + Set? _migratedKeys; + + Future> _loadMigratedKeys() async { + if (_migratedKeys != null) return _migratedKeys!; + final raw = await read(migratedMarkerKey); + if (raw == null || raw.isEmpty) return _migratedKeys = {}; + try { + return _migratedKeys = (jsonDecode(raw) as List) + .cast() + .toSet(); + } catch (e) { + // Refuse to guess. An unreadable marker means every key is treated as + // unmigrated, which is the safe direction: the blobs are all still there. + appLogger.error( + 'Could not decode the row-migration marker, treating every key as ' + 'unmigrated (the blobs are intact): $e', + tag: 'Storage', + ); + return _migratedKeys = {}; + } + } + + Future _markMigrated(String storeKey) async { + await synchronized(migratedMarkerKey, () async { + final keys = await _loadMigratedKeys(); + if (keys.contains(storeKey)) return; + keys.add(storeKey); + await write(migratedMarkerKey, jsonEncode(keys.toList())); + }); + } + + /// Whether [storeKey] reads from rows. False means its blob is still the + /// source of truth. + Future isOnMessageRows(String storeKey) async => + (await _loadMigratedKeys()).contains(storeKey); + + @visibleForTesting + void resetMigratedKeyCache() => _migratedKeys = null; + + /// Moves [storeKey]'s blob into rows, once, and reports whether the key is + /// now on rows (#920). + /// + /// Idempotent and safe to call on every save and load: a key already marked + /// migrated returns immediately from an in-memory set. + /// + /// The gate, in order: + /// + /// 1. No blob, or an empty one, means there is nothing to move. The key is + /// marked migrated and starts on rows. + /// 2. The blob's top-level element count is measured **independently of + /// [toRows]**, by a decode that does nothing but count. Asking the + /// converter how many records it saw would be a check that cannot fail; + /// the first version of this did exactly that and a deliberately lossy + /// converter sailed through it. + /// 3. [toRows] decodes the blob **off the UI isolate**, because it is the + /// same multi-megabyte decode #919 moved. If it throws, nothing is + /// written and the key stays on the blob. It must return one row per + /// source element; a shortfall against step 2 refuses the migration. + /// 4. The rows are inserted in one transaction, so a failure part-way leaves + /// no partial set behind. + /// 5. The stored row count is read back and compared to the number of + /// DISTINCT merge keys. Only an exact match marks the key migrated. + /// + /// Steps 2 and 5 together close the loop from blob element to stored row, + /// and either can fail on its own. + /// + /// **The blob is never deleted**, not even on a verified match. Rows become + /// the read and write path and the blob is left untouched. Reclaiming that + /// space is deliberately a separate, owner-gated step: the moment the delete + /// runs is the moment a mistake stops being recoverable. + /// + /// A mismatch keeps the key on the blob path, which still works, and says so + /// loudly. It does not retry: a count that did not match once will not match + /// on a loop, and a retry would just hammer the same keys. + Future ensureMessageRowsMigrated( + String storeKey, + BlobToRowsCallback toRows, + ) async { + if (await isOnMessageRows(storeKey)) return true; + + return synchronized(storeKey, () async { + // Re-check inside the lock: a concurrent save may have migrated it while + // this call waited its turn. + if (await isOnMessageRows(storeKey)) return true; + + final blob = await readWithPrefsFallback(storeKey); + if (blob == null || blob.isEmpty) { + await _markMigrated(storeKey); + return true; + } + + final int sourceCount; + final List rows; + try { + sourceCount = await compute(countJsonArrayElements, blob); + rows = await compute(toRows, blob); + } catch (e, stack) { + appLogger.error( + 'Row migration of $storeKey FAILED while decoding its ${blob.length} ' + 'char blob: $e. Nothing was written and the key still reads from the ' + 'blob.\n$stack', + tag: 'Storage', + ); + return false; + } + + if (rows.length != sourceCount) { + appLogger.error( + 'Row migration of $storeKey REFUSED: the blob holds $sourceCount ' + 'records but the converter produced ${rows.length} rows. Nothing ' + 'was written. The blob is kept and still the source of truth.', + tag: 'Storage', + ); + return false; + } + + final deduped = {}; + for (final r in rows) { + deduped[r.mergeKey] = r; + } + if (deduped.length != rows.length) { + appLogger.warn( + 'Row migration of $storeKey: the blob held ${rows.length} records ' + 'but only ${deduped.length} distinct identities, so ' + '${rows.length - deduped.length} were duplicates. Comparing against ' + 'the distinct count.', + tag: 'Storage', + ); + } + + try { + await upsertMessageRows(storeKey, deduped.values); + } catch (e, stack) { + appLogger.error( + 'Row migration of $storeKey FAILED while inserting ' + '${deduped.length} rows: $e. The blob is untouched and still the ' + 'source of truth for this key.\n$stack', + tag: 'Storage', + ); + return false; + } + + final stored = await messageRowCount(storeKey); + if (stored != deduped.length) { + appLogger.error( + 'Row migration of $storeKey REFUSED: read back $stored rows but ' + 'expected ${deduped.length}. The blob is kept, nothing is deleted, ' + 'and this key keeps reading from the blob. Rows written so far are ' + 'left in place and are harmless because the key is not marked ' + 'migrated.', + tag: 'Storage', + ); + return false; + } + + await _markMigrated(storeKey); + appLogger.info( + 'Row migration of $storeKey: ${deduped.length} messages now stored as ' + 'rows. The blob is retained untouched (#920).', + tag: 'Storage', + ); + return true; + }); + } + /// Read-modify-write for a bulk key with the decode, merge and re-encode run /// **off the UI isolate** (#919). /// @@ -517,6 +690,23 @@ class BlobStore { } } +/// Counts the top-level elements of a JSON array, and nothing else. +/// +/// Exists so [BlobStore.ensureMessageRowsMigrated] can learn how many records +/// a blob holds without asking the converter it is checking. Top-level so +/// `compute` can send it. +int countJsonArrayElements(String json) => + (jsonDecode(json) as List).length; + +/// Turns one history blob into per-message rows, run on another isolate. +/// +/// Must be a top-level or static function so `compute` can send it. Takes the +/// raw blob JSON and returns one [MessageRowInput] per message. Throwing is the +/// correct response to an undecodable blob: [BlobStore.ensureMessageRowsMigrated] +/// turns that into a refused migration and leaves the blob as the source of +/// truth. +typedef BlobToRowsCallback = List Function(String blobJson); + /// One message on its way into [MessageRecords] (#920). /// /// A plain record rather than a drift companion so the stores can build it diff --git a/test/storage/message_rows_migration_test.dart b/test/storage/message_rows_migration_test.dart new file mode 100644 index 00000000..7a87ff7f --- /dev/null +++ b/test/storage/message_rows_migration_test.dart @@ -0,0 +1,245 @@ +import 'dart:convert'; + +import 'package:drift/native.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/storage/drift/blob_store.dart'; +import 'package:meshcore_open/storage/drift/offband_database.dart'; +import 'package:meshcore_open/storage/prefs_manager.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +/// #920 B3: moving a history blob into rows. The gate matters more than the +/// move: every failure path here has to leave the blob as the source of truth, +/// because the blob is the only copy of someone's messages. +void main() { + late OffbandDatabase db; + late BlobStore blobs; + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + db = OffbandDatabase(NativeDatabase.memory()); + blobs = BlobStore(db); + BlobStore.overrideForTest(blobs); + }); + + tearDown(() async { + await db.close(); + BlobStore.clearTestOverride(); + }); + + String blobOf(List ids) => jsonEncode([ + for (final i in ids) {'messageId': 'id$i', 'timestamp': 1000 + i}, + ]); + + test('moves a blob into rows and marks the key migrated', () async { + await blobs.write('k', blobOf([1, 2, 3])); + + final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + expect(ok, isTrue); + expect(await blobs.messageRowCount('k'), 3); + expect(await blobs.isOnMessageRows('k'), isTrue); + }); + + test('NEVER deletes the blob, even on a verified match', () async { + final original = blobOf([1, 2]); + await blobs.write('k', original); + + await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + expect( + await blobs.read('k'), + original, + reason: 'reclaiming the space is a separate owner-gated step (#920)', + ); + }); + + test('rows carry the payload through unchanged', () async { + await blobs.write('k', blobOf([7])); + + await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + expect(await blobs.messagePayloads('k'), [ + '{"messageId":"id7","timestamp":1007}', + ]); + }); + + test('a second call is a no-op and does not re-import', () async { + await blobs.write('k', blobOf([1, 2])); + await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + // Something arrives and is saved as a row after the migration. + await blobs.upsertMessageRows('k', const [ + MessageRowInput(mergeKey: 'id:new', timestampMs: 9999, payload: '{}'), + ]); + final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + expect(ok, isTrue); + expect( + await blobs.messageRowCount('k'), + 3, + reason: 'a repeat migration must not touch the rows that exist', + ); + }); + + test('an absent blob marks the key migrated with no rows', () async { + final ok = await blobs.ensureMessageRowsMigrated('fresh', rowsFromTestBlob); + + expect(ok, isTrue); + expect(await blobs.isOnMessageRows('fresh'), isTrue); + expect(await blobs.messageRowCount('fresh'), 0); + }); + + test('an empty blob marks the key migrated with no rows', () async { + await blobs.write('k', ''); + + final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + expect(ok, isTrue); + expect(await blobs.isOnMessageRows('k'), isTrue); + }); + + group('failure paths all keep the blob authoritative', () { + test('an undecodable blob refuses the migration', () async { + const corrupt = '{not json at all'; + await blobs.write('k', corrupt); + + final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + expect(ok, isFalse); + expect(await blobs.isOnMessageRows('k'), isFalse); + expect(await blobs.read('k'), corrupt); + expect(await blobs.messageRowCount('k'), 0); + }); + + test('a decoder that throws refuses the migration', () async { + await blobs.write('k', blobOf([1])); + + final ok = await blobs.ensureMessageRowsMigrated('k', _alwaysThrows); + + expect(ok, isFalse); + expect(await blobs.isOnMessageRows('k'), isFalse); + expect(await blobs.read('k'), blobOf([1])); + }); + + test('a decoder that drops records refuses on the count gate', () async { + // The case the readback gate exists for: the decode "succeeded" but + // produced fewer messages than the blob held. Marking this migrated + // would silently shorten someone's history. + await blobs.write('k', blobOf([1, 2, 3, 4, 5])); + + final ok = await blobs.ensureMessageRowsMigrated('k', _dropsOneRecord); + + expect( + ok, + isFalse, + reason: 'fewer rows than records must never be accepted', + ); + expect(await blobs.isOnMessageRows('k'), isFalse); + expect( + await blobs.read('k'), + blobOf([1, 2, 3, 4, 5]), + reason: 'the blob is the only copy of those messages', + ); + }); + + test('a refused key can still be read from its blob afterwards', () async { + await blobs.write('k', blobOf([1, 2])); + await blobs.ensureMessageRowsMigrated('k', _alwaysThrows); + + // The whole point of refusing rather than throwing: the app keeps + // working on the old path. + expect(await blobs.read('k'), blobOf([1, 2])); + expect(await blobs.isOnMessageRows('k'), isFalse); + }); + }); + + test('duplicate identities in a blob are deduped, not refused', () async { + // A blob built by the old merge should not contain duplicates, but the + // legacy-key adoption path could have produced them. Comparing against + // the DISTINCT count means that does not read as data loss. + await blobs.write( + 'k', + jsonEncode([ + {'messageId': 'id1', 'timestamp': 1001}, + {'messageId': 'id1', 'timestamp': 1001}, + {'messageId': 'id2', 'timestamp': 1002}, + ]), + ); + + final ok = await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + expect(ok, isTrue); + expect(await blobs.messageRowCount('k'), 2); + }); + + test('the marker survives a reopen of the same database', () async { + await blobs.write('k', blobOf([1])); + await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + + // Same database, fresh in-memory cache: the marker has to be on disk. + blobs.resetMigratedKeyCache(); + + expect(await blobs.isOnMessageRows('k'), isTrue); + }); + + test('an unreadable marker treats every key as unmigrated', () async { + await blobs.write('k', blobOf([1])); + await blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob); + await blobs.write(BlobStore.migratedMarkerKey, 'not json'); + blobs.resetMigratedKeyCache(); + + // Safe direction: the blobs are all still there, so re-reading them is + // correct, where trusting a corrupt marker would hide histories. + expect(await blobs.isOnMessageRows('k'), isFalse); + }); + + test('migrating one key does not mark or touch another', () async { + await blobs.write('a', blobOf([1, 2])); + await blobs.write('b', blobOf([3])); + + await blobs.ensureMessageRowsMigrated('a', rowsFromTestBlob); + + expect(await blobs.isOnMessageRows('b'), isFalse); + expect(await blobs.messageRowCount('b'), 0); + expect(await blobs.read('b'), blobOf([3])); + }); + + test('concurrent calls on one key import it exactly once', () async { + await blobs.write('k', blobOf([1, 2, 3])); + + final results = await Future.wait([ + blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob), + blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob), + blobs.ensureMessageRowsMigrated('k', rowsFromTestBlob), + ]); + + expect(results, everyElement(isTrue)); + expect(await blobs.messageRowCount('k'), 3); + }); +} + +/// Stands in for a store's real decoder. Top-level so `compute` can send it. +List rowsFromTestBlob(String blobJson) { + return (jsonDecode(blobJson) as List) + .map((e) => e as Map) + .map( + (m) => MessageRowInput( + mergeKey: 'id:${m['messageId']}', + timestampMs: m['timestamp'] as int, + payload: jsonEncode(m), + ), + ) + .toList(); +} + +List _alwaysThrows(String blobJson) => + throw StateError('decoder blew up'); + +/// Silently loses the last record, which is exactly what the readback gate is +/// there to catch. +List _dropsOneRecord(String blobJson) { + final all = rowsFromTestBlob(blobJson); + return all.sublist(0, all.length - 1); +}