153c3577eSopenharmony_ci/*
253c3577eSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
353c3577eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
453c3577eSopenharmony_ci * you may not use this file except in compliance with the License.
553c3577eSopenharmony_ci * You may obtain a copy of the License at
653c3577eSopenharmony_ci *
753c3577eSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
853c3577eSopenharmony_ci *
953c3577eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1053c3577eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1153c3577eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1253c3577eSopenharmony_ci * See the License for the specific language governing permissions and
1353c3577eSopenharmony_ci * limitations under the License.
1453c3577eSopenharmony_ci */
1553c3577eSopenharmony_ci
1653c3577eSopenharmony_ci#ifndef OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_UPGRADE_H
1753c3577eSopenharmony_ci#define OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_UPGRADE_H
1853c3577eSopenharmony_ci#include <functional>
1953c3577eSopenharmony_ci#include <memory>
2053c3577eSopenharmony_ci
2153c3577eSopenharmony_ci#include "concurrent_map.h"
2253c3577eSopenharmony_ci#include "kv_store_delegate_manager.h"
2353c3577eSopenharmony_ci#include "kv_store_nb_delegate.h"
2453c3577eSopenharmony_ci#include "metadata/store_meta_data.h"
2553c3577eSopenharmony_ci#include "types.h"
2653c3577eSopenharmony_ci
2753c3577eSopenharmony_cinamespace OHOS::DistributedKv {
2853c3577eSopenharmony_ciclass Upgrade {
2953c3577eSopenharmony_cipublic:
3053c3577eSopenharmony_ci    using StoreMeta = DistributedData::StoreMetaData;
3153c3577eSopenharmony_ci    using DBPassword = DistributedDB::CipherPassword;
3253c3577eSopenharmony_ci    using DBStatus = DistributedDB::DBStatus;
3353c3577eSopenharmony_ci    using DBStore = DistributedDB::KvStoreNbDelegate;
3453c3577eSopenharmony_ci    using DBManager = DistributedDB::KvStoreDelegateManager;
3553c3577eSopenharmony_ci    using Exporter = std::function<std::string(const StoreMeta &, DBPassword &)>;
3653c3577eSopenharmony_ci    using Cleaner = std::function<Status(const StoreMeta &)>;
3753c3577eSopenharmony_ci
3853c3577eSopenharmony_ci    API_EXPORT static Upgrade &GetInstance();
3953c3577eSopenharmony_ci    API_EXPORT bool RegisterExporter(uint32_t version, Exporter exporter);
4053c3577eSopenharmony_ci    API_EXPORT bool RegisterCleaner(uint32_t version, Cleaner cleaner);
4153c3577eSopenharmony_ci
4253c3577eSopenharmony_ci    DBStatus UpdateStore(const StoreMeta &old, const StoreMeta &meta, const std::vector<uint8_t> &pwd);
4353c3577eSopenharmony_ci    DBStatus ExportStore(const StoreMeta &old, const StoreMeta &meta);
4453c3577eSopenharmony_ci    void UpdatePassword(const StoreMeta &meta, const std::vector<uint8_t> &password);
4553c3577eSopenharmony_ci    DBStatus UpdateUuid(const StoreMeta &old, const StoreMeta &meta, const std::vector<uint8_t> &pwd);
4653c3577eSopenharmony_ci    API_EXPORT std::string GetEncryptedUuidByMeta(const StoreMeta &meta);
4753c3577eSopenharmony_ci
4853c3577eSopenharmony_ciprivate:
4953c3577eSopenharmony_ci    using AutoStore = std::unique_ptr<DBStore, std::function<void(DBStore *)>>;
5053c3577eSopenharmony_ci    AutoStore GetDBStore(const StoreMeta &meta, const std::vector<uint8_t> &pwd);
5153c3577eSopenharmony_ci    ConcurrentMap<std::string, std::string> calcUuid_;
5253c3577eSopenharmony_ci
5353c3577eSopenharmony_ci    Exporter exporter_;
5453c3577eSopenharmony_ci    Cleaner cleaner_;
5553c3577eSopenharmony_ci};
5653c3577eSopenharmony_ci} // namespace OHOS::DistributedKv
5753c3577eSopenharmony_ci#endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_UPGRADE_H
58