| 905 | additional_header_(additional_header) {} |
| 906 | |
| 907 | Status GcsFileSystem::NewRandomAccessFile( |
| 908 | const string& fname, std::unique_ptr<RandomAccessFile>* result) { |
| 909 | string bucket, object; |
| 910 | TF_RETURN_IF_ERROR(ParseGcsPath(fname, false, &bucket, &object)); |
| 911 | TF_RETURN_IF_ERROR(CheckBucketLocationConstraint(bucket)); |
| 912 | bool cache_enabled; |
| 913 | { |
| 914 | mutex_lock l(block_cache_lock_); |
| 915 | cache_enabled = file_block_cache_->IsCacheEnabled(); |
| 916 | } |
| 917 | if (cache_enabled) { |
| 918 | result->reset(new GcsRandomAccessFile(fname, [this, bucket, object]( |
| 919 | const string& fname, |
| 920 | uint64 offset, size_t n, |
| 921 | StringPiece* result, |
| 922 | char* scratch) { |
| 923 | tf_shared_lock l(block_cache_lock_); |
| 924 | GcsFileStat stat; |
| 925 | TF_RETURN_IF_ERROR(stat_cache_->LookupOrCompute( |
| 926 | fname, &stat, |
| 927 | [this, bucket, object](const string& fname, GcsFileStat* stat) { |
| 928 | return UncachedStatForObject(fname, bucket, object, stat); |
| 929 | })); |
| 930 | if (!file_block_cache_->ValidateAndUpdateFileSignature( |
| 931 | fname, stat.generation_number)) { |
| 932 | VLOG(1) |
| 933 | << "File signature has been changed. Refreshing the cache. Path: " |
| 934 | << fname; |
| 935 | } |
| 936 | *result = StringPiece(); |
| 937 | size_t bytes_transferred; |
| 938 | TF_RETURN_IF_ERROR(file_block_cache_->Read(fname, offset, n, scratch, |
| 939 | &bytes_transferred)); |
| 940 | *result = StringPiece(scratch, bytes_transferred); |
| 941 | if (bytes_transferred < n) { |
| 942 | return errors::OutOfRange("EOF reached, ", result->size(), |
| 943 | " bytes were read out of ", n, |
| 944 | " bytes requested."); |
| 945 | } |
| 946 | return Status::OK(); |
| 947 | })); |
| 948 | } else { |
| 949 | result->reset(new BufferedGcsRandomAccessFile( |
| 950 | fname, block_size_, |
| 951 | [this, bucket, object](const string& fname, uint64 offset, size_t n, |
| 952 | StringPiece* result, char* scratch) { |
| 953 | *result = StringPiece(); |
| 954 | size_t bytes_transferred; |
| 955 | TF_RETURN_IF_ERROR( |
| 956 | LoadBufferFromGCS(fname, offset, n, scratch, &bytes_transferred)); |
| 957 | *result = StringPiece(scratch, bytes_transferred); |
| 958 | if (bytes_transferred < n) { |
| 959 | return errors::OutOfRange("EOF reached, ", result->size(), |
| 960 | " bytes were read out of ", n, |
| 961 | " bytes requested."); |
| 962 | } |
| 963 | return Status::OK(); |
| 964 | })); |