• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-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 
16 #include "medialibrary_kvstore_manager.h"
17 
18 #include <atomic>
19 #include <shared_mutex>
20 
21 #include "medialibrary_errno.h"
22 #include "media_log.h"
23 
24 using namespace OHOS::DistributedKv;
25 namespace OHOS::Media {
26 std::mutex MediaLibraryKvStoreManager::mutex_;
27 Utils::Timer MediaLibraryKvStoreManager::timer_("close_kvStore");
28 std::atomic<uint32_t> MediaLibraryKvStoreManager::insertImageCount_ = 0;
29 uint32_t MediaLibraryKvStoreManager::timerId_ = 0;
30 
~MediaLibraryKvStoreManager()31 MediaLibraryKvStoreManager::~MediaLibraryKvStoreManager()
32 {
33     timer_.Unregister(timerId_);
34     timer_.Shutdown();
35     timerId_ = 0;
36 }
37 
InitKvStore(const KvStoreRoleType & roleType,const KvStoreValueType & valueType)38 int32_t MediaLibraryKvStoreManager::InitKvStore(const KvStoreRoleType &roleType, const KvStoreValueType &valueType)
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     KvStoreSharedPtr ptr;
42     if (kvStoreMap_.Find(valueType, ptr)) {
43         return E_OK;
44     }
45 
46     std::string baseDir = "";
47     if (roleType == KvStoreRoleType::OWNER) {
48         baseDir = KV_STORE_OWNER_DIR;
49     } else if (roleType == KvStoreRoleType::VISITOR) {
50         baseDir = KV_STORE_VISITOR_DIR;
51     } else {
52         MEDIA_ERR_LOG("invalid role type");
53         return E_ERR;
54     }
55 
56     ptr = std::make_shared<MediaLibraryKvStore>();
57     int32_t status = ptr->Init(roleType, valueType, baseDir);
58     if (status != E_OK) {
59         MEDIA_ERR_LOG("init kvStore failed, status %{public}d", status);
60         return status;
61     }
62     kvStoreMap_.Insert(valueType, ptr);
63     return E_OK;
64 }
65 
GetKvStore(const KvStoreRoleType & roleType,const KvStoreValueType & valueType)66 std::shared_ptr<MediaLibraryKvStore> MediaLibraryKvStoreManager::GetKvStore(
67     const KvStoreRoleType &roleType, const KvStoreValueType &valueType)
68 {
69     RegisterTimer(roleType, valueType);
70     KvStoreSharedPtr ptr;
71     CHECK_AND_RETURN_RET(!kvStoreMap_.Find(valueType, ptr), ptr);
72 
73     InitKvStore(roleType, valueType);
74     kvStoreMap_.Find(valueType, ptr);
75     return ptr;
76 }
77 
CloseAllKvStore()78 void MediaLibraryKvStoreManager::CloseAllKvStore()
79 {
80     std::lock_guard<std::mutex> lock(mutex_);
81     if (kvStoreMap_.IsEmpty()) {
82         return;
83     }
84 
85     kvStoreMap_.Clear();
86 }
87 
CloseKvStore(const KvStoreValueType & valueType)88 bool MediaLibraryKvStoreManager::CloseKvStore(const KvStoreValueType &valueType)
89 {
90     std::lock_guard<std::mutex> lock(mutex_);
91     KvStoreSharedPtr ptr;
92     if (!kvStoreMap_.Find(valueType, ptr)) {
93         return false;
94     }
95 
96     if (ptr != nullptr && ptr->Close()) {
97         kvStoreMap_.Erase(valueType);
98         MEDIA_INFO_LOG("CloseKvStore success, valueType %{public}d", valueType);
99         return true;
100     }
101     return false;
102 }
103 
RegisterTimer(const KvStoreRoleType & roleType,const KvStoreValueType & valueType)104 void MediaLibraryKvStoreManager::RegisterTimer(const KvStoreRoleType &roleType, const KvStoreValueType &valueType)
105 {
106     if (roleType != KvStoreRoleType::OWNER) {
107         return;
108     }
109 
110     Utils::Timer::TimerCallback timerCallback = [this]() {
111         MEDIA_INFO_LOG("KvStore timerCallback, CloseAllKvStore");
112         insertImageCount_ = 0;
113         CloseAllKvStore();
114     };
115 
116     std::lock_guard<std::mutex> lock(mutex_);
117     if (timerId_ == 0) {
118         MEDIA_INFO_LOG("KvStore timer Setup");
119         timer_.Setup();
120     }
121 
122     if (insertImageCount_ == 0 || insertImageCount_ >= KVSTORE_INSERT_COUNT) {
123         timer_.Unregister(timerId_);
124         insertImageCount_ = 0;
125         timerId_ = timer_.Register(timerCallback, CLOSE_KVSTORE_TIME_INTERVAL, true);
126         MEDIA_INFO_LOG("KvStore timer Restart");
127     }
128     insertImageCount_++;
129 }
130 
InitMonthAndYearKvStore(const KvStoreRoleType & roleType)131 bool MediaLibraryKvStoreManager::InitMonthAndYearKvStore(const KvStoreRoleType& roleType)
132 {
133     if (roleType != KvStoreRoleType::OWNER) {
134         return false;
135     }
136     if (GetKvStore(roleType, KvStoreValueType::MONTH_ASTC) == nullptr ||
137         GetKvStore(roleType, KvStoreValueType::YEAR_ASTC) == nullptr) {
138         return false;
139     }
140     return true;
141 }
142 
IsKvStoreValid(const KvStoreValueType & valueType)143 bool MediaLibraryKvStoreManager::IsKvStoreValid(const KvStoreValueType &valueType)
144 {
145     KvStoreSharedPtr ptr;
146     CHECK_AND_RETURN_RET(!kvStoreMap_.Find(valueType, ptr), true);
147 
148     ptr = std::make_shared<MediaLibraryKvStore>();
149     int32_t status = ptr->Init(KvStoreRoleType::OWNER, valueType, KV_STORE_OWNER_DIR);
150     CHECK_AND_RETURN_RET_LOG(status != static_cast<int32_t>(Status::DATA_CORRUPTED), false,
151         "KvStore is invalid and needs to be deleted, status %{public}d, type %{public}d",
152         status, valueType);
153 
154     if (status == E_OK && ptr != nullptr) {
155         ptr->Close();
156     }
157     return true;
158 }
159 
RebuildInvalidKvStore(const KvStoreValueType & valueType)160 int32_t MediaLibraryKvStoreManager::RebuildInvalidKvStore(const KvStoreValueType &valueType)
161 {
162     std::lock_guard<std::mutex> lock(mutex_);
163     kvStoreMap_.Erase(valueType);
164     KvStoreSharedPtr ptr = std::make_shared<MediaLibraryKvStore>();
165     return ptr->RebuildKvStore(valueType, KV_STORE_OWNER_DIR);
166 }
167 
GetSingleKvStore(const KvStoreRoleType & roleType,const std::string & storeId,const std::string & baseDir)168 std::shared_ptr<MediaLibraryKvStore> MediaLibraryKvStoreManager::GetSingleKvStore(
169     const KvStoreRoleType &roleType, const std::string &storeId, const std::string &baseDir)
170 {
171     KvStoreSharedPtr ptr = std::make_shared<MediaLibraryKvStore>();
172     int32_t status = ptr->InitSingleKvstore(roleType, storeId, baseDir);
173     CHECK_AND_RETURN_RET_LOG(status == E_OK, nullptr, "Init kvStore failed, status %{public}d", status);
174     return ptr;
175 }
176 
CloneKvStore(const std::string & oldKvStoreId,const std::string & oldBaseDir,const std::string & newKvStoreId,const std::string & newBaseDir)177 int32_t MediaLibraryKvStoreManager::CloneKvStore(const std::string &oldKvStoreId, const std::string &oldBaseDir,
178     const std::string &newKvStoreId, const std::string &newBaseDir)
179 {
180     KvStoreSharedPtr oldKvStore = std::make_shared<MediaLibraryKvStore>();
181     int32_t status = oldKvStore->InitSingleKvstore(KvStoreRoleType::OWNER, oldKvStoreId, oldBaseDir);
182     CHECK_AND_RETURN_RET_LOG(status == E_OK, status, "Init old kvStore failed, status %{public}d", status);
183 
184     KvStoreSharedPtr newKvStore = std::make_shared<MediaLibraryKvStore>();
185     status = newKvStore->InitSingleKvstore(KvStoreRoleType::OWNER, newKvStoreId, newBaseDir);
186     CHECK_AND_RETURN_RET_LOG(status == E_OK, status, "Init new kvStore failed, status %{public}d", status);
187 
188     status = oldKvStore->PutAllValueToNewKvStore(newKvStore);
189     CHECK_AND_RETURN_RET_LOG(status == E_OK, status, "Clone kvstore failed, status %{public}d", status);
190     return E_OK;
191 }
192 } // namespace OHOS::Media