• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 
16 #ifndef OHOS_CLOUD_SYNC_SERVICE_ALBUM_DATA_HANDLER_H
17 #define OHOS_CLOUD_SYNC_SERVICE_ALBUM_DATA_HANDLER_H
18 
19 #include "album_data_convertor.h"
20 #include "gallery_file_const.h"
21 #include "gallery_sysevent.h"
22 #include "rdb_data_handler.h"
23 
24 namespace OHOS {
25 namespace FileManagement {
26 namespace CloudSync {
27 class AlbumDataHandler : public RdbDataHandler, public GallerySyncStatContainer {
28 public:
29     AlbumDataHandler(int32_t userId, const std::string &bundleName,
30                      std::shared_ptr<NativeRdb::RdbStore> rdb, std::shared_ptr<bool> stopFlag);
31     virtual ~AlbumDataHandler() = default;
32 
33     /* download */
34     virtual void GetFetchCondition(FetchCondition &cond) override;
35     virtual int32_t OnFetchRecords(std::shared_ptr<std::vector<DriveKit::DKRecord>> &records,
36                                    OnFetchParams &params) override;
37     virtual int32_t GetRetryRecords(std::vector<DriveKit::DKRecordId> &records) override;
38 
39     /* upload */
40     virtual int32_t GetCreatedRecords(std::vector<DriveKit::DKRecord> &records) override;
41     virtual int32_t GetDeletedRecords(std::vector<DriveKit::DKRecord> &records) override;
42     virtual int32_t GetMetaModifiedRecords(std::vector<DriveKit::DKRecord> &records) override;
43 
44     virtual int32_t OnCreateRecords(const std::map<DriveKit::DKRecordId,
45         DriveKit::DKRecordOperResult> &map) override;
46     virtual int32_t OnDeleteRecords(const std::map<DriveKit::DKRecordId,
47         DriveKit::DKRecordOperResult> &map) override;
48     virtual int32_t OnModifyMdirtyRecords(const std::map<DriveKit::DKRecordId,
49         DriveKit::DKRecordOperResult> &map) override;
50 
51     virtual int32_t Clean(const int action) override;
52 
53     virtual void SetChecking() override;
54     /* reset */
55     void Reset();
56 
57 private:
58     std::tuple<std::shared_ptr<NativeRdb::ResultSet>, int> QueryLocalMatch(const std::string &recordId);
59     int32_t InsertCloudAlbum(DriveKit::DKRecord &record);
60     int32_t DeleteCloudAlbum(DriveKit::DKRecord &record);
61     int32_t UpdateCloudAlbum(DriveKit::DKRecord &record);
62     int32_t HandleLocalDirty(int32_t dirty, const DriveKit::DKRecord &record);
63 
64     int32_t OnUploadSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry);
65     int32_t OnDeleteSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry);
66     int32_t OnCreateSuccess(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry);
67     int32_t OnCreateFail(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry);
68     int32_t OnDeleteFail(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry);
69     int32_t OnModifyFail(const std::pair<DriveKit::DKRecordId, DriveKit::DKRecordOperResult> &entry);
70 
71     bool IsConflict(DriveKit::DKRecord &record, int32_t &albumId);
72     int32_t MergeAlbumOnConflict(DriveKit::DKRecord &record, int32_t albumId);
73     int32_t QueryUserAlbum(std::vector<DriveKit::DKRecord> &records, Media::DirtyType dirty,
74                            const std::vector<std::string> &failSet, AlbumDataConvertor &convertor);
75     int32_t QuerySourceAlbum(std::vector<DriveKit::DKRecord> &records,  Media::DirtyType dirty,
76                              const std::vector<std::string> &failSet, AlbumDataConvertor &convertor);
77     int32_t QueryConflict(DriveKit::DKRecord &record, std::shared_ptr<NativeRdb::ResultSet> &resultSet);
78 
79     void UpdateDownloadAlbumStat(uint64_t success, uint64_t rdbFail, uint64_t dataFail);
80 
81     static inline const std::string TABLE_NAME = "albums";
82     static inline const int32_t LIMIT_SIZE = 5;
83     DriveKit::DKRecordType recordType_ = "album";
84     DriveKit::DKFieldKeyArray desiredKeys_;
85 
86     /* failure records */
87     std::vector<std::string> modifyFailSet_;
88     std::vector<std::string> createFailSet_;
89 
90     /* convert */
91     AlbumDataConvertor createConvertor_ = { FILE_CREATE };
92     AlbumDataConvertor deleteConvertor_ = { FILE_DELETE };
93     AlbumDataConvertor modifyConvertor_ = { FILE_METADATA_MODIFY };
94 };
95 } // namespace CloudSync
96 } // namespace FileManagement
97 } // namespace OHOS
98 #endif // OHOS_CLOUD_SYNC_SERVICE_ALBUM_DATA_HANDLER_H
99