1 /*
2  * Copyright (c) 2023 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_SERVICES_EXTENSION_CLOUD_SERVER_IMPL_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICES_EXTENSION_CLOUD_SERVER_IMPL_H
18 
19 #include "basic_rust_types.h"
20 #include "cloud_db_impl.h"
21 #include "cloud/asset_loader.h"
22 #include "cloud/cloud_db.h"
23 #include "cloud/cloud_server.h"
24 #include "cloud/subscription.h"
25 
26 namespace OHOS::CloudData {
27 using DBMeta = DistributedData::SchemaMeta::Database;
28 using DBAssetLoader = DistributedData::AssetLoader;
29 using DBCloudDB = DistributedData::CloudDB;
30 using DBCloudInfo = DistributedData::CloudInfo;
31 using DBSchemaMeta = DistributedData::SchemaMeta;
32 using DBTable = DistributedData::Table;
33 using DBField = DistributedData::Field;
34 using DBSub = DistributedData::Subscription;
35 using DBRelation = DBSub::Relation;
36 using DBErr = DistributedData::GeneralError;
37 class CloudServerImpl : public DistributedData::CloudServer {
38 public:
39     DBCloudInfo GetServerInfo(int32_t userId, bool needSpaceInfo) override;
40     std::pair<int32_t, DBSchemaMeta> GetAppSchema(int32_t userId, const std::string &bundleName) override;
41     int32_t Subscribe(int32_t userId, const std::map<std::string, std::vector<DBMeta>> &dbs) override;
42     int32_t Unsubscribe(int32_t userId, const std::map<std::string, std::vector<DBMeta>> &dbs) override;
43     std::shared_ptr<DBAssetLoader> ConnectAssetLoader(uint32_t tokenId, const DBMeta &dbMeta) override;
44     std::shared_ptr<DBAssetLoader> ConnectAssetLoader(
45         const std::string &bundleName, int user, const DBMeta &dbMeta) override;
46     std::shared_ptr<DBCloudDB> ConnectCloudDB(uint32_t tokenId, const DBMeta &dbMeta) override;
47     std::shared_ptr<DBCloudDB> ConnectCloudDB(const std::string &bundleName, int user, const DBMeta &dbMeta) override;
48 
49 private:
50     static constexpr uint64_t INTERVAL = 6 * 24;
51     void GetAppInfo(std::shared_ptr<OhCloudExtHashMap> briefInfo, DBCloudInfo &cloudInfo);
52     void GetDatabases(std::shared_ptr<OhCloudExtVector> databases, DBSchemaMeta &dbSchema);
53     void GetTables(std::shared_ptr<OhCloudExtHashMap> tables, DBMeta &dbMeta);
54     void GetTableInfo(std::shared_ptr<OhCloudExtTable> pTable, DBTable &dbTable);
55     void GetFields(std::shared_ptr<OhCloudExtVector> fields, DBTable &dbTable);
56     int32_t DoSubscribe(int32_t userId, std::shared_ptr<OhCloudExtCloudSync> server,
57         std::shared_ptr<OhCloudExtHashMap> databases);
58     int32_t SaveSubscription(int32_t userId, std::shared_ptr<OhCloudExtHashMap> relations,
59         std::shared_ptr<OhCloudExtCloudSync> server);
60     int32_t SaveRelation(std::shared_ptr<OhCloudExtVector> keys,
61         std::shared_ptr<OhCloudExtVector> values, DBSub &sub);
62     int32_t GetRelation(std::shared_ptr<OhCloudExtHashMap> relations, DBRelation &dbRelation);
63     int32_t DoUnsubscribe(std::shared_ptr<OhCloudExtCloudSync> server,
64         std::shared_ptr<OhCloudExtHashMap> relations, const std::vector<std::string> &bundles, DBSub &sub);
65 };
66 } // namespace OHOS::CloudData
67 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_EXTENSION_CLOUD_SERVER_IMPL_H