1 /*
2  * Copyright (c) 2021 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 #ifndef RELATIONAL_STORE_MANAGER_H
16 #define RELATIONAL_STORE_MANAGER_H
17 
18 #include <functional>
19 #include <mutex>
20 #include <string>
21 
22 #include "auto_launch_export.h"
23 #include "relational_store_delegate.h"
24 #include "store_types.h"
25 
26 namespace DistributedDB {
27 class RelationalStoreManager final {
28 public:
29     // Only calculate the table name with device hash, no guarantee for the table exists
30     DB_API static std::string GetDistributedTableName(const std::string &device, const std::string &tableName);
31 
32     DB_API static std::string GetDistributedLogTableName(const std::string &tableName);
33     // key:colName value:real value
34     DB_API static std::vector<uint8_t> CalcPrimaryKeyHash(const std::map<std::string, Type> &primaryKey,
35         const std::map<std::string, CollateType> &collateTypeMap = {});
36 
37     DB_API RelationalStoreManager(const std::string &appId, const std::string &userId, int32_t instanceId = 0);
38     DB_API RelationalStoreManager(const std::string &appId, const std::string &userId, const std::string &subUser,
39         int32_t instanceId = 0);
40     DB_API ~RelationalStoreManager() = default;
41 
42     RelationalStoreManager(const RelationalStoreManager &) = delete;
43     RelationalStoreManager(RelationalStoreManager &&) = delete;
44     RelationalStoreManager &operator=(const RelationalStoreManager &) = delete;
45     RelationalStoreManager &operator=(RelationalStoreManager &&) = delete;
46 
47     DB_API DBStatus OpenStore(const std::string &path, const std::string &storeId,
48         const RelationalStoreDelegate::Option &option, RelationalStoreDelegate *&delegate);
49 
50     DB_API DBStatus CloseStore(RelationalStoreDelegate *store);
51 
52     // deprecated
53     DB_API static void SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback);
54 
55     // deprecated
56     DB_API static std::string GetRelationalStoreIdentifier(const std::string &userId, const std::string &appId,
57         const std::string &storeId, bool syncDualTupleMode = false);
58 
59     DB_API static std::vector<QueryNode> ParserQueryNodes(const Bytes &queryBytes, DBStatus &status);
60 private:
61     bool PreCheckOpenStore(const std::string &path, const std::string &storeId,
62         RelationalStoreDelegate *&delegate, std::string &canonicalDir);
63     std::string appId_;
64     std::string userId_;
65     std::string subUser_;
66     int32_t instanceId_;
67 };
68 } // namespace DistributedDB
69 #endif // RELATIONAL_STORE_MANAGER_H
70