@@ -18,20 +18,19 @@ limitations under the License.
1818
1919#include < stdint.h>
2020#include < string>
21- #include < unordered_map>
2221#include < vector>
23- #include " tensorflow/core/lib/core/errors.h"
2422#include " tensorflow/core/lib/core/status.h"
2523#include " tensorflow/core/lib/core/stringpiece.h"
26- #include " tensorflow/core/platform/file_system.h"
2724#include " tensorflow/core/platform/macros.h"
28- #include " tensorflow/core/platform/mutex.h"
2925#include " tensorflow/core/platform/protobuf.h"
3026#include " tensorflow/core/platform/types.h"
3127
3228namespace tensorflow {
3329
30+ class RandomAccessFile ;
31+ class ReadOnlyMemoryRegion ;
3432class Thread ;
33+ class WritableFile ;
3534struct ThreadOptions ;
3635
3736// / \brief An interface used by the tensorflow implementation to
@@ -56,11 +55,6 @@ class Env {
5655 // / The result of Default() belongs to this library and must never be deleted.
5756 static Env* Default ();
5857
59- // / \brief Returns the FileSystem object to handle operations on the file
60- // / specified by 'fname'. The FileSystem object is used as the implementation
61- // / for the file system related (non-virtual) functions that follow.
62- virtual Status GetFileSystemForFile (const string& fname, FileSystem** result);
63-
6458 // / \brief Creates a brand new random access read-only file with the
6559 // / specified name.
6660
@@ -70,7 +64,8 @@ class Env {
7064 // / status.
7165 // /
7266 // / The returned file may be concurrently accessed by multiple threads.
73- Status NewRandomAccessFile (const string& fname, RandomAccessFile** result);
67+ virtual Status NewRandomAccessFile (const string& fname,
68+ RandomAccessFile** result) = 0;
7469
7570 // / \brief Creates an object that writes to a new file with the specified
7671 // / name.
@@ -81,7 +76,8 @@ class Env {
8176 // / returns non-OK.
8277 // /
8378 // / The returned file will only be accessed by one thread at a time.
84- Status NewWritableFile (const string& fname, WritableFile** result);
79+ virtual Status NewWritableFile (const string& fname,
80+ WritableFile** result) = 0;
8581
8682 // / \brief Creates an object that either appends to an existing file, or
8783 // / writes to a new file (if the file does not exist to begin with).
@@ -91,7 +87,8 @@ class Env {
9187 // / non-OK.
9288 // /
9389 // / The returned file will only be accessed by one thread at a time.
94- Status NewAppendableFile (const string& fname, WritableFile** result);
90+ virtual Status NewAppendableFile (const string& fname,
91+ WritableFile** result) = 0;
9592
9693 // / \brief Creates a readonly region of memory with the file context.
9794 // /
@@ -100,33 +97,34 @@ class Env {
10097 // / the caller. On failure stores nullptr in *result and returns non-OK.
10198 // /
10299 // / The returned memory region can be accessed from many threads in parallel.
103- Status NewReadOnlyMemoryRegionFromFile (const string& fname,
104- ReadOnlyMemoryRegion** result);
100+ virtual Status NewReadOnlyMemoryRegionFromFile (
101+ const string& fname, ReadOnlyMemoryRegion** result) = 0 ;
105102
106103 // / Returns true iff the named file exists.
107- bool FileExists (const string& fname);
104+ virtual bool FileExists (const string& fname) = 0 ;
108105
109106 // / \brief Stores in *result the names of the children of the specified
110107 // / directory. The names are relative to "dir".
111108 // /
112109 // / Original contents of *results are dropped.
113- Status GetChildren (const string& dir, std::vector<string>* result);
110+ virtual Status GetChildren (const string& dir,
111+ std::vector<string>* result) = 0;
114112
115113 // / Deletes the named file.
116- Status DeleteFile (const string& fname);
114+ virtual Status DeleteFile (const string& fname) = 0 ;
117115
118116 // / Creates the specified directory.
119- Status CreateDir (const string& dirname);
117+ virtual Status CreateDir (const string& dirname) = 0 ;
120118
121119 // / Deletes the specified directory.
122- Status DeleteDir (const string& dirname);
120+ virtual Status DeleteDir (const string& dirname) = 0 ;
123121
124122 // / Stores the size of `fname` in `*file_size`.
125- Status GetFileSize (const string& fname, uint64* file_size);
123+ virtual Status GetFileSize (const string& fname, uint64* file_size) = 0 ;
126124
127125 // / \brief Renames file src to target. If target already exists, it will be
128126 // / replaced.
129- Status RenameFile (const string& src, const string& target);
127+ virtual Status RenameFile (const string& src, const string& target) = 0 ;
130128
131129 // TODO(jeff,sanjay): Add back thread/thread-pool support if needed.
132130 // TODO(jeff,sanjay): if needed, tighten spec so relative to epoch, or
@@ -186,6 +184,68 @@ class Env {
186184 void operator =(const Env&);
187185};
188186
187+ // / A file abstraction for randomly reading the contents of a file.
188+ class RandomAccessFile {
189+ public:
190+ RandomAccessFile () {}
191+ virtual ~RandomAccessFile ();
192+
193+ // / \brief Reads up to `n` bytes from the file starting at `offset`.
194+ // /
195+ // / `scratch[0..n-1]` may be written by this routine. Sets `*result`
196+ // / to the data that was read (including if fewer than `n` bytes were
197+ // / successfully read). May set `*result` to point at data in
198+ // / `scratch[0..n-1]`, so `scratch[0..n-1]` must be live when
199+ // / `*result` is used.
200+ // /
201+ // / On OK returned status: `n` bytes have been stored in `*result`.
202+ // / On non-OK returned status: `[0..n]` bytes have been stored in `*result`.
203+ // /
204+ // / Returns `OUT_OF_RANGE` if fewer than n bytes were stored in `*result`
205+ // / because of EOF.
206+ // /
207+ // / Safe for concurrent use by multiple threads.
208+ virtual Status Read (uint64 offset, size_t n, StringPiece* result,
209+ char * scratch) const = 0;
210+
211+ private:
212+ // / No copying allowed
213+ RandomAccessFile (const RandomAccessFile&);
214+ void operator =(const RandomAccessFile&);
215+ };
216+
217+ // / \brief A file abstraction for sequential writing.
218+ // /
219+ // / The implementation must provide buffering since callers may append
220+ // / small fragments at a time to the file.
221+ class WritableFile {
222+ public:
223+ WritableFile () {}
224+ virtual ~WritableFile ();
225+
226+ virtual Status Append (const StringPiece& data) = 0;
227+ virtual Status Close () = 0;
228+ virtual Status Flush () = 0;
229+ virtual Status Sync () = 0;
230+
231+ private:
232+ // / No copying allowed
233+ WritableFile (const WritableFile&);
234+ void operator =(const WritableFile&);
235+ };
236+
237+ // / \brief A readonly memmapped file abstraction.
238+ // /
239+ // / The implementation must guarantee that all memory is accessable when the
240+ // / object exists, independently from the Env that created it.
241+ class ReadOnlyMemoryRegion {
242+ public:
243+ ReadOnlyMemoryRegion () {}
244+ virtual ~ReadOnlyMemoryRegion () = default ;
245+ virtual const void * data () = 0;
246+ virtual uint64 length () = 0;
247+ };
248+
189249// / \brief An implementation of Env that forwards all calls to another Env.
190250// /
191251// / May be useful to clients who wish to override just part of the
@@ -199,11 +259,33 @@ class EnvWrapper : public Env {
199259 // / Returns the target to which this Env forwards all calls
200260 Env* target () const { return target_; }
201261
202- Status GetFileSystemForFile (const string& fname,
203- FileSystem** result) override {
204- return target_->GetFileSystemForFile (fname, result);
262+ // The following text is boilerplate that forwards all methods to target()
263+ Status NewRandomAccessFile (const string& f, RandomAccessFile** r) override {
264+ return target_->NewRandomAccessFile (f, r);
265+ }
266+ Status NewWritableFile (const string& f, WritableFile** r) override {
267+ return target_->NewWritableFile (f, r);
268+ }
269+ Status NewAppendableFile (const string& f, WritableFile** r) override {
270+ return target_->NewAppendableFile (f, r);
271+ }
272+ Status NewReadOnlyMemoryRegionFromFile (
273+ const string& fname, ReadOnlyMemoryRegion** result) override {
274+ return target_->NewReadOnlyMemoryRegionFromFile (fname, result);
275+ }
276+ bool FileExists (const string& f) override { return target_->FileExists (f); }
277+ Status GetChildren (const string& dir, std::vector<string>* r) override {
278+ return target_->GetChildren (dir, r);
279+ }
280+ Status DeleteFile (const string& f) override { return target_->DeleteFile (f); }
281+ Status CreateDir (const string& d) override { return target_->CreateDir (d); }
282+ Status DeleteDir (const string& d) override { return target_->DeleteDir (d); }
283+ Status GetFileSize (const string& f, uint64* s) override {
284+ return target_->GetFileSize (f, s);
285+ }
286+ Status RenameFile (const string& s, const string& t) override {
287+ return target_->RenameFile (s, t);
205288 }
206-
207289 uint64 NowMicros () override { return target_->NowMicros (); }
208290 void SleepForMicroseconds (int micros) override {
209291 target_->SleepForMicroseconds (micros);
0 commit comments