Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
filesystem.h
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <vector>
26 
27 #include "graphar/result.h"
28 #include "graphar/status.h"
29 #include "graphar/types.h"
30 #include "graphar/util.h"
31 
32 #include "graphar/reader_util.h"
33 
34 // forward declarations
35 namespace arrow {
36 class Buffer;
37 class Table;
38 } // namespace arrow
39 
40 namespace arrow::fs {
41 class FileSystem;
42 } // namespace arrow::fs
43 
44 namespace arrow::io {
45 class RandomAccessFile;
46 } // namespace arrow::io
47 
48 namespace arrow::dataset {
49 class FileFormat;
50 } // namespace arrow::dataset
51 
52 namespace graphar {
53 
59 class FileSystem {
60  public:
65  explicit FileSystem(std::shared_ptr<arrow::fs::FileSystem> arrow_fs)
66  : arrow_fs_(arrow_fs) {}
67 
68  ~FileSystem();
69 
79  Result<std::shared_ptr<arrow::Table>> ReadFileToTable(
80  const std::string& path, FileType file_type,
81  const util::FilterOptions& options = {}) const noexcept;
82 
91  template <typename T>
92  Result<T> ReadFileToValue(const std::string& path) const noexcept;
93 
102  template <typename T>
103  Status WriteValueToFile(const T& value, const std::string& path) const
104  noexcept;
105 
113  Status WriteTableToFile(const std::shared_ptr<arrow::Table>& table,
114  FileType file_type, const std::string& path) const
115  noexcept;
116 
123  Status CopyFile(const std::string& src_path,
124  const std::string& dst_path) const noexcept;
125 
131  Result<IdType> GetFileNumOfDir(const std::string& dir_path,
132  bool recursive = false) const noexcept;
133 
134  private:
135  std::shared_ptr<arrow::dataset::FileFormat> GetFileFormat(
136  const FileType file_type) const;
137 
138  private:
139  std::shared_ptr<arrow::fs::FileSystem> arrow_fs_;
140 };
141 
153 Result<std::shared_ptr<FileSystem>> FileSystemFromUriOrPath(
154  const std::string& uri, std::string* out_path = nullptr);
155 
166 Status InitializeS3();
167 
177 Status FinalizeS3();
178 
179 } // namespace graphar
FileSystem(std::shared_ptr< arrow::fs::FileSystem > arrow_fs)
Create a FileSystem instance.
Definition: filesystem.h:65
Result< IdType > GetFileNumOfDir(const std::string &dir_path, bool recursive=false) const noexcept
Definition: filesystem.cc:268
Status WriteValueToFile(const T &value, const std::string &path) const noexcept
Write a value of type T to a file.
Definition: filesystem.cc:190
Status WriteTableToFile(const std::shared_ptr< arrow::Table > &table, FileType file_type, const std::string &path) const noexcept
Write a table to a file with a specific type.
Definition: filesystem.cc:213
Result< T > ReadFileToValue(const std::string &path) const noexcept
Read a file and convert its bytes to a value of type T.
Definition: filesystem.cc:168
Status CopyFile(const std::string &src_path, const std::string &dst_path) const noexcept
Definition: filesystem.cc:259
Result< std::shared_ptr< arrow::Table > > ReadFileToTable(const std::string &path, FileType file_type, const util::FilterOptions &options={}) const noexcept
Read and filter a file as an arrow::Table.
Definition: filesystem.cc:105