perf(inventory): lazy list, photo thumbnails, indexes, debounced reload

Scale the local inventory to 10k+ varieties:
- Render the list with ListView.builder over a flattened header/item model
  instead of building every tile upfront.
- Store a small regenerable JPEG thumbnail per photo (schema v14) and use it
  for the 48px list avatar; full bytes stay for offer image hosting. Existing
  photos are backfilled lazily at startup. Thumbnail is local-only (excluded
  from CRDT sync and backups by the JSON codec).
- Add indexes on varieties(is_deleted,is_draft), attachments(parent_type,
  parent_id,kind), lots(variety_id) via @TableIndex.
- Debounce watchInventoryView (~250ms) so a burst of table writes triggers one
  reload, not seven.
- cacheWidth/cacheHeight on the list avatar decode.
- Scale test raised 3k -> 10k; migration test v13 -> v14.
This commit is contained in:
vjrj 2026-07-20 17:48:37 +02:00
parent 2884ddd3c7
commit 3de01bd948
12 changed files with 5114 additions and 34 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:async/async.dart'; import 'package:async/async.dart';
import 'package:commons_core/commons_core.dart'; import 'package:commons_core/commons_core.dart';
import 'package:drift/drift.dart'; import 'package:drift/drift.dart';
@ -652,13 +654,27 @@ class VarietyRepository {
required this.idGen, required this.idGen,
required this.nodeId, required this.nodeId,
int Function()? nowMillis, int Function()? nowMillis,
Uint8List? Function(Uint8List)? thumbnailBuilder,
}) : _now = nowMillis ?? (() => DateTime.now().millisecondsSinceEpoch), }) : _now = nowMillis ?? (() => DateTime.now().millisecondsSinceEpoch),
_thumbnailBuilder = thumbnailBuilder,
_clock = Hlc.zero(nodeId); _clock = Hlc.zero(nodeId);
final AppDatabase _db; final AppDatabase _db;
final IdGen idGen; final IdGen idGen;
final String nodeId; final String nodeId;
final int Function() _now; final int Function() _now;
/// Builds the small list-avatar thumbnail from a full photo. Injected (from
/// `services/offer_thumbnail.dart` in production) so the data layer stays free
/// of the image codec and unit tests run without decoding. Null no
/// thumbnail is stored and the list falls back to the full photo.
final Uint8List? Function(Uint8List)? _thumbnailBuilder;
/// The thumbnail for [photoBytes], or null when no builder is wired or the
/// bytes aren't decodable. Wrapped in a Value for direct use in a companion.
Value<Uint8List?> _thumbValue(Uint8List photoBytes) =>
Value(_thumbnailBuilder?.call(photoBytes));
Hlc _clock; Hlc _clock;
/// Emits the non-deleted inventory, ordered by category then label, each with /// Emits the non-deleted inventory, ordered by category then label, each with
@ -696,7 +712,11 @@ class VarietyRepository {
_db.select(_db.species).watch().map((_) {}), _db.select(_db.species).watch().map((_) {}),
_db.select(_db.lots).watch().map((_) {}), _db.select(_db.lots).watch().map((_) {}),
]); ]);
return triggers.asyncMap( // Coalesce bursts: a single quick-add or handover touches several of these
// tables at once, and each would otherwise re-run the full (7-query) load.
// Debouncing collapses the burst into one reload decisive with a large
// inventory.
return _debounce(triggers, const Duration(milliseconds: 250)).asyncMap(
(_) async => (items: await _loadInventory(), drafts: await _loadDrafts()), (_) async => (items: await _loadInventory(), drafts: await _loadDrafts()),
); );
} }
@ -929,7 +949,10 @@ class VarietyRepository {
return rows.map((l) => l.varietyId).toSet(); return rows.map((l) => l.varietyId).toSet();
} }
/// Loads the first photo BLOB for each of [varietyIds] (one query). /// Loads the first photo's small [thumbnail] for each of [varietyIds] (one
/// query) for the inventory-list avatar. Falls back to the full-resolution
/// [bytes] only when a thumbnail hasn't been generated yet (older rows,
/// pending lazy backfill), so the list stays correct meanwhile.
Future<Map<String, Uint8List>> _firstPhotosFor( Future<Map<String, Uint8List>> _firstPhotosFor(
List<String> varietyIds, List<String> varietyIds,
) async { ) async {
@ -950,17 +973,69 @@ class VarietyRepository {
.get(); .get();
final byVariety = <String, Uint8List>{}; final byVariety = <String, Uint8List>{};
for (final row in rows) { for (final row in rows) {
final bytes = row.bytes; final image = row.thumbnail ?? row.bytes;
if (bytes != null) byVariety.putIfAbsent(row.parentId, () => bytes); if (image != null) byVariety.putIfAbsent(row.parentId, () => image);
} }
return byVariety; return byVariety;
} }
/// Generates the missing list thumbnails for photos that predate the
/// thumbnail column (or arrived via sync/backup restore, which never carry
/// one). Processes in small batches so decoding doesn't block the UI; safe to
/// call at startup as fire-and-forget. No-op when no thumbnail builder is
/// wired. Returns how many thumbnails were written.
Future<int> backfillThumbnails({int batchSize = 20}) async {
final build = _thumbnailBuilder;
if (build == null) return 0;
var written = 0;
while (true) {
final batch =
await (_db.select(_db.attachments)
..where(
(a) =>
a.kind.equalsValue(AttachmentKind.photo) &
a.isDeleted.equals(false) &
a.thumbnail.isNull() &
a.bytes.isNotNull(),
)
..limit(batchSize))
.get();
if (batch.isEmpty) break;
for (final row in batch) {
final thumb = build(row.bytes!);
// No decodable image store the full bytes as the "thumbnail" so this
// row isn't re-scanned forever. It's rare (corrupt photo) and still
// bounded by the avatar's cacheWidth at render time.
await (_db.update(_db.attachments)..where((a) => a.id.equals(row.id)))
.write(AttachmentsCompanion(thumbnail: Value(thumb ?? row.bytes)));
written++;
}
if (batch.length < batchSize) break;
}
return written;
}
/// The cover photo (lowest `sortOrder`) for a single variety, or null when it /// The cover photo (lowest `sortOrder`) for a single variety, or null when it
/// has none. Used by the publish step to host an offer's image; reuses the /// has none. Used by the publish step to host an offer's image, so it returns
/// same "first photo" rule as the inventory avatar. /// the FULL-resolution [bytes] (not the list thumbnail).
Future<Uint8List?> coverPhotoFor(String varietyId) async => Future<Uint8List?> coverPhotoFor(String varietyId) async {
(await _firstPhotosFor([varietyId]))[varietyId]; final rows =
await (_db.select(_db.attachments)
..where(
(a) =>
a.parentId.equals(varietyId) &
a.parentType.equalsValue(ParentType.variety) &
a.kind.equalsValue(AttachmentKind.photo) &
a.isDeleted.equals(false),
)
..orderBy([
(a) => OrderingTerm(expression: a.sortOrder),
(a) => OrderingTerm(expression: a.createdAt),
])
..limit(1))
.get();
return rows.isEmpty ? null : rows.first.bytes;
}
/// Maps each of [speciesIds] to its scientific name (one query). /// Maps each of [speciesIds] to its scientific name (one query).
Future<Map<String, String>> _scientificNamesFor( Future<Map<String, String>> _scientificNamesFor(
@ -1040,6 +1115,7 @@ class VarietyRepository {
parentId: varietyId, parentId: varietyId,
kind: AttachmentKind.photo, kind: AttachmentKind.photo,
bytes: Value(photoBytes), bytes: Value(photoBytes),
thumbnail: _thumbValue(photoBytes),
mimeType: const Value('image/jpeg'), mimeType: const Value('image/jpeg'),
), ),
); );
@ -1080,6 +1156,7 @@ class VarietyRepository {
parentId: varietyId, parentId: varietyId,
kind: AttachmentKind.photo, kind: AttachmentKind.photo,
bytes: Value(photoBytes), bytes: Value(photoBytes),
thumbnail: _thumbValue(photoBytes),
mimeType: const Value('image/jpeg'), mimeType: const Value('image/jpeg'),
), ),
); );
@ -1795,6 +1872,7 @@ class VarietyRepository {
parentId: varietyId, parentId: varietyId,
kind: AttachmentKind.photo, kind: AttachmentKind.photo,
bytes: Value(bytes), bytes: Value(bytes),
thumbnail: _thumbValue(bytes),
mimeType: const Value('image/jpeg'), mimeType: const Value('image/jpeg'),
), ),
); );
@ -3006,3 +3084,46 @@ class VarietyRepository {
return maxPacked == null ? null : Hlc.parse(maxPacked); return maxPacked == null ? null : Hlc.parse(maxPacked);
} }
} }
/// Emits the latest event from [source] only once [duration] has elapsed with
/// no newer event collapsing a burst of rapid change-triggers into a single
/// downstream reload. Trailing-edge; single-subscription (matches the merged
/// Drift trigger stream it wraps).
Stream<T> _debounce<T>(Stream<T> source, Duration duration) {
late StreamController<T> controller;
StreamSubscription<T>? sub;
Timer? timer;
T? pending;
var hasPending = false;
void flush() {
if (hasPending) {
hasPending = false;
controller.add(pending as T);
}
}
controller = StreamController<T>(
onListen: () {
sub = source.listen(
(value) {
pending = value;
hasPending = true;
timer?.cancel();
timer = Timer(duration, flush);
},
onError: controller.addError,
onDone: () {
timer?.cancel();
flush();
controller.close();
},
);
},
onCancel: () async {
timer?.cancel();
await sub?.cancel();
},
);
return controller.stream;
}

View file

@ -32,7 +32,7 @@ class AppDatabase extends _$AppDatabase {
/// Current schema version; also stamped into interchange exports so an /// Current schema version; also stamped into interchange exports so an
/// importer knows which app generation wrote the file (data-model §7). /// importer knows which app generation wrote the file (data-model §7).
static const int currentSchemaVersion = 13; static const int currentSchemaVersion = 14;
@override @override
int get schemaVersion => currentSchemaVersion; int get schemaVersion => currentSchemaVersion;
@ -196,9 +196,40 @@ class AppDatabase extends _$AppDatabase {
await m.createTable(gardenOutcomes); await m.createTable(gardenOutcomes);
} }
} }
// v14: scalability for large inventories. A local, regenerable [thumbnail]
// BLOB on attachments (so the list never decodes full-resolution photos)
// plus indexes on the columns the inventory list filters/joins on. All
// additive and guarded, so a half-migrated dev database re-runs cleanly
// (see the v7 note above). Existing thumbnails are backfilled lazily in
// the background, not here (image decoding in a migration is slow/fragile).
if (from < 14) {
if (!await _hasColumn('attachments', 'thumbnail')) {
await m.addColumn(attachments, attachments.thumbnail);
}
// Declared as `@TableIndex` on the tables, so a fresh install gets them
// via createAll(); existing databases get them here. Guarded against a
// half-migrated dev database (see the v7 note above).
for (final index in [
idxVarietiesDeletedDraft,
idxAttachmentsParent,
idxLotsVariety,
]) {
if (!await _hasIndex(index.entityName)) await m.create(index);
}
}
}, },
); );
/// Whether an index named [name] already exists keeps the additive v14
/// index creation idempotent against partially-migrated databases.
Future<bool> _hasIndex(String name) async {
final rows = await customSelect(
"SELECT 1 FROM sqlite_master WHERE type = 'index' AND name = ?",
variables: [Variable.withString(name)],
).get();
return rows.isNotEmpty;
}
/// Whether a table named [table] already exists. Keeps the additive v8 /// Whether a table named [table] already exists. Keeps the additive v8
/// table creation idempotent against partially-migrated databases. /// table creation idempotent against partially-migrated databases.
Future<bool> _hasTable(String table) async { Future<bool> _hasTable(String table) async {

View file

@ -7975,6 +7975,17 @@ class $AttachmentsTable extends Attachments
type: DriftSqlType.blob, type: DriftSqlType.blob,
requiredDuringInsert: false, requiredDuringInsert: false,
); );
static const VerificationMeta _thumbnailMeta = const VerificationMeta(
'thumbnail',
);
@override
late final GeneratedColumn<Uint8List> thumbnail = GeneratedColumn<Uint8List>(
'thumbnail',
aliasedName,
true,
type: DriftSqlType.blob,
requiredDuringInsert: false,
);
static const VerificationMeta _mimeTypeMeta = const VerificationMeta( static const VerificationMeta _mimeTypeMeta = const VerificationMeta(
'mimeType', 'mimeType',
); );
@ -8011,6 +8022,7 @@ class $AttachmentsTable extends Attachments
kind, kind,
uri, uri,
bytes, bytes,
thumbnail,
mimeType, mimeType,
sortOrder, sortOrder,
]; ];
@ -8090,6 +8102,12 @@ class $AttachmentsTable extends Attachments
bytes.isAcceptableOrUnknown(data['bytes']!, _bytesMeta), bytes.isAcceptableOrUnknown(data['bytes']!, _bytesMeta),
); );
} }
if (data.containsKey('thumbnail')) {
context.handle(
_thumbnailMeta,
thumbnail.isAcceptableOrUnknown(data['thumbnail']!, _thumbnailMeta),
);
}
if (data.containsKey('mime_type')) { if (data.containsKey('mime_type')) {
context.handle( context.handle(
_mimeTypeMeta, _mimeTypeMeta,
@ -8159,6 +8177,10 @@ class $AttachmentsTable extends Attachments
DriftSqlType.blob, DriftSqlType.blob,
data['${effectivePrefix}bytes'], data['${effectivePrefix}bytes'],
), ),
thumbnail: attachedDatabase.typeMapping.read(
DriftSqlType.blob,
data['${effectivePrefix}thumbnail'],
),
mimeType: attachedDatabase.typeMapping.read( mimeType: attachedDatabase.typeMapping.read(
DriftSqlType.string, DriftSqlType.string,
data['${effectivePrefix}mime_type'], data['${effectivePrefix}mime_type'],
@ -8193,6 +8215,13 @@ class Attachment extends DataClass implements Insertable<Attachment> {
final AttachmentKind kind; final AttachmentKind kind;
final String? uri; final String? uri;
final Uint8List? bytes; final Uint8List? bytes;
/// A small JPEG thumbnail of [bytes] (photos only), decoded once on save so
/// the inventory list never has to decode the full-resolution photo for a
/// 48px avatar. Purely derived, local and regenerable it is EXCLUDED from
/// CRDT sync payloads and backups (see [SyncColumns] usage); a peer or a
/// restored backup regenerates it lazily via `backfillThumbnails`.
final Uint8List? thumbnail;
final String? mimeType; final String? mimeType;
/// Display order among sibling attachments (lower first). The lowest-ordered /// Display order among sibling attachments (lower first). The lowest-ordered
@ -8212,6 +8241,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
required this.kind, required this.kind,
this.uri, this.uri,
this.bytes, this.bytes,
this.thumbnail,
this.mimeType, this.mimeType,
required this.sortOrder, required this.sortOrder,
}); });
@ -8241,6 +8271,9 @@ class Attachment extends DataClass implements Insertable<Attachment> {
if (!nullToAbsent || bytes != null) { if (!nullToAbsent || bytes != null) {
map['bytes'] = Variable<Uint8List>(bytes); map['bytes'] = Variable<Uint8List>(bytes);
} }
if (!nullToAbsent || thumbnail != null) {
map['thumbnail'] = Variable<Uint8List>(thumbnail);
}
if (!nullToAbsent || mimeType != null) { if (!nullToAbsent || mimeType != null) {
map['mime_type'] = Variable<String>(mimeType); map['mime_type'] = Variable<String>(mimeType);
} }
@ -8263,6 +8296,9 @@ class Attachment extends DataClass implements Insertable<Attachment> {
bytes: bytes == null && nullToAbsent bytes: bytes == null && nullToAbsent
? const Value.absent() ? const Value.absent()
: Value(bytes), : Value(bytes),
thumbnail: thumbnail == null && nullToAbsent
? const Value.absent()
: Value(thumbnail),
mimeType: mimeType == null && nullToAbsent mimeType: mimeType == null && nullToAbsent
? const Value.absent() ? const Value.absent()
: Value(mimeType), : Value(mimeType),
@ -8291,6 +8327,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
), ),
uri: serializer.fromJson<String?>(json['uri']), uri: serializer.fromJson<String?>(json['uri']),
bytes: serializer.fromJson<Uint8List?>(json['bytes']), bytes: serializer.fromJson<Uint8List?>(json['bytes']),
thumbnail: serializer.fromJson<Uint8List?>(json['thumbnail']),
mimeType: serializer.fromJson<String?>(json['mimeType']), mimeType: serializer.fromJson<String?>(json['mimeType']),
sortOrder: serializer.fromJson<int>(json['sortOrder']), sortOrder: serializer.fromJson<int>(json['sortOrder']),
); );
@ -8314,6 +8351,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
), ),
'uri': serializer.toJson<String?>(uri), 'uri': serializer.toJson<String?>(uri),
'bytes': serializer.toJson<Uint8List?>(bytes), 'bytes': serializer.toJson<Uint8List?>(bytes),
'thumbnail': serializer.toJson<Uint8List?>(thumbnail),
'mimeType': serializer.toJson<String?>(mimeType), 'mimeType': serializer.toJson<String?>(mimeType),
'sortOrder': serializer.toJson<int>(sortOrder), 'sortOrder': serializer.toJson<int>(sortOrder),
}; };
@ -8331,6 +8369,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
AttachmentKind? kind, AttachmentKind? kind,
Value<String?> uri = const Value.absent(), Value<String?> uri = const Value.absent(),
Value<Uint8List?> bytes = const Value.absent(), Value<Uint8List?> bytes = const Value.absent(),
Value<Uint8List?> thumbnail = const Value.absent(),
Value<String?> mimeType = const Value.absent(), Value<String?> mimeType = const Value.absent(),
int? sortOrder, int? sortOrder,
}) => Attachment( }) => Attachment(
@ -8345,6 +8384,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
kind: kind ?? this.kind, kind: kind ?? this.kind,
uri: uri.present ? uri.value : this.uri, uri: uri.present ? uri.value : this.uri,
bytes: bytes.present ? bytes.value : this.bytes, bytes: bytes.present ? bytes.value : this.bytes,
thumbnail: thumbnail.present ? thumbnail.value : this.thumbnail,
mimeType: mimeType.present ? mimeType.value : this.mimeType, mimeType: mimeType.present ? mimeType.value : this.mimeType,
sortOrder: sortOrder ?? this.sortOrder, sortOrder: sortOrder ?? this.sortOrder,
); );
@ -8367,6 +8407,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
kind: data.kind.present ? data.kind.value : this.kind, kind: data.kind.present ? data.kind.value : this.kind,
uri: data.uri.present ? data.uri.value : this.uri, uri: data.uri.present ? data.uri.value : this.uri,
bytes: data.bytes.present ? data.bytes.value : this.bytes, bytes: data.bytes.present ? data.bytes.value : this.bytes,
thumbnail: data.thumbnail.present ? data.thumbnail.value : this.thumbnail,
mimeType: data.mimeType.present ? data.mimeType.value : this.mimeType, mimeType: data.mimeType.present ? data.mimeType.value : this.mimeType,
sortOrder: data.sortOrder.present ? data.sortOrder.value : this.sortOrder, sortOrder: data.sortOrder.present ? data.sortOrder.value : this.sortOrder,
); );
@ -8386,6 +8427,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
..write('kind: $kind, ') ..write('kind: $kind, ')
..write('uri: $uri, ') ..write('uri: $uri, ')
..write('bytes: $bytes, ') ..write('bytes: $bytes, ')
..write('thumbnail: $thumbnail, ')
..write('mimeType: $mimeType, ') ..write('mimeType: $mimeType, ')
..write('sortOrder: $sortOrder') ..write('sortOrder: $sortOrder')
..write(')')) ..write(')'))
@ -8405,6 +8447,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
kind, kind,
uri, uri,
$driftBlobEquality.hash(bytes), $driftBlobEquality.hash(bytes),
$driftBlobEquality.hash(thumbnail),
mimeType, mimeType,
sortOrder, sortOrder,
); );
@ -8423,6 +8466,7 @@ class Attachment extends DataClass implements Insertable<Attachment> {
other.kind == this.kind && other.kind == this.kind &&
other.uri == this.uri && other.uri == this.uri &&
$driftBlobEquality.equals(other.bytes, this.bytes) && $driftBlobEquality.equals(other.bytes, this.bytes) &&
$driftBlobEquality.equals(other.thumbnail, this.thumbnail) &&
other.mimeType == this.mimeType && other.mimeType == this.mimeType &&
other.sortOrder == this.sortOrder); other.sortOrder == this.sortOrder);
} }
@ -8439,6 +8483,7 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
final Value<AttachmentKind> kind; final Value<AttachmentKind> kind;
final Value<String?> uri; final Value<String?> uri;
final Value<Uint8List?> bytes; final Value<Uint8List?> bytes;
final Value<Uint8List?> thumbnail;
final Value<String?> mimeType; final Value<String?> mimeType;
final Value<int> sortOrder; final Value<int> sortOrder;
final Value<int> rowid; final Value<int> rowid;
@ -8454,6 +8499,7 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
this.kind = const Value.absent(), this.kind = const Value.absent(),
this.uri = const Value.absent(), this.uri = const Value.absent(),
this.bytes = const Value.absent(), this.bytes = const Value.absent(),
this.thumbnail = const Value.absent(),
this.mimeType = const Value.absent(), this.mimeType = const Value.absent(),
this.sortOrder = const Value.absent(), this.sortOrder = const Value.absent(),
this.rowid = const Value.absent(), this.rowid = const Value.absent(),
@ -8470,6 +8516,7 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
required AttachmentKind kind, required AttachmentKind kind,
this.uri = const Value.absent(), this.uri = const Value.absent(),
this.bytes = const Value.absent(), this.bytes = const Value.absent(),
this.thumbnail = const Value.absent(),
this.mimeType = const Value.absent(), this.mimeType = const Value.absent(),
this.sortOrder = const Value.absent(), this.sortOrder = const Value.absent(),
this.rowid = const Value.absent(), this.rowid = const Value.absent(),
@ -8492,6 +8539,7 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
Expression<String>? kind, Expression<String>? kind,
Expression<String>? uri, Expression<String>? uri,
Expression<Uint8List>? bytes, Expression<Uint8List>? bytes,
Expression<Uint8List>? thumbnail,
Expression<String>? mimeType, Expression<String>? mimeType,
Expression<int>? sortOrder, Expression<int>? sortOrder,
Expression<int>? rowid, Expression<int>? rowid,
@ -8508,6 +8556,7 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
if (kind != null) 'kind': kind, if (kind != null) 'kind': kind,
if (uri != null) 'uri': uri, if (uri != null) 'uri': uri,
if (bytes != null) 'bytes': bytes, if (bytes != null) 'bytes': bytes,
if (thumbnail != null) 'thumbnail': thumbnail,
if (mimeType != null) 'mime_type': mimeType, if (mimeType != null) 'mime_type': mimeType,
if (sortOrder != null) 'sort_order': sortOrder, if (sortOrder != null) 'sort_order': sortOrder,
if (rowid != null) 'rowid': rowid, if (rowid != null) 'rowid': rowid,
@ -8526,6 +8575,7 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
Value<AttachmentKind>? kind, Value<AttachmentKind>? kind,
Value<String?>? uri, Value<String?>? uri,
Value<Uint8List?>? bytes, Value<Uint8List?>? bytes,
Value<Uint8List?>? thumbnail,
Value<String?>? mimeType, Value<String?>? mimeType,
Value<int>? sortOrder, Value<int>? sortOrder,
Value<int>? rowid, Value<int>? rowid,
@ -8542,6 +8592,7 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
kind: kind ?? this.kind, kind: kind ?? this.kind,
uri: uri ?? this.uri, uri: uri ?? this.uri,
bytes: bytes ?? this.bytes, bytes: bytes ?? this.bytes,
thumbnail: thumbnail ?? this.thumbnail,
mimeType: mimeType ?? this.mimeType, mimeType: mimeType ?? this.mimeType,
sortOrder: sortOrder ?? this.sortOrder, sortOrder: sortOrder ?? this.sortOrder,
rowid: rowid ?? this.rowid, rowid: rowid ?? this.rowid,
@ -8588,6 +8639,9 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
if (bytes.present) { if (bytes.present) {
map['bytes'] = Variable<Uint8List>(bytes.value); map['bytes'] = Variable<Uint8List>(bytes.value);
} }
if (thumbnail.present) {
map['thumbnail'] = Variable<Uint8List>(thumbnail.value);
}
if (mimeType.present) { if (mimeType.present) {
map['mime_type'] = Variable<String>(mimeType.value); map['mime_type'] = Variable<String>(mimeType.value);
} }
@ -8614,6 +8668,7 @@ class AttachmentsCompanion extends UpdateCompanion<Attachment> {
..write('kind: $kind, ') ..write('kind: $kind, ')
..write('uri: $uri, ') ..write('uri: $uri, ')
..write('bytes: $bytes, ') ..write('bytes: $bytes, ')
..write('thumbnail: $thumbnail, ')
..write('mimeType: $mimeType, ') ..write('mimeType: $mimeType, ')
..write('sortOrder: $sortOrder, ') ..write('sortOrder: $sortOrder, ')
..write('rowid: $rowid') ..write('rowid: $rowid')
@ -11435,6 +11490,18 @@ abstract class _$AppDatabase extends GeneratedDatabase {
late final $ExternalLinksTable externalLinks = $ExternalLinksTable(this); late final $ExternalLinksTable externalLinks = $ExternalLinksTable(this);
late final $PlantaresTable plantares = $PlantaresTable(this); late final $PlantaresTable plantares = $PlantaresTable(this);
late final $SalesTable sales = $SalesTable(this); late final $SalesTable sales = $SalesTable(this);
late final Index idxVarietiesDeletedDraft = Index(
'idx_varieties_deleted_draft',
'CREATE INDEX idx_varieties_deleted_draft ON varieties (is_deleted, is_draft)',
);
late final Index idxLotsVariety = Index(
'idx_lots_variety',
'CREATE INDEX idx_lots_variety ON lots (variety_id)',
);
late final Index idxAttachmentsParent = Index(
'idx_attachments_parent',
'CREATE INDEX idx_attachments_parent ON attachments (parent_type, parent_id, kind)',
);
@override @override
Iterable<TableInfo<Table, Object?>> get allTables => Iterable<TableInfo<Table, Object?>> get allTables =>
allSchemaEntities.whereType<TableInfo<Table, Object?>>(); allSchemaEntities.whereType<TableInfo<Table, Object?>>();
@ -11454,6 +11521,9 @@ abstract class _$AppDatabase extends GeneratedDatabase {
externalLinks, externalLinks,
plantares, plantares,
sales, sales,
idxVarietiesDeletedDraft,
idxLotsVariety,
idxAttachmentsParent,
]; ];
} }
@ -15118,6 +15188,7 @@ typedef $$AttachmentsTableCreateCompanionBuilder =
required AttachmentKind kind, required AttachmentKind kind,
Value<String?> uri, Value<String?> uri,
Value<Uint8List?> bytes, Value<Uint8List?> bytes,
Value<Uint8List?> thumbnail,
Value<String?> mimeType, Value<String?> mimeType,
Value<int> sortOrder, Value<int> sortOrder,
Value<int> rowid, Value<int> rowid,
@ -15135,6 +15206,7 @@ typedef $$AttachmentsTableUpdateCompanionBuilder =
Value<AttachmentKind> kind, Value<AttachmentKind> kind,
Value<String?> uri, Value<String?> uri,
Value<Uint8List?> bytes, Value<Uint8List?> bytes,
Value<Uint8List?> thumbnail,
Value<String?> mimeType, Value<String?> mimeType,
Value<int> sortOrder, Value<int> sortOrder,
Value<int> rowid, Value<int> rowid,
@ -15206,6 +15278,11 @@ class $$AttachmentsTableFilterComposer
builder: (column) => ColumnFilters(column), builder: (column) => ColumnFilters(column),
); );
ColumnFilters<Uint8List> get thumbnail => $composableBuilder(
column: $table.thumbnail,
builder: (column) => ColumnFilters(column),
);
ColumnFilters<String> get mimeType => $composableBuilder( ColumnFilters<String> get mimeType => $composableBuilder(
column: $table.mimeType, column: $table.mimeType,
builder: (column) => ColumnFilters(column), builder: (column) => ColumnFilters(column),
@ -15281,6 +15358,11 @@ class $$AttachmentsTableOrderingComposer
builder: (column) => ColumnOrderings(column), builder: (column) => ColumnOrderings(column),
); );
ColumnOrderings<Uint8List> get thumbnail => $composableBuilder(
column: $table.thumbnail,
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<String> get mimeType => $composableBuilder( ColumnOrderings<String> get mimeType => $composableBuilder(
column: $table.mimeType, column: $table.mimeType,
builder: (column) => ColumnOrderings(column), builder: (column) => ColumnOrderings(column),
@ -15341,6 +15423,9 @@ class $$AttachmentsTableAnnotationComposer
GeneratedColumn<Uint8List> get bytes => GeneratedColumn<Uint8List> get bytes =>
$composableBuilder(column: $table.bytes, builder: (column) => column); $composableBuilder(column: $table.bytes, builder: (column) => column);
GeneratedColumn<Uint8List> get thumbnail =>
$composableBuilder(column: $table.thumbnail, builder: (column) => column);
GeneratedColumn<String> get mimeType => GeneratedColumn<String> get mimeType =>
$composableBuilder(column: $table.mimeType, builder: (column) => column); $composableBuilder(column: $table.mimeType, builder: (column) => column);
@ -15390,6 +15475,7 @@ class $$AttachmentsTableTableManager
Value<AttachmentKind> kind = const Value.absent(), Value<AttachmentKind> kind = const Value.absent(),
Value<String?> uri = const Value.absent(), Value<String?> uri = const Value.absent(),
Value<Uint8List?> bytes = const Value.absent(), Value<Uint8List?> bytes = const Value.absent(),
Value<Uint8List?> thumbnail = const Value.absent(),
Value<String?> mimeType = const Value.absent(), Value<String?> mimeType = const Value.absent(),
Value<int> sortOrder = const Value.absent(), Value<int> sortOrder = const Value.absent(),
Value<int> rowid = const Value.absent(), Value<int> rowid = const Value.absent(),
@ -15405,6 +15491,7 @@ class $$AttachmentsTableTableManager
kind: kind, kind: kind,
uri: uri, uri: uri,
bytes: bytes, bytes: bytes,
thumbnail: thumbnail,
mimeType: mimeType, mimeType: mimeType,
sortOrder: sortOrder, sortOrder: sortOrder,
rowid: rowid, rowid: rowid,
@ -15422,6 +15509,7 @@ class $$AttachmentsTableTableManager
required AttachmentKind kind, required AttachmentKind kind,
Value<String?> uri = const Value.absent(), Value<String?> uri = const Value.absent(),
Value<Uint8List?> bytes = const Value.absent(), Value<Uint8List?> bytes = const Value.absent(),
Value<Uint8List?> thumbnail = const Value.absent(),
Value<String?> mimeType = const Value.absent(), Value<String?> mimeType = const Value.absent(),
Value<int> sortOrder = const Value.absent(), Value<int> sortOrder = const Value.absent(),
Value<int> rowid = const Value.absent(), Value<int> rowid = const Value.absent(),
@ -15437,6 +15525,7 @@ class $$AttachmentsTableTableManager
kind: kind, kind: kind,
uri: uri, uri: uri,
bytes: bytes, bytes: bytes,
thumbnail: thumbnail,
mimeType: mimeType, mimeType: mimeType,
sortOrder: sortOrder, sortOrder: sortOrder,
rowid: rowid, rowid: rowid,

View file

@ -5,6 +5,10 @@ import 'sync_columns.dart';
/// The identity/accession one row per distinct thing in the inventory. /// The identity/accession one row per distinct thing in the inventory.
/// Only [label] is mandatory (progressive disclosure). /// Only [label] is mandatory (progressive disclosure).
///
/// The index covers the inventory list's hot filter — non-deleted, non-draft
/// rows so it stays fast with a large catalogue.
@TableIndex(name: 'idx_varieties_deleted_draft', columns: {#isDeleted, #isDraft})
class Varieties extends Table with SyncColumns { class Varieties extends Table with SyncColumns {
TextColumn get label => text()(); TextColumn get label => text()();
TextColumn get speciesId => text().nullable()(); // Species TextColumn get speciesId => text().nullable()(); // Species
@ -77,6 +81,10 @@ class SpeciesCommonNames extends Table with SyncColumns {
/// A homogeneous batch held for a Variety its own year and its own unit. /// A homogeneous batch held for a Variety its own year and its own unit.
/// Quantity (commons_core value type) is flattened into columns here. /// Quantity (commons_core value type) is flattened into columns here.
///
/// Indexed by [varietyId] the inventory list joins lots per variety (types,
/// shared status, viability), so this avoids a full scan per reload.
@TableIndex(name: 'idx_lots_variety', columns: {#varietyId})
class Lots extends Table with SyncColumns { class Lots extends Table with SyncColumns {
TextColumn get varietyId => text()(); TextColumn get varietyId => text()();
TextColumn get type => TextColumn get type =>
@ -181,12 +189,26 @@ class Parties extends Table with SyncColumns {
/// rest by SQLCipher) via [bytes]; external files use [uri]. Storing bytes here /// rest by SQLCipher) via [bytes]; external files use [uri]. Storing bytes here
/// keeps the "no plaintext at rest" rule for photos in Block 1; an external /// keeps the "no plaintext at rest" rule for photos in Block 1; an external
/// encrypted file store is a later optimization. /// encrypted file store is a later optimization.
///
/// Indexed by (parentType, parentId, kind) the list's "first photo per
/// variety" lookup filters on exactly these.
@TableIndex(
name: 'idx_attachments_parent',
columns: {#parentType, #parentId, #kind},
)
class Attachments extends Table with SyncColumns { class Attachments extends Table with SyncColumns {
TextColumn get parentType => textEnum<ParentType>()(); TextColumn get parentType => textEnum<ParentType>()();
TextColumn get parentId => text()(); TextColumn get parentId => text()();
TextColumn get kind => textEnum<AttachmentKind>()(); TextColumn get kind => textEnum<AttachmentKind>()();
TextColumn get uri => text().nullable()(); TextColumn get uri => text().nullable()();
BlobColumn get bytes => blob().nullable()(); BlobColumn get bytes => blob().nullable()();
/// A small JPEG thumbnail of [bytes] (photos only), decoded once on save so
/// the inventory list never has to decode the full-resolution photo for a
/// 48px avatar. Purely derived, local and regenerable it is EXCLUDED from
/// CRDT sync payloads and backups (see [SyncColumns] usage); a peer or a
/// restored backup regenerates it lazily via `backfillThumbnails`.
BlobColumn get thumbnail => blob().nullable()();
TextColumn get mimeType => text().nullable()(); TextColumn get mimeType => text().nullable()();
/// Display order among sibling attachments (lower first). The lowest-ordered /// Display order among sibling attachments (lower first). The lowest-ordered

View file

@ -28,6 +28,7 @@ import '../services/file_service.dart';
import '../services/inbox_service.dart'; import '../services/inbox_service.dart';
import '../services/locale_store.dart'; import '../services/locale_store.dart';
import '../services/notification_service.dart'; import '../services/notification_service.dart';
import '../services/offer_thumbnail.dart';
import '../services/ocr/label_text_extractor.dart'; import '../services/ocr/label_text_extractor.dart';
import '../services/ocr/ocr_language.dart'; import '../services/ocr/ocr_language.dart';
import '../services/ocr/tesseract_label_extractor.dart'; import '../services/ocr/tesseract_label_extractor.dart';
@ -152,7 +153,11 @@ Future<void> configureDependencies() async {
database, database,
idGen: IdGen(), idGen: IdGen(),
nodeId: nodeId, nodeId: nodeId,
thumbnailBuilder: inventoryThumbnailBytes,
); );
// Backfill list thumbnails for photos that predate the thumbnail column (or
// arrived via a restored backup). Fire-and-forget so startup isn't blocked.
unawaited(varietyRepository.backfillThumbnails());
const fileService = FilePickerFileService(); const fileService = FilePickerFileService();

View file

@ -34,6 +34,26 @@ String? offerThumbnailDataUri(
} }
} }
/// Builds a small JPEG thumbnail (longest edge [edge]px) for the inventory-list
/// avatar, so the list never has to decode the full-resolution photo. Returns
/// raw JPEG bytes, or null on undecodable input (the caller falls back to the
/// full photo). Kept as local bytes regenerable, so it is excluded from sync
/// and backups.
Uint8List? inventoryThumbnailBytes(
Uint8List bytes, {
int edge = 96,
int quality = 72,
}) {
try {
final decoded = img.decodeImage(bytes);
if (decoded == null) return null;
final resized = _fitWithin(decoded, edge);
return Uint8List.fromList(img.encodeJpg(resized, quality: quality));
} catch (_) {
return null;
}
}
/// Extracts the raw bytes from a `data:...;base64,` URI, or null when [uri] is /// Extracts the raw bytes from a `data:...;base64,` URI, or null when [uri] is
/// not a base64 data URI. Used by the UI to render an inline thumbnail with /// not a base64 data URI. Used by the UI to render an inline thumbnail with
/// `Image.memory` instead of a network fetch. /// `Image.memory` instead of a network fetch.

View file

@ -582,25 +582,45 @@ class _InventoryBody extends StatelessWidget {
} }
// Items arrive ordered by category then label; insert a header whenever the // Items arrive ordered by category then label; insert a header whenever the
// category changes. // category changes. Precompute a flat row model (header or item) so the
final rows = <Widget>[]; // ListView can build tiles lazily with a large inventory, building every
// tile upfront (the old `ListView(children:)`) stalled the first paint.
final rows = <_InventoryRow>[];
String? currentCategory; String? currentCategory;
for (final item in items) { for (final item in items) {
final category = item.category ?? t.inventory.uncategorized; final category = item.category ?? t.inventory.uncategorized;
if (category != currentCategory) { if (category != currentCategory) {
currentCategory = category; currentCategory = category;
rows.add(_CategoryHeader(title: category)); rows.add(_InventoryRow.header(category));
} }
rows.add( rows.add(_InventoryRow.variety(item));
_VarietyTile( }
item: item, return ListView.builder(
itemCount: rows.length,
itemBuilder: (context, i) {
final row = rows[i];
final header = row.header;
if (header != null) {
return _CategoryHeader(title: header);
}
return _VarietyTile(
item: row.item!,
selectionMode: selectionMode, selectionMode: selectionMode,
selected: selectedIds.contains(item.id), selected: selectedIds.contains(row.item!.id),
), );
},
); );
} }
return ListView(children: rows); }
}
/// A single row in the flattened inventory list: either a category [header] or
/// a variety [item] (exactly one is non-null).
class _InventoryRow {
const _InventoryRow.header(this.header) : item = null;
const _InventoryRow.variety(this.item) : header = null;
final String? header;
final VarietyListItem? item;
} }
class _CategoryHeader extends StatelessWidget { class _CategoryHeader extends StatelessWidget {
@ -767,10 +787,21 @@ class _Avatar extends StatelessWidget {
// Decorative: the tile title already announces the variety name, so keep // Decorative: the tile title already announces the variety name, so keep
// the thumbnail / initial out of the semantics tree. // the thumbnail / initial out of the semantics tree.
if (photo != null) { if (photo != null) {
// Decode straight to the 48px avatar size (× device pixel ratio) instead
// of holding the full-resolution photo in the image cache decisive for
// memory with a large inventory of photographed varieties.
final cachePx = (48 * MediaQuery.devicePixelRatioOf(context)).round();
return ExcludeSemantics( return ExcludeSemantics(
child: ClipRRect( child: ClipRRect(
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
child: Image.memory(photo, width: 48, height: 48, fit: BoxFit.cover), child: Image.memory(
photo,
width: 48,
height: 48,
cacheWidth: cachePx,
cacheHeight: cachePx,
fit: BoxFit.cover,
),
), ),
); );
} }

View file

@ -19,8 +19,8 @@ void main() {
}); });
tearDown(() => db.close()); tearDown(() => db.close());
test('the inventory view loads 3000 varieties quickly', () async { test('the inventory view loads 10000 varieties quickly', () async {
const count = 3000; const count = 10000;
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
final id = await repo.addQuickVariety( final id = await repo.addQuickVariety(
label: 'Variety $i', label: 'Variety $i',
@ -43,11 +43,12 @@ void main() {
sw.stop(); sw.stop();
expect(view.items, hasLength(count)); expect(view.items, hasLength(count));
// Generous ceiling for CI; typical local runs are well under 500ms. The // Generous ceiling for CI (the point is to catch an N+1 regression, not to
// point is to catch an accidental N+1 regression, not to micro-benchmark. // micro-benchmark). Includes the ~250ms debounce on the first emit; typical
// local runs are well under 2s for 10k rows with the v14 indexes.
expect( expect(
sw.elapsedMilliseconds, sw.elapsedMilliseconds,
lessThan(3000), lessThan(6000),
reason: 'inventory view took ${sw.elapsedMilliseconds}ms for $count rows', reason: 'inventory view took ${sw.elapsedMilliseconds}ms for $count rows',
); );
}); });

View file

@ -11,19 +11,19 @@ void main() {
verifier = SchemaVerifier(GeneratedHelper()); verifier = SchemaVerifier(GeneratedHelper());
}); });
test('freshly created database matches the exported schema v13', () async { test('freshly created database matches the exported schema v14', () async {
final schema = await verifier.schemaAt(13); final schema = await verifier.schemaAt(14);
final db = AppDatabase(schema.newConnection()); final db = AppDatabase(schema.newConnection());
await verifier.migrateAndValidate(db, 13); await verifier.migrateAndValidate(db, 14);
await db.close(); await db.close();
}); });
// Every historical version upgrades cleanly to the current schema (v13). // Every historical version upgrades cleanly to the current schema (v14).
for (var from = 1; from <= 12; from++) { for (var from = 1; from <= 13; from++) {
test('upgrades v$from → v13 and matches the fresh schema', () async { test('upgrades v$from → v14 and matches the fresh schema', () async {
final connection = await verifier.startAt(from); final connection = await verifier.startAt(from);
final db = AppDatabase(connection); final db = AppDatabase(connection);
await verifier.migrateAndValidate(db, 13); await verifier.migrateAndValidate(db, 14);
await db.close(); await db.close();
}); });
} }

View file

@ -17,6 +17,7 @@ import 'schema_v10.dart' as v10;
import 'schema_v11.dart' as v11; import 'schema_v11.dart' as v11;
import 'schema_v12.dart' as v12; import 'schema_v12.dart' as v12;
import 'schema_v13.dart' as v13; import 'schema_v13.dart' as v13;
import 'schema_v14.dart' as v14;
class GeneratedHelper implements SchemaInstantiationHelper { class GeneratedHelper implements SchemaInstantiationHelper {
@override @override
@ -48,10 +49,12 @@ class GeneratedHelper implements SchemaInstantiationHelper {
return v12.DatabaseAtV12(db); return v12.DatabaseAtV12(db);
case 13: case 13:
return v13.DatabaseAtV13(db); return v13.DatabaseAtV13(db);
case 14:
return v14.DatabaseAtV14(db);
default: default:
throw MissingSchemaException(version, versions); throw MissingSchemaException(version, versions);
} }
} }
static const versions = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; static const versions = const [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
} }

File diff suppressed because it is too large Load diff