From 7c47bd6f42ca5b89dcfd5245079b605d780e3a57 Mon Sep 17 00:00:00 2001 From: feathercyc Date: Sat, 28 Sep 2024 16:33:52 +0000 Subject: [PATCH] feat: add NebulaGraph config Signed-off-by: feathercyc --- core/Cargo.toml | 1 + core/src/services/mod.rs | 3 + core/src/services/nebula_graph/backend.rs | 122 ++++++++++++++++++++++ core/src/services/nebula_graph/config.rs | 64 ++++++++++++ core/src/services/nebula_graph/docs.md | 53 ++++++++++ core/src/services/nebula_graph/mod.rs | 24 +++++ core/src/types/operator/builder.rs | 2 + core/src/types/scheme.rs | 6 ++ 8 files changed, 275 insertions(+) create mode 100644 core/src/services/nebula_graph/backend.rs create mode 100644 core/src/services/nebula_graph/config.rs create mode 100644 core/src/services/nebula_graph/docs.md create mode 100644 core/src/services/nebula_graph/mod.rs diff --git a/core/Cargo.toml b/core/Cargo.toml index c6326bf9acc..6b4e5d592d9 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -204,6 +204,7 @@ services-vercel-blob = [] services-webdav = [] services-webhdfs = [] services-yandex-disk = [] +services-nebula-graph = [] [lib] bench = false diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index 753838b3131..0437dff4a75 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -145,6 +145,9 @@ pub use monoiofs::*; mod mysql; pub use self::mysql::*; +mod nebula_graph; +pub use nebula_graph::*; + mod obs; pub use obs::*; diff --git a/core/src/services/nebula_graph/backend.rs b/core/src/services/nebula_graph/backend.rs new file mode 100644 index 00000000000..99900aa9362 --- /dev/null +++ b/core/src/services/nebula_graph/backend.rs @@ -0,0 +1,122 @@ +// 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 crate::raw::adapters::kv; +use crate::raw::*; +use crate::services::NebulaGraphConfig; +use crate::*; + +impl Configurator for NebulaGraphConfig { + type Builder = NebulaGraphBuilder; + fn into_builder(self) -> Self::Builder { + NebulaGraphBuilder { config: self } + } +} + +#[doc = include_str!("docs.md")] +#[derive(Default)] +pub struct NebulaGraphBuilder { + config: NebulaGraphConfig, +} + +impl Debug for NebulaGraphBuilder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("MysqlBuilder"); + + d.field("config", &self.config).finish() + } +} + +impl NebulaGraphBuilder { + /// Set the host addr of nebulagraph's graphd server + pub fn host(&mut self, host: &str) -> &mut Self { + if !host.is_empty() { + self.config.host = Some(host.to_string()); + } + self + } + + /// Set the host port of nebulagraph's graphd server + pub fn port(&mut self, port: u16) -> &mut Self { + self.config.port = Some(port); + self + } + + /// Set the username of nebulagraph's graphd server + pub fn username(&mut self, username: &str) -> &mut Self { + if !username.is_empty() { + self.config.username = Some(username.to_string()); + } + self + } + + /// Set the password of nebulagraph's graphd server + pub fn password(&mut self, password: &str) -> &mut Self { + if !password.is_empty() { + self.config.password = Some(password.to_string()); + } + self + } + + /// Set the space name of nebulagraph's graphd server + pub fn space(&mut self, space: &str) -> &mut Self { + if !space.is_empty() { + self.config.space = Some(space.to_string()); + } + self + } + + /// Set the tag name of nebulagraph's graphd server + pub fn tag(&mut self, tag: &str) -> &mut Self { + if !tag.is_empty() { + self.config.tag = Some(tag.to_string()); + } + self + } + + /// Set the key field name of the NebulaGraph service to read/write. + /// + /// Default to `key` if not specified. + pub fn key_field(&mut self, key_field: &str) -> &mut Self { + if !key_field.is_empty() { + self.config.key_field = Some(key_field.to_string()); + } + self + } + + /// Set the value field name of the NebulaGraph service to read/write. + /// + /// Default to `value` if not specified. + pub fn value_field(&mut self, value_field: &str) -> &mut Self { + if !value_field.is_empty() { + self.config.value_field = Some(value_field.to_string()); + } + self + } + + /// set the working directory, all operations will be performed under it. + /// + /// default: "/" + pub fn root(&mut self, root: &str) -> &mut Self { + if !root.is_empty() { + self.config.root = Some(root.to_string()); + } + self + } +} diff --git a/core/src/services/nebula_graph/config.rs b/core/src/services/nebula_graph/config.rs new file mode 100644 index 00000000000..6c3da6c7c38 --- /dev/null +++ b/core/src/services/nebula_graph/config.rs @@ -0,0 +1,64 @@ +// 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 serde::Deserialize; +use serde::Serialize; + +/// Config for Mysql services support. +#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)] +#[serde(default)] +#[non_exhaustive] +pub struct NebulaGraphConfig { + /// The host addr of nebulagraph's graphd server + pub host: Option, + /// The host port of nebulagraph's graphd server + pub port: Option, + /// The username of nebulagraph's graphd server + pub username: Option, + /// The password of nebulagraph's graphd server + pub password: Option, + + /// The space name of nebulagraph's graphd server + pub space: Option, + /// The tag name of nebulagraph's graphd server + pub tag: Option, + /// The key field name of the NebulaGraph service to read/write. + pub key_field: Option, + /// The value field name of the NebulaGraph service to read/write. + pub value_field: Option, + /// The root for NebulaGraph + pub root: Option, +} + +impl Debug for NebulaGraphConfig { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("NebulaGraphConfig"); + + d.field("host", &self.host) + .field("port", &self.port) + .field("username", &self.username) + .field("password", &self.password) + .field("space", &self.space) + .field("tag", &self.tag) + .field("key_field", &self.key_field) + .field("value_field", &self.value_field) + .field("root", &self.root) + .finish() + } +} diff --git a/core/src/services/nebula_graph/docs.md b/core/src/services/nebula_graph/docs.md new file mode 100644 index 00000000000..bd219e71769 --- /dev/null +++ b/core/src/services/nebula_graph/docs.md @@ -0,0 +1,53 @@ +## Capabilities + +This service can be used to: + +- [x] stat +- [x] read +- [x] write +- [x] create_dir +- [x] delete +- [ ] copy +- [ ] rename +- [x] list +- [ ] ~~presign~~ +- [ ] blocking + +## Configuration + +- `root`: Set the working directory of `OpenDAL` +- `host`: Set the host address of NebulaGraph's graphd server +- `port`: Set the port of NebulaGraph's graphd server +- `username`: Set the username of NebulaGraph's graphd server +- `password`: Set the password of NebulaGraph's graphd server +- `space`: Set the passspaceword of NebulaGraph +- `tag`: Set the tag of NebulaGraph +- `key_field`: Set the key_field of NebulaGraph +- `value_field`: Set the value_field of NebulaGraph + +## Example + +### Via Builder + +```rust,no_run +use anyhow::Result; +use opendal::services::NebulaGraph; +use opendal::Operator; + +#[tokio::main] +async fn main() -> Result<()> { + let mut builder = NebulaGraph::default(); + builder.root("/"); + builder.host("127.0.0.1"); + builder.port(9669); + builder.space("your_space"); + builder.tag("your_tag"); + // key field type in the table should be compatible with Rust's &str like text + builder.key_field("key"); + // value field type in the table should be compatible with Rust's Vec like bytea + builder.value_field("value"); + + let op = Operator::new(builder)?.finish(); + Ok(()) +} +``` diff --git a/core/src/services/nebula_graph/mod.rs b/core/src/services/nebula_graph/mod.rs new file mode 100644 index 00000000000..af5429b8c2b --- /dev/null +++ b/core/src/services/nebula_graph/mod.rs @@ -0,0 +1,24 @@ +// 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. + +#[cfg(feature = "services-nebula-graph")] +mod backend; +#[cfg(feature = "services-nebula-graph")] +pub use backend::NebulaGraphBuilder as NebulaGraph; + +mod config; +pub use config::NebulaGraphConfig; diff --git a/core/src/types/operator/builder.rs b/core/src/types/operator/builder.rs index 4691eda6b24..95dfa1c17a1 100644 --- a/core/src/types/operator/builder.rs +++ b/core/src/types/operator/builder.rs @@ -292,6 +292,8 @@ impl Operator { Scheme::HdfsNative => Self::from_iter::(iter)?.finish(), #[cfg(feature = "services-lakefs")] Scheme::Lakefs => Self::from_iter::(iter)?.finish(), + #[cfg(feature = "services-nebula-graph")] + Scheme::NebulaGraph => Self::from_iter::(iter)?.finish(), v => { return Err(Error::new( ErrorKind::Unsupported, diff --git a/core/src/types/scheme.rs b/core/src/types/scheme.rs index 4dd8acc69fb..eb5a61c8975 100644 --- a/core/src/types/scheme.rs +++ b/core/src/types/scheme.rs @@ -107,6 +107,8 @@ pub enum Scheme { Moka, /// [monoiofs][crate::services::Monoiofs]: monoio fs services. Monoiofs, + /// [NebulaGraph](crate::services::NebulaGraph): NebulaGraph Services + NebulaGraph, /// [obs][crate::services::Obs]: Huawei Cloud OBS services. Obs, /// [onedrive][crate::services::Onedrive]: Microsoft OneDrive services. @@ -315,6 +317,8 @@ impl Scheme { Scheme::Surrealdb, #[cfg(feature = "services-lakefs")] Scheme::Lakefs, + #[cfg(feature = "services-nebula-graph")] + Scheme::NebulaGraph, ]) } } @@ -406,6 +410,7 @@ impl FromStr for Scheme { "hdfs_native" => Ok(Scheme::HdfsNative), "surrealdb" => Ok(Scheme::Surrealdb), "lakefs" => Ok(Scheme::Lakefs), + "nebula_graph" => Ok(Scheme::NebulaGraph), _ => Ok(Scheme::Custom(Box::leak(s.into_boxed_str()))), } } @@ -480,6 +485,7 @@ impl From for &'static str { Scheme::HdfsNative => "hdfs_native", Scheme::Surrealdb => "surrealdb", Scheme::Lakefs => "lakefs", + Scheme::NebulaGraph => "nebula_graph", Scheme::Custom(v) => v, } }