• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DATASHARESERVICE_KV_DELEGATE_H
17 #define DATASHARESERVICE_KV_DELEGATE_H
18 
19 #include <mutex>
20 #include <string>
21 
22 #include "db_delegate.h"
23 #include "executor_pool.h"
24 #include "grd_base/grd_db_api.h"
25 
26 namespace OHOS::DataShare {
27 class KvDelegate final : public KvDBDelegate {
28 public:
29     KvDelegate(const std::string &path, const std::shared_ptr<ExecutorPool> &executors);
30     ~KvDelegate() override;
31     std::pair<int32_t, int32_t> Upsert(const std::string &collectionName, const KvData &value) override;
32     std::pair<int32_t, int32_t> Delete(const std::string &collectionName, const std::string &filter) override;
33     int32_t Get(const std::string &collectionName, const Id &id, std::string &value) override;
34 
35     int32_t Get(const std::string &collectionName, const std::string &filter, const std::string &projection,
36         std::string &result) override;
37     int32_t GetBatch(const std::string &collectionName, const std::string &filter, const std::string &projection,
38         std::vector<std::string> &result) override;
39     void NotifyBackup() override;
40 
41 private:
42     bool Init();
43     bool GetVersion(const std::string &collectionName, const std::string &filter, int &version);
44     std::pair<int32_t, int32_t> Upsert(const std::string &collectionName, const std::string &filter,
45         const std::string &value);
46     void Flush();
47     bool RestoreIfNeed(int32_t dbStatus);
48     void Backup();
49     void Restore();
50     void RemoveDbFile(bool isBackUp) const;
51     bool CopyFile(bool isBackup);
52     std::recursive_mutex mutex_;
53     std::string path_;
54     GRD_DB *db_ = nullptr;
55     bool isInitDone_ = false;
56     std::shared_ptr<ExecutorPool> executors_ = nullptr;
57     ExecutorPool::TaskId taskId_ = ExecutorPool::INVALID_TASK_ID;
58     bool hasChange_ = false;
59 };
60 } // namespace OHOS::DataShare
61 #endif // DATASHARESERVICE_KV_DELEGATE_H
62