xref: /aosp_15_r20/external/tensorflow/tensorflow/c/experimental/filesystem/plugins/gcs/gcs_filesystem.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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 #ifndef TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_GCS_GCS_FILESYSTEM_H_
16 #define TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_GCS_GCS_FILESYSTEM_H_
17 
18 #include "google/cloud/storage/client.h"
19 #include "tensorflow/c/experimental/filesystem/filesystem_interface.h"
20 #include "tensorflow/c/experimental/filesystem/plugins/gcs/expiring_lru_cache.h"
21 #include "tensorflow/c/experimental/filesystem/plugins/gcs/ram_file_block_cache.h"
22 #include "tensorflow/c/tf_status.h"
23 
24 void ParseGCSPath(const std::string& fname, bool object_empty_ok,
25                   std::string* bucket, std::string* object, TF_Status* status);
26 
27 namespace tf_random_access_file {
28 void Cleanup(TF_RandomAccessFile* file);
29 int64_t Read(const TF_RandomAccessFile* file, uint64_t offset, size_t n,
30              char* buffer, TF_Status* status);
31 }  // namespace tf_random_access_file
32 
33 namespace tf_writable_file {
34 void Cleanup(TF_WritableFile* file);
35 void Append(const TF_WritableFile* file, const char* buffer, size_t n,
36             TF_Status* status);
37 int64_t Tell(const TF_WritableFile* file, TF_Status* status);
38 void Flush(const TF_WritableFile* file, TF_Status* status);
39 void Sync(const TF_WritableFile* file, TF_Status* status);
40 void Close(const TF_WritableFile* file, TF_Status* status);
41 }  // namespace tf_writable_file
42 
43 namespace tf_read_only_memory_region {
44 void Cleanup(TF_ReadOnlyMemoryRegion* region);
45 const void* Data(const TF_ReadOnlyMemoryRegion* region);
46 uint64_t Length(const TF_ReadOnlyMemoryRegion* region);
47 }  // namespace tf_read_only_memory_region
48 
49 namespace tf_gcs_filesystem {
50 typedef struct GcsFileStat {
51   TF_FileStatistics base;
52   int64_t generation_number;
53 } GcsFileStat;
54 
55 typedef struct GCSFile {
56   google::cloud::storage::Client gcs_client;  // owned
57   bool compose;
58   absl::Mutex block_cache_lock;
59   std::shared_ptr<RamFileBlockCache> file_block_cache
60       ABSL_GUARDED_BY(block_cache_lock);
61   uint64_t block_size;  // Reads smaller than block_size will trigger a read
62                         // of block_size.
63   std::unique_ptr<ExpiringLRUCache<GcsFileStat>> stat_cache;
64   GCSFile(google::cloud::storage::Client&& gcs_client);
65   // This constructor is used for testing purpose only.
66   GCSFile(google::cloud::storage::Client&& gcs_client, bool compose,
67           uint64_t block_size, size_t max_bytes, uint64_t max_staleness,
68           uint64_t stat_cache_max_age, size_t stat_cache_max_entries);
69 } GCSFile;
70 
71 // This function is used to initialize a filesystem without the need of setting
72 // manually environement variables.
73 void InitTest(TF_Filesystem* filesystem, bool compose, uint64_t block_size,
74               size_t max_bytes, uint64_t max_staleness,
75               uint64_t stat_cache_max_age, size_t stat_cache_max_entries,
76               TF_Status* status);
77 
78 void Init(TF_Filesystem* filesystem, TF_Status* status);
79 void Cleanup(TF_Filesystem* filesystem);
80 void NewRandomAccessFile(const TF_Filesystem* filesystem, const char* path,
81                          TF_RandomAccessFile* file, TF_Status* status);
82 void NewWritableFile(const TF_Filesystem* filesystem, const char* path,
83                      TF_WritableFile* file, TF_Status* status);
84 void NewAppendableFile(const TF_Filesystem* filesystem, const char* path,
85                        TF_WritableFile* file, TF_Status* status);
86 void NewReadOnlyMemoryRegionFromFile(const TF_Filesystem* filesystem,
87                                      const char* path,
88                                      TF_ReadOnlyMemoryRegion* region,
89                                      TF_Status* status);
90 int64_t GetFileSize(const TF_Filesystem* filesystem, const char* path,
91                     TF_Status* status);
92 void PathExists(const TF_Filesystem* filesystem, const char* path,
93                 TF_Status* status);
94 void CreateDir(const TF_Filesystem* filesystem, const char* path,
95                TF_Status* status);
96 int GetChildren(const TF_Filesystem* filesystem, const char* path,
97                 char*** entries, TF_Status* status);
98 void DeleteFile(const TF_Filesystem* filesystem, const char* path,
99                 TF_Status* status);
100 void Stat(const TF_Filesystem* filesystem, const char* path,
101           TF_FileStatistics* stats, TF_Status* status);
102 void DeleteDir(const TF_Filesystem* filesystem, const char* path,
103                TF_Status* status);
104 void CopyFile(const TF_Filesystem* filesystem, const char* src, const char* dst,
105               TF_Status* status);
106 void RenameFile(const TF_Filesystem* filesystem, const char* src,
107                 const char* dst, TF_Status* status);
108 }  // namespace tf_gcs_filesystem
109 
110 #endif  // TENSORFLOW_C_EXPERIMENTAL_FILESYSTEM_PLUGINS_GCS_GCS_FILESYSTEM_H_
111