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 <string>
24 #include <vector>
25 
26 #include "graphar/result.h"
27 #include "graphar/status.h"
28 #include "graphar/types.h"
29 #include "graphar/util.h"
30 
31 #include "graphar/reader_util.h"
32 #include "graphar/writer_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 
83  Result<std::shared_ptr<arrow::Table>> ReadFileToTable(
84  const std::string& path, FileType file_type,
85  const std::vector<int>& column_indices) const noexcept;
86 
95  template <typename T>
96  Result<T> ReadFileToValue(const std::string& path) const noexcept;
97 
106  template <typename T>
107  Status WriteValueToFile(const T& value, const std::string& path) const
108  noexcept;
109 
118  Status WriteTableToFile(const std::shared_ptr<arrow::Table>& table,
119  FileType file_type, const std::string& path,
120  const std::shared_ptr<WriterOptions>& options) const
121  noexcept;
122 
129  Status WriteLabelTableToFile(const std::shared_ptr<arrow::Table>& table,
130  const std::string& path) const noexcept;
131 
138  Status CopyFile(const std::string& src_path,
139  const std::string& dst_path) const noexcept;
140 
146  Result<IdType> GetFileNumOfDir(const std::string& dir_path,
147  bool recursive = false) const noexcept;
148 
149  private:
150  std::shared_ptr<arrow::dataset::FileFormat> GetFileFormat(
151  const FileType file_type) const;
152 
153  private:
154  std::shared_ptr<arrow::fs::FileSystem> arrow_fs_;
155 };
156 
168 Result<std::shared_ptr<FileSystem>> FileSystemFromUriOrPath(
169  const std::string& uri, std::string* out_path = nullptr);
170 
181 Status InitializeS3();
182 
192 Status FinalizeS3();
193 
194 } // 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:322
Status WriteValueToFile(const T &value, const std::string &path) const noexcept
Write a value of type T to a file.
Definition: filesystem.cc:229
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:207
Status CopyFile(const std::string &src_path, const std::string &dst_path) const noexcept
Definition: filesystem.cc:313
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:142
Status WriteLabelTableToFile(const std::shared_ptr< arrow::Table > &table, const std::string &path) const noexcept
Write a label table to a file with parquet type.
Definition: filesystem.cc:295
Status WriteTableToFile(const std::shared_ptr< arrow::Table > &table, FileType file_type, const std::string &path, const std::shared_ptr< WriterOptions > &options) const noexcept
Write a table to a file with a specific type.
Definition: filesystem.cc:252