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/in-memory-model-data.h"
18
19#include "common/file-utils.h"
20#include "common/memory_image/memory-image-common.h"
21#include "util/base/logging.h"
22#include "util/strings/stringpiece.h"
23
24namespace libtextclassifier {
25namespace nlp_core {
26
27const char InMemoryModelData::kTaskSpecDataStoreEntryName[] = "TASK-SPEC-#@";
28const char InMemoryModelData::kFilePatternPrefix[] = "in-mem-model::";
29
30bool InMemoryModelData::GetTaskSpec(TaskSpec *task_spec) const {
31  DataBlobView blob = data_store_.GetData(kTaskSpecDataStoreEntryName);
32  if (blob.data() == nullptr) {
33    TC_LOG(ERROR) << "Can't find data blob for TaskSpec, i.e., entry "
34                  << kTaskSpecDataStoreEntryName;
35    return false;
36  }
37  bool parse_status = file_utils::ParseProtoFromMemory(
38      blob.to_stringpiece(), task_spec);
39  if (!parse_status) {
40    TC_LOG(ERROR) << "Error parsing TaskSpec";
41    return false;
42  }
43  return true;
44}
45
46StringPiece InMemoryModelData::GetBytesForInputFile(
47    const std::string &file_name) const {
48  // TODO(salcianu): replace our DataBlobView with StringPiece everywhere.
49  DataBlobView blob = data_store_.GetData(file_name);
50  return StringPiece(reinterpret_cast<const char *>(blob.data()),
51                     blob.size());
52}
53
54}  // namespace nlp_core
55}  // namespace libtextclassifier
56