• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_DISTRIBUTED_DATA_SERVICE_KVDB_UPGRADE_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_UPGRADE_H
18 #include <functional>
19 #include <memory>
20 #include "kv_store_delegate_manager.h"
21 #include "kv_store_nb_delegate.h"
22 #include "metadata/store_meta_data.h"
23 #include "types.h"
24 #include "concurrent_map.h"
25 
26 namespace OHOS::DistributedKv {
27 class Upgrade {
28 public:
29     using StoreMeta = DistributedData::StoreMetaData;
30     using DBPassword = DistributedDB::CipherPassword;
31     using DBStatus = DistributedDB::DBStatus;
32     using DBStore = DistributedDB::KvStoreNbDelegate;
33     using DBManager = DistributedDB::KvStoreDelegateManager;
34     using Exporter = std::function<std::string(const StoreMeta &, DBPassword &)>;
35     using Cleaner = std::function<Status(const StoreMeta &)>;
36     API_EXPORT static Upgrade &GetInstance();
37     API_EXPORT bool RegisterExporter(uint32_t version, Exporter exporter);
38     API_EXPORT bool RegisterCleaner(uint32_t version, Cleaner cleaner);
39 
40     DBStatus UpdateStore(const StoreMeta &old, const StoreMeta &metaData, const std::vector<uint8_t> &pwd);
41     DBStatus ExportStore(const StoreMeta &old, const StoreMeta &meta);
42     void UpdatePassword(const StoreMeta &meta, const std::vector<uint8_t> &password);
43     DBStatus UpdateUuid(const StoreMeta &old, const StoreMeta &meta, const std::vector<uint8_t> &pwd);
44     API_EXPORT std::string GetEncryptedUuidByMeta(const StoreMeta &meta);
45 
46 private:
47     using AutoStore = std::unique_ptr<DBStore, std::function<void(DBStore *)>>;
48     AutoStore GetDBStore(const StoreMeta &meta, const std::vector<uint8_t> &pwd);
49     ConcurrentMap<std::string, std::string> calcUuid_;
50 
51     Exporter exporter_;
52     Cleaner cleaner_;
53 };
54 } // namespace OHOS::DistributedKv
55 #endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_UPGRADE_H
56