forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.cc
More file actions
783 lines (662 loc) · 26.7 KB
/
Copy pathfilesystem.cc
File metadata and controls
783 lines (662 loc) · 26.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <sstream>
#include <utility>
#include "arrow/util/config.h"
#include "arrow/filesystem/filesystem.h"
#ifdef ARROW_HDFS
#include "arrow/filesystem/hdfs.h"
#endif
#ifdef ARROW_GCS
#include "arrow/filesystem/gcsfs.h"
#endif
#ifdef ARROW_S3
#include "arrow/filesystem/s3fs.h"
#endif
#include "arrow/filesystem/localfs.h"
#include "arrow/filesystem/mockfs.h"
#include "arrow/filesystem/path_util.h"
#include "arrow/filesystem/util_internal.h"
#include "arrow/io/slow.h"
#include "arrow/io/util_internal.h"
#include "arrow/result.h"
#include "arrow/status.h"
#include "arrow/util/async_generator.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/logging.h"
#include "arrow/util/macros.h"
#include "arrow/util/parallel.h"
#include "arrow/util/uri.h"
#include "arrow/util/vector.h"
#include "arrow/util/windows_fixup.h"
namespace arrow {
using internal::checked_pointer_cast;
using internal::TaskHints;
using internal::Uri;
using io::internal::SubmitIO;
namespace fs {
using internal::ConcatAbstractPath;
using internal::EnsureTrailingSlash;
using internal::GetAbstractPathParent;
using internal::kSep;
using internal::RemoveLeadingSlash;
using internal::RemoveTrailingSlash;
using internal::ToSlashes;
std::string ToString(FileType ftype) {
switch (ftype) {
case FileType::NotFound:
return "not-found";
case FileType::Unknown:
return "unknown";
case FileType::File:
return "file";
case FileType::Directory:
return "directory";
default:
ARROW_LOG(FATAL) << "Invalid FileType value: " << static_cast<int>(ftype);
return "???";
}
}
// For googletest
ARROW_EXPORT std::ostream& operator<<(std::ostream& os, FileType ftype) {
#define FILE_TYPE_CASE(value_name) \
case FileType::value_name: \
os << "FileType::" ARROW_STRINGIFY(value_name); \
break;
switch (ftype) {
FILE_TYPE_CASE(NotFound)
FILE_TYPE_CASE(Unknown)
FILE_TYPE_CASE(File)
FILE_TYPE_CASE(Directory)
default:
ARROW_LOG(FATAL) << "Invalid FileType value: " << static_cast<int>(ftype);
}
#undef FILE_TYPE_CASE
return os;
}
std::string FileInfo::base_name() const {
return internal::GetAbstractPathParent(path_).second;
}
std::string FileInfo::dir_name() const {
return internal::GetAbstractPathParent(path_).first;
}
// Debug helper
std::string FileInfo::ToString() const {
std::stringstream os;
os << *this;
return os.str();
}
std::ostream& operator<<(std::ostream& os, const FileInfo& info) {
return os << "FileInfo(" << info.type() << ", " << info.path() << ")";
}
std::string FileInfo::extension() const {
return internal::GetAbstractPathExtension(path_);
}
//////////////////////////////////////////////////////////////////////////
// FileSystem default method implementations
FileSystem::~FileSystem() {}
Result<std::string> FileSystem::NormalizePath(std::string path) { return path; }
Result<std::vector<FileInfo>> FileSystem::GetFileInfo(
const std::vector<std::string>& paths) {
std::vector<FileInfo> res;
res.reserve(paths.size());
for (const auto& path : paths) {
ARROW_ASSIGN_OR_RAISE(FileInfo info, GetFileInfo(path));
res.push_back(std::move(info));
}
return res;
}
namespace {
template <typename DeferredFunc>
auto FileSystemDefer(FileSystem* fs, bool synchronous, DeferredFunc&& func)
-> decltype(DeferNotOk(
fs->io_context().executor()->Submit(func, std::shared_ptr<FileSystem>{}))) {
auto self = fs->shared_from_this();
if (synchronous) {
return std::forward<DeferredFunc>(func)(std::move(self));
}
return DeferNotOk(io::internal::SubmitIO(
fs->io_context(), std::forward<DeferredFunc>(func), std::move(self)));
}
} // namespace
Future<std::vector<FileInfo>> FileSystem::GetFileInfoAsync(
const std::vector<std::string>& paths) {
return FileSystemDefer(
this, default_async_is_sync_,
[paths](std::shared_ptr<FileSystem> self) { return self->GetFileInfo(paths); });
}
FileInfoGenerator FileSystem::GetFileInfoGenerator(const FileSelector& select) {
auto fut = FileSystemDefer(
this, default_async_is_sync_,
[select](std::shared_ptr<FileSystem> self) { return self->GetFileInfo(select); });
return MakeSingleFutureGenerator(std::move(fut));
}
Status FileSystem::DeleteFiles(const std::vector<std::string>& paths) {
Status st = Status::OK();
for (const auto& path : paths) {
st &= DeleteFile(path);
}
return st;
}
namespace {
Status ValidateInputFileInfo(const FileInfo& info) {
if (info.type() == FileType::NotFound) {
return internal::PathNotFound(info.path());
}
if (info.type() != FileType::File && info.type() != FileType::Unknown) {
return internal::NotAFile(info.path());
}
return Status::OK();
}
} // namespace
Future<> FileSystem::DeleteDirContentsAsync(const std::string& path,
bool missing_dir_ok) {
return FileSystemDefer(this, default_async_is_sync_,
[path, missing_dir_ok](std::shared_ptr<FileSystem> self) {
return self->DeleteDirContents(path, missing_dir_ok);
});
}
Result<std::shared_ptr<io::InputStream>> FileSystem::OpenInputStream(
const FileInfo& info) {
RETURN_NOT_OK(ValidateInputFileInfo(info));
return OpenInputStream(info.path());
}
Result<std::shared_ptr<io::RandomAccessFile>> FileSystem::OpenInputFile(
const FileInfo& info) {
RETURN_NOT_OK(ValidateInputFileInfo(info));
return OpenInputFile(info.path());
}
Future<std::shared_ptr<io::InputStream>> FileSystem::OpenInputStreamAsync(
const std::string& path) {
return FileSystemDefer(
this, default_async_is_sync_,
[path](std::shared_ptr<FileSystem> self) { return self->OpenInputStream(path); });
}
Future<std::shared_ptr<io::InputStream>> FileSystem::OpenInputStreamAsync(
const FileInfo& info) {
RETURN_NOT_OK(ValidateInputFileInfo(info));
return FileSystemDefer(
this, default_async_is_sync_,
[info](std::shared_ptr<FileSystem> self) { return self->OpenInputStream(info); });
}
Future<std::shared_ptr<io::RandomAccessFile>> FileSystem::OpenInputFileAsync(
const std::string& path) {
return FileSystemDefer(
this, default_async_is_sync_,
[path](std::shared_ptr<FileSystem> self) { return self->OpenInputFile(path); });
}
Future<std::shared_ptr<io::RandomAccessFile>> FileSystem::OpenInputFileAsync(
const FileInfo& info) {
RETURN_NOT_OK(ValidateInputFileInfo(info));
return FileSystemDefer(
this, default_async_is_sync_,
[info](std::shared_ptr<FileSystem> self) { return self->OpenInputFile(info); });
}
Result<std::shared_ptr<io::OutputStream>> FileSystem::OpenOutputStream(
const std::string& path) {
return OpenOutputStream(path, std::shared_ptr<const KeyValueMetadata>{});
}
Result<std::shared_ptr<io::OutputStream>> FileSystem::OpenAppendStream(
const std::string& path) {
return OpenAppendStream(path, std::shared_ptr<const KeyValueMetadata>{});
}
//////////////////////////////////////////////////////////////////////////
// SubTreeFileSystem implementation
namespace {
Status ValidateSubPath(std::string_view s) {
if (internal::IsLikelyUri(s)) {
return Status::Invalid("Expected a filesystem path, got a URI: '", s, "'");
}
return Status::OK();
}
} // namespace
SubTreeFileSystem::SubTreeFileSystem(const std::string& base_path,
std::shared_ptr<FileSystem> base_fs)
: FileSystem(base_fs->io_context()),
base_path_(NormalizeBasePath(base_path, base_fs).ValueOrDie()),
base_fs_(base_fs) {}
SubTreeFileSystem::~SubTreeFileSystem() {}
Result<std::string> SubTreeFileSystem::NormalizeBasePath(
std::string base_path, const std::shared_ptr<FileSystem>& base_fs) {
ARROW_ASSIGN_OR_RAISE(base_path, base_fs->NormalizePath(std::move(base_path)));
return EnsureTrailingSlash(std::move(base_path));
}
bool SubTreeFileSystem::Equals(const FileSystem& other) const {
if (this == &other) {
return true;
}
if (other.type_name() != type_name()) {
return false;
}
const auto& subfs = ::arrow::internal::checked_cast<const SubTreeFileSystem&>(other);
return base_path_ == subfs.base_path_ && base_fs_->Equals(subfs.base_fs_);
}
Result<std::string> SubTreeFileSystem::PrependBase(const std::string& s) const {
RETURN_NOT_OK(ValidateSubPath(s));
if (s.empty()) {
return base_path_;
} else {
return ConcatAbstractPath(base_path_, s);
}
}
Result<std::string> SubTreeFileSystem::PrependBaseNonEmpty(const std::string& s) const {
RETURN_NOT_OK(ValidateSubPath(s));
if (s.empty()) {
return Status::IOError("Empty path");
} else {
return ConcatAbstractPath(base_path_, s);
}
}
Result<std::string> SubTreeFileSystem::StripBase(const std::string& s) const {
auto len = base_path_.length();
// Note base_path_ ends with a slash (if not empty)
if (s.length() >= len && s.substr(0, len) == base_path_) {
return s.substr(len);
} else {
return Status::UnknownError("Underlying filesystem returned path '", s,
"', which is not a subpath of '", base_path_, "'");
}
}
Status SubTreeFileSystem::FixInfo(FileInfo* info) const {
ARROW_ASSIGN_OR_RAISE(auto fixed_path, StripBase(info->path()));
info->set_path(std::move(fixed_path));
return Status::OK();
}
Result<std::string> SubTreeFileSystem::NormalizePath(std::string path) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBase(path));
ARROW_ASSIGN_OR_RAISE(auto normalized, base_fs_->NormalizePath(real_path));
return StripBase(std::move(normalized));
}
Result<FileInfo> SubTreeFileSystem::GetFileInfo(const std::string& path) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBase(path));
ARROW_ASSIGN_OR_RAISE(FileInfo info, base_fs_->GetFileInfo(real_path));
RETURN_NOT_OK(FixInfo(&info));
return info;
}
Result<std::vector<FileInfo>> SubTreeFileSystem::GetFileInfo(const FileSelector& select) {
auto selector = select;
ARROW_ASSIGN_OR_RAISE(selector.base_dir, PrependBase(selector.base_dir));
ARROW_ASSIGN_OR_RAISE(auto infos, base_fs_->GetFileInfo(selector));
for (auto& info : infos) {
RETURN_NOT_OK(FixInfo(&info));
}
return infos;
}
FileInfoGenerator SubTreeFileSystem::GetFileInfoGenerator(const FileSelector& select) {
auto selector = select;
auto maybe_base_dir = PrependBase(selector.base_dir);
if (!maybe_base_dir.ok()) {
return MakeFailingGenerator<std::vector<FileInfo>>(maybe_base_dir.status());
}
selector.base_dir = *std::move(maybe_base_dir);
auto gen = base_fs_->GetFileInfoGenerator(selector);
auto self = checked_pointer_cast<SubTreeFileSystem>(shared_from_this());
std::function<Result<std::vector<FileInfo>>(const std::vector<FileInfo>& infos)>
fix_infos = [self](std::vector<FileInfo> infos) -> Result<std::vector<FileInfo>> {
for (auto& info : infos) {
RETURN_NOT_OK(self->FixInfo(&info));
}
return infos;
};
return MakeMappedGenerator(gen, fix_infos);
}
Status SubTreeFileSystem::CreateDir(const std::string& path, bool recursive) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->CreateDir(real_path, recursive);
}
Status SubTreeFileSystem::DeleteDir(const std::string& path) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->DeleteDir(real_path);
}
Status SubTreeFileSystem::DeleteDirContents(const std::string& path,
bool missing_dir_ok) {
if (internal::IsEmptyPath(path)) {
return internal::InvalidDeleteDirContents(path);
}
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBase(path));
return base_fs_->DeleteDirContents(real_path, missing_dir_ok);
}
Status SubTreeFileSystem::DeleteRootDirContents() {
if (base_path_.empty()) {
return base_fs_->DeleteRootDirContents();
} else {
return base_fs_->DeleteDirContents(base_path_);
}
}
Status SubTreeFileSystem::DeleteFile(const std::string& path) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->DeleteFile(real_path);
}
Status SubTreeFileSystem::Move(const std::string& src, const std::string& dest) {
ARROW_ASSIGN_OR_RAISE(auto real_src, PrependBaseNonEmpty(src));
ARROW_ASSIGN_OR_RAISE(auto real_dest, PrependBaseNonEmpty(dest));
return base_fs_->Move(real_src, real_dest);
}
Status SubTreeFileSystem::CopyFile(const std::string& src, const std::string& dest) {
ARROW_ASSIGN_OR_RAISE(auto real_src, PrependBaseNonEmpty(src));
ARROW_ASSIGN_OR_RAISE(auto real_dest, PrependBaseNonEmpty(dest));
return base_fs_->CopyFile(real_src, real_dest);
}
Result<std::shared_ptr<io::InputStream>> SubTreeFileSystem::OpenInputStream(
const std::string& path) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->OpenInputStream(real_path);
}
Result<std::shared_ptr<io::InputStream>> SubTreeFileSystem::OpenInputStream(
const FileInfo& info) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(info.path()));
FileInfo new_info(info);
new_info.set_path(std::move(real_path));
return base_fs_->OpenInputStream(new_info);
}
Future<std::shared_ptr<io::InputStream>> SubTreeFileSystem::OpenInputStreamAsync(
const std::string& path) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->OpenInputStreamAsync(real_path);
}
Future<std::shared_ptr<io::InputStream>> SubTreeFileSystem::OpenInputStreamAsync(
const FileInfo& info) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(info.path()));
FileInfo new_info(info);
new_info.set_path(std::move(real_path));
return base_fs_->OpenInputStreamAsync(new_info);
}
Result<std::shared_ptr<io::RandomAccessFile>> SubTreeFileSystem::OpenInputFile(
const std::string& path) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->OpenInputFile(real_path);
}
Result<std::shared_ptr<io::RandomAccessFile>> SubTreeFileSystem::OpenInputFile(
const FileInfo& info) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(info.path()));
FileInfo new_info(info);
new_info.set_path(std::move(real_path));
return base_fs_->OpenInputFile(new_info);
}
Future<std::shared_ptr<io::RandomAccessFile>> SubTreeFileSystem::OpenInputFileAsync(
const std::string& path) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->OpenInputFileAsync(real_path);
}
Future<std::shared_ptr<io::RandomAccessFile>> SubTreeFileSystem::OpenInputFileAsync(
const FileInfo& info) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(info.path()));
FileInfo new_info(info);
new_info.set_path(std::move(real_path));
return base_fs_->OpenInputFileAsync(new_info);
}
Result<std::shared_ptr<io::OutputStream>> SubTreeFileSystem::OpenOutputStream(
const std::string& path, const std::shared_ptr<const KeyValueMetadata>& metadata) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->OpenOutputStream(real_path, metadata);
}
Result<std::shared_ptr<io::OutputStream>> SubTreeFileSystem::OpenAppendStream(
const std::string& path, const std::shared_ptr<const KeyValueMetadata>& metadata) {
ARROW_ASSIGN_OR_RAISE(auto real_path, PrependBaseNonEmpty(path));
return base_fs_->OpenAppendStream(real_path, metadata);
}
//////////////////////////////////////////////////////////////////////////
// SlowFileSystem implementation
SlowFileSystem::SlowFileSystem(std::shared_ptr<FileSystem> base_fs,
std::shared_ptr<io::LatencyGenerator> latencies)
: FileSystem(base_fs->io_context()), base_fs_(base_fs), latencies_(latencies) {}
SlowFileSystem::SlowFileSystem(std::shared_ptr<FileSystem> base_fs,
double average_latency)
: FileSystem(base_fs->io_context()),
base_fs_(base_fs),
latencies_(io::LatencyGenerator::Make(average_latency)) {}
SlowFileSystem::SlowFileSystem(std::shared_ptr<FileSystem> base_fs,
double average_latency, int32_t seed)
: FileSystem(base_fs->io_context()),
base_fs_(base_fs),
latencies_(io::LatencyGenerator::Make(average_latency, seed)) {}
bool SlowFileSystem::Equals(const FileSystem& other) const { return this == &other; }
Result<FileInfo> SlowFileSystem::GetFileInfo(const std::string& path) {
latencies_->Sleep();
return base_fs_->GetFileInfo(path);
}
Result<std::vector<FileInfo>> SlowFileSystem::GetFileInfo(const FileSelector& selector) {
latencies_->Sleep();
return base_fs_->GetFileInfo(selector);
}
Status SlowFileSystem::CreateDir(const std::string& path, bool recursive) {
latencies_->Sleep();
return base_fs_->CreateDir(path, recursive);
}
Status SlowFileSystem::DeleteDir(const std::string& path) {
latencies_->Sleep();
return base_fs_->DeleteDir(path);
}
Status SlowFileSystem::DeleteDirContents(const std::string& path, bool missing_dir_ok) {
latencies_->Sleep();
return base_fs_->DeleteDirContents(path, missing_dir_ok);
}
Status SlowFileSystem::DeleteRootDirContents() {
latencies_->Sleep();
return base_fs_->DeleteRootDirContents();
}
Status SlowFileSystem::DeleteFile(const std::string& path) {
latencies_->Sleep();
return base_fs_->DeleteFile(path);
}
Status SlowFileSystem::Move(const std::string& src, const std::string& dest) {
latencies_->Sleep();
return base_fs_->Move(src, dest);
}
Status SlowFileSystem::CopyFile(const std::string& src, const std::string& dest) {
latencies_->Sleep();
return base_fs_->CopyFile(src, dest);
}
Result<std::shared_ptr<io::InputStream>> SlowFileSystem::OpenInputStream(
const std::string& path) {
latencies_->Sleep();
ARROW_ASSIGN_OR_RAISE(auto stream, base_fs_->OpenInputStream(path));
return std::make_shared<io::SlowInputStream>(stream, latencies_);
}
Result<std::shared_ptr<io::InputStream>> SlowFileSystem::OpenInputStream(
const FileInfo& info) {
latencies_->Sleep();
ARROW_ASSIGN_OR_RAISE(auto stream, base_fs_->OpenInputStream(info));
return std::make_shared<io::SlowInputStream>(stream, latencies_);
}
Result<std::shared_ptr<io::RandomAccessFile>> SlowFileSystem::OpenInputFile(
const std::string& path) {
latencies_->Sleep();
ARROW_ASSIGN_OR_RAISE(auto file, base_fs_->OpenInputFile(path));
return std::make_shared<io::SlowRandomAccessFile>(file, latencies_);
}
Result<std::shared_ptr<io::RandomAccessFile>> SlowFileSystem::OpenInputFile(
const FileInfo& info) {
latencies_->Sleep();
ARROW_ASSIGN_OR_RAISE(auto file, base_fs_->OpenInputFile(info));
return std::make_shared<io::SlowRandomAccessFile>(file, latencies_);
}
Result<std::shared_ptr<io::OutputStream>> SlowFileSystem::OpenOutputStream(
const std::string& path, const std::shared_ptr<const KeyValueMetadata>& metadata) {
latencies_->Sleep();
// XXX Should we have a SlowOutputStream that waits on Flush() and Close()?
return base_fs_->OpenOutputStream(path, metadata);
}
Result<std::shared_ptr<io::OutputStream>> SlowFileSystem::OpenAppendStream(
const std::string& path, const std::shared_ptr<const KeyValueMetadata>& metadata) {
latencies_->Sleep();
return base_fs_->OpenAppendStream(path, metadata);
}
Status CopyFiles(const std::vector<FileLocator>& sources,
const std::vector<FileLocator>& destinations,
const io::IOContext& io_context, int64_t chunk_size, bool use_threads) {
if (sources.size() != destinations.size()) {
return Status::Invalid("Trying to copy ", sources.size(), " files into ",
destinations.size(), " paths.");
}
auto copy_one_file = [&](int i) {
if (sources[i].filesystem->Equals(destinations[i].filesystem)) {
return sources[i].filesystem->CopyFile(sources[i].path, destinations[i].path);
}
ARROW_ASSIGN_OR_RAISE(auto source,
sources[i].filesystem->OpenInputStream(sources[i].path));
ARROW_ASSIGN_OR_RAISE(const auto metadata, source->ReadMetadata());
ARROW_ASSIGN_OR_RAISE(auto destination, destinations[i].filesystem->OpenOutputStream(
destinations[i].path, metadata));
RETURN_NOT_OK(internal::CopyStream(source, destination, chunk_size, io_context));
return destination->Close();
};
return ::arrow::internal::OptionalParallelFor(
use_threads, static_cast<int>(sources.size()), std::move(copy_one_file),
io_context.executor());
}
Status CopyFiles(const std::shared_ptr<FileSystem>& source_fs,
const FileSelector& source_sel,
const std::shared_ptr<FileSystem>& destination_fs,
const std::string& destination_base_dir, const io::IOContext& io_context,
int64_t chunk_size, bool use_threads) {
ARROW_ASSIGN_OR_RAISE(auto source_infos, source_fs->GetFileInfo(source_sel));
if (source_infos.empty()) {
return Status::OK();
}
std::vector<FileLocator> sources, destinations;
std::vector<std::string> dirs;
for (const FileInfo& source_info : source_infos) {
auto relative = internal::RemoveAncestor(source_sel.base_dir, source_info.path());
if (!relative.has_value()) {
return Status::Invalid("GetFileInfo() yielded path '", source_info.path(),
"', which is outside base dir '", source_sel.base_dir, "'");
}
auto destination_path =
internal::ConcatAbstractPath(destination_base_dir, std::string(*relative));
if (source_info.IsDirectory()) {
dirs.push_back(destination_path);
} else if (source_info.IsFile()) {
sources.push_back({source_fs, source_info.path()});
destinations.push_back({destination_fs, destination_path});
}
}
auto create_one_dir = [&](int i) { return destination_fs->CreateDir(dirs[i]); };
dirs = internal::MinimalCreateDirSet(std::move(dirs));
RETURN_NOT_OK(::arrow::internal::OptionalParallelFor(
use_threads, static_cast<int>(dirs.size()), std::move(create_one_dir),
io_context.executor()));
return CopyFiles(sources, destinations, io_context, chunk_size, use_threads);
}
namespace {
Result<Uri> ParseFileSystemUri(const std::string& uri_string) {
Uri uri;
auto status = uri.Parse(uri_string);
if (!status.ok()) {
#ifdef _WIN32
// Could be a "file:..." URI with backslashes instead of regular slashes.
RETURN_NOT_OK(uri.Parse(ToSlashes(uri_string)));
if (uri.scheme() != "file") {
return status;
}
#else
return status;
#endif
}
return std::move(uri);
}
Result<std::shared_ptr<FileSystem>> FileSystemFromUriReal(const Uri& uri,
const std::string& uri_string,
const io::IOContext& io_context,
std::string* out_path) {
const auto scheme = uri.scheme();
if (scheme == "file") {
std::string path;
ARROW_ASSIGN_OR_RAISE(auto options, LocalFileSystemOptions::FromUri(uri, &path));
if (out_path != nullptr) {
*out_path = path;
}
return std::make_shared<LocalFileSystem>(options, io_context);
}
if (scheme == "gs" || scheme == "gcs") {
#ifdef ARROW_GCS
ARROW_ASSIGN_OR_RAISE(auto options, GcsOptions::FromUri(uri, out_path));
return GcsFileSystem::Make(options, io_context);
#else
return Status::NotImplemented("Got GCS URI but Arrow compiled without GCS support");
#endif
}
if (scheme == "hdfs" || scheme == "viewfs") {
#ifdef ARROW_HDFS
ARROW_ASSIGN_OR_RAISE(auto options, HdfsOptions::FromUri(uri));
if (out_path != nullptr) {
*out_path = uri.path();
}
ARROW_ASSIGN_OR_RAISE(auto hdfs, HadoopFileSystem::Make(options, io_context));
return hdfs;
#else
return Status::NotImplemented("Got HDFS URI but Arrow compiled without HDFS support");
#endif
}
if (scheme == "s3") {
#ifdef ARROW_S3
RETURN_NOT_OK(EnsureS3Initialized());
ARROW_ASSIGN_OR_RAISE(auto options, S3Options::FromUri(uri, out_path));
ARROW_ASSIGN_OR_RAISE(auto s3fs, S3FileSystem::Make(options, io_context));
return s3fs;
#else
return Status::NotImplemented("Got S3 URI but Arrow compiled without S3 support");
#endif
}
if (scheme == "mock") {
// MockFileSystem does not have an absolute / relative path distinction,
// normalize path by removing leading slash.
if (out_path != nullptr) {
*out_path = std::string(RemoveLeadingSlash(uri.path()));
}
return std::make_shared<internal::MockFileSystem>(internal::CurrentTimePoint(),
io_context);
}
return Status::Invalid("Unrecognized filesystem type in URI: ", uri_string);
}
} // namespace
Result<std::shared_ptr<FileSystem>> FileSystemFromUri(const std::string& uri_string,
std::string* out_path) {
return FileSystemFromUri(uri_string, io::default_io_context(), out_path);
}
Result<std::shared_ptr<FileSystem>> FileSystemFromUri(const std::string& uri_string,
const io::IOContext& io_context,
std::string* out_path) {
ARROW_ASSIGN_OR_RAISE(auto fsuri, ParseFileSystemUri(uri_string));
return FileSystemFromUriReal(fsuri, uri_string, io_context, out_path);
}
Result<std::shared_ptr<FileSystem>> FileSystemFromUriOrPath(const std::string& uri_string,
std::string* out_path) {
return FileSystemFromUriOrPath(uri_string, io::default_io_context(), out_path);
}
Result<std::shared_ptr<FileSystem>> FileSystemFromUriOrPath(
const std::string& uri_string, const io::IOContext& io_context,
std::string* out_path) {
if (internal::DetectAbsolutePath(uri_string)) {
// Normalize path separators
if (out_path != nullptr) {
*out_path = ToSlashes(uri_string);
}
return std::make_shared<LocalFileSystem>();
}
return FileSystemFromUri(uri_string, io_context, out_path);
}
Status FileSystemFromUri(const std::string& uri, std::shared_ptr<FileSystem>* out_fs,
std::string* out_path) {
return FileSystemFromUri(uri, out_path).Value(out_fs);
}
Status Initialize(const FileSystemGlobalOptions& options) {
internal::global_options = options;
return Status::OK();
}
} // namespace fs
} // namespace arrow