mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-29 07:34:13 +01:00
perf(history): find downloads by id and by path through an index
Resolving a finished download and looking a file up by its path walked the whole history and built the path keys of every entry each time. The state now builds an id index and a path key index on first use, and carries both over when a download is added.
This commit is contained in:
1 parent
3582a58d3a
commit
e01b1028cc
2 files changed
+59
-17
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import 'package:mornye/utils/path_match_keys.dart';
|
||||
import 'package:mornye/utils/string_utils.dart';
|
||||
|
||||
/// Another transfer published the same origin while this one was fetching.
|
||||
@@ -297,6 +298,8 @@ class DownloadHistoryState {
|
||||
final Map<String, DownloadHistoryItem> _bySpotifyId;
|
||||
final Map<String, DownloadHistoryItem> _byIsrc;
|
||||
final Map<String, DownloadHistoryItem> _byTrackArtistKey;
|
||||
Map<String, DownloadHistoryItem>? _byId;
|
||||
Map<String, DownloadHistoryItem>? _byPathKey;
|
||||
|
||||
DownloadHistoryState({
|
||||
this.items = const [],
|
||||
@@ -325,7 +328,9 @@ class DownloadHistoryState {
|
||||
}) : _lookupItems = source._lookupItems,
|
||||
_bySpotifyId = source._bySpotifyId,
|
||||
_byIsrc = source._byIsrc,
|
||||
_byTrackArtistKey = source._byTrackArtistKey;
|
||||
_byTrackArtistKey = source._byTrackArtistKey,
|
||||
_byId = source._byId,
|
||||
_byPathKey = source._byPathKey;
|
||||
|
||||
DownloadHistoryState._withPrependedLookup({
|
||||
required DownloadHistoryState source,
|
||||
@@ -344,7 +349,55 @@ class DownloadHistoryState {
|
||||
source._byTrackArtistKey,
|
||||
trackArtistKey(item.trackName, item.artistName),
|
||||
item,
|
||||
);
|
||||
),
|
||||
_byId = source._byId == null
|
||||
? null
|
||||
: _indexWithNewest(source._byId!, item.id, item),
|
||||
_byPathKey = source._byPathKey == null
|
||||
? null
|
||||
: _indexPathKeysWithNewest(source._byPathKey!, item);
|
||||
|
||||
static Map<String, DownloadHistoryItem> _indexPathKeysWithNewest(
|
||||
Map<String, DownloadHistoryItem> index,
|
||||
DownloadHistoryItem item,
|
||||
) {
|
||||
final keys = buildPathMatchKeys(item.filePath);
|
||||
if (keys.isEmpty) return index;
|
||||
final next = Map<String, DownloadHistoryItem>.of(index);
|
||||
for (final key in keys) {
|
||||
next[key] = item;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
DownloadHistoryItem? getById(String id) {
|
||||
if (id.isEmpty) return null;
|
||||
final byId = _byId ??= _indexRecentHistoryItems(
|
||||
_lookupItems,
|
||||
(item) => item.id,
|
||||
);
|
||||
return byId[id];
|
||||
}
|
||||
|
||||
DownloadHistoryItem? getByFilePath(String filePath) {
|
||||
final targetKeys = buildPathMatchKeys(filePath);
|
||||
if (targetKeys.isEmpty) return null;
|
||||
var byPath = _byPathKey;
|
||||
if (byPath == null) {
|
||||
byPath = <String, DownloadHistoryItem>{};
|
||||
for (final item in _lookupItems) {
|
||||
for (final key in buildPathMatchKeys(item.filePath)) {
|
||||
byPath.putIfAbsent(key, () => item);
|
||||
}
|
||||
}
|
||||
_byPathKey = byPath;
|
||||
}
|
||||
for (final key in targetKeys) {
|
||||
final hit = byPath[key];
|
||||
if (hit != null) return hit;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The key [findByTrackArtistKey] reads; '' for an empty track name.
|
||||
static String trackArtistKey(String trackName, String artistName) {
|
||||
|
||||
@@ -291,13 +291,8 @@ class DownloadHistoryNotifier extends Notifier<DownloadHistoryState> {
|
||||
Future<({DownloadHistoryItem item, String? existingId})> _resolveHistoryItem(
|
||||
DownloadHistoryItem item,
|
||||
) async {
|
||||
DownloadHistoryItem? existing;
|
||||
for (final candidate in state.lookupItems) {
|
||||
if (historyItemsReferToSameStoredFile(candidate, item)) {
|
||||
existing = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
DownloadHistoryItem? existing =
|
||||
state.getById(item.id) ?? state.getByFilePath(item.filePath);
|
||||
|
||||
if (existing == null) {
|
||||
final json = await _db.getById(item.id);
|
||||
@@ -629,14 +624,8 @@ class DownloadHistoryNotifier extends Notifier<DownloadHistoryState> {
|
||||
}
|
||||
|
||||
Future<DownloadHistoryItem?> getByFilePathAsync(String filePath) async {
|
||||
final targetKeys = buildPathMatchKeys(filePath);
|
||||
if (targetKeys.isEmpty) return null;
|
||||
|
||||
for (final item in state.lookupItems) {
|
||||
if (buildPathMatchKeys(item.filePath).any(targetKeys.contains)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
final inMemory = state.getByFilePath(filePath);
|
||||
if (inMemory != null) return inMemory;
|
||||
|
||||
final json = await _db.findByFilePath(filePath);
|
||||
if (json == null) return null;
|
||||
|
||||
Reference in new issue
Block a user