• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "medialibrary_tracer.h"
20 #include "media_log.h"
21 
22 namespace OHOS {
23 namespace Media {
24 using namespace DataShare;
25 using namespace DistributedKv;
ThumbnailSemaphore(int32_t count)26 ThumbnailSemaphore::ThumbnailSemaphore(int32_t count) : count_(count)
27 {}
28 
Signal()29 void ThumbnailSemaphore::Signal()
30 {
31     {
32         std::unique_lock<std::mutex> lock(mutex_);
33         ++count_;
34     }
35     cv_.notify_one();
36 }
37 
Wait()38 void ThumbnailSemaphore::Wait()
39 {
40     std::unique_lock<std::mutex> lock(mutex_);
41     cv_.wait(lock, [=] { return count_ > 0; });
42     --count_;
43 }
44 
45 static constexpr int32_t THUMBNAIL_SEM_NUM = 4;
46 ThumbnailSemaphore ThumbnailDataShareBridge::sem_(THUMBNAIL_SEM_NUM);
GetRowCount(int32_t & count)47 int ThumbnailDataShareBridge::GetRowCount(int32_t &count)
48 {
49     count = 1;
50     return E_OK;
51 }
52 
GetAllColumnNames(std::vector<std::string> & columnsName)53 int ThumbnailDataShareBridge::GetAllColumnNames(std::vector<std::string> &columnsName)
54 {
55     columnsName = { "key", "velue" };
56     return E_OK;
57 }
58 
FillBlock(int pos,ResultSetBridge::Writer & writer)59 bool ThumbnailDataShareBridge::FillBlock(int pos, ResultSetBridge::Writer &writer)
60 {
61     if (singleKvStorePtr_ == nullptr) {
62         MEDIA_ERR_LOG("singleKvStorePtr_ nullptr");
63         return false;
64     }
65 
66     MediaLibraryTracer tracer;
67     tracer.Start("ThumbnailDataShareBridge::Get");
68     sem_.Wait();
69     Key key(thumbnailKey_);
70     Value value;
71     Status status = singleKvStorePtr_->Get(key, value);
72     if (status != Status::SUCCESS) {
73         MEDIA_ERR_LOG("GetEntry failed: %{public}d", status);
74         sem_.Signal();
75         return false;
76     }
77     sem_.Signal();
78     tracer.Finish();
79 
80     tracer.Start("ThumbnailDataShareBridge::Writer");
81     int statusAlloc = writer.AllocRow();
82     if (statusAlloc != E_OK) {
83         MEDIA_ERR_LOG("ShraedBlock is full: %{public}d", statusAlloc);
84         return false;
85     }
86     int keyStatus = writer.Write(0, key.ToString().c_str(), key.Size() + 1);
87     if (keyStatus != E_OK) {
88         MEDIA_ERR_LOG("WriterBlob key error: %{public}d", keyStatus);
89         return false;
90     }
91     int valueStatus = writer.Write(1, value.ToString().c_str(), value.Size() + 1);
92     if (valueStatus != E_OK) {
93         MEDIA_ERR_LOG("WriterBlob key error: %{public}d", valueStatus);
94         return false;
95     }
96 
97     return true;
98 }
99 
OnGo(int32_t start,int32_t target,ResultSetBridge::Writer & writer)100 int ThumbnailDataShareBridge::OnGo(int32_t start, int32_t target, ResultSetBridge::Writer &writer)
101 {
102     if ((start < 0) || (target < 0) || (start > target)) {
103         MEDIA_ERR_LOG("nowRowIndex out of line: %{pubilc}d", target);
104         return -1;
105     }
106     for (int pos = start; pos <= target; pos++) {
107         bool ret = FillBlock(pos, writer);
108         if (!ret) {
109             MEDIA_ERR_LOG("nowRowIndex out of line: %{pubilc}d", target);
110             return pos - 1;
111         }
112     }
113     return target;
114 }
115 
ThumbnailDataShareBridge(const std::shared_ptr<DistributedKv::SingleKvStore> & kvStore,const std::string & key)116 ThumbnailDataShareBridge::ThumbnailDataShareBridge(const std::shared_ptr<DistributedKv::SingleKvStore> &kvStore,
117     const std::string &key)
118 {
119     singleKvStorePtr_ = kvStore;
120     thumbnailKey_ = key;
121 }
122 
Create(const std::shared_ptr<SingleKvStore> & kvStore,const std::string & key)123 std::shared_ptr<ResultSetBridge> ThumbnailDataShareBridge::Create(const std::shared_ptr<SingleKvStore> &kvStore,
124     const std::string &key)
125 {
126     if (kvStore == nullptr) {
127         MEDIA_ERR_LOG("param error, kvStore nullptr");
128         return nullptr;
129     }
130 
131     return std::shared_ptr<ResultSetBridge>(
132         new (std::nothrow) ThumbnailDataShareBridge(kvStore, key));
133 }
134 } // namespace Media
135 } // namespace OHOS
136