tensorflow_io_gcs_filesystem/core/gcs_filesystem.cc
// TODO(vnvo2409): We could download into a local temporary file and use
// memory-mapping.
We could download into a local temporary file and use
memory-mapping.
We could download into a local temporary file and use memory - mapping .
void NewReadOnlyMemoryRegionFromFile(const TF_Filesystem* filesystem,
                                     const char* path,
                                     TF_ReadOnlyMemoryRegion* region,
                                     TF_Status* status) {
  std::string bucket, object;
  ParseGCSPath(path, false, &bucket, &object, status);
  if (TF_GetCode(status) != TF_OK) return;
  auto gcs_file =
      static_cast<GCSFileSystem*>(filesystem->plugin_filesystem)->Load(status);
  if (TF_GetCode(status) != TF_OK) {
    return;
  }
  auto metadata = gcs_file->gcs_client.GetObjectMetadata(bucket, object,
                                                         gcs::Fields("size"));
  if (!metadata) {
    TF_SetStatusFromGCSStatus(metadata.status(), status);
    return;
  }
  TF_RandomAccessFile reader;
  NewRandomAccessFile(filesystem, path, &reader, status);
  if (TF_GetCode(status) != TF_OK) return;
  char* buffer = static_cast<char*>(plugin_memory_allocate(metadata->size()));
  int64_t read =
      tf_random_access_file::Read(&reader, 0, metadata->size(), buffer, status);
  tf_random_access_file::Cleanup(&reader);
  if (TF_GetCode(status) != TF_OK) return;
  if (read > 0 && buffer) {
    region->plugin_memory_region =
        new tf_read_only_memory_region::GCSReadOnlyMemoryRegion(
            {buffer, static_cast<uint64_t>(read)});
    TF_SetStatus(status, TF_OK, "");
  } else if (read == 0) {
    TF_SetStatus(status, TF_INVALID_ARGUMENT, "File is empty");
  }
}
void NewReadOnlyMemoryRegionFromFile ( const TF_Filesystem * filesystem , const char * path , TF_ReadOnlyMemoryRegion * region , TF_Status * status ) { std :: string bucket , object ; ParseGCSPath ( path , false , & bucket , & object , status ) ; if ( TF_GetCode ( status ) != TF_OK ) return ; auto gcs_file = static_cast < GCSFileSystem * > ( filesystem -> plugin_filesystem ) -> Load ( status ) ; if ( TF_GetCode ( status ) != TF_OK ) { return ; } auto metadata = gcs_file -> gcs_client . GetObjectMetadata ( bucket , object , gcs :: Fields ( " " ) ) ; if ( ! metadata ) { TF_SetStatusFromGCSStatus ( metadata . status ( ) , status ) ; return ; } TF_RandomAccessFile reader ; NewRandomAccessFile ( filesystem , path , & reader , status ) ; if ( TF_GetCode ( status ) != TF_OK ) return ; char * buffer = static_cast < char * > ( plugin_memory_allocate ( metadata -> size ( ) ) ) ; int64_t read = tf_random_access_file :: Read ( & reader , 0 , metadata -> size ( ) , buffer , status ) ; tf_random_access_file :: Cleanup ( & reader ) ; if ( TF_GetCode ( status ) != TF_OK ) return ; if ( read > 0 && buffer ) { region -> plugin_memory_region = new tf_read_only_memory_region :: GCSReadOnlyMemoryRegion ( { buffer , static_cast < uint64_t > ( read ) } ) ; TF_SetStatus ( status , TF_OK , " " ) ; } else if ( read == 0 ) { TF_SetStatus ( status , TF_INVALID_ARGUMENT , " " ) ; } }
TODO(vnvo2409): We could download into a local temporary file and use
memory-mapping.
TODO ( vnvo2409 ) : We could download into a local temporary file and use memory - mapping .