-
Notifications
You must be signed in to change notification settings - Fork 0
/
Repository.cpp
1451 lines (1237 loc) · 45.8 KB
/
Repository.cpp
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 2021 Garmin Ltd. or its subsidiaries.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------*/
/**
@file
@brief contains access to ActiveCaptain database.
Copyright 2017-2021 by Garmin Ltd. or its subsidiaries.
*/
#define DBG_MODULE "ACDB"
#define DBG_TAG "Repository"
#include <iterator>
#include <map>
#include <memory>
#include <set>
#include <time.h>
#include <utility>
#include <vector>
#include "DBG_pub.h"
#include "Acdb/DatabaseConfig.hpp"
#include "Acdb/EventDispatcher.hpp"
#include "Acdb/FileUtil.hpp"
#include "Acdb/MapMarker.hpp"
#include "Acdb/Presentation/PresentationMarker.hpp"
#include "Acdb/Repository.hpp"
#include "Acdb/PrvTypes.hpp"
#include "Acdb/PubTypes.hpp"
#include "Acdb/RwlLocker.hpp"
#include "Acdb/SqliteCppUtil.hpp"
#include "Acdb/StringUtil.hpp"
#include "Acdb/Version.hpp"
#include "Acdb/TextTranslator.hpp"
#include "acdb_prv_config.h"
#if (acdb_MFD_DB_SHARING_SUPPORT)
#include "Acdb/Manifest.hpp"
#include "SYNC_pub.h"
#include "SYNC_pub_dataset.h"
#include "UFS_pub.h"
#include "UFS_pub_search.h"
#include "UFS_pub_types.h"
#include "VCM_pub_card.h"
#endif
#if (acdb_SIDELOAD_SERVER_SUPPORT)
#include "Acdb/SideloadServer.hpp"
#endif
namespace Acdb {
const char* ExternalDbPath = "/Garmin/acdb";
const std::string DbName("active_captain");
const std::string DbExt(".db");
const std::string JournalExt("-wal");
const std::string TmpExt(".tmp");
const std::string ZipExt(".gz");
const std::string SupportedSchemaVer("2.0.0.0");
//----------------------------------------------------------------
//!
//! @public
//! @brief Constructor
//!
//----------------------------------------------------------------
Repository::Repository(const std::string& aDbPath)
: mDbPath(aDbPath),
mRwl(),
mInfoAdapter(),
mMarkerAdapter(),
mPresentationAdapter(),
mTranslationAdapter(),
mUpdateAdapter() {} // end of Repository
//----------------------------------------------------------------
//!
//! @public
//! @details Start a transaction. The caller must hold the
//! database write lock. Assumes the database is
//! open.
//!
//----------------------------------------------------------------
bool Repository::BeginTransaction() const {
DBG_ASSERT(mDatabase, "Database must be open.");
try {
mDatabase->exec("BEGIN TRANSACTION;");
return true;
} catch (...) {
return false;
}
} // end of BeginTransaction
//----------------------------------------------------------------
//!
//! @public
//! @details Finalize the transaction, it will be committed
//! if success is true or rolled back if it is false.
//! Assumes the database is open.
//!
//----------------------------------------------------------------
void Repository::EndTransaction(const bool aSuccess) const {
bool success = aSuccess;
DBG_ASSERT(mDatabase, "Database must be open.");
if (success) {
try {
mDatabase->exec("END TRANSACTION;");
} catch (...) {
success = false;
}
}
if (!success) {
DBG_D("Transaction failed, rolling back.");
try {
mDatabase->exec("ROLLBACK;");
} catch (...) {
}
}
} // end of EndTransaction
//----------------------------------------------------------------
//!
//! @public
//! @brief Apply the marker update lists to database
//!
//! Takes a list of item entries and performs one of the
//! following actions based on the item's type:
//! * Removes deleted item from the database.
//! * Inserts new item to the database.
//! * Updates existing items in the database.
//!
//! @returns true on success, false otherwise.
//!
//----------------------------------------------------------------
bool Repository::ApplyMarkerUpdateToDb(std::vector<MarkerTableDataCollection>& aMarkerList,
const TileXY* aTileXY) {
if (aMarkerList.empty()) {
return false;
}
RwlLocker locker{mRwl, true};
if (!IsOpen()) {
DBG_ASSERT_ALWAYS("Database is not open. Update applied in bad state.");
return false;
}
bool success = BeginTransaction();
// Process all marker entries
if (success) {
uint64_t lastUpdateMax = 0;
success = mUpdateAdapter->UpdateMarkers(aMarkerList, lastUpdateMax);
// If this update came from syncing a tile, update tileLastUpdate table.
if (aTileXY != nullptr) {
LastUpdateInfoType lastUpdateInfo;
success = success && mInfoAdapter->GetTileLastUpdateInfo(*aTileXY, lastUpdateInfo);
// Sanity check -- only write a new lastUpdate value if it's newer than what we already have.
if (lastUpdateMax > lastUpdateInfo.mMarkerLastUpdate) {
lastUpdateInfo.mMarkerLastUpdate = lastUpdateMax;
success = success && mInfoAdapter->WriteTileLastUpdateInfo(*aTileXY, lastUpdateInfo);
}
}
}
EndTransaction(success);
return success;
} // end of ApplyMarkerUpdateToDb
//----------------------------------------------------------------
//!
//! @public
//! @brief Apply the review update lists to database
//!
//! Takes a list of review entries and performs one of the
//! following actions:
//! * Inserts new review to the database.
//! * Updates an existing review in the database
//!
//! @returns true on success, false otherwise.
//!
//----------------------------------------------------------------
bool Repository::ApplyReviewUpdateToDb(std::vector<ReviewTableDataCollection>& aReviewList,
const TileXY* aTileXY) {
if (aReviewList.empty()) {
return false;
}
RwlLocker locker{mRwl, true};
if (!IsOpen()) {
DBG_ASSERT_ALWAYS("Database is not open. Update applied in bad state.");
return false;
}
bool success = BeginTransaction();
// Process all review entries
if (success) {
uint64_t lastUpdateMax = 0;
success = mUpdateAdapter->UpdateReviews(aReviewList, lastUpdateMax);
// If this update came from syncing a tile, update tileLastUpdate table.
if (aTileXY != nullptr) {
LastUpdateInfoType lastUpdateInfo;
success = success && mInfoAdapter->GetTileLastUpdateInfo(*aTileXY, lastUpdateInfo);
// Sanity check -- only write a new lastUpdate value if it's newer than what we already have.
if (lastUpdateMax > lastUpdateInfo.mUserReviewLastUpdate) {
lastUpdateInfo.mUserReviewLastUpdate = lastUpdateMax;
success = success && mInfoAdapter->WriteTileLastUpdateInfo(*aTileXY, lastUpdateInfo);
}
}
}
EndTransaction(success);
return success;
} // end of ApplyReviewUpdateToDb
//----------------------------------------------------------------
//!
//! @public
//! @brief Apply the updates to support tables to database
//!
//! Takes a list of entries and performs one of the
//! following actions:
//! * Inserts new entry to the database.
//! * Updates an existing entry in the database
//!
//! @returns true on success, false otherwise.
//!
//----------------------------------------------------------------
bool Repository::ApplySupportTableUpdateToDb(
std::vector<LanguageTableDataType>& aLanguageList,
std::vector<MustacheTemplateTableDataType>& aMustacheTemplateList,
std::vector<TranslationTableDataType>& aTranslations) {
RwlLocker locker{mRwl, true};
if (!IsOpen()) {
DBG_ASSERT_ALWAYS("Database is not open. Update applied in bad state.");
return false;
}
bool success = BeginTransaction();
success = success && mUpdateAdapter->UpdateSupportTables(aLanguageList, aMustacheTemplateList,
aTranslations);
EndTransaction(success);
return success;
} // end of ApplySupportTableUpdateToDb
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Deletes the Active Captain database and notifies listeners.
//!
//----------------------------------------------------------------
void Repository::Delete() {
if (DeleteDatabaseFile()) {
EventDispatcher::SendEvent(MessageId::StateNotInstalled);
}
} // end of Delete
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//! @returns a list of business photos for a marker. This
//! returns all presentation data regarding the specified
//! photos.
//!
//----------------------------------------------------------------
Presentation::BusinessPhotoListPtr Repository::GetBusinessPhotoList(
const ACDB_marker_idx_type aIdx) {
Presentation::BusinessPhotoListPtr result = nullptr;
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = mPresentationAdapter->GetBusinessPhotoList(aIdx);
}
return result;
} // end of GetBusinessPhotoList
//----------------------------------------------------------------
//!
//! @public
//! @details
//! Returns the size of the database on disk.
//!
//----------------------------------------------------------------
void Repository::GetDatabaseSize(uint64_t& aUsedSpace) {
FileUtil::GetSize(GetDbPath(), aUsedSpace);
} // end of GetDatabaseSize
//----------------------------------------------------------------
//!
//! @public
//! @details Retrieve version and last update info for the
//! specified database
//!
//! @returns true on success and outputs the version and
//! most recent marker and user review timestamps.
//! Returns false on failure.
//!
//----------------------------------------------------------------
bool Repository::GetDbFileVersionInfo(const std::string& aPath, Version& aVersionOut,
LastUpdateInfoType& aUpdateInfoOut) const {
bool success(true);
Version retVersion;
// Check encryption:
// Previous versions of the module used an encrypted database. In case of an update, this
// encrypted file might still be in place. We would not be able to open it.
success = IsValidDatabaseFile(aPath);
DBG_I_IF(
!success,
"Invalid DB file found, possibly an encrypted file from a previously installed app version.");
// Open database
std::string expandedPath = DatabaseConfig::GetExpandedPath(aPath);
auto tempDatabase = OpenDatabaseFile(expandedPath);
success = success && tempDatabase;
if (success) {
InfoAdapter infoAdapter{*tempDatabase};
// Setup to access the database.
success = ReadyDbAccess(*tempDatabase);
DBG_W_IF(!success, "Failed to access database.");
// Retrieve last update info
success = success && infoAdapter.GetLastUpdateInfo(aUpdateInfoOut);
// Retrieve version info
if (success) {
infoAdapter.GetVersion(aVersionOut);
}
}
return success;
} // end of GetDbFileVersionInfo
//----------------------------------------------------------------
//!
//! @public
//! @details Retrieve last update info for the open database
//!
//! @returns true on success and outputs the most recent marker and
//! user review timestamps in the database. Returns false
//! on failure.
//!
//----------------------------------------------------------------
bool Repository::GetLastUpdateInfo(LastUpdateInfoType& aUpdateInfoOut) {
bool result(false);
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = BeginTransaction();
result = result && mInfoAdapter->GetLastUpdateInfo(aUpdateInfoOut);
EndTransaction(result);
}
return result;
} // end of GetLastUpdateInfo
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//! @returns a specific Marker of the respository. This
//! returns all data regarding the specified marker.
//!
//----------------------------------------------------------------
IMapMarkerPtr Repository::GetMapMarker(const ACDB_marker_idx_type aIdx) {
IMapMarkerPtr result = nullptr;
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = mMarkerAdapter->GetMapMarker(aIdx);
}
return result;
} // end of GetMapMarker
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//! @returns a specific Marker of the respository. This
//! returns all data regarding the specified marker.
//!
//----------------------------------------------------------------
ISearchMarkerPtr Repository::GetSearchMarker(const ACDB_marker_idx_type aIdx) {
ISearchMarkerPtr result = nullptr;
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = mMarkerAdapter->GetSearchMarker(aIdx);
}
return result;
} // end of GetSearchMarker
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Find points in the provided bounding box.
//!
//----------------------------------------------------------------
void Repository::GetMapMarkersByFilter(const MapMarkerFilter& aFilter,
std::vector<IMapMarkerPtr>& aResults) {
RwlLocker locker{mRwl, false};
if (!mDatabase) {
return;
}
bbox_type leftBbox;
bbox_type rightBbox;
if (MakeSplitBoundingBoxForCrossMeridianSearch(aFilter.GetBbox(), leftBbox, rightBbox)) {
MapMarkerFilter adaptedFilter = aFilter;
std::vector<IMapMarkerPtr> leftResults;
adaptedFilter.SetBbox(leftBbox);
mMarkerAdapter->GetMapMarkersByFilter(adaptedFilter, leftResults);
std::vector<IMapMarkerPtr> rightResults;
adaptedFilter.SetBbox(rightBbox);
mMarkerAdapter->GetMapMarkersByFilter(adaptedFilter, rightResults);
std::move(leftResults.begin(), leftResults.end(), std::back_inserter(aResults));
std::move(rightResults.begin(), rightResults.end(), std::back_inserter(aResults));
} else {
mMarkerAdapter->GetMapMarkersByFilter(aFilter, aResults);
}
} // end of GetMapMarkersByFilter
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Get data for merging
//!
//----------------------------------------------------------------
bool Repository::GetMergePageData(const int aPageNumber, const int aPageSize,
std::vector<MarkerTableDataCollection>& aMarkers_out,
std::vector<ReviewTableDataCollection>& aReviews_out) {
bool success = false;
RwlLocker locker{mRwl, false};
if (mDatabase) {
aMarkers_out.clear();
aReviews_out.clear();
aMarkers_out.reserve(MergePageSize);
// Create a transaction while retrieving data from the database. This avoids having to acquire
// a lock for every SELECT statement.
success = BeginTransaction();
std::vector<ACDB_marker_idx_type> markerIds;
markerIds.reserve(MergePageSize);
GetMergeMarkerIds(aPageNumber, aPageSize, markerIds);
for (auto markerId : markerIds) {
MarkerTableDataCollection marker;
success = success && GetMergeMarker(markerId, marker);
if (success) {
aMarkers_out.push_back(std::move(marker));
std::vector<ReviewTableDataCollection> markerReviews;
success = success && GetMergeReviews(markerId, markerReviews);
if (!markerReviews.empty()) {
aReviews_out.insert(aReviews_out.end(), markerReviews.begin(), markerReviews.end());
}
}
}
EndTransaction(success);
}
return success;
} // end of GetMergePageData
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Get marker for merging
//!
//----------------------------------------------------------------
bool Repository::GetMergeMarker(const ACDB_marker_idx_type aIdx,
MarkerTableDataCollection& aMarker) {
Presentation::PresentationMarkerPtr result = nullptr;
RwlLocker locker{mRwl, false};
if (mDatabase) {
aMarker = mMergeAdapter->GetMarker(aIdx);
return true;
}
return false;
} // end of GetMergeMarker
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Find marker IDs for merging
//!
//----------------------------------------------------------------
void Repository::GetMergeMarkerIds(const uint32_t aPageNumber, const uint32_t aPageSize,
std::vector<ACDB_marker_idx_type>& aResults) {
mMergeAdapter->GetMarkerIds(aPageNumber, aPageSize, aResults);
} // end of GetMergeMarkerIds
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Get reviews for merging
//!
//----------------------------------------------------------------
bool Repository::GetMergeReviews(const ACDB_marker_idx_type aIdx,
std::vector<ReviewTableDataCollection>& aReviews) {
RwlLocker locker{mRwl, false};
if (mDatabase) {
aReviews = mMergeAdapter->GetReviews(aIdx);
return true;
}
return false;
} // end of GetMergeReviews
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Find points matching the provided filter
//!
//----------------------------------------------------------------
void Repository::GetSearchMarkersByFilter(const SearchMarkerFilter& aFilter,
std::vector<ISearchMarkerPtr>& aResults) {
RwlLocker locker{mRwl, false};
if (!mDatabase) {
return;
}
bbox_type leftBbox;
bbox_type rightBbox;
if (MakeSplitBoundingBoxForCrossMeridianSearch(aFilter.GetBbox(), leftBbox, rightBbox)) {
SearchMarkerFilter adaptedFilter = aFilter;
std::vector<ISearchMarkerPtr> leftResults;
adaptedFilter.SetBbox(leftBbox);
mMarkerAdapter->GetSearchMarkersByFilter(adaptedFilter, leftResults);
std::vector<ISearchMarkerPtr> rightResults;
adaptedFilter.SetBbox(rightBbox);
mMarkerAdapter->GetSearchMarkersByFilter(adaptedFilter, rightResults);
std::move(leftResults.begin(), leftResults.end(), std::back_inserter(aResults));
std::move(rightResults.begin(), rightResults.end(), std::back_inserter(aResults));
} else {
mMarkerAdapter->GetSearchMarkersByFilter(aFilter, aResults);
}
} // end of GetSearchMarkersByFilter
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//! @returns Mustache template with specified name.
//!
//----------------------------------------------------------------
std::string Repository::GetMustacheTemplate(const std::string& aName) {
std::string result;
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = mPresentationAdapter->GetTemplate(aName);
}
return result;
} // end of GetMustacheTemplate
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//! @returns a specific Marker of the respository. This
//! returns all presentation data regarding the specified
//! marker.
//!
//----------------------------------------------------------------
Presentation::PresentationMarkerPtr Repository::GetPresentationMarker(
const ACDB_marker_idx_type aIdx, const std::string& aCaptainName) {
Presentation::PresentationMarkerPtr result = nullptr;
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = mPresentationAdapter->GetMarker(aIdx, aCaptainName);
}
return result;
} // end of GetPresentationMarker
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//! @returns a list of reviews for a marker. This returns
//! all presentation data regarding the specified reviews.
//!
//----------------------------------------------------------------
Presentation::ReviewListPtr Repository::GetReviewList(const ACDB_marker_idx_type aIdx,
const int aPageNumber, const int aPageSize,
const std::string& aCaptainName) {
Presentation::ReviewListPtr result = nullptr;
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = mPresentationAdapter->GetReviewList(aIdx, aPageNumber, aPageSize, aCaptainName);
}
return result;
} // end of GetReviewList
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//! @returns support table data for merging
//!
//----------------------------------------------------------------
bool Repository::GetSupportTableData(std::vector<LanguageTableDataType>& aLanguages,
std::vector<MustacheTemplateTableDataType>& aMustacheTemplates,
std::vector<TranslationTableDataType>& aTranslations) {
bool success = false;
if (mDatabase) {
success = BeginTransaction();
success = success &&
mMergeAdapter->GetSupportTableData(aLanguages, aMustacheTemplates, aTranslations);
EndTransaction(success);
}
return success;
} // end of GetSupportTableData
//----------------------------------------------------------------
//!
//! @public
//! @details Retrieve last update info for the open database
//!
//! @returns true on success and outputs the most recent marker and
//! user review timestamps in the database. Returns false
//! on failure.
//!
//----------------------------------------------------------------
bool Repository::GetTileLastUpdateInfo(const TileXY& aTileXY, LastUpdateInfoType& aUpdateInfoOut) {
bool result(false);
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = mInfoAdapter->GetTileLastUpdateInfo(aTileXY, aUpdateInfoOut);
}
return result;
} // end of GetTileLastUpdateInfo
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Get tiles, last update info overlapped by the given
//! bounding boxes.
//!
//----------------------------------------------------------------
void Repository::GetTilesLastUpdateInfoByBoundingBoxes(
const std::vector<bbox_type>& aBboxes, std::map<TileXY, LastUpdateInfoType>& aTiles) {
RwlLocker locker{mRwl, false};
if (mDatabase) {
for (auto bbox : aBboxes) {
bbox_type leftBbox;
bbox_type rightBbox;
if (MakeSplitBoundingBoxForCrossMeridianSearch(bbox, leftBbox, rightBbox)) {
mInfoAdapter->GetTileLastUpdateInfoBbox(leftBbox, aTiles);
mInfoAdapter->GetTileLastUpdateInfoBbox(rightBbox, aTiles);
} else {
mInfoAdapter->GetTileLastUpdateInfoBbox(bbox, aTiles);
}
}
}
} // end of GetTilesLastUpdateInfoByBoundingBoxes
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Get the average star rating for the specified marker.
//!
//----------------------------------------------------------------
float Repository::GetUserReviewAverageStars(const ACDB_marker_idx_type aIdx) {
float result = 0;
RwlLocker locker{mRwl, false};
if (mDatabase) {
result = mMarkerAdapter->GetAverageStars(aIdx);
}
return result;
} // end of GetUserReviewAverageStars
//----------------------------------------------------------------
//!
//! @public
//! @details Informs the caller if the repository is
//! open and usable
//!
//----------------------------------------------------------------
bool Repository::IsOpen() { return (mDatabase != nullptr); } // end of IsOpen
//----------------------------------------------------------------
//!
//! @public
//! @detail
//! Open an ActiveCaptain database. It is acceptable if
//! the database is not present in the expected location.
//!
//! @returns if the open was successful
//----------------------------------------------------------------
bool Repository::Open() {
bool success = OpenDatabase(true /*updateStateOnFailure*/);
if (success) {
EventDispatcher::SendEvent(MessageId::StateInstalled);
} else {
EventDispatcher::SendEvent(MessageId::StateNotInstalled);
}
return success;
} // end of Open
//----------------------------------------------------------------
//!
//! @private
//! @detail
//! Private implementation of Open(), allowing the caller to
//! determine if a status update should be send automatically at
//! the end.
//!
//! @returns if the open was successful
//----------------------------------------------------------------
bool Repository::OpenDatabase(bool updateStateOnFailure) {
bool success{true};
bool notCompatible{false};
// Find paths to database
std::string filePath = GetDbPath();
std::string expandedPath = DatabaseConfig::GetExpandedPath(filePath);
success = (!expandedPath.empty() && !filePath.empty());
// Database may not be present, so tolerate this failure.
success = success && FileUtil::Exists(filePath);
// Check encryption:
// Previous versions of the module used an encrypted database. In case of an update, this
// encrypted file might still be in place. We would not be able to open it.
bool invalidFile = false;
if (success) {
success = IsValidDatabaseFile(filePath);
if (!success) {
DBG_I(
"Invalid DB file found, possibly an encrypted file from a previously installed app version.");
invalidFile = true;
}
}
RwlLocker locker{mRwl, true};
if (mDatabase) {
// the database is already open. Nothing to do.
return true;
}
// Open database file
if (success) {
mDatabase = OpenDatabaseFile(expandedPath);
success = (mDatabase != nullptr);
}
// Setup to access the database. If anything goes wrong here, delete the database so we can get a
// new copy.
if (success) {
success = ReadyDbAccess(*mDatabase);
if (!success) {
DBG_W("Failed access setup, removing from system.");
invalidFile = true;
}
}
if (success) {
mInfoAdapter.reset(new InfoAdapter{*mDatabase});
mMarkerAdapter.reset(new MarkerAdapter{*mDatabase});
mMergeAdapter.reset(new MergeAdapter{*mDatabase});
mPresentationAdapter.reset(new PresentationAdapter{*mDatabase});
mTranslationAdapter.reset(new TranslationAdapter{*mDatabase});
mUpdateAdapter.reset(new UpdateAdapter{*mDatabase});
// check if compatible
Version newVersion;
mInfoAdapter->GetVersion(newVersion);
if (!newVersion.SchemaCompatible()) {
success = false;
notCompatible = true;
Close();
}
}
if (notCompatible || invalidFile) {
if (updateStateOnFailure) {
Delete(); // this updates the module state after deletion
} else {
DeleteDatabaseFile();
}
}
return success;
} // end of OpenDatabase
//----------------------------------------------------------------
//!
//! @public
//! @details Releases prepared queries and closes the
//! underlying .db file
//!
//! @returns if the close was successful
//!
//----------------------------------------------------------------
void Repository::Close() {
RwlLocker locker{mRwl, true};
DBG_D_IF(!mDatabase, "DB already closed");
if (mDatabase) {
mUpdateAdapter.reset();
mInfoAdapter.reset();
mMarkerAdapter.reset();
mMergeAdapter.reset();
mPresentationAdapter.reset();
mTranslationAdapter.reset();
mDatabase.reset();
}
} // end of Close
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//----------------------------------------------------------------
Version Repository::GetVersion() {
Version retVersion;
RwlLocker locker{mRwl, false};
if (mDatabase) {
mInfoAdapter->GetVersion(retVersion);
}
return retVersion;
} // end of GetVersion
//----------------------------------------------------------------
//!
//! @public
//! @details
//! Gets the local path to the Active Captain database.
//!
//! @returns the path or an empty string on failure.
//!
//----------------------------------------------------------------
std::string Repository::GetDbPath() {
if (!mDbPath.empty()) {
return mDbPath;
}
std::string ret;
#if (acdb_MFD_DB_SHARING_SUPPORT)
char* cardPath = NULL;
VCM_card_path_get(&cardPath);
if (NULL != cardPath) {
std::string searchPath = String::Format("%s%s/", cardPath, ExternalDbPath);
FindNewestDbFile(searchPath, ret);
}
free(cardPath);
#else
std::string dbBasePath = DatabaseConfig::GetBasePath();
ret = String::Format("%s/%s%s", dbBasePath.c_str(), DbName.c_str(), DbExt.c_str());
#endif
DBG_D_IF(ret.empty(), "Found no DB");
return ret;
} // end of GetDbPath
//----------------------------------------------------------------
//!
//! @public
//! @details
//! Gets the local path to the Active Captain database's journal.
//!
//! @returns the path or an empty string on failure.
//!
//----------------------------------------------------------------
std::string Repository::GetDbJournalPath() {
std::string dbPath = GetDbPath();
if (dbPath.empty()) {
return dbPath;
}
return dbPath + JournalExt;
} // end of GetDbJournalPath
//----------------------------------------------------------------
//!
//! @public
//! @details Begin the sideload process by locking the repository.
//!
//! @return if the lock was successful.
//!
//----------------------------------------------------------------
bool Repository::BeginSideload() {
if (!mDatabase) {
return false;
}
// Flush WAL file to the database file after each update to
// make sure the database is prepared for a sideload
SqliteCppUtil::FlushWalFile(*mDatabase);
mRwl.LockShared();
return true;
} // end of BeginSideload
//----------------------------------------------------------------
//!
//! @public
//! @details End the sideload process by unlocking the repository.
//!
//----------------------------------------------------------------
void Repository::EndSideload() { mRwl.Unlock(); } // end of EndSideload
//----------------------------------------------------------------
//!
//! @public
//! @details
//! Turn off the WAL journaling mode of the specified database
//! file so that it can be opened in read-only mode by
//! multiple chartplotters. WAL mode requires R/W access, and
//! this would preclude multiple openers.
//!
//! ToDo: move this function out of Repository by refactoring
//! SQL interface.