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 #define LOG_TAG "KvStoreSyncCallbackClient"
17
18 #include "kvstore_sync_callback_client.h"
19 #include <cinttypes>
20 #include <atomic>
21 #include "dds_trace.h"
22 #include "log_print.h"
23
24 namespace OHOS {
25 namespace DistributedKv {
26 using namespace OHOS::DistributedDataDfx;
~KvStoreSyncCallbackClient()27 KvStoreSyncCallbackClient::~KvStoreSyncCallbackClient()
28 {
29 syncCallbackInfo_.Clear();
30 }
31
SyncCompleted(const std::map<std::string,Status> & results,uint64_t sequenceId)32 void KvStoreSyncCallbackClient::SyncCompleted(const std::map<std::string, Status> &results, uint64_t sequenceId)
33 {
34 DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__), TraceSwitch::BYTRACE_ON);
35 auto finded = syncCallbackInfo_.Find(sequenceId);
36 if (finded.first) {
37 finded.second->SyncCompleted(results);
38 DeleteSyncCallback(sequenceId);
39 }
40 }
41
AddSyncCallback(const std::shared_ptr<KvStoreSyncCallback> callback,uint64_t sequenceId)42 void KvStoreSyncCallbackClient::AddSyncCallback(
43 const std::shared_ptr<KvStoreSyncCallback> callback, uint64_t sequenceId)
44 {
45 if (callback == nullptr) {
46 ZLOGE("callback is nullptr");
47 return;
48 }
49 auto inserted = syncCallbackInfo_.Insert(sequenceId, callback);
50 if (!inserted) {
51 ZLOGE("The sequeuceId %{public}" PRIu64 "is repeat!", sequenceId);
52 }
53 }
54
DeleteSyncCallback(uint64_t sequenceId)55 void KvStoreSyncCallbackClient::DeleteSyncCallback(uint64_t sequenceId)
56 {
57 syncCallbackInfo_.Erase(sequenceId);
58 }
59 } // namespace DistributedKv
60 } // namespace OHOS
61