Skip to content

Commit

Permalink
feat: add NebulaGraph config
Browse files Browse the repository at this point in the history
Signed-off-by: feathercyc <[email protected]>
  • Loading branch information
feathercyc committed Sep 28, 2024
1 parent 860f5b2 commit 7c47bd6
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ services-vercel-blob = []
services-webdav = []
services-webhdfs = []
services-yandex-disk = []
services-nebula-graph = []

[lib]
bench = false
Expand Down
3 changes: 3 additions & 0 deletions core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
122 changes: 122 additions & 0 deletions core/src/services/nebula_graph/backend.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
64 changes: 64 additions & 0 deletions core/src/services/nebula_graph/config.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
/// The host port of nebulagraph's graphd server
pub port: Option<u16>,
/// The username of nebulagraph's graphd server
pub username: Option<String>,
/// The password of nebulagraph's graphd server
pub password: Option<String>,

/// The space name of nebulagraph's graphd server
pub space: Option<String>,
/// The tag name of nebulagraph's graphd server
pub tag: Option<String>,
/// The key field name of the NebulaGraph service to read/write.
pub key_field: Option<String>,
/// The value field name of the NebulaGraph service to read/write.
pub value_field: Option<String>,
/// The root for NebulaGraph
pub root: Option<String>,
}

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()
}
}
53 changes: 53 additions & 0 deletions core/src/services/nebula_graph/docs.md
Original file line number Diff line number Diff line change
@@ -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<u8> like bytea
builder.value_field("value");
let op = Operator::new(builder)?.finish();
Ok(())
}
```
24 changes: 24 additions & 0 deletions core/src/services/nebula_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions core/src/types/operator/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ impl Operator {
Scheme::HdfsNative => Self::from_iter::<services::HdfsNative>(iter)?.finish(),
#[cfg(feature = "services-lakefs")]
Scheme::Lakefs => Self::from_iter::<services::Lakefs>(iter)?.finish(),
#[cfg(feature = "services-nebula-graph")]
Scheme::NebulaGraph => Self::from_iter::<services::NebulaGraph>(iter)?.finish(),
v => {
return Err(Error::new(
ErrorKind::Unsupported,
Expand Down
6 changes: 6 additions & 0 deletions core/src/types/scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -315,6 +317,8 @@ impl Scheme {
Scheme::Surrealdb,
#[cfg(feature = "services-lakefs")]
Scheme::Lakefs,
#[cfg(feature = "services-nebula-graph")]
Scheme::NebulaGraph,
])
}
}
Expand Down Expand Up @@ -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()))),
}
}
Expand Down Expand Up @@ -480,6 +485,7 @@ impl From<Scheme> for &'static str {
Scheme::HdfsNative => "hdfs_native",
Scheme::Surrealdb => "surrealdb",
Scheme::Lakefs => "lakefs",
Scheme::NebulaGraph => "nebula_graph",
Scheme::Custom(v) => v,
}
}
Expand Down

0 comments on commit 7c47bd6

Please sign in to comment.