forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.cpp
493 lines (407 loc) · 16.9 KB
/
filesystem.cpp
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
// 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 "./arrow_types.h"
#include "./safe-call-into-r.h"
#include <arrow/filesystem/filesystem.h>
#include <arrow/filesystem/localfs.h>
#include <arrow/util/key_value_metadata.h>
namespace fs = ::arrow::fs;
namespace io = ::arrow::io;
namespace cpp11 {
const char* r6_class_name<fs::FileSystem>::get(
const std::shared_ptr<fs::FileSystem>& file_system) {
auto type_name = file_system->type_name();
if (type_name == "local") {
return "LocalFileSystem";
} else if (type_name == "s3") {
return "S3FileSystem";
} else if (type_name == "gcs") {
return "GcsFileSystem";
// Uncomment these once R6 classes for these filesystems are added
// } else if (type_name == "abfs") {
// return "AzureBlobFileSystem";
// } else if (type_name == "hdfs") {
// return "HadoopFileSystem";
} else if (type_name == "subtree") {
return "SubTreeFileSystem";
} else {
return "FileSystem";
}
}
} // namespace cpp11
// [[arrow::export]]
fs::FileType fs___FileInfo__type(const std::shared_ptr<fs::FileInfo>& x) {
return x->type();
}
// [[arrow::export]]
void fs___FileInfo__set_type(const std::shared_ptr<fs::FileInfo>& x, fs::FileType type) {
x->set_type(type);
}
// [[arrow::export]]
std::string fs___FileInfo__path(const std::shared_ptr<fs::FileInfo>& x) {
return x->path();
}
// [[arrow::export]]
void fs___FileInfo__set_path(const std::shared_ptr<fs::FileInfo>& x,
const std::string& path) {
x->set_path(path);
}
// [[arrow::export]]
r_vec_size fs___FileInfo__size(const std::shared_ptr<fs::FileInfo>& x) {
return r_vec_size(x->size());
}
// [[arrow::export]]
void fs___FileInfo__set_size(const std::shared_ptr<fs::FileInfo>& x, int64_t size) {
x->set_size(size);
}
// [[arrow::export]]
std::string fs___FileInfo__base_name(const std::shared_ptr<fs::FileInfo>& x) {
return x->base_name();
}
// [[arrow::export]]
std::string fs___FileInfo__extension(const std::shared_ptr<fs::FileInfo>& x) {
return x->extension();
}
// [[arrow::export]]
SEXP fs___FileInfo__mtime(const std::shared_ptr<fs::FileInfo>& x) {
SEXP res = PROTECT(Rf_allocVector(REALSXP, 1));
// .mtime() gets us nanoseconds since epoch, POSIXct is seconds since epoch as a double
REAL(res)[0] = static_cast<double>(x->mtime().time_since_epoch().count()) / 1000000000;
Rf_classgets(res, arrow::r::data::classes_POSIXct);
UNPROTECT(1);
return res;
}
// [[arrow::export]]
void fs___FileInfo__set_mtime(const std::shared_ptr<fs::FileInfo>& x, SEXP time) {
auto nanosecs =
std::chrono::nanoseconds(static_cast<int64_t>(REAL(time)[0] * 1000000000));
x->set_mtime(fs::TimePoint(nanosecs));
}
// Selector
// [[arrow::export]]
std::string fs___FileSelector__base_dir(
const std::shared_ptr<fs::FileSelector>& selector) {
return selector->base_dir;
}
// [[arrow::export]]
bool fs___FileSelector__allow_not_found(
const std::shared_ptr<fs::FileSelector>& selector) {
return selector->allow_not_found;
}
// [[arrow::export]]
bool fs___FileSelector__recursive(const std::shared_ptr<fs::FileSelector>& selector) {
return selector->recursive;
}
// [[arrow::export]]
std::shared_ptr<fs::FileSelector> fs___FileSelector__create(const std::string& base_dir,
bool allow_not_found,
bool recursive) {
auto selector = std::make_shared<fs::FileSelector>();
selector->base_dir = base_dir;
selector->allow_not_found = allow_not_found;
selector->recursive = recursive;
return selector;
}
// FileSystem
template <typename T>
std::vector<std::shared_ptr<T>> shared_ptr_vector(const std::vector<T>& vec) {
std::vector<std::shared_ptr<fs::FileInfo>> res(vec.size());
std::transform(vec.begin(), vec.end(), res.begin(),
[](const fs::FileInfo& x) { return std::make_shared<fs::FileInfo>(x); });
return res;
}
// [[arrow::export]]
cpp11::list fs___FileSystem__GetTargetInfos_Paths(
const std::shared_ptr<fs::FileSystem>& file_system,
const std::vector<std::string>& paths) {
auto results = ValueOrStop(file_system->GetFileInfo(paths));
return arrow::r::to_r_list(shared_ptr_vector(results));
}
// [[arrow::export]]
cpp11::list fs___FileSystem__GetTargetInfos_FileSelector(
const std::shared_ptr<fs::FileSystem>& file_system,
const std::shared_ptr<fs::FileSelector>& selector) {
auto results = ValueOrStop(file_system->GetFileInfo(*selector));
return arrow::r::to_r_list(shared_ptr_vector(results));
}
// [[arrow::export]]
void fs___FileSystem__CreateDir(const std::shared_ptr<fs::FileSystem>& file_system,
const std::string& path, bool recursive) {
StopIfNotOk(file_system->CreateDir(path, recursive));
}
// [[arrow::export]]
void fs___FileSystem__DeleteDir(const std::shared_ptr<fs::FileSystem>& file_system,
const std::string& path) {
StopIfNotOk(file_system->DeleteDir(path));
}
// [[arrow::export]]
void fs___FileSystem__DeleteDirContents(
const std::shared_ptr<fs::FileSystem>& file_system, const std::string& path) {
StopIfNotOk(file_system->DeleteDirContents(path));
}
// [[arrow::export]]
void fs___FileSystem__DeleteFile(const std::shared_ptr<fs::FileSystem>& file_system,
const std::string& path) {
StopIfNotOk(file_system->DeleteFile(path));
}
// [[arrow::export]]
void fs___FileSystem__DeleteFiles(const std::shared_ptr<fs::FileSystem>& file_system,
const std::vector<std::string>& paths) {
StopIfNotOk(file_system->DeleteFiles(paths));
}
// [[arrow::export]]
void fs___FileSystem__Move(const std::shared_ptr<fs::FileSystem>& file_system,
const std::string& src, const std::string& dest) {
StopIfNotOk(file_system->Move(src, dest));
}
// [[arrow::export]]
void fs___FileSystem__CopyFile(const std::shared_ptr<fs::FileSystem>& file_system,
const std::string& src, const std::string& dest) {
StopIfNotOk(file_system->CopyFile(src, dest));
}
// [[arrow::export]]
std::shared_ptr<arrow::io::InputStream> fs___FileSystem__OpenInputStream(
const std::shared_ptr<fs::FileSystem>& file_system, const std::string& path) {
return ValueOrStop(file_system->OpenInputStream(path));
}
// [[arrow::export]]
std::shared_ptr<arrow::io::RandomAccessFile> fs___FileSystem__OpenInputFile(
const std::shared_ptr<fs::FileSystem>& file_system, const std::string& path) {
return ValueOrStop(file_system->OpenInputFile(path));
}
// [[arrow::export]]
std::shared_ptr<arrow::io::OutputStream> fs___FileSystem__OpenOutputStream(
const std::shared_ptr<fs::FileSystem>& file_system, const std::string& path) {
return ValueOrStop(file_system->OpenOutputStream(path));
}
// [[arrow::export]]
std::shared_ptr<arrow::io::OutputStream> fs___FileSystem__OpenAppendStream(
const std::shared_ptr<fs::FileSystem>& file_system, const std::string& path) {
return ValueOrStop(file_system->OpenAppendStream(path));
}
// [[arrow::export]]
std::string fs___FileSystem__type_name(
const std::shared_ptr<fs::FileSystem>& file_system) {
return file_system->type_name();
}
// [[arrow::export]]
std::shared_ptr<fs::LocalFileSystem> fs___LocalFileSystem__create() {
// Affects OpenInputFile/OpenInputStream
auto io_context = MainRThread::GetInstance().CancellableIOContext();
return std::make_shared<fs::LocalFileSystem>(io_context);
}
// [[arrow::export]]
std::shared_ptr<fs::SubTreeFileSystem> fs___SubTreeFileSystem__create(
const std::string& base_path, const std::shared_ptr<fs::FileSystem>& base_fs) {
return std::make_shared<fs::SubTreeFileSystem>(base_path, base_fs);
}
// [[arrow::export]]
std::shared_ptr<fs::FileSystem> fs___SubTreeFileSystem__base_fs(
const std::shared_ptr<fs::SubTreeFileSystem>& file_system) {
return file_system->base_fs();
}
// [[arrow::export]]
std::string fs___SubTreeFileSystem__base_path(
const std::shared_ptr<fs::SubTreeFileSystem>& file_system) {
return file_system->base_path();
}
// [[arrow::export]]
cpp11::writable::list fs___FileSystemFromUri(const std::string& path) {
using cpp11::literals::operator"" _nm;
std::string out_path;
return cpp11::writable::list(
{"fs"_nm = cpp11::to_r6(ValueOrStop(fs::FileSystemFromUri(path, &out_path))),
"path"_nm = out_path});
}
// [[arrow::export]]
void fs___CopyFiles(const std::shared_ptr<fs::FileSystem>& source_fs,
const std::shared_ptr<fs::FileSelector>& source_sel,
const std::shared_ptr<fs::FileSystem>& destination_fs,
const std::string& destination_base_dir,
int64_t chunk_size = 1024 * 1024, bool use_threads = true) {
StopIfNotOk(fs::CopyFiles(source_fs, *source_sel, destination_fs, destination_base_dir,
io::default_io_context(), chunk_size, use_threads));
}
#if defined(ARROW_R_WITH_S3)
#include <arrow/filesystem/s3fs.h>
// [[s3::export]]
std::shared_ptr<fs::S3FileSystem> fs___S3FileSystem__create(
bool anonymous = false, std::string access_key = "", std::string secret_key = "",
std::string session_token = "", std::string role_arn = "",
std::string session_name = "", std::string external_id = "", int load_frequency = 900,
std::string region = "", std::string endpoint_override = "", std::string scheme = "",
std::string proxy_options = "", bool background_writes = true,
bool allow_bucket_creation = false, bool allow_bucket_deletion = false,
double connect_timeout = -1, double request_timeout = -1) {
// We need to ensure that S3 is initialized before we start messing with the
// options
StopIfNotOk(fs::EnsureS3Initialized());
fs::S3Options s3_opts;
// Handle auth (anonymous, keys, default)
// (validation/internal coherence handled in R)
if (anonymous) {
s3_opts = fs::S3Options::Anonymous();
} else if (access_key != "" && secret_key != "") {
s3_opts = fs::S3Options::FromAccessKey(access_key, secret_key, session_token);
} else if (role_arn != "") {
s3_opts = fs::S3Options::FromAssumeRole(role_arn, session_name, external_id,
load_frequency);
} else {
s3_opts = fs::S3Options::Defaults();
}
// Now handle the rest of the options
/// AWS region to connect to (default determined by AWS SDK)
if (region != "") {
s3_opts.region = region;
}
/// If non-empty, override region with a connect string such as "localhost:9000"
s3_opts.endpoint_override = endpoint_override;
/// S3 connection transport, default "https"
if (scheme != "") {
s3_opts.scheme = scheme;
}
if (proxy_options != "") {
auto s3_proxy_opts = fs::S3ProxyOptions::FromUri(proxy_options);
s3_opts.proxy_options = ValueOrStop(s3_proxy_opts);
}
/// Whether OutputStream writes will be issued in the background, without blocking
/// default true
s3_opts.background_writes = background_writes;
s3_opts.allow_bucket_creation = allow_bucket_creation;
s3_opts.allow_bucket_deletion = allow_bucket_deletion;
s3_opts.request_timeout = request_timeout;
s3_opts.connect_timeout = connect_timeout;
auto io_context = MainRThread::GetInstance().CancellableIOContext();
return ValueOrStop(fs::S3FileSystem::Make(s3_opts, io_context));
}
// [[s3::export]]
std::string fs___S3FileSystem__region(const std::shared_ptr<fs::S3FileSystem>& fs) {
return fs->region();
}
#endif
// [[arrow::export]]
void FinalizeS3() {
#if defined(ARROW_R_WITH_S3)
StopIfNotOk(fs::FinalizeS3());
#endif
}
#if defined(ARROW_R_WITH_GCS)
#include <arrow/filesystem/gcsfs.h>
std::shared_ptr<arrow::KeyValueMetadata> strings_to_kvm(cpp11::strings metadata);
// [[gcs::export]]
std::shared_ptr<fs::GcsFileSystem> fs___GcsFileSystem__Make(bool anonymous,
cpp11::list options) {
fs::GcsOptions gcs_opts;
// Handle auth (anonymous, credentials, default)
// (validation/internal coherence handled in R)
if (anonymous) {
gcs_opts = fs::GcsOptions::Anonymous();
} else if (!Rf_isNull(options["access_token"])) {
// Convert POSIXct timestamp seconds to nanoseconds
std::chrono::nanoseconds ns_count(
static_cast<int64_t>(cpp11::as_cpp<double>(options["expiration"])) * 1000000000);
auto expiration_timepoint =
fs::TimePoint(std::chrono::duration_cast<fs::TimePoint::duration>(ns_count));
gcs_opts = fs::GcsOptions::FromAccessToken(
cpp11::as_cpp<std::string>(options["access_token"]), expiration_timepoint);
// TODO(ARROW-16885): implement FromImpersonatedServiceAccount
// } else if (base_credentials != "") {
// // static GcsOptions FromImpersonatedServiceAccount(
// // const GcsCredentials& base_credentials, const std::string&
// target_service_account);
// // TODO: construct GcsCredentials
// gcs_opts = fs::GcsOptions::FromImpersonatedServiceAccount(base_credentials,
// target_service_account);
} else if (!Rf_isNull(options["json_credentials"])) {
gcs_opts = fs::GcsOptions::FromServiceAccountCredentials(
cpp11::as_cpp<std::string>(options["json_credentials"]));
} else {
gcs_opts = fs::GcsOptions::Defaults();
}
// Handle other attributes
if (!Rf_isNull(options["endpoint_override"])) {
gcs_opts.endpoint_override = cpp11::as_cpp<std::string>(options["endpoint_override"]);
}
if (!Rf_isNull(options["scheme"])) {
gcs_opts.scheme = cpp11::as_cpp<std::string>(options["scheme"]);
}
// /// \brief Location to use for creating buckets.
if (!Rf_isNull(options["default_bucket_location"])) {
gcs_opts.default_bucket_location =
cpp11::as_cpp<std::string>(options["default_bucket_location"]);
}
// /// \brief If set used to control total time allowed for retrying underlying
// /// errors.
// ///
// /// The default policy is to retry for up to 15 minutes.
if (!Rf_isNull(options["retry_limit_seconds"])) {
gcs_opts.retry_limit_seconds = cpp11::as_cpp<double>(options["retry_limit_seconds"]);
}
// /// \brief Default metadata for OpenOutputStream.
// ///
// /// This will be ignored if non-empty metadata is passed to OpenOutputStream.
if (!Rf_isNull(options["default_metadata"])) {
gcs_opts.default_metadata = strings_to_kvm(options["default_metadata"]);
}
auto io_context = MainRThread::GetInstance().CancellableIOContext();
// TODO(ARROW-16884): update when this returns Result
return fs::GcsFileSystem::Make(gcs_opts, io_context);
}
// [[gcs::export]]
cpp11::list fs___GcsFileSystem__options(const std::shared_ptr<fs::GcsFileSystem>& fs) {
using cpp11::literals::operator"" _nm;
cpp11::writable::list out;
fs::GcsOptions opts = fs->options();
// GcsCredentials
out.push_back({"anonymous"_nm = opts.credentials.anonymous()});
if (opts.credentials.access_token() != "") {
out.push_back({"access_token"_nm = opts.credentials.access_token()});
}
if (opts.credentials.expiration().time_since_epoch().count() != 0) {
out.push_back({"expiration"_nm = cpp11::as_sexp<double>(
opts.credentials.expiration().time_since_epoch().count())});
}
if (opts.credentials.target_service_account() != "") {
out.push_back(
{"target_service_account"_nm = opts.credentials.target_service_account()});
}
if (opts.credentials.json_credentials() != "") {
out.push_back({"json_credentials"_nm = opts.credentials.json_credentials()});
}
// GcsOptions direct members
if (opts.endpoint_override != "") {
out.push_back({"endpoint_override"_nm = opts.endpoint_override});
}
if (opts.scheme != "") {
out.push_back({"scheme"_nm = opts.scheme});
}
if (opts.default_bucket_location != "") {
out.push_back({"default_bucket_location"_nm = opts.default_bucket_location});
}
out.push_back({"retry_limit_seconds"_nm = opts.retry_limit_seconds.value()});
// default_metadata
if (opts.default_metadata != nullptr && opts.default_metadata->size() > 0) {
cpp11::writable::strings metadata(opts.default_metadata->size());
metadata.names() = opts.default_metadata->keys();
for (int64_t i = 0; i < opts.default_metadata->size(); i++) {
metadata[static_cast<size_t>(i)] = opts.default_metadata->value(i);
}
out.push_back({"default_metadata"_nm = metadata});
}
return out;
}
#endif