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 DATASHARESERVICE_PUBLISHED_DATA_H
17 #define DATASHARESERVICE_PUBLISHED_DATA_H
18 
19 #include "db_delegate.h"
20 #include "serializable/serializable.h"
21 
22 namespace OHOS::DataShare {
23 class PublishedDataNode final : public VersionData {
24 public:
25     struct BytesData : public DistributedData::Serializable {
26         explicit BytesData(std::string &&data);
27         BytesData() = default;
28         bool Marshal(json &node) const override;
29         bool Unmarshal(const json &node) override;
30         std::string data;
31     };
32     using Data = std::variant<BytesData, std::string>;
33     static std::variant<std::vector<uint8_t>, std::string> MoveTo(const Data &data);
34     static Data MoveTo(std::variant<std::vector<uint8_t>, std::string> &data);
35     PublishedDataNode();
36     PublishedDataNode(const std::string &key, const std::string &bundleName, int64_t subscriberId,
37         const int32_t userId, const Data &value);
38     ~PublishedDataNode() = default;
39     bool Marshal(json &node) const override;
40     bool Unmarshal(const json &node) override;
41     std::string key;
42     std::string bundleName;
43     int64_t subscriberId = 0;
44     Data value;
45     int32_t userId = Id::INVALID_USER;
46     std::time_t timestamp = 0;
47 };
48 
49 class PublishedData final : public KvData {
50 public:
51     explicit PublishedData(const PublishedDataNode &node);
52     static std::vector<PublishedData> Query(const std::string &bundleName, int32_t userId);
53     static void Delete(const std::string &bundleName, const int32_t userId);
54     static void UpdateTimestamp(
55         const std::string &key, const std::string &bundleName, int64_t subscriberId, const int32_t userId);
56     static void ClearAging();
57     static int32_t Query(const std::string &filter, PublishedDataNode::Data &publishedData);
58     static std::string GenId(const std::string &key, const std::string &bundleName, int64_t subscriberId);
59     PublishedData(const PublishedDataNode &node, const int version);
60     ~PublishedData() = default;
61     bool HasVersion() const override;
62     int GetVersion() const override;
63     std::string GetValue() const override;
64     friend class GetDataStrategy;
65 
66 private:
67     PublishedDataNode value;
68 };
69 } // namespace OHOS::DataShare
70 #endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H
71