1 /*
2  * Copyright (c) 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 NATIVE_RDB_RD_CONNECTION_H
17 #define NATIVE_RDB_RD_CONNECTION_H
18 
19 #include <mutex>
20 #include <memory>
21 #include <vector>
22 
23 #include "rd_utils.h"
24 #include "rdb_common.h"
25 #include "connection.h"
26 #include "rdb_store_config.h"
27 #include "value_object.h"
28 
29 typedef struct ClientChangedData ClientChangedData;
30 namespace OHOS {
31 namespace NativeRdb {
32 
33 class RdConnection : public Connection {
34 public:
35     static std::pair<int32_t, std::shared_ptr<Connection>> Create(const RdbStoreConfig& config, bool isWrite);
36     static int32_t Repair(const RdbStoreConfig& config);
37     static int32_t Delete(const RdbStoreConfig& config);
38     RdConnection(const RdbStoreConfig &config, bool isWriteConnection);
39     ~RdConnection();
40     int32_t OnInitialize() override;
41     std::pair<int32_t, Stmt> CreateStatement(const std::string& sql, SConn conn) override;
42     int32_t GetDBType() const override;
43     bool IsWriter() const override;
44     int32_t ReSetKey(const RdbStoreConfig& config) override;
45     int32_t TryCheckPoint(bool timeout) override;
46     int32_t LimitWalSize() override;
47     int32_t ConfigLocale(const std::string& localeStr) override;
48     int32_t CleanDirtyData(const std::string& table, uint64_t cursor) override;
49     int32_t SubscribeTableChanges(const Notifier& notifier) override;
50     int32_t GetMaxVariable() const override;
51     int32_t GetJournalMode() override;
52     int32_t ClearCache() override;
53     int32_t Subscribe(const std::string& event,
54         const std::shared_ptr<DistributedRdb::RdbStoreObserver>& observer) override;
55     int32_t Unsubscribe(const std::string& event,
56         const std::shared_ptr<DistributedRdb::RdbStoreObserver>& observer) override;
57     int32_t Backup(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey,
58         bool isAsync, SlaveStatus &slaveStatus) override;
59     int32_t Restore(const std::string &databasePath, const std::vector<uint8_t> &destEncryptKey,
60         SlaveStatus &slaveStatus) override;
61     ExchangeStrategy GenerateExchangeStrategy(const SlaveStatus &status) override;
62 
63 private:
64     static constexpr int MAX_VARIABLE_NUM = 500;
65     static constexpr const char *GRD_OPEN_CONFIG_STR =
66         "{\"pageSize\":8, \"crcCheckEnable\":0, \"redoFlushByTrx\":1, \"bufferPoolSize\":10240,"
67         "\"sharedModeEnable\":1, \"metaInfoBak\":1, \"maxConnNum\":500 }";
68     static constexpr uint32_t NO_ITER = 0;
69     static constexpr uint32_t ITER_V1 = 5000;
70     static constexpr uint32_t ITERS[] = {NO_ITER, ITER_V1};
71     static constexpr uint32_t ITERS_COUNT = sizeof(ITERS) / sizeof(ITERS[0]);
72     static const int32_t regCreator_;
73     static const int32_t regRepairer_;
74     static const int32_t regDeleter_;
75 
76     int InnerOpen(const RdbStoreConfig &config);
77     bool isWriter_ = false;
78     GRD_DB *dbHandle_ = nullptr;
79     std::string configStr_ = GRD_OPEN_CONFIG_STR;
80     const RdbStoreConfig config_;
81 };
82 
83 } // namespace NativeRdb
84 } // namespace OHOS
85 #endif // NATIVE_RDB_RD_CONNECTION_H
86