• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef OHOS_MEDIALIBRARY_METARECOVERY_H
16 #define OHOS_MEDIALIBRARY_METARECOVERY_H
17 
18 #include <map>
19 #include <nlohmann/json.hpp>
20 #include <string>
21 #include <set>
22 
23 #include "file_asset.h"
24 #include "media_column.h"
25 #include "photo_album.h"
26 #include "values_bucket.h"
27 
28 namespace OHOS {
29 namespace Media {
30 #define EXPORT __attribute__ ((visibility ("default")))
31 
32 enum class MediaLibraryMetaRecoveryState : int32_t {
33     STATE_NONE = 0,
34     STATE_RECOVERING,
35     STATE_RECOVERING_ABORT,
36     STATE_BACKING_UP
37 };
38 
39 class MediaLibraryMetaRecovery {
40 public:
41     EXPORT static MediaLibraryMetaRecovery &GetInstance();
42     EXPORT static int32_t DeleteMetaDataByPath(const std::string &filePath);
43 
44     EXPORT void CheckRecoveryState();
45     EXPORT void InterruptRecovery();
46     EXPORT int32_t WriteSingleMetaDataById(int32_t rowId);
47     EXPORT int32_t StartAsyncRecovery();
48     EXPORT int32_t SetRdbRebuiltStatus(bool status);
49     EXPORT int32_t ResetAllMetaDirty();
50     EXPORT void RecoveryStatistic();
51     EXPORT void StatisticSave();
52 
53 private:
54     MediaLibraryMetaRecovery() = default;
55     virtual ~MediaLibraryMetaRecovery() = default;
56     EXPORT void StatisticRestore();
57     EXPORT void StatisticReset();
58     // Backup
59     EXPORT void DoBackupMetadata();
60     void AlbumBackup();
61     void PhotoBackupBatch();
62     void PhotoBackup(const std::vector<std::shared_ptr<FileAsset>>&, int32_t&, int32_t&);
63 
64     // Recovery
65     void DoDataBaseRecovery();
66     EXPORT int32_t AlbumRecovery(const std::string &path);
67     EXPORT int32_t PhotoRecovery(const std::string &path);
68     EXPORT int32_t ScanMetaDir(const std::string &path, int32_t bucket_id);
69 
70     // Json
71     bool WriteJsonFile(const std::string &filePath, const nlohmann::json &j);
72     bool ReadJsonFile(const std::string &filePath, nlohmann::json &j);
73     EXPORT int32_t WriteMetadataToFile(const std::string &filePath, const FileAsset &fileAsset);
74     EXPORT int32_t ReadMetadataFromFile(const std::string &filePath, FileAsset &fileAsset);
75     void AddMetadataToJson(nlohmann::json &j, const FileAsset &fileAsset);
76     bool GetMetadataFromJson(const nlohmann::json &j, FileAsset &fileAsset);
77     int32_t WriteSingleMetaData(const FileAsset &asset);
78     EXPORT int32_t WritePhotoAlbumToFile(const std::string &filePath,
79                                   const std::vector<std::shared_ptr<PhotoAlbum>> &vecPhotoAlbum);
80     EXPORT int32_t ReadPhotoAlbumFromFile(const std::string &filePath,
81                                    std::vector<std::shared_ptr<PhotoAlbum>> &photoAlbumVector);
82     void AddPhotoAlbumToJson(nlohmann::json &j, const PhotoAlbum &photoAlbum);
83     bool GetPhotoAlbumFromJson(const nlohmann::json &j, PhotoAlbum &photoAlbum);
84     void LoadAlbumMaps(const std::string &path);
85     EXPORT int32_t ReadMetaStatusFromFile(std::set<int32_t> &status);
86     EXPORT int32_t WriteMetaStatusToFile(const std::string &keyPath, const int32_t status);
87     EXPORT int32_t ReadMetaRecoveryCountFromFile();
88 
89     // DB
90     bool UpdatePhotoOwnerAlbumId(NativeRdb::ValuesBucket&);
91     int32_t InsertMetadataInDbRetry(const FileAsset &fileAsset);
92     int32_t InsertMetadataInDb(const FileAsset &fileAsset);
93     int32_t InsertMetadataInDb(const std::vector<std::shared_ptr<PhotoAlbum>> &vecPhotoAlbum);
94     int32_t UpdateMetadataFlagInDb(const int32_t fieldId, const MetadataFlags &flag);
95 
96     // cloudsync
97     EXPORT void StopCloudSync();
98     EXPORT void RestartCloudSync();
99 
100     // RecoveryPhotosTableColumn
101     EXPORT ResultSetDataType GetDataType(const std::string &name);
102     EXPORT std::unordered_map<std::string, ResultSetDataType> QueryRecoveryPhotosTableColumnInfo();
103 
104 private:
105     std::atomic<MediaLibraryMetaRecoveryState> recoveryState_{MediaLibraryMetaRecoveryState::STATE_NONE};
106     bool rdbRebuilt_{false};
107     std::set<int32_t> metaStatus;
108     std::map<int32_t, std::string> oldAlbumIdToLpath;
109     std::map<std::string, int32_t> lpathToNewAlbumId;
110 
111     int64_t backupSuccCnt_{0};   // succ in 12 hours
112     int64_t backupCostTime_{0};  // costtime in 12 hours
113 
114     int64_t recoverySuccCnt_{0};  // file recovered successfully after the recovery process
115     int64_t recoveryTotalBackupCnt_{0};   // total metafile in phone at the beginning of recovery
116 
117     int64_t reBuiltCount_{0};      // rebuilt count in 12 hours
118     int64_t recoveryCostTime_{0};  // costtime in 12 hours
119 };
120 } // namespace Media
121 } // namespace OHOS
122 #endif // OHOS_MEDIALIBRARY_METARECOVERY_H