• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LIB_IO_FORMAT_H_
17 #define TENSORFLOW_LIB_IO_FORMAT_H_
18 
19 #include <stdint.h>
20 #include <string>
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/lib/core/stringpiece.h"
23 #include "tensorflow/core/lib/io/table_builder.h"
24 
25 namespace tensorflow {
26 class RandomAccessFile;
27 namespace table {
28 
29 class Block;
30 
31 // BlockHandle is a pointer to the extent of a file that stores a data
32 // block or a meta block.
33 class BlockHandle {
34  public:
35   BlockHandle();
36 
37   // The offset of the block in the file.
offset()38   uint64 offset() const { return offset_; }
set_offset(uint64 offset)39   void set_offset(uint64 offset) { offset_ = offset; }
40 
41   // The size of the stored block
size()42   uint64 size() const { return size_; }
set_size(uint64 size)43   void set_size(uint64 size) { size_ = size; }
44 
45   void EncodeTo(string* dst) const;
46   Status DecodeFrom(StringPiece* input);
47 
48   // Maximum encoding length of a BlockHandle
49   enum { kMaxEncodedLength = 10 + 10 };
50 
51  private:
52   uint64 offset_;
53   uint64 size_;
54 };
55 
56 // Footer encapsulates the fixed information stored at the tail
57 // end of every table file.
58 class Footer {
59  public:
Footer()60   Footer() {}
61 
62   // The block handle for the metaindex block of the table
metaindex_handle()63   const BlockHandle& metaindex_handle() const { return metaindex_handle_; }
set_metaindex_handle(const BlockHandle & h)64   void set_metaindex_handle(const BlockHandle& h) { metaindex_handle_ = h; }
65 
66   // The block handle for the index block of the table
index_handle()67   const BlockHandle& index_handle() const { return index_handle_; }
set_index_handle(const BlockHandle & h)68   void set_index_handle(const BlockHandle& h) { index_handle_ = h; }
69 
70   void EncodeTo(string* dst) const;
71   Status DecodeFrom(StringPiece* input);
72 
73   // Encoded length of a Footer.  Note that the serialization of a
74   // Footer will always occupy exactly this many bytes.  It consists
75   // of two block handles and a magic number.
76   enum { kEncodedLength = 2 * BlockHandle::kMaxEncodedLength + 8 };
77 
78  private:
79   BlockHandle metaindex_handle_;
80   BlockHandle index_handle_;
81 };
82 
83 // kTableMagicNumber was picked by running
84 //    echo http://code.google.com/p/leveldb/ | sha1sum
85 // and taking the leading 64 bits.
86 static const uint64 kTableMagicNumber = 0xdb4775248b80fb57ull;
87 
88 // 1-byte type + 32-bit crc
89 static const size_t kBlockTrailerSize = 5;
90 
91 struct BlockContents {
92   StringPiece data;     // Actual contents of data
93   bool cachable;        // True iff data can be cached
94   bool heap_allocated;  // True iff caller should delete[] data.data()
95 };
96 
97 // Read the block identified by "handle" from "file".  On failure
98 // return non-OK.  On success fill *result and return OK.
99 extern Status ReadBlock(RandomAccessFile* file, const BlockHandle& handle,
100                         BlockContents* result);
101 
102 // Implementation details follow.  Clients should ignore,
103 
BlockHandle()104 inline BlockHandle::BlockHandle()
105     : offset_(~static_cast<uint64>(0)), size_(~static_cast<uint64>(0)) {}
106 
107 }  // namespace table
108 }  // namespace tensorflow
109 
110 #endif  // TENSORFLOW_LIB_IO_FORMAT_H_
111