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_MEDIALIBRARY_KVSTORE_H
17 #define OHOS_MEDIALIBRARY_KVSTORE_H
18 
19 #include "distributed_kv_data_manager.h"
20 
21 namespace OHOS {
22 namespace Media {
23 #define EXPORT __attribute__ ((visibility ("default")))
24 const std::string MEDIA_LIBRARY_DB_DIR = "/data/medialibrary/database";
25 const std::string KV_STORE_OWNER_DIR = MEDIA_LIBRARY_DB_DIR;
26 const std::string KV_STORE_VISITOR_DIR = MEDIA_LIBRARY_DB_DIR;
27 const std::string KV_STORE_OWNER_DIR_OLD_VERSION = "/storage/cloud/files/.thumbs/Photo";
28 
29 enum class KvStoreRoleType : int32_t {
30     OWNER,
31     VISITOR,
32 };
33 
34 enum class KvStoreValueType : int32_t {
35     MONTH_ASTC,
36     YEAR_ASTC,
37     MONTH_ASTC_OLD_VERSION,
38     YEAR_ASTC_OLD_VERSION,
39 };
40 
41 class MediaLibraryKvStore {
42 public:
43     EXPORT MediaLibraryKvStore() = default;
44     EXPORT ~MediaLibraryKvStore();
45 
46     // return 0 means init KvStore success, others mean init kvstore fail
47     EXPORT int32_t Init(const KvStoreRoleType &roleType, const KvStoreValueType &valueType, const std::string &baseDir);
48     EXPORT int32_t Insert(const std::string &key, const std::vector<uint8_t> &value);
49     EXPORT int32_t Delete(const std::string &key);
50     EXPORT int32_t Query(const std::string &key, std::vector<uint8_t> &value);
51     EXPORT int32_t GetCount(const std::string& key, int32_t& count);
52     EXPORT int32_t BatchQuery(std::vector<std::string> &batchKeys, std::vector<std::vector<uint8_t>> &values);
53     EXPORT bool Close();
54     EXPORT int32_t RebuildKvStore(const KvStoreValueType &valueType, const std::string &baseDir);
55     EXPORT int32_t BatchInsert(const std::vector<DistributedKv::Entry> &entries);
56     EXPORT int32_t InitSingleKvstore(const KvStoreRoleType &roleType,
57         const std::string &storeId, const std::string &baseDir);
58     EXPORT int32_t PutAllValueToNewKvStore(std::shared_ptr<MediaLibraryKvStore> &newKvstore);
59 
60 private:
61     bool GetKvStoreOption(DistributedKv::Options &options, const KvStoreRoleType &roleType, const std::string &baseDir);
62 
63     std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_ = nullptr;
64     DistributedKv::DistributedKvDataManager dataManager_;
65 };
66 } // namespace Media
67 } // namespace OHOS
68 
69 #endif // OHOS_MEDIALIBRARY_KVSTORE_H