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 #ifndef OHOS_MEDIALIBRARY_KVSTORE_MANAGER_H
17 #define OHOS_MEDIALIBRARY_KVSTORE_MANAGER_H
18 
19 #include <safe_map.h>
20 
21 #include "medialibrary_kvstore.h"
22 #include "timer.h"
23 
24 namespace OHOS {
25 namespace Media {
26 #define EXPORT __attribute__ ((visibility ("default")))
27 using KvStoreSharedPtr = std::shared_ptr<MediaLibraryKvStore>;
28 constexpr size_t KVSTORE_INSERT_COUNT = 20;
29 constexpr size_t CLOSE_KVSTORE_TIME_INTERVAL = 270000;
30 
31 class MediaLibraryKvStoreManager {
32 public:
GetInstance()33     EXPORT static MediaLibraryKvStoreManager &GetInstance()
34     {
35         static MediaLibraryKvStoreManager instance;
36         return instance;
37     }
38 
39     EXPORT int32_t InitKvStore(const KvStoreRoleType &roleType, const KvStoreValueType &valueType);
40     EXPORT std::shared_ptr<MediaLibraryKvStore> GetKvStore(
41         const KvStoreRoleType &roleType, const KvStoreValueType &valueType);
42     EXPORT bool InitMonthAndYearKvStore(const KvStoreRoleType &roleType);
43     EXPORT bool CloseKvStore(const KvStoreValueType &valueType);
44     EXPORT void CloseAllKvStore();
45     EXPORT bool IsKvStoreValid(const KvStoreValueType &valueType);
46     EXPORT int32_t RebuildInvalidKvStore(const KvStoreValueType &valueType);
47     EXPORT std::shared_ptr<MediaLibraryKvStore> GetSingleKvStore(const KvStoreRoleType &roleType,
48         const std::string &storeId, const std::string &baseDir);
49     EXPORT int32_t CloneKvStore(const std::string &oldKvStoreId, const std::string &oldBaseDir,
50         const std::string &newKvStoreId, const std::string &newBaseDir);
51 
52 private:
53     MediaLibraryKvStoreManager() = default;
54     ~MediaLibraryKvStoreManager();
55 
56     void RegisterTimer(const KvStoreRoleType &roleType, const KvStoreValueType &valueType);
57 
58     SafeMap<KvStoreValueType, KvStoreSharedPtr> kvStoreMap_;
59     static std::mutex mutex_;
60     static Utils::Timer timer_;
61     static uint32_t timerId_;
62     static std::atomic<uint32_t> insertImageCount_;
63 };
64 } // namespace Media
65 } // namespace OHOS
66 
67 #endif // OHOS_MEDIALIBRARY_KVSTORE_MANAGER_H