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 SQLITE_SINGLE_VER_CONTINUE_TOKEN_H
16 #define SQLITE_SINGLE_VER_CONTINUE_TOKEN_H
17 
18 #include <map>
19 
20 #include "db_types.h"
21 #include "query_object.h"
22 #include "single_ver_kvdb_sync_interface.h"
23 
24 namespace DistributedDB {
25 class SQLiteSingleVerContinueToken {
26 public:
27     // For one device.
28     SQLiteSingleVerContinueToken(Timestamp begin, Timestamp end);
29 
30     // For one device in query sync.
31     SQLiteSingleVerContinueToken(const SyncTimeRange &timeRange, const QueryObject &queryObject);
32 
33     // For multiple device.
34     explicit SQLiteSingleVerContinueToken(MulDevTimeRanges timeRanges);
35 
36     ~SQLiteSingleVerContinueToken();
37 
38     /*
39      * function: Check the magic number at the beginning and end of the SingleVerContinueToken.
40      * returnValue: Return true if the begin and end magic number is OK.
41      *              Return false if the begin or end magic number is error.
42      */
43     bool CheckValid() const;
44 
45     Timestamp GetQueryBeginTime() const;
46     Timestamp GetQueryEndTime() const;
47     Timestamp GetDeletedBeginTime() const;
48     Timestamp GetDeletedEndTime() const;
49 
50     void SetNextBeginTime(const DeviceID &deviceID, Timestamp nextBeginTime);
51     const MulDevTimeRanges &GetTimeRanges();
52     void SetDeletedNextBeginTime(const DeviceID &deviceID, Timestamp nextBeginTime);
53     const MulDevTimeRanges &GetDeletedTimeRanges() const;
54 
55     void FinishGetQueryData();
56     void FinishGetDeletedData();
57 
58     bool IsGetQueryDataFinished() const;
59     bool IsGetDeletedDataFinished() const;
60 
61     bool IsQuerySync() const;
62     QueryObject GetQuery() const;
63 
64     std::pair<int, sqlite3_stmt *> GetCloudQueryStmt(sqlite3 *db, bool forcePush, bool &stepNext,
65         const CloudWaterType mode);
66     void ReleaseCloudQueryStmt();
67 
68     void SetUser(const std::string &user);
69 private:
70     void RemovePrevDevAndSetBeginTime(const DeviceID &deviceID, Timestamp nextBeginTime, MulDevTimeRanges &timeRanges);
71 
72     Timestamp GetBeginTimestamp(const MulDevTimeRanges &timeRanges) const;
73     Timestamp GetEndTimestamp(const MulDevTimeRanges &timeRanges) const;
74 
75     static const unsigned int MAGIC_BEGIN = 0x600D0AC7;  // for token guard
76     static const unsigned int MAGIC_END = 0x0AC7600D;    // for token guard
77     unsigned int magicBegin_ = MAGIC_BEGIN;
78     std::map<DeviceID, QueryObject> queryObject_;
79     MulDevTimeRanges timeRanges_;
80     MulDevTimeRanges deleteTimeRanges_;
81     unsigned int magicEnd_ = MAGIC_END;
82 
83     sqlite3_stmt *queryDataStmt_ = nullptr;
84     std::string user_;
85 };
86 }  // namespace DistributedDB
87 #endif  // SQLITE_SINGLE_VER_CONTINUE_TOKEN_H