1bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi/*
2bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * Copyright (C) 2017 The Android Open Source Project
3bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi *
4bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * Licensed under the Apache License, Version 2.0 (the "License");
5bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * you may not use this file except in compliance with the License.
6bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * You may obtain a copy of the License at
7bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi *
8bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi *      http://www.apache.org/licenses/LICENSE-2.0
9bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi *
10bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * Unless required by applicable law or agreed to in writing, software
11bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * distributed under the License is distributed on an "AS IS" BASIS,
12bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * See the License for the specific language governing permissions and
14bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi * limitations under the License.
15bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi */
16bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
17bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi#ifndef LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_DATA_STORE_H_
18bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi#define LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_DATA_STORE_H_
19bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
20bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi#include <string>
21bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
22bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi#include "common/memory_image/data-store.pb.h"
23bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi#include "common/memory_image/memory-image-common.h"
24bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi#include "common/memory_image/memory-image-reader.h"
25bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi#include "util/strings/stringpiece.h"
26bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
27bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifinamespace libtextclassifier {
28bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifinamespace nlp_core {
29bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifinamespace memory_image {
30bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
31bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi// Class to access a data store.  See usage example in comments for
32bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi// DataStoreBuilder.
33bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharificlass DataStore {
34bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi public:
35bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // Constructs a DataStore using the indicated bytes, i.e., bytes.size() bytes
36bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // starting at address bytes.data().  These bytes should contain the
37bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // serialization of a data store, see DataStoreBuilder::SerializeAsString().
38bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  explicit DataStore(StringPiece bytes);
39bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
40bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // Retrieves (start_addr, num_bytes) info for piece of memory that contains
41bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // the data associated with the indicated name.  Note: this piece of memory is
42bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // inside the [start, start + size) (see constructor).  This piece of memory
43bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // starts at an offset from start which is a multiple of the alignment
44bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // specified when the data store was built using DataStoreBuilder.
45bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  //
46bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // If the alignment is a low power of 2 (e..g, 4, 8, or 16) and "start" passed
47bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // to constructor corresponds to the beginning of a memory page or an address
48bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  // returned by new or malloc(), then start_addr is divisible with alignment.
49bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  DataBlobView GetData(const std::string &name) const;
50bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
51bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi private:
52bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi  MemoryImageReader<DataStoreProto> reader_;
53bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi};
54bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
55bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi}  // namespace memory_image
56bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi}  // namespace nlp_core
57bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi}  // namespace libtextclassifier
58bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi
59bda09f1da39ce38a5ece4757b82a64776e53214cMatt Sharifi#endif  // LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_DATA_STORE_H_
60