hoslo commented on code in PR #3564: URL: https://github.com/apache/incubator-opendal/pull/3564#discussion_r1390141400
########## core/src/services/alluxio/core.rs: ########## @@ -0,0 +1,407 @@ +// 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. + +use std::fmt::Debug; +use std::fmt::Formatter; + +use http::Request; + +use http::header::RANGE; +use http::Response; + +use serde::{Deserialize, Serialize}; + +use crate::raw::*; +use crate::*; + +#[derive(Debug, Serialize)] +#[serde(untagged)] +enum Op { + CreateFile { + /// Whether to create file recursively + #[serde(skip_serializing_if = "Option::is_none")] + recursive: Option<bool>, + }, + CreateDir { + /// Whether to create dir recursively + #[serde(skip_serializing_if = "Option::is_none")] + recursive: Option<bool>, + }, + OpenFile, + Read, + Write, + Close, + Delete, + Rename, + ListStatus, + GetStatus, +} + +/// Metadata of alluxio object +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub(super) struct FileInfo { + /// The path of the object + pub path: String, + /// The last modification time of the object + pub last_modification_time_ms: i64, + /// Whether the object is a folder + pub folder: bool, + /// The length of the object in bytes + pub length: u64, +} + +impl TryFrom<FileInfo> for Metadata { + type Error = Error; + + fn try_from(file_info: FileInfo) -> Result<Metadata> { + let mut metadata = if file_info.folder { + Metadata::new(EntryMode::DIR) + } else { + Metadata::new(EntryMode::FILE) + }; + metadata + .set_content_length(file_info.length) + .set_last_modified(parse_datetime_from_from_timestamp_millis( + file_info.last_modification_time_ms, + )?); + Ok(metadata) + } +} + +impl Op { + /// Get operation name + fn as_str(&self) -> &str { + match self { + Op::CreateFile { .. } => "create-file", + Op::CreateDir { .. } => "create-dir", + Op::OpenFile => "open-file", + Op::Read => "read", + Op::Write => "write", + Op::Close => "close", + Op::Delete => "delete", + Op::Rename => "rename", + Op::ListStatus => "list-status", + Op::GetStatus => "get-status", + } + } +} + +/// the status code of alluxio +#[derive(Debug, Deserialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +enum StatusCode { + AlreadyExists, + NotFound, + Internal, +} + +/// the error response of alluxio +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +struct ErrRsponse { + status_code: StatusCode, + message: String, +} + +impl From<ErrRsponse> for Error { + fn from(value: ErrRsponse) -> Self { + match value.status_code { + StatusCode::AlreadyExists => Error::new(ErrorKind::AlreadyExists, &value.message), + StatusCode::NotFound => Error::new(ErrorKind::NotFound, &value.message), + StatusCode::Internal => Error::new(ErrorKind::Unexpected, &value.message), + } + } +} + +/// Alluxio core +#[derive(Clone)] +pub struct AlluxioCore { + /// prefix of alluxio api, e.g. "api/v1" + pub api_prefix: String, + /// prefix of alluxio paths, e.g. "paths" + pub paths_prefix: String, + /// prefix of alluxio streams, e.g. "streams" + pub streams_prefix: String, + + /// root of this backend. + pub root: String, + /// endpoint of alluxio + pub endpoint: String, + /// prefix of alluxio + pub client: HttpClient, +} + +impl Debug for AlluxioCore { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Backend") + .field("root", &self.root) + .field("endpoint", &self.endpoint) + .finish_non_exhaustive() + } +} + +impl AlluxioCore { + /// join a alluxio path + fn join_path(&self, path: &str, op: &str) -> String { + let path = build_abs_path(&self.root, path); + format!( + "{}/{}/{}//{}/{}", Review Comment: the alluxio file or dir name is begin with / -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
