• 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 // TableBuilder provides the interface used to build a Table
17 // (an immutable and sorted map from keys to values).
18 //
19 // Multiple threads can invoke const methods on a TableBuilder without
20 // external synchronization, but if any of the threads may call a
21 // non-const method, all threads accessing the same TableBuilder must use
22 // external synchronization.
23 
24 #ifndef TENSORFLOW_CORE_LIB_IO_TABLE_BUILDER_H_
25 #define TENSORFLOW_CORE_LIB_IO_TABLE_BUILDER_H_
26 
27 #include <stdint.h>
28 #include "tensorflow/core/lib/core/status.h"
29 #include "tensorflow/core/lib/io/table_options.h"
30 
31 namespace tensorflow {
32 class WritableFile;
33 namespace table {
34 
35 class BlockBuilder;
36 class BlockHandle;
37 
38 class TableBuilder {
39  public:
40   // Create a builder that will store the contents of the table it is
41   // building in *file.  Does not close the file.  It is up to the
42   // caller to close the file after calling Finish().
43   TableBuilder(const Options& options, WritableFile* file);
44 
45   // REQUIRES: Either Finish() or Abandon() has been called.
46   ~TableBuilder();
47 
48   // Add key,value to the table being constructed.
49   // REQUIRES: key is after any previously added key in lexicographic order.
50   // REQUIRES: Finish(), Abandon() have not been called
51   void Add(const StringPiece& key, const StringPiece& value);
52 
53   // Advanced operation: writes any buffered key/value pairs to file.
54   // Can be used to ensure that two adjacent entries never live in
55   // the same data block.  Most clients should not need to use this method.
56   // Does not flush the file itself.
57   // REQUIRES: Finish(), Abandon() have not been called
58   void Flush();
59 
60   // Return non-ok iff some error has been detected.
61   Status status() const;
62 
63   // Finish building the table.  Stops using the file passed to the
64   // constructor after this function returns.
65   // REQUIRES: Finish(), Abandon() have not been called
66   Status Finish();
67 
68   // Indicate that the contents of this builder should be abandoned.  Stops
69   // using the file passed to the constructor after this function returns.
70   // If the caller is not going to call Finish(), it must call Abandon()
71   // before destroying this builder.
72   // REQUIRES: Finish(), Abandon() have not been called
73   void Abandon();
74 
75   // Number of calls to Add() so far.
76   uint64 NumEntries() const;
77 
78   // Size of the file generated so far.  If invoked after a successful
79   // Finish() call, returns the size of the final generated file.
80   uint64 FileSize() const;
81 
82  private:
ok()83   bool ok() const { return status().ok(); }
84   void WriteBlock(BlockBuilder* block, BlockHandle* handle);
85   void WriteRawBlock(const StringPiece& data, CompressionType,
86                      BlockHandle* handle);
87 
88   struct Rep;
89   Rep* rep_;
90 
91   // No copying allowed
92   TableBuilder(const TableBuilder&);
93   void operator=(const TableBuilder&);
94 };
95 
96 }  // namespace table
97 }  // namespace tensorflow
98 
99 #endif  // TENSORFLOW_CORE_LIB_IO_TABLE_BUILDER_H_
100