• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "common/memory_image/data-store.h"
18 
19 #include "util/base/logging.h"
20 
21 namespace libtextclassifier {
22 namespace nlp_core {
23 namespace memory_image {
24 
DataStore(StringPiece bytes)25 DataStore::DataStore(StringPiece bytes)
26     : reader_(bytes.data(), bytes.size()) {
27   if (!reader_.success_status()) {
28     TC_LOG(ERROR) << "Unable to successfully initialize DataStore.";
29   }
30 }
31 
GetData(const std::string & name) const32 DataBlobView DataStore::GetData(const std::string &name) const {
33   if (!reader_.success_status()) {
34     TC_LOG(ERROR) << "DataStore::GetData(" << name << ") "
35                   << "called on invalid DataStore; will return empty data "
36                   << "chunk";
37     return DataBlobView();
38   }
39 
40   const auto &entries = reader_.trimmed_proto().entries();
41   const auto &it = entries.find(name);
42   if (it == entries.end()) {
43     TC_LOG(ERROR) << "Unknown key: " << name
44                   << "; will return empty data chunk";
45     return DataBlobView();
46   }
47 
48   const DataStoreEntryBytes &entry_bytes = it->second;
49   if (!entry_bytes.has_blob_index()) {
50     TC_LOG(ERROR) << "DataStoreEntryBytes with no blob_index; "
51                   << "will return empty data chunk.";
52     return DataBlobView();
53   }
54 
55   int blob_index = entry_bytes.blob_index();
56   return reader_.data_blob_view(blob_index);
57 }
58 
59 }  // namespace memory_image
60 }  // namespace nlp_core
61 }  // namespace libtextclassifier
62