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
16 #include "sqlite_single_ver_continue_token.h"
17
18 namespace DistributedDB {
SQLiteSingleVerContinueToken(Timestamp begin,Timestamp end)19 SQLiteSingleVerContinueToken::SQLiteSingleVerContinueToken(Timestamp begin, Timestamp end)
20 : timeRanges_(MulDevTimeRanges{{"", {begin, end}}})
21 {}
22
SQLiteSingleVerContinueToken(const SyncTimeRange & timeRange,const QueryObject & queryObject)23 SQLiteSingleVerContinueToken::SQLiteSingleVerContinueToken(
24 const SyncTimeRange &timeRange, const QueryObject &queryObject)
25 : queryObject_(std::map<DeviceID, QueryObject>{{"", queryObject}}),
26 timeRanges_(MulDevTimeRanges{{"", {timeRange.beginTime, timeRange.endTime}}}),
27 deleteTimeRanges_(MulDevTimeRanges{{"", {timeRange.deleteBeginTime, timeRange.deleteEndTime}}})
28 {}
29
SQLiteSingleVerContinueToken(MulDevTimeRanges timeRanges)30 SQLiteSingleVerContinueToken::SQLiteSingleVerContinueToken(MulDevTimeRanges timeRanges)
31 : timeRanges_(timeRanges)
32 {}
33
~SQLiteSingleVerContinueToken()34 SQLiteSingleVerContinueToken::~SQLiteSingleVerContinueToken()
35 {}
36
CheckValid() const37 bool SQLiteSingleVerContinueToken::CheckValid() const
38 {
39 return ((magicBegin_ == MAGIC_BEGIN) && (magicEnd_ == MAGIC_END));
40 }
41
GetQueryBeginTime() const42 Timestamp SQLiteSingleVerContinueToken::GetQueryBeginTime() const
43 {
44 return GetBeginTimestamp(timeRanges_);
45 }
46
GetQueryEndTime() const47 Timestamp SQLiteSingleVerContinueToken::GetQueryEndTime() const
48 {
49 return GetEndTimestamp(timeRanges_);
50 }
51
GetDeletedBeginTime() const52 Timestamp SQLiteSingleVerContinueToken::GetDeletedBeginTime() const
53 {
54 return GetBeginTimestamp(deleteTimeRanges_);
55 }
56
GetDeletedEndTime() const57 Timestamp SQLiteSingleVerContinueToken::GetDeletedEndTime() const
58 {
59 return GetEndTimestamp(deleteTimeRanges_);
60 }
61
SetNextBeginTime(const DeviceID & deviceID,Timestamp nextBeginTime)62 void SQLiteSingleVerContinueToken::SetNextBeginTime(const DeviceID &deviceID, Timestamp nextBeginTime)
63 {
64 RemovePrevDevAndSetBeginTime(deviceID, nextBeginTime, timeRanges_);
65 }
66
GetTimeRanges()67 const MulDevTimeRanges& SQLiteSingleVerContinueToken::GetTimeRanges()
68 {
69 return timeRanges_;
70 }
71
SetDeletedNextBeginTime(const DeviceID & deviceID,Timestamp nextBeginTime)72 void SQLiteSingleVerContinueToken::SetDeletedNextBeginTime(const DeviceID &deviceID, Timestamp nextBeginTime)
73 {
74 RemovePrevDevAndSetBeginTime(deviceID, nextBeginTime, deleteTimeRanges_);
75 }
76
GetDeletedTimeRanges() const77 const MulDevTimeRanges& SQLiteSingleVerContinueToken::GetDeletedTimeRanges() const
78 {
79 return deleteTimeRanges_;
80 }
81
FinishGetQueryData()82 void SQLiteSingleVerContinueToken::FinishGetQueryData()
83 {
84 timeRanges_.clear();
85 }
86
FinishGetDeletedData()87 void SQLiteSingleVerContinueToken::FinishGetDeletedData()
88 {
89 deleteTimeRanges_.clear();
90 }
91
IsGetQueryDataFinished() const92 bool SQLiteSingleVerContinueToken::IsGetQueryDataFinished() const
93 {
94 return timeRanges_.empty();
95 }
96
IsGetDeletedDataFinished() const97 bool SQLiteSingleVerContinueToken::IsGetDeletedDataFinished() const
98 {
99 return deleteTimeRanges_.empty();
100 }
101
IsQuerySync() const102 bool SQLiteSingleVerContinueToken::IsQuerySync() const
103 {
104 return !queryObject_.empty();
105 }
106
GetQuery() const107 QueryObject SQLiteSingleVerContinueToken::GetQuery() const
108 {
109 if (!queryObject_.empty()) {
110 return queryObject_.begin()->second;
111 }
112 return QueryObject{};
113 }
114
RemovePrevDevAndSetBeginTime(const DeviceID & deviceID,Timestamp nextBeginTime,MulDevTimeRanges & timeRanges)115 void SQLiteSingleVerContinueToken::RemovePrevDevAndSetBeginTime(const DeviceID &deviceID, Timestamp nextBeginTime,
116 MulDevTimeRanges &timeRanges)
117 {
118 auto iter = timeRanges.find(deviceID);
119 if (iter == timeRanges.end()) {
120 return;
121 }
122 iter = timeRanges.erase(timeRanges.begin(), iter);
123 iter->second.first = nextBeginTime;
124 }
125
GetBeginTimestamp(const MulDevTimeRanges & timeRanges) const126 Timestamp SQLiteSingleVerContinueToken::GetBeginTimestamp(const MulDevTimeRanges &timeRanges) const
127 {
128 if (timeRanges.empty()) {
129 return 0;
130 }
131 return timeRanges.begin()->second.first;
132 }
133
GetEndTimestamp(const MulDevTimeRanges & timeRanges) const134 Timestamp SQLiteSingleVerContinueToken::GetEndTimestamp(const MulDevTimeRanges &timeRanges) const
135 {
136 if (timeRanges.empty()) {
137 return static_cast<Timestamp>(INT64_MAX);
138 }
139 return timeRanges.begin()->second.second;
140 }
141 } // namespace DistributedDB
142