1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#ifndef TENSORFLOW_CORE_UTIL_MEMMAPPED_FILE_SYSTEM_WRITER_H_
16#define TENSORFLOW_CORE_UTIL_MEMMAPPED_FILE_SYSTEM_WRITER_H_
17
18#include <memory>
19#include <vector>
20
21#include "tensorflow/core/framework/tensor.h"
22#include "tensorflow/core/platform/env.h"
23#include "tensorflow/core/util/memmapped_file_system.h"
24#include "tensorflow/core/util/memmapped_file_system.pb.h"
25
26namespace tensorflow {
27
28// A class for saving into the memmapped format that can be read by
29// MemmappedFileSystem.
30class MemmappedFileSystemWriter {
31 public:
32 MemmappedFileSystemWriter() = default;
33 ~MemmappedFileSystemWriter() = default;
34 Status InitializeToFile(Env* env, const string& filename);
35 Status SaveTensor(const Tensor& tensor, const string& element_name);
36 Status SaveProtobuf(const protobuf::MessageLite& message,
37 const string& element_name);
38 // Writes out the directory of regions and closes the output file.
39 Status FlushAndClose();
40
41 private:
42 Status AdjustAlignment(uint64 alignment);
43 void AddToDirectoryElement(const string& element_name, uint64 length);
44 MemmappedFileSystemDirectory directory_;
45 // The current offset in the file, to support alignment.
46 uint64 output_file_offset_ = 0;
47 std::unique_ptr<WritableFile> output_file_;
48 TF_DISALLOW_COPY_AND_ASSIGN(MemmappedFileSystemWriter);
49};
50
51} // namespace tensorflow
52
53#endif // TENSORFLOW_CORE_UTIL_MEMMAPPED_FILE_SYSTEM_WRITER_H_
54