Repository navigation
Expand file tree
/
Copy pathmodel_index_duplicates.cpp
More file actions
1150 lines (953 loc) · 39.3 KB
/
Copy pathmodel_index_duplicates.cpp
File metadata and controls
1150 lines (953 loc) · 39.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: Duplicate prediction and presence. Groups the index by identity - byte-identical CRC, then
// perceptual hash within capture time and picture shape - to predict duplicate sets, asks for the
// hashes a set still needs, and answers whether an item is already present in the collection.
#include "pch.h"
#include "model_index.h"
#include "model_index_internal.h"
#include "model_locations.h"
#include "model_property.h"
#include "model.h"
#include "util_crash_files_db.h"
#include "util_text.h"
// Capture time is recorded to the second and cameras shoot faster than that. A picture that matches
// this many others in the SAME orientation under one timestamp is a burst frame rather than a
// re-save: continuous shooting produces frames that match each other at any threshold. A turned
// match is never a burst frame, so it is not counted here (docs/collections.md section 7.3).
constexpr size_t max_similar_pictures_at_one_capture_time = 2;
// A work bound, not a judgement: past this a capture time is unambiguously continuous shooting, and
// hashing every frame of it would decode a great deal to reach a refusal. Duplicate search and
// presence share it, so neither can report a set the other cannot see.
constexpr size_t max_photos_sharing_capture_time = 8;
// Hashing reads and decodes, so a pass asks for a bounded batch and the pass that follows asks for
// the next. A large collection converges over several rounds rather than stalling on the first.
constexpr size_t max_phash_requests_per_pass = 256;
// A picture worth this much I/O to identify. Beyond it the read costs more than the answer is worth.
// The hash reads the file whole, so the bound is also the most a whole read may hold: above that the
// read answers empty, and a file refused for its size was recorded as one that failed to decode.
constexpr uint64_t max_phash_file_bytes = core::max_blob_size;
// Runs once an anchor has been chosen and before its hash is compared, where a concurrent rescan can
// clear the record's hash.
std::function<void(const df::index_file_item& anchor)> test_after_duplicate_anchor;
static auto next_dup_group = 1000u;
// A set keeps the id one of its members already carries, unless another set has claimed that id this
// pass; only a set with none takes a new one. Renumbering every set on every pass rewrote every grouped
// record each time, and left the id an item had already taken - the one its tooltip lists copies by -
// matching nothing.
template <typename RootOf, typename PreviousId>
static core::hash_map<size_t, uint32_t> assign_group_ids(const size_t file_count, const RootOf& root_of,
const core::hash_map<size_t, core::int_counter>& counts,
const PreviousId& previous_id)
{
core::hash_map<size_t, uint32_t> ids;
core::hash_set<uint32_t> claimed;
for (size_t i = 0; i < file_count; ++i)
{
const auto root = root_of(i);
if (counts.at(root) < 2 || ids.contains(root)) continue;
const auto previous = previous_id(i);
if (previous != 0 && claimed.emplace(previous).second) ids.emplace(root, previous);
}
for (const auto& [root, count] : counts)
{
if (count > 1 && !ids.contains(root)) ids.emplace(root, ++next_dup_group);
}
return ids;
}
static __forceinline uint32_t x64to32(const uint64_t n)
{
constexpr uint64_t fnv_prime = 1099511628211u;
constexpr uint64_t fnv_offset_basis = 14695981039346656037u;
const auto result = (fnv_offset_basis ^ n) * fnv_prime;
return static_cast<uint32_t>(result);
}
std::vector<std::pair<core::file_path, df::index_file_item>> index_state::duplicate_list(const uint32_t group) const
{
std::vector<std::pair<core::file_path, df::index_file_item>> result;
const auto folders = _items.all_folders();
// Read-only: this answers a tooltip on the UI thread. Clearing the ids of files outside the
// collection is update_predictions' work.
for (const auto& ifn : folders)
{
if (ifn.second->is_in_collection)
{
for (const auto& file : ifn.second->files)
{
if (file.duplicates.load().group == group)
{
result.emplace_back(core::file_path(ifn.first, file.name), file);
}
}
}
}
return result;
}
void index_state::update_predictions()
{
const auto folders = _items.all_folders();
const auto start_ms = core::now_ms();
const auto folder_count = folders.size();
// dup on:
// filename
// metadata created
// file created and size
// crc32c
// the same picture at the same capture time, by perceptual hash
struct duplicate_candidate
{
const df::index_folder_item_ptr& folder;
const df::index_file_item* file;
core::folder_path path;
};
std::vector<duplicate_candidate> files;
enum class duplicate_evidence_kind : uint8_t
{
crc,
av_name_size,
name_created,
};
struct duplicate_evidence
{
duplicate_evidence_kind kind = duplicate_evidence_kind::crc;
std::string name;
uint64_t first = 0;
uint64_t second = 0;
size_t index = 0;
df::copy_grade grade = df::copy_grade::none;
auto key() const { return std::tie(kind, name, first, second); }
};
std::vector<duplicate_evidence> dups;
{
// Three complete evidence tuples per file - reserve from the real count rather than a fixed guess.
size_t indexed_file_count = 0;
for (const auto& ifn : folders)
{
if (ifn.second->is_in_collection) indexed_file_count += ifn.second->files.size();
}
dups.reserve(indexed_file_count * 3);
files.reserve(indexed_file_count);
}
int indexed_crc_count = 0;
for (const auto& ifn : folders)
{
if (ifn.second->is_in_collection)
{
for (const auto& file : ifn.second->files)
{
if (platform::is_closing) return;
const auto file_index = files.size();
files.push_back({ifn.second, &file, ifn.first});
if (file.crc32c)
{
dups.emplace_back(duplicate_evidence_kind::crc, std::string{}, file.crc32c,
static_cast<uint64_t>(file.size.to_int64()), file_index,
df::copy_grade::identical);
++indexed_crc_count;
}
if (file.ft->has_trait(file_traits::av) && file.size.to_int64() != 0)
{
dups.emplace_back(duplicate_evidence_kind::av_name_size, core::to_lower(file.name),
static_cast<uint64_t>(file.size.to_int64()), 0, file_index,
df::copy_grade::same_file);
}
const auto created = file.created();
if (created.is_valid())
{
dups.emplace_back(duplicate_evidence_kind::name_created, core::to_lower(file.name),
static_cast<uint64_t>(created.to_int64()), 0, file_index,
df::copy_grade::same_file);
}
}
}
else
{
for (const auto& file : ifn.second->files)
{
_items.update_file(core::file_path(ifn.first, file.name),
[](const df::index_folder_item_ptr&, const df::index_file_item& current_file)
{
if (current_file.exact_duplicate_group.load() == 0) return false;
current_file.exact_duplicate_group = 0;
return true;
}, index_items::file_change::derived);
}
}
}
std::ranges::sort(dups, [](const duplicate_evidence& left, const duplicate_evidence& right)
{ return left.key() < right.key(); });
std::vector<size_t> parents(files.size());
std::vector<uint8_t> ranks(files.size());
std::iota(parents.begin(), parents.end(), 0);
const auto find_root = [&parents](size_t item)
{
while (parents[item] != item)
{
parents[item] = parents[parents[item]];
item = parents[item];
}
return item;
};
const auto unite = [&parents, &ranks, &find_root](const size_t left, const size_t right)
{
auto left_root = find_root(left);
auto right_root = find_root(right);
if (left_root == right_root) return;
if (ranks[left_root] < ranks[right_root]) std::swap(left_root, right_root);
parents[right_root] = left_root;
if (ranks[left_root] == ranks[right_root]) ++ranks[left_root];
};
const auto& cdups = dups;
int max_compare_count = 0;
const auto dup_size = cdups.size();
// How each file joined its set. Only the strongest evidence for a file is kept, so a file that is
// byte-identical to one member and merely a possible copy of another reports the stronger claim.
std::vector<df::copy_grade> grades(files.size(), df::copy_grade::none);
const auto record_grade = [&grades](const size_t item, const df::copy_grade grade)
{
grades[item] = df::strongest(grades[item], grade);
};
for (auto i = 0u; i < dup_size; ++i)
{
if (platform::is_closing) return;
auto hi = i + 1;
while (hi < dup_size && cdups[hi].key() == cdups[i].key()) ++hi;
const auto bucket_size = static_cast<int>(hi - i);
max_compare_count = std::max(max_compare_count, bucket_size);
if (bucket_size > 1)
{
const auto first = cdups[i].index;
for (auto member = i; member < hi; ++member)
{
if (platform::is_closing) return;
unite(first, cdups[member].index);
record_grade(cdups[member].index, cdups[member].grade);
}
}
i = hi - 1;
}
core::hash_map<size_t, core::int_counter> exact_component_counts;
for (auto i = 0u; i < files.size(); ++i)
{
++exact_component_counts[find_root(i)];
}
const auto exact_component_groups = assign_group_ids(files.size(), find_root, exact_component_counts,
[&files](const size_t i)
{ return files[i].file->exact_duplicate_group.load(); });
for (auto i = 0u; i < files.size(); ++i)
{
const auto root = find_root(i);
const auto found = exact_component_groups.find(root);
const auto group = found == exact_component_groups.end() ? 0u : found->second;
_items.update_file(core::file_path(files[i].path, files[i].file->name),
[group](const df::index_folder_item_ptr&, const df::index_file_item& file)
{
if (file.exact_duplicate_group.load() == group) return false;
file.exact_duplicate_group = group;
return true;
}, index_items::file_change::derived);
}
std::vector<uint8_t> crowded(files.size(), 0);
// The perceptual stage. It is deliberately not part of the walk above: a picture match is a
// tolerance, not an equality, so it may not be closed over transitively. Every candidate is
// compared against one anchor chosen for the capture time and never against another candidate,
// which makes each set a star rather than a chain (docs/collections.md section 7.2).
std::vector<phash_request> phash_wanted;
{
core::hash_map<uint64_t, std::vector<size_t>> capture_times;
uint32_t dated_pictures = 0;
for (size_t i = 0; i < files.size(); ++i)
{
const auto* const file = files[i].file;
if (!file->ft->has_trait(file_traits::bitmap)) continue;
const auto md = file->metadata.load(); // important to hold ref
if (!md) continue;
const auto created = md->created();
if (!created.is_valid()) continue;
capture_times[created.to_int64()].push_back(i);
++dated_pictures;
}
// A hash is only earned by a picture that shares a capture time with another, so marking the
// candidates is what makes an excess measurable rather than merely suspected.
std::vector<uint8_t> is_candidate(files.size(), 0);
uint32_t candidate_count = 0;
uint32_t wanted_count = 0;
uint32_t matched_count = 0;
uint32_t crowded_count = 0;
// The shape narrowing the gate applies, and what a gate blind to rotation would have refused.
uint32_t dims_unknown = 0;
uint32_t aspect_solo = 0;
uint32_t aspect_solo_swap = 0;
uint32_t matched_cross_aspect = 0;
const auto dims_of = [&files](const size_t i)
{
const auto md = files[i].file->metadata.load(); // important to hold ref
return md ? md->dimensions() : sizei{};
};
for (const auto& [created, members] : capture_times)
{
if (platform::is_closing) return;
// One photograph proves nothing, and past this a capture time is unambiguously continuous
// shooting. Presence applies the same bound, so neither surface can see a set the other
// cannot (docs/collections.md section 7).
if (members.size() < 2) continue;
if (members.size() > max_photos_sharing_capture_time)
{
for (const auto member : members) crowded[member] = 1;
continue;
}
// Sharing a capture second is a weak claim on its own. A picture whose neighbours are all
// a different shape cannot be a copy of any of them, and refusing it here is a decode
// saved rather than a judgement made. A quarter turn transposes the stored extent, so a
// transposed neighbour still counts (docs/collections.md section 7.2).
std::vector<size_t> shaped;
shaped.reserve(members.size());
for (const auto member : members)
{
const auto member_dims = dims_of(member);
if (member_dims.is_empty())
{
++dims_unknown;
// Shape is unknown rather than different, so the picture keeps its place.
shaped.push_back(member);
continue;
}
auto has_peer = false;
auto has_peer_with_swap = false;
for (const auto other : members)
{
if (other == member) continue;
const auto other_dims = dims_of(other);
if (same_picture_shape(member_dims, other_dims, false)) has_peer = true;
if (same_picture_shape(member_dims, other_dims, true)) has_peer_with_swap = true;
if (has_peer && has_peer_with_swap) break;
}
if (!has_peer) ++aspect_solo;
if (!has_peer_with_swap) ++aspect_solo_swap;
if (has_peer_with_swap) shaped.push_back(member);
}
if (shaped.size() < 2) continue;
candidate_count += static_cast<uint32_t>(shaped.size());
for (const auto member : shaped) is_candidate[member] = 1;
// Every picture here has to be compared before any of them is reported: the crowd rule
// counts matches, so judging a half-hashed capture time could let a burst through it.
auto evidence_complete = true;
for (const auto member : shaped)
{
if (files[member].file->phash.load() != nullptr) continue;
evidence_complete = false;
++wanted_count;
// Hashing reads the file, which this walk of the index must not wait on, so the work is
// only noted here. The pass that follows the hashes will see them and compare.
if (phash_wanted.size() < max_phash_requests_per_pass)
{
phash_wanted.emplace_back(core::file_path(files[member].path, files[member].file->name),
revision_of(*files[member].file));
}
}
if (!evidence_complete) continue;
// Lowest path, so which item anchors the set never depends on the order the index
// happened to be walked in. A picture that declined to be identified cannot anchor, and
// skipping it here stops one blank frame from suppressing the whole capture time.
// Sentinel is files.size(): shaped holds indices into files, so files.size() is not a
// value a real member can take.
auto anchor = files.size();
// The set the anchor was chosen for. This walk holds no index lock, and a rescan of an
// edited picture clears its hash at any moment, so loading the record again here could
// answer null for the anchor this loop has just seen hashed.
df::picture_hashes_ptr anchor_hashes;
for (const auto member : shaped)
{
auto held = files[member].file->phash.load();
if (!held || !held->is_usable()) continue;
if (anchor == files.size() || core::file_path(files[member].path, files[member].file->name) <
core::file_path(files[anchor].path, files[anchor].file->name))
{
anchor = member;
anchor_hashes = std::move(held);
}
}
if (anchor == files.size()) continue;
if (test_after_duplicate_anchor) test_after_duplicate_anchor(*files[anchor].file);
const auto anchor_hash = anchor_hashes->stored();
// Collected rather than applied, because how many match decides whether any of them are
// reported: a crowd around one anchor is a burst.
std::vector<size_t> matched;
// Continuous shooting produces frames in one orientation; it never produces a turned one.
// So only an untuned match is evidence of a burst, and the crowd rule counts those alone.
size_t same_orientation_matches = 0;
for (const auto member : shaped)
{
if (member == anchor) continue;
const auto member_hashes = files[member].file->phash.load();
if (!member_hashes || !member_hashes->is_usable()) continue;
// The member's four turns are the complete orbit, so this covers every relative
// rotation without the anchor needing its own.
if (crypto::phash_distance(anchor_hash, member_hashes->rotations) > max_duplicate_phash_distance)
{
continue;
}
matched.push_back(member);
if (crypto::phash_distance(anchor_hash, member_hashes->stored()) <= max_duplicate_phash_distance)
{
++same_orientation_matches;
}
}
if (same_orientation_matches > max_similar_pictures_at_one_capture_time)
{
for (const auto member : shaped) crowded[member] = 1;
++crowded_count;
continue;
}
const auto anchor_dims = dims_of(anchor);
for (const auto member : matched)
{
unite(anchor, member);
record_grade(anchor, df::copy_grade::same_picture);
record_grade(member, df::copy_grade::same_picture);
if (!same_picture_shape(anchor_dims, dims_of(member), true)) ++matched_cross_aspect;
}
matched_count += static_cast<uint32_t>(matched.size());
}
// Held against invited. A picture keeps its hash once computed, so a large uninvited count is
// not drift - it is hashing that was asked for by something other than the candidate rule.
uint32_t usable_held = 0;
uint32_t declined_held = 0;
uint32_t uninvited = 0;
for (size_t i = 0; i < files.size(); ++i)
{
const auto held = files[i].file->phash.load();
if (!held) continue;
if (held->is_usable()) ++usable_held;
else ++declined_held;
if (!is_candidate[i]) ++uninvited;
}
render::set_gauge(df::index_perf.pass_pictures, dated_pictures);
render::set_gauge(df::index_perf.pass_buckets, static_cast<uint32_t>(capture_times.size()));
render::set_gauge(df::index_perf.pass_candidates, candidate_count);
render::set_gauge(df::index_perf.pass_wanted, wanted_count);
render::set_gauge(df::index_perf.pass_matched, matched_count);
render::set_gauge(df::index_perf.pass_crowded, crowded_count);
render::set_gauge(df::index_perf.pass_usable_held, usable_held);
render::set_gauge(df::index_perf.pass_declined_held, declined_held);
render::set_gauge(df::index_perf.pass_uninvited, uninvited);
render::set_gauge(df::index_perf.pass_dims_unknown, dims_unknown);
render::set_gauge(df::index_perf.pass_aspect_solo, aspect_solo);
render::set_gauge(df::index_perf.pass_aspect_solo_swap, aspect_solo_swap);
render::set_gauge(df::index_perf.pass_matched_cross_aspect, matched_cross_aspect);
stats.indexed_phash_count = static_cast<int>(usable_held);
stats.indexed_phash_declined_count = static_cast<int>(declined_held);
stats.indexed_phash_uninvited_count = static_cast<int>(uninvited);
}
core::hash_map<size_t, core::int_counter> component_counts;
for (auto i = 0u; i < files.size(); ++i)
{
++component_counts[find_root(i)];
}
const auto component_groups = assign_group_ids(files.size(), find_root, component_counts, [&files](const size_t i)
{ return files[i].file->duplicates.load().group; });
auto duplicates_changed = false;
for (auto i = 0u; i < files.size(); ++i)
{
if (platform::is_closing) return;
const auto root = find_root(i);
const auto count = static_cast<uint32_t>(component_counts[root]);
const auto found_group = component_groups.find(root);
const auto group = found_group == component_groups.end() ? 0u : found_group->second;
const auto grade = group == 0 ? df::copy_grade::none : grades[i];
const auto dup_info = df::duplicate_info{group, count, crowded[i], grade};
duplicates_changed |= _items.update_file(core::file_path(files[i].path, files[i].file->name),
[dup_info](const df::index_folder_item_ptr& folder,
const df::index_file_item& file)
{
const auto existing = file.duplicates.load();
if (existing.count == dup_info.count && existing.group == dup_info.group &&
existing.grade == dup_info.grade && existing.same_picture_crowded == dup_info.same_picture_crowded)
{
return false;
}
file.update_duplicates(folder, dup_info);
return true;
},
index_items::file_change::derived);
}
stats.indexed_dup_folder_count = static_cast<int>(component_groups.size());
stats.indexed_crc_count = indexed_crc_count;
stats.indexed_max_compare_count = max_compare_count;
stats.predictions_ms = static_cast<int>(core::now_ms() - start_ms);
render::set_gauge(df::index_perf.pass_files, static_cast<uint32_t>(files.size()));
render::set_gauge(df::index_perf.pass_crc_held, static_cast<uint32_t>(indexed_crc_count));
render::set_gauge(df::index_perf.pass_dup_groups, static_cast<uint32_t>(component_groups.size()));
core::trace(std::format("Index update predictions: {} folders in {} ms", folder_count, stats.predictions_ms));
// Items already listed copied their duplicate counts when they were listed; presence hands them
// the new ones. Presence never asks for predictions, so this cannot cycle the way refresh_items did.
if (duplicates_changed)
{
_async.invalidate_view(view_invalid::presence);
}
if (!phash_wanted.empty() && !platform::is_closing)
{
queue_calc_perceptual_hashes(std::move(phash_wanted));
}
}
// Decoding cannot happen on the predictions walk, so the pairs it could not judge are hashed here
// and the pass is asked for again. Each pass narrows the work, so a large collection converges over
// several rounds instead of stalling on the first.
void index_state::queue_calc_perceptual_hashes(std::vector<phash_request> requests)
{
_async.queue_async(async_queue::crc, [this, requests = std::move(requests)]
{
// Results are published in groups. Hashing a file takes milliseconds, so publishing each one on
// its own found both the work queue and the write queue empty every time and woke two threads
// per file for a few microseconds of work each.
constexpr size_t publish_group = 32;
std::vector<phash_result> hashed;
hashed.reserve(publish_group);
auto published_any = false;
for (const auto& request : requests)
{
if (platform::is_closing) break;
const auto& path = request.path;
crypto::phash_rotations hash{};
auto readable = false;
// A file on the crash list faulted the process while it was open, so decoding it here would
// repeat that crash on every launch. It is declined unread, and the refusal is not stored.
const auto known_crash = crash_files().is_known_crash_file(path);
if (!known_crash)
{
core::scope_locked_inc loading(df::loading_media);
core::perf_timer timer(df::index_perf.phash_us, &df::index_perf.phash_max_us);
core::bump(df::index_perf.phash_computed);
record_open_path record(crash_files(), path, "queue_calc_perceptual_hashes");
file_read_stream stream;
if (stream.open(path) && stream.size() <= max_phash_file_bytes)
{
files ff;
core::blob owner;
readable = true;
core::bump(df::index_perf.phash_bytes, stream.size());
hash = ff.calc_perceptual_hash_rotations(stream.view_all(owner));
}
else
{
core::bump(df::index_perf.phash_unreadable);
}
}
// Every attempt is recorded, including a refusal and a file that could not be read or
// decoded. Without that the next pass asks for the same file again, forever.
if (crypto::phash_is_usable(hash[0]))
{
hashed.emplace_back(path, request.revision, hash);
core::bump(df::index_perf.phash_usable);
}
else
{
hashed.emplace_back(path, request.revision, crypto::phash_rotations{crypto::phash_declined, 0, 0, 0},
!known_crash);
if (readable) core::bump(df::index_perf.phash_declined);
}
if (hashed.size() >= publish_group)
{
published_any = true;
save_phashes(std::move(hashed), false);
hashed.clear();
hashed.reserve(publish_group);
}
}
// Published even when shutdown cut the loop short, so attempts already made are not repeated.
published_any = published_any || !hashed.empty();
save_phashes(std::move(hashed), published_any);
});
}
struct presence_match
{
item_presence state = item_presence::unknown;
df::duplicate_info duplicates = {};
};
struct presence_request
{
std::weak_ptr<df::item_element> lifetime;
core::file_path path;
core::file_size size;
core::date_t file_modified;
core::date_t media_created;
uint32_t crc32c = 0;
bool is_folder = false;
bool is_bitmap = false;
sizei dimensions;
};
struct presence_result
{
presence_request source;
presence_match match;
};
static int presence_rank(const item_presence presence)
{
switch (presence)
{
case item_presence::newer_in: return 3;
case item_presence::similar_in: return 2;
case item_presence::older_in: return 1;
default: return 0;
}
}
static bool prefer_duplicate_info(const df::duplicate_info candidate, const df::duplicate_info current)
{
if (current.group == 0) return candidate.group != 0;
if (candidate.group == 0) return false;
if (candidate.group != current.group) return candidate.group < current.group;
return candidate.count > current.count;
}
static df::copy_grade dup_match_grade(const df::index_file_item& file, const presence_request& other)
{
if (file.crc32c != 0 && file.size == other.size && file.crc32c == other.crc32c)
{
return df::copy_grade::identical;
}
const auto name_match = icmp(file.name, other.path.name()) == 0;
if (name_match && file.ft->has_trait(file_traits::av) && file.size.to_int64() != 0 && other.size.to_int64() != 0 &&
file.size == other.size)
{
return df::copy_grade::same_file;
}
const auto created = file.created();
return name_match && created.is_valid() && created == other.media_created ? df::copy_grade::same_file
: df::copy_grade::none;
}
// An outside file the cheap grades could not place, held until the walk is over because deciding it
// means decoding a picture and the walk is reading the index.
struct presence_similar_candidate
{
size_t request_index = 0;
core::file_path path;
crypto::phash_rotations phash{};
core::date_t file_modified;
uint64_t size = 0;
df::duplicate_info duplicates;
sizei dimensions;
};
static void items_possible_hashes_contains(std::vector<presence_match>& matches,
const std::vector<std::pair<unsigned, size_t>>& possible,
const std::vector<presence_request>& requests,
const df::index_file_item& indexed_file, const uint32_t hash)
{
auto lb = std::lower_bound(possible.begin(), possible.end(), hash, [](auto&& l, auto&& r) { return l.first < r; });
while (lb != possible.end() && lb->first == hash)
{
const auto request_index = lb->second;
const auto& request = requests[request_index];
const auto grade = dup_match_grade(indexed_file, request);
if (grade != df::copy_grade::none)
{
auto candidate = item_presence::unknown;
if (indexed_file.file_modified == request.file_modified ||
(indexed_file.crc32c != 0 && indexed_file.crc32c == request.crc32c))
{
candidate = item_presence::similar_in;
}
else if (indexed_file.file_modified < request.file_modified)
{
candidate = item_presence::older_in;
}
else if (indexed_file.file_modified > request.file_modified)
{
candidate = item_presence::newer_in;
}
auto duplicates = indexed_file.duplicates.load();
duplicates.grade = grade;
auto& current = matches[request_index];
if (presence_rank(candidate) > presence_rank(current.state) ||
(candidate == current.state && prefer_duplicate_info(duplicates, current.duplicates)))
{
current.state = candidate;
current.duplicates = duplicates;
}
}
++lb;
}
}
// Decides the outside photographs the cheap grades could not place. Reading and decoding happens
// here, after the index walk, and only for a capture time the collection does not crowd: the same
// refusal duplicate search makes, so both surfaces answer alike (docs/collections.md section 7.3).
static void resolve_similar_presence(index_state& index, const std::vector<presence_request>& requests,
std::vector<presence_match>& matches,
std::vector<presence_similar_candidate>& candidates)
{
if (candidates.empty()) return;
std::ranges::sort(candidates, [](auto&& left, auto&& right) { return left.request_index < right.request_index; });
files decoder;
// A member is only hashed by the predictions pass when another member shares its capture time, so
// the picture an outside file is being compared against often has no hash yet. It is computed
// here and saved, because answering "checking" forever would be an absence in all but name.
const auto hash_of = [&decoder, &index](const core::file_path path, const index_file_revision revision,
const crypto::phash_rotations& known) -> crypto::phash_rotations
{
if (known[0] != 0) return known;
// As in the predictions pass, a file on the crash list is declined unread and the refusal is
// published without being stored.
if (crash_files().is_known_crash_file(path))
{
const crypto::phash_rotations declined{crypto::phash_declined, 0, 0, 0};
index.save_phash(path, revision, declined, false);
return declined;
}
crypto::phash_rotations hash{};
{
core::scope_locked_inc loading(df::loading_media);
core::perf_timer timer(df::index_perf.phash_us, &df::index_perf.phash_max_us);
core::bump(df::index_perf.phash_computed);
core::bump(df::index_perf.phash_presence);
record_open_path record(crash_files(), path, "resolve_similar_presence");
file_read_stream stream;
if (stream.open(path) && stream.size() <= max_phash_file_bytes)
{
core::blob owner;
core::bump(df::index_perf.phash_bytes, stream.size());
hash = decoder.calc_perceptual_hash_rotations(stream.view_all(owner));
core::bump(crypto::phash_is_usable(hash[0]) ? df::index_perf.phash_usable
: df::index_perf.phash_declined);
}
else
{
core::bump(df::index_perf.phash_unreadable);
}
}
if (!crypto::phash_is_usable(hash[0])) hash = {crypto::phash_declined, 0, 0, 0};
index.save_phash(path, revision, hash);
return hash;
};
for (auto i = candidates.begin(); i != candidates.end();)
{
if (platform::is_closing) return;
auto group_end = i;
while (group_end != candidates.end() && group_end->request_index == i->request_index) ++group_end;
const auto member_count = static_cast<size_t>(std::distance(i, group_end));
const auto request_index = i->request_index;
const auto& request = requests[request_index];
// A cheaper grade already answered this file; a picture cannot make that claim stronger.
const auto already_matched = matches[request_index].state != item_presence::unknown;
if (!already_matched && member_count <= max_photos_sharing_capture_time)
{
const auto probe_hash = hash_of(request.path, {request.file_modified, request.size.to_int64()}, {});
if (crypto::phash_is_usable(probe_hash[0]))
{
// The outside file is the anchor here, so the same crowd rule applies: many members
// matching one picture in one orientation at one capture time is a burst, not a set
// of copies. A turned match is never burst evidence, exactly as duplicate search
// counts it (docs/collections.md section 7.3).
std::vector<const presence_similar_candidate*> matched;
size_t same_orientation_matches = 0;
for (auto candidate = i; candidate != group_end; ++candidate)
{
if (candidate->duplicates.same_picture_crowded) continue;
// Shape narrows before the picture is decoded, exactly as duplicate search does.
if (!same_picture_shape(request.dimensions, candidate->dimensions)) continue;
const auto candidate_hash = hash_of(candidate->path, {candidate->file_modified, candidate->size},
candidate->phash);
if (!crypto::phash_is_usable(candidate_hash[0])) continue;
if (crypto::phash_distance(probe_hash[0], candidate_hash) > max_duplicate_phash_distance)
{
continue;
}
matched.push_back(&*candidate);
if (crypto::phash_distance(probe_hash[0], candidate_hash[0]) <= max_duplicate_phash_distance)
{
++same_orientation_matches;
}
}
if (same_orientation_matches <= max_similar_pictures_at_one_capture_time)
{
for (const auto* const candidate : matched)
{
auto state = item_presence::similar_in;
if (candidate->file_modified < request.file_modified) state = item_presence::older_in;
else if (candidate->file_modified > request.file_modified) state = item_presence::newer_in;
auto duplicates = candidate->duplicates;
duplicates.grade = df::copy_grade::same_picture;
auto& current = matches[request_index];
if (presence_rank(state) > presence_rank(current.state))
{
current.state = state;
current.duplicates = duplicates;
}
}
}
}
}
i = group_end;
}
}
void index_state::queue_update_presence(const df::item_set& items)
{
core::assert_true(platform::is_ui_thread());
if (items.empty()) return;
std::vector<presence_request> requests;
requests.reserve(items.size());
for (const auto& item : items.items())
{
const auto ft = item->file_type();
const auto md = item->metadata();
requests.emplace_back(item, item->path(), item->file_size(), item->file_modified(), item->media_created(),
item->crc32c(), item->is_folder(), ft && ft->has_trait(file_traits::bitmap),
md ? md->dimensions() : sizei{});
}
_async.queue_async(async_queue::index_presence_single, [this, requests = std::move(requests)]() mutable
{
core::measure_ms ms(stats.update_presence_ms);
df::index_folder_info_map indexed_folders;
std::vector<presence_match> matches(requests.size());
for (const auto& request : requests)
{
if (!request.is_folder)
{
const auto folder = _items.find(request.path.folder());
if (folder && folder->is_in_collection)
{
indexed_folders[request.path.folder()] = folder;
}
}
}
std::vector<std::pair<uint32_t, size_t>> items_possible_hashes;
// Outside photographs whose capture time a member might share, keyed exactly rather than by the
// folded hash above, because a picture comparison is too expensive to run on a fold collision.
core::hash_map<uint64_t, std::vector<size_t>> requests_by_capture_time;
for (size_t index = 0; index < requests.size(); ++index)
{
const auto& request = requests[index];
if (request.is_folder) continue;
const auto is_indexed_folder = indexed_folders.contains(request.path.folder());
if (is_indexed_folder)
{
auto& match = matches[index];