1 /*
2 * Copyright (C) 2022 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 #define MLOG_TAG "Thumbnail"
16
17 #include "thumbnail_datashare_bridge.h"
18 #include "medialibrary_errno.h"
19 #include "media_log.h"
20
21 namespace OHOS {
22 namespace Media {
23 using namespace DataShare;
24 using namespace DistributedKv;
ThumbnailSemaphore(int32_t count)25 ThumbnailSemaphore::ThumbnailSemaphore(int32_t count) : count_(count)
26 {}
27
Signal()28 void ThumbnailSemaphore::Signal()
29 {
30 {
31 std::unique_lock<std::mutex> lock(mutex_);
32 ++count_;
33 }
34 cv_.notify_one();
35 }
36
Wait()37 void ThumbnailSemaphore::Wait()
38 {
39 std::unique_lock<std::mutex> lock(mutex_);
40 cv_.wait(lock, [=] { return count_ > 0; });
41 --count_;
42 }
43
44 static constexpr int32_t THUMBNAIL_SEM_NUM = 4;
45 ThumbnailSemaphore ThumbnailDataShareBridge::sem_(THUMBNAIL_SEM_NUM);
GetRowCount(int32_t & count)46 int ThumbnailDataShareBridge::GetRowCount(int32_t &count)
47 {
48 std::shared_ptr<KvStoreResultSet> kvResultSet;
49 if (singleKvStorePtr_ == nullptr) {
50 MEDIA_ERR_LOG("singleKvStorePtr_ nullptr");
51 return E_ERR;
52 }
53 sem_.Wait();
54 singleKvStorePtr_->GetResultSet(thumbnailKey_, kvResultSet);
55 if (kvResultSet == nullptr) {
56 MEDIA_ERR_LOG("kvResultSet nullptr");
57 sem_.Signal();
58 return E_ERR;
59 }
60 count = kvResultSet->GetCount();
61 if (count < 0) {
62 MEDIA_ERR_LOG("kvResultSet count error: %{public}d", count);
63 sem_.Signal();
64 return E_ERR;
65 }
66 singleKvStorePtr_->CloseResultSet(kvResultSet);
67 sem_.Signal();
68 return E_OK;
69 }
70
GetAllColumnNames(std::vector<std::string> & columnsName)71 int ThumbnailDataShareBridge::GetAllColumnNames(std::vector<std::string> &columnsName)
72 {
73 columnsName = { "key", "velue" };
74 return E_OK;
75 }
76
FillBlock(int pos,ResultSetBridge::Writer & writer)77 bool ThumbnailDataShareBridge::FillBlock(int pos, ResultSetBridge::Writer &writer)
78 {
79 if (singleKvStorePtr_ == nullptr) {
80 MEDIA_ERR_LOG("singleKvStorePtr_ nullptr");
81 return false;
82 }
83
84 sem_.Wait();
85 std::shared_ptr<KvStoreResultSet> kvResultSet;
86 singleKvStorePtr_->GetResultSet(thumbnailKey_, kvResultSet);
87 if (kvResultSet == nullptr) {
88 MEDIA_ERR_LOG("kvResultSet nullptr");
89 sem_.Signal();
90 return false;
91 }
92 if (!kvResultSet->MoveToPosition(pos)) {
93 MEDIA_ERR_LOG("MoveToPosition failed");
94 sem_.Signal();
95 return false;
96 }
97 Entry entry;
98 Status status = kvResultSet->GetEntry(entry);
99 if (status != Status::SUCCESS) {
100 MEDIA_ERR_LOG("GetEntry failed: %{public}d", status);
101 sem_.Signal();
102 return false;
103 }
104 status = singleKvStorePtr_->CloseResultSet(kvResultSet);
105 if (status != Status::SUCCESS) {
106 MEDIA_ERR_LOG("CloseResultSet failed: %{public}d", status);
107 sem_.Signal();
108 return false;
109 }
110 sem_.Signal();
111
112 int statusAlloc = writer.AllocRow();
113 if (statusAlloc != E_OK) {
114 MEDIA_ERR_LOG("ShraedBlock is full: %{public}d", statusAlloc);
115 return false;
116 }
117 int keyStatus = writer.Write(0, entry.key.ToString().c_str(), entry.key.Size() + 1);
118 if (keyStatus != E_OK) {
119 MEDIA_ERR_LOG("WriterBlob key error: %{public}d", keyStatus);
120 return false;
121 }
122 int valueStatus = writer.Write(1, entry.value.ToString().c_str(), entry.value.Size() + 1);
123 if (valueStatus != E_OK) {
124 MEDIA_ERR_LOG("WriterBlob key error: %{public}d", valueStatus);
125 return false;
126 }
127
128 return true;
129 }
130
Count(std::shared_ptr<KvStoreResultSet> & kvResultSet)131 int ThumbnailDataShareBridge::Count(std::shared_ptr<KvStoreResultSet> &kvResultSet)
132 {
133 if (kvResultSet == nullptr) {
134 MEDIA_ERR_LOG("kvResultSet nullptr");
135 return E_ERR;
136 }
137 if (resultRowCount_ != INVALID_COUNT) {
138 return resultRowCount_;
139 }
140 int count = kvResultSet->GetCount();
141 if (count < 0) {
142 MEDIA_ERR_LOG("kvResultSet count invalid: %{pubilc}d", count);
143 return E_ERR;
144 }
145 resultRowCount_ = count;
146 return count;
147 }
148
OnGo(int32_t start,int32_t target,ResultSetBridge::Writer & writer)149 int ThumbnailDataShareBridge::OnGo(int32_t start, int32_t target, ResultSetBridge::Writer &writer)
150 {
151 if ((start < 0) || (target < 0) || (start > target)) {
152 MEDIA_ERR_LOG("nowRowIndex out of line: %{pubilc}d", target);
153 return -1;
154 }
155 for (int pos = start; pos <= target; pos++) {
156 bool ret = FillBlock(pos, writer);
157 if (!ret) {
158 MEDIA_ERR_LOG("nowRowIndex out of line: %{pubilc}d", target);
159 return pos - 1;
160 }
161 }
162 return target;
163 }
164
ThumbnailDataShareBridge(const std::shared_ptr<DistributedKv::SingleKvStore> & kvStore,const std::string & key)165 ThumbnailDataShareBridge::ThumbnailDataShareBridge(const std::shared_ptr<DistributedKv::SingleKvStore> &kvStore,
166 const std::string &key)
167 {
168 singleKvStorePtr_ = kvStore;
169 thumbnailKey_ = key;
170 }
171
Create(const std::shared_ptr<SingleKvStore> & kvStore,const std::string & key)172 std::shared_ptr<ResultSetBridge> ThumbnailDataShareBridge::Create(const std::shared_ptr<SingleKvStore> &kvStore,
173 const std::string &key)
174 {
175 if (kvStore == nullptr) {
176 MEDIA_ERR_LOG("param error, kvStore nullptr");
177 return nullptr;
178 }
179
180 return std::shared_ptr<ResultSetBridge>(
181 new (std::nothrow) ThumbnailDataShareBridge(kvStore, key));
182 }
183 } // namespace Media
184 } // namespace OHOS
185