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 KVSTORE_OBSERVER_IMPL_H
16 #define KVSTORE_OBSERVER_IMPL_H
17 
18 #include <condition_variable>
19 #include <list>
20 #include <mutex>
21 #include "distributeddb_data_generator.h"
22 #include "kv_store_changed_data.h"
23 #include "kv_store_observer.h"
24 
25 enum ListType {
26     INSERT_LIST = 0,
27     UPDATE_LIST = 1,
28     DELETE_LIST = 2
29 };
30 
31 class KvStoreObserverImpl final : public DistributedDB::KvStoreObserver {
32 public:
33     void OnChange(const DistributedDB::KvStoreChangedData &data);
34 
35     KvStoreObserverImpl();
36 
37     ~KvStoreObserverImpl();
38 
KvStoreObserverImpl(bool isCumulatedFlag)39     KvStoreObserverImpl(bool isCumulatedFlag) : isSaveCumulatedData_(isCumulatedFlag)
40     {
41     }
42     KvStoreObserverImpl(const KvStoreObserverImpl &);
43     KvStoreObserverImpl& operator=(const KvStoreObserverImpl &);
44 
45     const std::list<DistributedDB::Entry> GetInsertList() const;
46 
47     const std::list<DistributedDB::Entry> GetUpdateList() const;
48 
49     const std::list<DistributedDB::Entry> GetDeleteList() const;
50 
51     int GetChanged() const;
52 
53     void WaitUntilReachChangeCount(unsigned int countGoal, uint32_t timeout = 0) const; // timeout in second
54     // timeout in second
55     void WaitUntilReachRecordCount(unsigned int countExpect, ListType waitWhat, uint32_t timeout = 0) const;
56 
57     microClock_type GetOnChangeTime();
58 
59     void Clear();
60 
61     void SetCumulatedFlag(bool isSaveCumulatedData);
62 
63     bool GetCumulatedFlag() const;
64 
65     const std::list<DistributedDB::Entry> GetCumulatedInsertList() const;
66 
67     const std::list<DistributedDB::Entry> GetCumulatedUpdateList() const;
68 
69     const std::list<DistributedDB::Entry> GetCumulatedDeleteList() const;
70 
71 private:
72     std::list<DistributedDB::Entry> insertedEntries_ = {};
73     std::list<DistributedDB::Entry> updatedEntries_ = {};
74     std::list<DistributedDB::Entry> deleteEntries_ = {};
75     unsigned int changed_ = 0;
76     microClock_type onChangeTime_
77         = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::steady_clock::now());
78     bool isSaveCumulatedData_ = false;
79     std::list<DistributedDB::Entry> cumulatedInsertList_ = {};
80     std::list<DistributedDB::Entry> cumulatedUpdateList_ = {};
81     std::list<DistributedDB::Entry> cumulatedDeleteList_ = {};
82     // For waiting method
83     mutable std::mutex waitChangeMutex_;
84     mutable std::condition_variable waitChangeCv_;
85 };
86 #endif // KVSTORE_OBSERVER_IMPL_H