1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 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 16 #ifndef TENSORFLOW_CORE_LIB_IO_TABLE_H_ 17 #define TENSORFLOW_CORE_LIB_IO_TABLE_H_ 18 19 #include <stdint.h> 20 21 #include "tensorflow/core/lib/io/iterator.h" 22 23 namespace tensorflow { 24 25 class RandomAccessFile; 26 27 namespace table { 28 29 struct Options; 30 31 // A Table is a sorted map from strings to strings. Tables are 32 // immutable and persistent. A Table may be safely accessed from 33 // multiple threads without external synchronization. 34 class Table { 35 public: 36 // Attempt to open the table that is stored in bytes [0..file_size) 37 // of "file", and read the metadata entries necessary to allow 38 // retrieving data from the table. 39 // 40 // If successful, returns ok and sets "*table" to the newly opened 41 // table. The client should delete "*table" when no longer needed. 42 // If there was an error while initializing the table, sets "*table" 43 // to NULL and returns a non-ok status. Does not take ownership of 44 // "*file", but the client must ensure that "file" remains live 45 // for the duration of the returned table's lifetime. 46 static Status Open(const Options& options, RandomAccessFile* file, 47 uint64 file_size, Table** table); 48 49 ~Table(); 50 51 // Returns a new iterator over the table contents. 52 // The result of NewIterator() is initially invalid (caller must 53 // call one of the Seek methods on the iterator before using it). 54 Iterator* NewIterator() const; 55 56 // Given a key, return an approximate byte offset in the file where 57 // the data for that key begins (or would begin if the key were 58 // present in the file). The returned value is in terms of file 59 // bytes, and so includes effects like compression of the underlying data. 60 // E.g., the approximate offset of the last key in the table will 61 // be close to the file length. 62 uint64 ApproximateOffsetOf(const StringPiece& key) const; 63 64 private: 65 struct Rep; 66 Rep* rep_; 67 Table(Rep * rep)68 explicit Table(Rep* rep) { rep_ = rep; } 69 static Iterator* BlockReader(void*, const StringPiece&); 70 71 // Calls (*handle_result)(arg, ...) with the entry found after a call 72 // to Seek(key). May not make such a call if filter policy says 73 // that key is not present. 74 Status InternalGet(const StringPiece& key, void* arg, 75 void (*handle_result)(void* arg, const StringPiece& k, 76 const StringPiece& v)); 77 78 // No copying allowed 79 Table(const Table&); 80 void operator=(const Table&); 81 }; 82 83 } // namespace table 84 } // namespace tensorflow 85 86 #endif // TENSORFLOW_CORE_LIB_IO_TABLE_H_ 87