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