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 #include "dk_database.h"
16
17 #include <functional>
18 #include <iostream>
19 #include <thread>
20
21 #include "dk_context.h"
22
23 namespace DriveKit {
SaveRecords(std::shared_ptr<DKContext> context,std::vector<DKRecord> && records,DKSavePolicy policy,SaveRecordsCallback callback)24 DKLocalErrorCode DKDatabase::SaveRecords(std::shared_ptr<DKContext> context,
25 std::vector<DKRecord> &&records,
26 DKSavePolicy policy,
27 SaveRecordsCallback callback)
28 {
29 /* mock */
30 auto result = std::make_shared<std::map<DKRecordId, DKRecordOperResult>>();
31 srand(time(nullptr));
32 for (auto &record : records) {
33 DKRecordId recordId = std::to_string(rand() + 1);
34 DKRecordOperResult operResult;
35 operResult.SetDKRecord(std::move(record));
36 (*result)[recordId] = operResult;
37 }
38
39 DKError err;
40 std::thread ([=]() { callback(context, this->shared_from_this(), result, err); }).detach();
41
42 return DKLocalErrorCode::NO_ERROR;
43 }
SaveRecord(std::shared_ptr<DKContext> context,DKRecord && record,DKSavePolicy policy,SaveRecordCallback callback)44 DKLocalErrorCode DKDatabase::SaveRecord(std::shared_ptr<DKContext> context,
45 DKRecord &&record,
46 DKSavePolicy policy,
47 SaveRecordCallback callback)
48 {
49 return DKLocalErrorCode::NO_ERROR;
50 }
FetchRecords(std::shared_ptr<DKContext> context,DKRecordType recordType,DKFieldKeyArray & desiredKeys,int resultLimit,DKQueryCursor cursor,FetchRecordsCallback callback)51 DKLocalErrorCode DKDatabase::FetchRecords(std::shared_ptr<DKContext> context,
52 DKRecordType recordType,
53 DKFieldKeyArray &desiredKeys,
54 int resultLimit,
55 DKQueryCursor cursor,
56 FetchRecordsCallback callback)
57 {
58 auto result = std::make_shared<std::vector<DKRecord>>();
59 DKQueryCursor newCursor;
60
61 DKError err;
62 std::thread ([=]() { callback(context, this->shared_from_this(), result, newCursor, err); }).detach();
63 return DKLocalErrorCode::NO_ERROR;
64 }
65
FetchRecordWithId(std::shared_ptr<DKContext> context,DKRecordType recordType,DKRecordId recordId,DKFieldKeyArray & desiredKeys,FetchRecordCallback callback)66 DKLocalErrorCode DKDatabase::FetchRecordWithId(std::shared_ptr<DKContext> context,
67 DKRecordType recordType,
68 DKRecordId recordId,
69 DKFieldKeyArray &desiredKeys,
70 FetchRecordCallback callback)
71 {
72 return DKLocalErrorCode::NO_ERROR;
73 }
74
DeleteRecords(std::shared_ptr<DKContext> context,std::vector<DKRecord> && records,DKSavePolicy policy,DeleteRecordsCallback callback)75 DKLocalErrorCode DKDatabase::DeleteRecords(std::shared_ptr<DKContext> context,
76 std::vector<DKRecord> &&records,
77 DKSavePolicy policy,
78 DeleteRecordsCallback callback)
79 {
80 /* mock */
81 auto result = std::make_shared<std::map<DKRecordId, DKRecordOperResult>>();
82 for (auto &record : records) {
83 DKRecordId recordId = record.GetRecordId();
84 DKRecordOperResult operResult;
85 operResult.SetDKRecord(std::move(record));
86 (*result)[recordId] = operResult;
87 }
88
89 DKError err;
90 std::thread ([=]() { callback(context, this->shared_from_this(), result, err); }).detach();
91 return DKLocalErrorCode::NO_ERROR;
92 }
93
ModifyRecords(std::shared_ptr<DKContext> context,std::vector<DKRecord> && recordsToSave,std::vector<DKRecord> && recordsToDelete,DKSavePolicy policy,bool atomically,ModifyRecordsCallback callback)94 DKLocalErrorCode DKDatabase::ModifyRecords(std::shared_ptr<DKContext> context,
95 std::vector<DKRecord> &&recordsToSave,
96 std::vector<DKRecord> &&recordsToDelete,
97 DKSavePolicy policy,
98 bool atomically,
99 ModifyRecordsCallback callback)
100 {
101 /* mock */
102 auto result = std::make_shared<std::map<DKRecordId, DKRecordOperResult>>();
103 for (auto &record : recordsToSave) {
104 DKRecordId recordId = record.GetRecordId();
105 DKRecordOperResult operResult;
106 operResult.SetDKRecord(std::move(record));
107 (*result)[recordId] = operResult;
108 }
109
110 DKError err;
111 std::thread ([=]() { callback(context, this->shared_from_this(), result, nullptr, err); }).detach();
112 return DKLocalErrorCode::NO_ERROR;
113 }
114
FetchRecordsWithQuery(std::shared_ptr<DKContext> context,DKFieldKeyArray & desiredKeys,DKQuery query,int resultLimit,DKQueryCursor cursor,FetchRecordsCallback callback)115 DKLocalErrorCode DKDatabase::FetchRecordsWithQuery(std::shared_ptr<DKContext> context,
116 DKFieldKeyArray &desiredKeys,
117 DKQuery query,
118 int resultLimit,
119 DKQueryCursor cursor,
120 FetchRecordsCallback callback)
121 {
122 return DKLocalErrorCode::NO_ERROR;
123 }
124
FetchDatabaseChanges(std::shared_ptr<DKContext> context,DKRecordType recordType,DKFieldKeyArray & desiredKeys,int resultLimit,DKQueryCursor cursor,FetchDatabaseCallback callback)125 DKLocalErrorCode DKDatabase::FetchDatabaseChanges(std::shared_ptr<DKContext> context,
126 DKRecordType recordType,
127 DKFieldKeyArray &desiredKeys,
128 int resultLimit,
129 DKQueryCursor cursor,
130 FetchDatabaseCallback callback)
131 {
132 auto result = std::make_shared<std::vector<DKRecord>>();
133 bool hasMore = false;
134 DKQueryCursor newCursor;
135
136 DKError err;
137 std::thread ([=]() { callback(context, this->shared_from_this(), result, newCursor, hasMore, err); }).detach();
138 return DKLocalErrorCode::NO_ERROR;
139 }
140
GetStartCursor(DKRecordType recordType,DKQueryCursor & cursor)141 DKError DKDatabase::GetStartCursor(DKRecordType recordType, DKQueryCursor &cursor)
142 {
143 DKError e;
144 return e;
145 }
GenerateIds(int count,std::vector<DKRecordId> & ids)146 DKError DKDatabase::GenerateIds(int count, std::vector<DKRecordId> &ids)
147 {
148 DKError e;
149 return e;
150 }
GetLock(DKLock & lock)151 DKError DKDatabase::GetLock(DKLock &lock)
152 {
153 DKError e;
154 return e;
155 }
DeleteLock(DKLock lock)156 void DKDatabase::DeleteLock(DKLock lock) {}
GetAssetsDownloader()157 std::shared_ptr<DKAssetsDownloader> DKDatabase::GetAssetsDownloader()
158 {
159 return std::make_shared<DKAssetsDownloader>(shared_from_this());
160 }
NewAssetReadSession(DKRecordType recordType,DKRecordId recordId,DKFieldKey assetKey,DKAssetPath assetPath)161 std::shared_ptr<DKAssetReadSession> DKDatabase::NewAssetReadSession(DKRecordType recordType,
162 DKRecordId recordId,
163 DKFieldKey assetKey,
164 DKAssetPath assetPath)
165 {
166 return std::make_shared<DKAssetReadSession>();
167 }
DKDatabase(std::shared_ptr<DKContainer> container,DKDatabaseScope scope)168 DKDatabase::DKDatabase(std::shared_ptr<DKContainer> container, DKDatabaseScope scope)
169 {
170 }
Init()171 void DKDatabase::Init()
172 {
173 }
174 } // namespace DriveKit
175