|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Object store that represents the Local File System. |
| 19 | +
|
| 20 | +use crate::datasource::object_store::{ObjectReader, ObjectStore}; |
| 21 | +use crate::error::DataFusionError; |
| 22 | +use crate::error::Result; |
| 23 | +use crate::parquet::file::reader::{ChunkReader, Length}; |
| 24 | +use std::any::Any; |
| 25 | +use std::fs; |
| 26 | +use std::fs::{metadata, File}; |
| 27 | +use std::io::Read; |
| 28 | +use std::sync::Arc; |
| 29 | + |
| 30 | +#[derive(Debug)] |
| 31 | +/// Local File System as Object Store. |
| 32 | +pub struct LocalFileSystem; |
| 33 | + |
| 34 | +impl ObjectStore for LocalFileSystem { |
| 35 | + fn as_any(&self) -> &dyn Any { |
| 36 | + self |
| 37 | + } |
| 38 | + |
| 39 | + fn list_all_files(&self, path: &str, ext: &str) -> Result<Vec<String>> { |
| 40 | + list_all(path, ext) |
| 41 | + } |
| 42 | + |
| 43 | + fn get_reader(&self, file_path: &str) -> Result<Arc<dyn ObjectReader>> { |
| 44 | + let file = File::open(file_path)?; |
| 45 | + let reader = LocalFSObjectReader::new(file)?; |
| 46 | + Ok(Arc::new(reader)) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +struct LocalFSObjectReader { |
| 51 | + file: File, |
| 52 | +} |
| 53 | + |
| 54 | +impl LocalFSObjectReader { |
| 55 | + fn new(file: File) -> Result<Self> { |
| 56 | + Ok(Self { file }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl ObjectReader for LocalFSObjectReader { |
| 61 | + fn get_reader(&self, start: u64, length: usize) -> Box<dyn Read> { |
| 62 | + Box::new(FileSegmentReader::new( |
| 63 | + self.file.try_clone().unwrap(), |
| 64 | + start, |
| 65 | + length, |
| 66 | + )) |
| 67 | + } |
| 68 | + |
| 69 | + fn length(&self) -> u64 { |
| 70 | + self.file.len() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +struct FileSegmentReader { |
| 75 | + file: File, |
| 76 | + start: u64, |
| 77 | + length: usize, |
| 78 | +} |
| 79 | + |
| 80 | +impl FileSegmentReader { |
| 81 | + fn new(file: File, start: u64, length: usize) -> Self { |
| 82 | + Self { |
| 83 | + file, |
| 84 | + start, |
| 85 | + length, |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl Read for FileSegmentReader { |
| 91 | + fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> { |
| 92 | + let mut file_source = self.file.get_read(self.start, self.length)?; |
| 93 | + file_source.read(buf) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn list_all(root_path: &str, ext: &str) -> Result<Vec<String>> { |
| 98 | + let mut filenames: Vec<String> = Vec::new(); |
| 99 | + list_all_files(root_path, &mut filenames, ext)?; |
| 100 | + Ok(filenames) |
| 101 | +} |
| 102 | + |
| 103 | +/// Recursively build a list of files in a directory with a given extension with an accumulator list |
| 104 | +fn list_all_files(dir: &str, filenames: &mut Vec<String>, ext: &str) -> Result<()> { |
| 105 | + let metadata = metadata(dir)?; |
| 106 | + if metadata.is_file() { |
| 107 | + if dir.ends_with(ext) { |
| 108 | + filenames.push(dir.to_string()); |
| 109 | + } |
| 110 | + } else { |
| 111 | + for entry in fs::read_dir(dir)? { |
| 112 | + let entry = entry?; |
| 113 | + let path = entry.path(); |
| 114 | + if let Some(path_name) = path.to_str() { |
| 115 | + if path.is_dir() { |
| 116 | + list_all_files(path_name, filenames, ext)?; |
| 117 | + } else if path_name.ends_with(ext) { |
| 118 | + filenames.push(path_name.to_string()); |
| 119 | + } |
| 120 | + } else { |
| 121 | + return Err(DataFusionError::Plan("Invalid path".to_string())); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + Ok(()) |
| 126 | +} |
0 commit comments