Home | History | Annotate | Download | only in memory_image
      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 #ifndef LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_DATA_STORE_H_
     18 #define LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_DATA_STORE_H_
     19 
     20 #include <string>
     21 
     22 #include "common/memory_image/data-store.pb.h"
     23 #include "common/memory_image/memory-image-common.h"
     24 #include "common/memory_image/memory-image-reader.h"
     25 #include "util/strings/stringpiece.h"
     26 
     27 namespace libtextclassifier {
     28 namespace nlp_core {
     29 namespace memory_image {
     30 
     31 // Class to access a data store.  See usage example in comments for
     32 // DataStoreBuilder.
     33 class DataStore {
     34  public:
     35   // Constructs a DataStore using the indicated bytes, i.e., bytes.size() bytes
     36   // starting at address bytes.data().  These bytes should contain the
     37   // serialization of a data store, see DataStoreBuilder::SerializeAsString().
     38   explicit DataStore(StringPiece bytes);
     39 
     40   // Retrieves (start_addr, num_bytes) info for piece of memory that contains
     41   // the data associated with the indicated name.  Note: this piece of memory is
     42   // inside the [start, start + size) (see constructor).  This piece of memory
     43   // starts at an offset from start which is a multiple of the alignment
     44   // specified when the data store was built using DataStoreBuilder.
     45   //
     46   // If the alignment is a low power of 2 (e..g, 4, 8, or 16) and "start" passed
     47   // to constructor corresponds to the beginning of a memory page or an address
     48   // returned by new or malloc(), then start_addr is divisible with alignment.
     49   DataBlobView GetData(const std::string &name) const;
     50 
     51  private:
     52   MemoryImageReader<DataStoreProto> reader_;
     53 };
     54 
     55 }  // namespace memory_image
     56 }  // namespace nlp_core
     57 }  // namespace libtextclassifier
     58 
     59 #endif  // LIBTEXTCLASSIFIER_COMMON_MEMORY_IMAGE_DATA_STORE_H_
     60