-
Notifications
You must be signed in to change notification settings - Fork 519
/
PhotoTable.cc
1095 lines (978 loc) · 35.3 KB
/
PhotoTable.cc
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
// Copyright 2012 Viewfinder. All rights reserved.
// Author: Peter Mattis.
//
// PhotoTable maintain the following tables for assets:
//
// <asset-url>#<asset-fingerprint> -> <local-photo-id>
// <asset-fingerprint>#<asset-url> -> <local-photo-id>
//
// When an asset is encountered during a scan, we either create a new photo, or
// add the above mappings pointing to an empty string. When we notice an asset
// has been deleted we update the corresponding PhotoMetadata to remove the
// asset-url but leave the asset-fingerprint in place.
#import <re2/re2.h>
#import "AppState.h"
#import "AsyncState.h"
#import "DayTable.h"
#import "GeocodeManager.h"
#import "ImageIndex.h"
#import "LazyStaticPtr.h"
#import "LocationUtils.h"
#import "NetworkQueue.h"
#import "PhotoStorage.h"
#import "PhotoTable.h"
#import "PlacemarkHistogram.h"
#import "PlacemarkTable.h"
#import "ServerUtils.h"
#import "StringUtils.h"
#import "Timer.h"
#import "WallTime.h"
const string PhotoTable::kPhotoDuplicateQueueKeyPrefix = DBFormat::photo_duplicate_queue_key();
namespace {
const int kPhotoFSCKVersion = 3;
const int kUnquarantineVersion = 4;
const int kSecondsInHour = 60 * 60;
const int kSecondsInDay = kSecondsInHour * 24;
const WallTime kURLExpirationSlop = 60;
const string kAssetFingerprintKeyPrefix = DBFormat::asset_fingerprint_key("");
const string kDeprecatedAssetReverseKeyPrefix = DBFormat::deprecated_asset_reverse_key("");
const string kUnquarantineVersionKey = DBFormat::metadata_key("unquarantine_version");
const string kPerceptualFingerprintPrefix = "P";
const int kPerceptualFingerprintBinarySize = 20;
const int kPerceptualFingerprintSize = 29;
// S3 urls look like:
// https://s3/foo?Signature=bar&Expires=1347112861&AWSAccessKeyId=blah
LazyStaticPtr<RE2, const char*> kS3URLRE = {
".*[?&]Expires=([0-9]+).*"
};
const DBRegisterKeyIntrospect kPhotoKeyIntrospect(
DBFormat::photo_key(), NULL, [](Slice value) {
return DBIntrospect::FormatProto<PhotoMetadata>(value);
});
const DBRegisterKeyIntrospect kPhotoDuplicateQueueKeyIntrospect(
PhotoTable::kPhotoDuplicateQueueKeyPrefix,
[](Slice key) {
int64_t local_id;
if (!DecodePhotoDuplicateQueueKey(key, &local_id)) {
return string();
}
return string(Format("%d", local_id));
}, NULL);
const DBRegisterKeyIntrospect kPhotoServerKeyIntrospect(
DBFormat::photo_server_key(), NULL, [](Slice value) {
return value.ToString();
});
const DBRegisterKeyIntrospect kPhotoURLKey(
DBFormat::photo_url_key(""), NULL, [](Slice value) {
return value.ToString();
});
const DBRegisterKeyIntrospect kAssetKeyIntrospect(
DBFormat::asset_key(""), NULL, [](Slice value) {
return value.ToString();
});
const DBRegisterKeyIntrospect kAssetDeprecatedReverseKeyIntrospect(
DBFormat::deprecated_asset_reverse_key(""), NULL, [](Slice value) {
return value.ToString();
});
const DBRegisterKeyIntrospect kAssetFingerprintKeyIntrospect(
DBFormat::asset_fingerprint_key(""), NULL, [](Slice value) {
return value.ToString();
});
string EncodePhotoURLKey(int64_t id, const string& name) {
return DBFormat::photo_url_key(Format("%d/%s", id, name));
}
} // namespace
string EncodeAssetFingerprintKey(const Slice& fingerprint) {
string s = kAssetFingerprintKeyPrefix;
fingerprint.AppendToString(&s);
return s;
}
string EncodePhotoDuplicateQueueKey(int64_t local_id) {
string s = PhotoTable::kPhotoDuplicateQueueKeyPrefix;
OrderedCodeEncodeVarint64(&s, local_id);
return s;
}
string EncodePerceptualFingerprint(const Slice& term) {
DCHECK_EQ(kPerceptualFingerprintBinarySize, term.size());
return kPerceptualFingerprintPrefix + Base64Encode(term);
}
bool DecodeAssetFingerprintKey(Slice key, Slice* fingerprint) {
if (!key.starts_with(kAssetFingerprintKeyPrefix)) {
return false;
}
key.remove_prefix(kAssetFingerprintKeyPrefix.size());
*fingerprint = key;
return true;
}
bool DecodePhotoDuplicateQueueKey(Slice key, int64_t* local_id) {
if (!key.starts_with(PhotoTable::kPhotoDuplicateQueueKeyPrefix)) {
return false;
}
key.remove_prefix(PhotoTable::kPhotoDuplicateQueueKeyPrefix.size());
*local_id = OrderedCodeDecodeVarint64(&key);
return true;
}
bool DecodePerceptualFingerprint(Slice fingerprint, string* term) {
if (fingerprint.size() != kPerceptualFingerprintSize ||
!fingerprint.starts_with(kPerceptualFingerprintPrefix)) {
return false;
}
fingerprint.remove_prefix(kPerceptualFingerprintPrefix.size());
if (term) {
*term = Base64Decode(fingerprint);
}
return true;
}
bool DecodeDeprecatedAssetReverseKey(Slice key, Slice* fingerprint, Slice* url) {
if (!key.starts_with(kDeprecatedAssetReverseKeyPrefix)) {
return false;
}
key.remove_prefix(kDeprecatedAssetReverseKeyPrefix.size());
const int pos = key.rfind('#');
if (pos == key.npos) {
return false;
}
if (fingerprint) {
*fingerprint = key.substr(0, pos);
}
if (url) {
*url = key.substr(pos + 1);
}
return true;
}
PhotoTable_Photo::PhotoTable_Photo(AppState* state, const DBHandle& db, int64_t id)
: state_(state),
db_(db) {
mutable_id()->set_local_id(id);
}
void PhotoTable_Photo::MergeFrom(const PhotoMetadata& m) {
// Some assertions that immutable properties don't change.
if (episode_id().has_server_id() && m.episode_id().has_server_id()) {
DCHECK_EQ(episode_id().server_id(), m.episode_id().server_id());
}
if (has_user_id() && m.has_user_id()) {
DCHECK_EQ(user_id(), m.user_id());
}
if (has_timestamp() && m.has_timestamp()) {
// TODO(peter): I have photos in my asset library with timestamps that
// differ by more than a second from the data stored on the server.
// DCHECK_EQ(trunc(timestamp()), trunc(m.timestamp()));
}
PhotoMetadata::MergeFrom(m);
}
void PhotoTable_Photo::MergeFrom(const ::google::protobuf::Message&) {
DIE("MergeFrom(Message&) should not be used");
}
int64_t PhotoTable_Photo::GetDeviceId() const {
if (!id().has_server_id()) {
return state_->device_id();
}
int64_t device_id = 0;
int64_t dummy_id = 0;
WallTime dummy_timestamp = 0;
DecodePhotoId(
id().server_id(), &device_id, &dummy_id, &dummy_timestamp);
return device_id;
}
int64_t PhotoTable_Photo::GetUserId() const {
return has_user_id() ? user_id() : state_->user_id();
}
bool PhotoTable_Photo::GetLocation(Location* loc, Placemark* pm) {
if (has_location() && loc) {
loc->CopyFrom(location());
}
// If we have a location, but the placemark isn't set, try to
// reverse geocode.
// NOTE: there are some photos which have location and placemark
// is set, but is empty. Make sure we reverse geocode in this case.
if (has_location() && (!has_placemark() || !placemark().has_country())) {
state_->photo_table()->MaybeReverseGeocode(local_id());
return true;
}
if (has_placemark() && pm) {
pm->CopyFrom(placemark());
}
return has_location();
}
string PhotoTable_Photo::FormatLocation(bool shorten) {
Location location;
Placemark placemark;
if (GetLocation(&location, &placemark)) {
string s;
state_->placemark_histogram()->FormatLocation(location, placemark, shorten, &s);
return s;
}
return shorten ? "" : "Location Unavailable";
}
string PhotoTable_Photo::GetURL(const string& name) {
return db_->Get<string>(EncodePhotoURLKey(local_id(), name));
}
string PhotoTable_Photo::GetUnexpiredURL(
const string& name, const DBHandle& updates) {
const string url = GetURL(name);
if (!url.empty()) {
int expires = 0;
if (!RE2::FullMatch(url, *kS3URLRE, &expires)) {
return url;
}
if (expires >= WallTime_Now() + kURLExpirationSlop) {
return url;
}
DeleteURL(name, updates);
}
return string();
}
void PhotoTable_Photo::SetURL(
const string& name, const string& url, const DBHandle& updates) {
updates->Put(EncodePhotoURLKey(local_id(), name), url);
}
void PhotoTable_Photo::DeleteURL(const string& name, const DBHandle& updates) {
updates->Delete(EncodePhotoURLKey(local_id(), name));
}
bool PhotoTable_Photo::ShouldUpdateTimestamp(WallTime exif_timestamp) {
const int64_t orig_seconds = trunc(timestamp());
const int64_t exif_seconds = trunc(exif_timestamp);
if (fabs(orig_seconds - exif_seconds) < kSecondsInDay &&
(orig_seconds % kSecondsInHour) == (exif_seconds % kSecondsInHour)) {
LOG("photo: %s: original (%s) and exif (%s) timestamps [probably] differ as "
"time zone information is not captured in exif data; ignoring difference "
"and continuing with original", id(),
WallTimeFormat("%F %T", timestamp()),
WallTimeFormat("%F %T", exif_timestamp));
return false;
}
return true;
}
void PhotoTable_Photo::Quarantine(
const string& reason, const DBHandle& updates) {
LOG("photo: quarantining %s: %s", id(), reason);
// Mark the photo as quarantined in the database. This will prevent the photo
// from reappearing the next time the app starts.
set_label_error(true);
// Remove the photo from every episode it is posted to. This causes it to
// disappear in the UI.
vector<int64_t> episode_ids;
state_->episode_table()->ListEpisodes(local_id(), &episode_ids, updates);
for (int i = 0; i < episode_ids.size(); ++i) {
EpisodeHandle e = state_->episode_table()->LoadEpisode(episode_ids[i], updates);
e->Lock();
e->QuarantinePhoto(local_id());
e->SaveAndUnlock(updates);
}
}
void PhotoTable_Photo::Invalidate(const DBHandle& updates) {
vector<int64_t> episode_ids;
state_->episode_table()->ListEpisodes(local_id(), &episode_ids, updates);
for (int i = 0; i < episode_ids.size(); ++i) {
EpisodeHandle e = state_->episode_table()->LoadEpisode(episode_ids[i], updates);
if (e.get()) {
e->Invalidate(updates);
}
}
}
bool PhotoTable_Photo::Load() {
disk_asset_keys_ = GetAssetKeySet();
disk_perceptual_fingerprint_ = perceptual_fingerprint();
day_table_fields_ = GetDayTableFields();
return true;
}
void PhotoTable_Photo::SaveHook(const DBHandle& updates) {
StringSet asset_keys_set = GetAssetKeySet();
for (StringSet::iterator it = disk_asset_keys_.begin(); it != disk_asset_keys_.end(); ++it) {
if (!ContainsKey(asset_keys_set, *it)) {
updates->Delete(*it);
}
}
disk_asset_keys_ = asset_keys_set;
for (int i = 0; i < asset_keys_size(); i++) {
updates->Put(asset_keys(i), local_id());
}
if (disk_perceptual_fingerprint_.SerializeAsString() !=
perceptual_fingerprint().SerializeAsString()) {
const string id_str = ToString(id().local_id());
state_->image_index()->Remove(disk_perceptual_fingerprint_, id_str, updates);
disk_perceptual_fingerprint_ = perceptual_fingerprint();
state_->image_index()->Add(disk_perceptual_fingerprint_, id_str, updates);
for (int i = 0; i < disk_perceptual_fingerprint_.terms_size(); ++i) {
AddAssetFingerprint(EncodePerceptualFingerprint(
disk_perceptual_fingerprint_.terms(i)), false);
}
}
for (int i = 0; i < asset_fingerprints_size(); i++) {
updates->Put(EncodeAssetFingerprintKey(asset_fingerprints(i)), local_id());
}
// The "removed" and "unshared" labels affect the post relationship between
// an episode and a photo. They are only used in client/server communication
// and should not be persisted to disk.
clear_label_removed();
clear_label_unshared();
if (has_location() && has_placemark() && !placemark_histogram()) {
set_placemark_histogram(true);
state_->placemark_histogram()->AddPlacemark(
placemark(), location(), updates);
}
const string new_day_table_fields = GetDayTableFields();
if (day_table_fields_ != new_day_table_fields) {
// Only invalidate if the day table fields have changed. Note that we don't
// have to invalidate the episodes if the day table fields are empty, which
// indicates that the photo did not previously exist. If the photo was just
// created the episode it was added to (if any) will have already been
// invalidated.
if (!day_table_fields_.empty()) {
// Invalidate all activities which have shared this photo and all episodes
// which contain it.
Invalidate(updates);
}
day_table_fields_ = new_day_table_fields;
}
// Ugh, PhotoTable_Photo is the base class but PhotoHandle needs a pointer to
// the superclass.
typedef ContentTable<PhotoTable_Photo>::Content Content;
Content* content = reinterpret_cast<Content*>(this);
state_->net_queue()->QueuePhoto(PhotoHandle(content), updates);
if (!label_error() && candidate_duplicates_size() > 0) {
updates->Put(EncodePhotoDuplicateQueueKey(local_id()), string());
AppState* s = state_;
updates->AddCommitTrigger("PhotoDuplicateQueue", [s] {
s->ProcessPhotoDuplicateQueue();
});
} else {
updates->Delete(EncodePhotoDuplicateQueueKey(local_id()));
}
}
void PhotoTable_Photo::DeleteHook(const DBHandle& updates) {
for (StringSet::iterator it = disk_asset_keys_.begin(); it != disk_asset_keys_.end(); ++it) {
updates->Delete(*it);
}
if (disk_perceptual_fingerprint_.terms_size() > 0) {
const string id_str = ToString(id().local_id());
state_->image_index()->Remove(disk_perceptual_fingerprint_, id_str, updates);
}
if (has_location() && has_placemark() && placemark_histogram()) {
clear_placemark_histogram();
state_->placemark_histogram()->RemovePlacemark(
placemark(), location(), updates);
}
// Ugh, PhotoTable_Photo is the base class but PhotoHandle needs a pointer to
// the superclass.
typedef ContentTable<PhotoTable_Photo>::Content Content;
Content* content = reinterpret_cast<Content*>(this);
state_->net_queue()->DequeuePhoto(PhotoHandle(content), updates);
updates->Delete(EncodePhotoDuplicateQueueKey(local_id()));
state_->photo_storage()->DeleteAll(local_id(), updates);
}
StringSet PhotoTable_Photo::GetAssetKeySet() const {
return StringSet(asset_keys().begin(), asset_keys().end());
}
string PhotoTable_Photo::GetDayTableFields() const {
PhotoMetadata m;
if (has_id()) {
m.mutable_id()->CopyFrom(id());
// The server-id for a photo is set the first time the photo is loaded and
// its original timestamp is verified. We don't need to perform a day table
// refresh when that occurs.
m.mutable_id()->clear_server_id();
}
if (has_episode_id()) {
m.mutable_episode_id()->CopyFrom(episode_id());
}
if (has_placemark()) {
m.mutable_placemark()->CopyFrom(placemark());
}
if (has_aspect_ratio()) {
m.set_aspect_ratio(aspect_ratio());
}
if (has_timestamp()) {
m.set_timestamp(timestamp());
}
return m.SerializeAsString();
}
bool PhotoTable_Photo::ShouldAddPhotoToEpisode() const {
if (!has_aspect_ratio() ||
std::isnan(aspect_ratio()) ||
!has_timestamp() ||
(candidate_duplicates_size() > 0)) {
// We don't have enough photo metadata to match the photo to an
// episode. Note that we'll add a photo to an episode before we've
// downloaded any of the photo images and rely on the prioritization of
// images needed for the UI.
return false;
}
return true;
}
void PhotoTable_Photo::FindCandidateDuplicates() {
if (!has_perceptual_fingerprint()) {
return;
}
WallTimer timer;
StringSet matched_ids;
state_->image_index()->Search(state_->db(), perceptual_fingerprint(), &matched_ids);
clear_candidate_duplicates();
for (StringSet::iterator iter(matched_ids.begin());
iter != matched_ids.end();
++iter) {
const int64_t local_id = FromString<int64_t>(*iter);
if (local_id == id().local_id()) {
// Never consider a photo its own candidate duplicate.
continue;
}
add_candidate_duplicates(local_id);
}
if (candidate_duplicates_size() > 0) {
LOG("photo: %s: %d candidate duplicates (%.2f ms): %s",
id(), candidate_duplicates_size(), timer.Milliseconds(), matched_ids);
}
}
bool PhotoTable_Photo::HasAssetUrl() const {
return asset_keys_size() > 0;
}
bool PhotoTable_Photo::AddAssetKey(const string& asset_key) {
Slice url;
Slice fingerprint;
if (!DecodeAssetKey(asset_key, &url, &fingerprint)) {
LOG("photo: invalid asset key %s", asset_key);
return false;
}
bool changed = false;
if (!fingerprint.empty()) {
changed = AddAssetFingerprint(fingerprint, false);
}
DCHECK(!url.empty()); // Shouldn't happen any more, but just in case.
if (!url.empty()) {
bool found = false;
for (int i = 0; i < asset_keys_size(); i++) {
if (asset_keys(i) == asset_key) {
found = true;
break;
}
Slice url2, fingerprint2;
if (!DecodeAssetKey(asset_keys(i), &url2, &fingerprint2)) {
continue;
}
if (url == url2 && !fingerprint.empty() && fingerprint2.empty()) {
// Upgrade the existing url-only asset key to include the fingerprint.
set_asset_keys(i, asset_key);
changed = true;
found = true;
}
}
if (!found) {
add_asset_keys(asset_key);
changed = true;
}
}
return changed;
}
bool PhotoTable_Photo::AddAssetFingerprint(const Slice& fingerprint, bool from_server) {
for (int i = 0; i < asset_fingerprints_size(); i++) {
if (asset_fingerprints(i) == fingerprint) {
return false;
}
}
add_asset_fingerprints(fingerprint.as_string());
// If the server doesn't know about this fingerprint, upload the metadata.
if (!from_server) {
set_update_metadata(true);
} else {
string term;
if (DecodePerceptualFingerprint(fingerprint, &term)) {
ImageFingerprint* pf = mutable_perceptual_fingerprint();
for (int i = 0; i < pf->terms_size(); i++) {
if (pf->terms(i) == term) {
return true;
}
}
pf->add_terms(term);
}
}
return true;
}
bool PhotoTable_Photo::RemoveAssetKey(const string& asset_key) {
for (int i = 0; i < asset_keys_size(); i++) {
if (asset_keys(i) == asset_key) {
RemoveAssetKeyByIndex(i);
return true;
}
}
return false;
}
bool PhotoTable_Photo::RemoveAssetKeyByIndex(int index) {
DCHECK_GE(index, 0);
DCHECK_LT(index, asset_keys_size());
if (index < 0 || index >= asset_keys_size()) {
return false;
}
ProtoRepeatedFieldRemoveElement(mutable_asset_keys(), index);
return true;
}
bool PhotoTable_Photo::InLibrary() {
return state_->episode_table()->ListLibraryEpisodes(
id().local_id(), NULL, db_);
}
bool PhotoTable_Photo::MaybeSetServerId() {
const int64_t device_id = state_->device_id();
if (id().has_server_id() || !device_id) {
return false;
}
mutable_id()->set_server_id(
EncodePhotoId(device_id, id().local_id(), timestamp()));
return true;
}
PhotoTable::PhotoTable(AppState* state)
: ContentTable<Photo>(state,
DBFormat::photo_key(),
DBFormat::photo_server_key(),
kPhotoFSCKVersion,
DBFormat::metadata_key("photo_table_fsck")),
geocode_in_progress_(0) {
}
PhotoTable::~PhotoTable() {
}
void PhotoTable::Reset() {
}
PhotoHandle PhotoTable::LoadPhoto(const PhotoId& id, const DBHandle& db) {
PhotoHandle ph;
if (id.has_local_id()) {
ph = LoadPhoto(id.local_id(), db);
}
if (!ph.get() && id.has_server_id()) {
ph = LoadPhoto(id.server_id(), db);
}
return ph;
}
PhotoHandle PhotoTable::LoadAssetPhoto(
const Slice& asset_key, const DBHandle& db) {
const int64_t id = AssetToDeviceId(asset_key, false, db);
if (id == -1) {
return ContentHandle();
}
return LoadPhoto(id, db);
}
bool PhotoTable::AssetPhotoExists(
const Slice& url, const Slice& fingerprint, const DBHandle& db) {
return AssetToDeviceId(url, fingerprint, true, db) != -1;
}
bool PhotoTable::AssetPhotoExists(
const Slice& asset_key, const DBHandle& db) {
return AssetToDeviceId(asset_key, true, db) != -1;
}
void PhotoTable::AssetsNotFound(
const StringSet& not_found, const DBHandle& updates) {
for (StringSet::const_iterator iter(not_found.begin());
iter != not_found.end();
++iter) {
const string& url = *iter;
DCHECK(!url.empty());
if (url.empty()) {
// TODO(peter): Perhaps do something slightly more encompassing as far as
// validation. A super short prefix could still remove a bunch of stuff,
// though asset-urls currently appear to be a fixed length (78 bytes?).
continue;
}
// Loop over all (there is normally only 1, but might be more if this is
// the first full scan after asset urls have changed) of the asset-keys
// with url as a prefix.
for (DB::PrefixIterator iter(updates, EncodeAssetKey(url, ""));
iter.Valid();
iter.Next()) {
const Slice key = iter.key();
Slice fingerprint;
if (!DecodeAssetKey(key, NULL, &fingerprint)) {
// This shouldn't happen.
DCHECK(false) << ": unable to decode: " << key;
continue;
}
// Load the associated photo and clear the url from the asset key.
PhotoHandle ph = LoadAssetPhoto(key, updates);
if (ph.get()) {
ph->Lock();
// Clear the url, but leave the fingerprint.
ph->RemoveAssetKey(key.as_string());
if (ph->upload_metadata() && !ph->HasAssetUrl()) {
// The photo has not been uploaded to the server and only exists
// locally. The local asset has disappeared. Quarantine.
ph->Quarantine("garbage collect", updates);
}
ph->SaveAndUnlock(updates);
} else {
// No asset associated with the key. Nothing to update, just
// delete.
updates->Delete(key);
if (!fingerprint.empty()) {
updates->Delete(EncodeAssetFingerprintKey(fingerprint));
}
}
}
}
}
void PhotoTable::DeleteAllImages(int64_t photo_id, const DBHandle& updates) {
const PhotoHandle ph = LoadPhoto(photo_id, updates);
LOG("photo table: deleting all images for photo %d", photo_id);
if (ph.get()) {
// Clear the download bits: the photo won't ever be displayed in the UI, so
// there is no need to download any images.
ph->Lock();
ph->clear_download_thumbnail();
ph->clear_download_full();
ph->clear_download_medium();
ph->clear_download_original();
// Clear asset keys from photo metadata, delete from index, and try
// to delete underlying assets.
for (int i = 0; i < ph->asset_keys_size(); i++) {
const string key = ph->asset_keys(i);
// Clear the url, but leave the fingerprint.
ph->RemoveAssetKey(key);
updates->Delete(key);
state_->DeleteAsset(key);
}
ph->SaveAndUnlock(updates);
}
state_->photo_storage()->DeleteAll(photo_id, updates);
}
bool PhotoTable::PhotoInLibrary(int64_t photo_id, const DBHandle& db) {
PhotoHandle ph = LoadPhoto(photo_id, db);
return ph.get() && ph->InLibrary();
}
bool PhotoTable::IsAssetPhotoEqual(
const PhotoMetadata& local, const PhotoMetadata& server) {
std::set<string> local_fingerprints;
std::set<string> server_fingerprints;
for (int i = 0; i < local.asset_fingerprints_size(); ++i) {
local_fingerprints.insert(local.asset_fingerprints(i));
}
for (int i = 0; i < server.asset_fingerprints_size(); ++i) {
server_fingerprints.insert(server.asset_fingerprints(i));
}
if (!server_fingerprints.empty()) {
// If we have an asset fingerprint on the server, only consider the two
// photos equal if we have the matching fingerprint locally.
// All photos uploaded since version 1.2 have fingerprints.
return SetsIntersect(local_fingerprints, server_fingerprints);
}
// If we don't have fingerprints, use some crude heuristics to tell if the photo
// is definitely different, otherwise assume nothing has happened that would
// cause asset ids to be reused.
// Unfortunately, most metadata fields are unreliable for this purpose.
// Photos can losslessly have their orientation fields normalized, and iTunes
// appears to sometimes alter timestamps based on the current local timezone.
if (local.has_location() != server.has_location()) {
// The local photo has a location while the server photo does not (or vice
// versa). These cannot be the same photo.
return false;
}
if (local.has_location()) {
if (DistanceBetweenLocations(local.location(), server.location()) > .1) {
// The local and server photo locations differ too much. These cannot be
// the same photo.
return false;
}
}
return true;
}
bool PhotoTable::ReverseGeocode(
int64_t photo_id, GeocodeCallback completion) {
// This must always be running on the main thread.
DCHECK(dispatch_is_main_thread());
if (!dispatch_is_main_thread()) {
LOG("cannot reverse geocode except on main thread; this should never happen");
return false;
}
if (!state_->geocode_manager()) {
return false;
}
PhotoHandle p = LoadPhoto(photo_id, state_->db());
if (!p.get()) {
LOG("photo: %s is not a valid photo id", photo_id);
return false;
}
// Exit if:
// - No location set.
// - Invalid location.
// - A previous error with placemark was encountered.
// - We already have a valid placemark.
if (!p->has_location() ||
!PlacemarkTable::IsLocationValid(p->location()) ||
p->error_placemark_invalid() ||
(p->has_placemark() && PlacemarkTable::IsPlacemarkValid(p->placemark()))) {
return false;
}
VLOG("reverse geocode on location: %s", p->location());
PlacemarkHandle h = state_->placemark_table()->FindPlacemark(p->location(), state_->db());
if (h->valid()) {
// Optimized the reverse geocode out of existence by reusing the placemark
// for a previously reverse geocoded photo at the same location.
LOG("reverse geocode photo %d with location: %s, placemark: %s",
p->id().local_id(), p->location(), *h);
p->Lock();
p->mutable_placemark()->CopyFrom(*h);
DBHandle updates = state_->NewDBTransaction();
p->SaveAndUnlock(updates);
updates->Commit();
return false;
}
// Look up the callback set for this location.
const Location* l = &h->location();
GeocodeCallbackSet* callbacks = geocode_callback_map_[l];
const bool do_reverse_geocode = !callbacks;
if (!callbacks) {
callbacks = new GeocodeCallbackSet;
geocode_callback_map_[l] = callbacks;
}
// Add the completion to the callback set.
callbacks->Add(completion);
if (do_reverse_geocode) {
// Only run reverse geocoding for the first caller interested in reverse
// geocoding this location.
CHECK(state_->geocode_manager()->ReverseGeocode(
l, [this, p, h, l, callbacks](const Placemark* m) {
DBHandle updates = state_->NewDBTransaction();
p->Lock();
if (m && PlacemarkTable::IsPlacemarkValid(*m)) {
h->Lock();
h->CopyFrom(*m);
h->SaveAndUnlock(updates);
p->mutable_placemark()->CopyFrom(*m);
} else if (m) {
// On an invalid placemark, indicate a placemark error so we don't retry.
p->set_error_placemark_invalid(true);
} else {
p->mutable_placemark()->Clear();
}
p->SaveAndUnlock(updates);
updates->Commit();
// Erase from geocode_callback_map_ before running the
// callbacks because running the callbacks might want to
// reverse geocode the same placemark again if geocoding
// failed.
geocode_callback_map_.erase(l);
callbacks->Run(p->has_placemark());
delete callbacks;
}));
} else {
CHECK_GT(callbacks->size(), 1);
}
return true;
}
void PhotoTable::MaybeUnquarantinePhoto(
const PhotoHandle& ph, const DBHandle& updates) {
vector<int64_t> episode_ids;
state_->episode_table()->ListEpisodes(ph->id().local_id(), &episode_ids, updates);
if (episode_ids.empty()) {
// Try to load original episode.
episode_ids.push_back(ph->episode_id().local_id());
}
int count = 0;
for (int i = 0; i < episode_ids.size(); ++i) {
EpisodeHandle eh = state_->episode_table()->LoadEpisode(episode_ids[i], updates);
if (eh.get()) {
eh->Lock();
eh->AddPhoto(ph->id().local_id());
eh->SaveAndUnlock(updates);
++count;
}
}
if (!count) {
LOG("photo unquarantine: photo %s had no episodes", *ph);
return;
}
LOG("photo unquarantine: resetting error bit on photo %s and "
"re-posting to %d episodes", *ph, count);
ph->Lock();
ph->clear_label_error();
if (ph->error_upload_thumbnail()) {
// If we encountered an error uploading the thumbnail, try
// uploading the metadata again.
ph->set_upload_metadata(true);
}
ph->SaveAndUnlock(updates);
}
void PhotoTable::MaybeUnquarantinePhoto(int64_t photo_id) {
DBHandle updates = state_->NewDBTransaction();
PhotoHandle ph = LoadPhoto(photo_id, updates);
MaybeUnquarantinePhoto(ph, updates);
updates->Commit();
}
bool PhotoTable::MaybeUnquarantinePhotos(ProgressUpdateBlock progress_update) {
const int cur_version = state_->db()->Get<int>(kUnquarantineVersionKey, 0);
if (cur_version >= kUnquarantineVersion) {
return false;
}
if (progress_update) {
progress_update("Reviving Missing Photos");
}
int unquarantines = 0;
DBHandle updates = state_->NewDBTransaction();
for (DB::PrefixIterator iter(updates, DBFormat::photo_key());
iter.Valid();
iter.Next()) {
const int64_t ph_id = DecodeContentKey(iter.key());
PhotoHandle ph = LoadPhoto(ph_id, updates);
if (ph->label_error()) {
MaybeUnquarantinePhoto(ph, updates);
++unquarantines;
}
}
updates->Put<int>(kUnquarantineVersionKey, kUnquarantineVersion);
updates->Commit();
return unquarantines > 0;
}
void PhotoTable::MaybeReverseGeocode(int64_t photo_id) {
MutexLock l(&mu_);
if (geocode_in_progress_) {
return;
}
const GeocodeCallback completion = [this](bool success) {
MutexLock l(&mu_);
--geocode_in_progress_;
};
// Need to run this on a different thread as we can arrive at this
// point with locks held and PhotoManager::ReverseGeocode may invoke
// its callback synchronously in case the requested location is in
// the placemark table.
state_->async()->dispatch_after_main(
0, [this, photo_id, completion] {
if (ReverseGeocode(photo_id, completion)) {
++geocode_in_progress_;
}
});
}
int64_t PhotoTable::AssetToDeviceId(
const Slice& url, const Slice& fingerprint,
bool require_url_match, const DBHandle& db) {
// Look up the existing mapping for the url.
int64_t local_id = -1;
if (!url.empty()) {
for (DB::PrefixIterator iter(db, EncodeAssetKey(url, ""));
iter.Valid();
iter.Next()) {
Slice existing_url;
Slice existing_fingerprint;
if (!DecodeAssetKey(iter.key(), &existing_url, &existing_fingerprint)) {
// This shouldn't happen.
DCHECK(false) << ": unable to decode: " << iter.key();
continue;
}
if (require_url_match && url != existing_url) {
continue;
}
if (fingerprint.empty() ||
existing_fingerprint.empty() ||
fingerprint == existing_fingerprint) {
local_id = FromString<int64_t>(iter.value(), -1);
}
break;
}
}
if (local_id == -1 && !require_url_match && !fingerprint.empty()) {
local_id = db->Get<int64_t>(EncodeAssetFingerprintKey(fingerprint), -1);
}
return local_id;
}
int64_t PhotoTable::AssetToDeviceId(
const Slice& asset_key, bool require_url_match,
const DBHandle& db) {
Slice url;
Slice fingerprint;
if (!DecodeAssetKey(asset_key, &url, &fingerprint)) {
return false;
}
return AssetToDeviceId(url, fingerprint, require_url_match, db);
}
bool PhotoTable::FSCKImpl(int prev_fsck_version, const DBHandle& updates) {
LOG("FSCK: PhotoTable");
bool changes = false;
for (DB::PrefixIterator iter(updates, DBFormat::photo_key());
iter.Valid();
iter.Next()) {
const Slice key = iter.key();
const Slice value = iter.value();
PhotoMetadata pm;
if (pm.ParseFromArray(value.data(), value.size())) {
PhotoHandle ph = LoadPhoto(pm.id().local_id(), updates);