Skip to content

Commit b85e1ae

Browse files
committed
[#2] Add python api
1 parent c84f72c commit b85e1ae

File tree

3 files changed

+332
-0
lines changed

3 files changed

+332
-0
lines changed

curvine-libsdk/src/python/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod python_filesystem;
2+
pub mod python_abi;
3+
pub use self::python_filesystem::PythonFilesystem;
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#![allow(clippy::missing_safety_doc)]
2+
3+
use crate::{LibFsReader, LibFsWriter};
4+
use crate::python::PythonFilesystem;
5+
use orpc::sys::DataSlice;
6+
use orpc::sys::{FFIUtils, RawVec};
7+
use pyo3::prelude::*;
8+
use pyo3::types::{PyList, PyBytes};
9+
10+
11+
#[pyfunction]
12+
pub fn python_io_curvine_curvine_native_new_filesystem(conf: String) -> PyResult<i64>{
13+
let fs = PythonFilesystem::new(conf)?;
14+
Ok(FFIUtils::into_raw_ptr(fs))
15+
}
16+
17+
#[pyfunction]
18+
pub fn python_io_curvine_curvine_native_create(
19+
bound_fs: &Bound<'_, PythonFilesystem>,
20+
path: String,
21+
overwrite: bool
22+
) -> PyResult<i64>{
23+
24+
let raw_ptr = bound_fs.as_ptr();
25+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
26+
let fs = unsafe{ &*fs_ptr};
27+
let writer = fs.create(path,overwrite)?;
28+
Ok(FFIUtils::into_raw_ptr(writer))
29+
}
30+
31+
#[pyfunction]
32+
pub fn python_io_curvine_curvine_native_append(
33+
bound_fs: &Bound<'_, PythonFilesystem>,
34+
path: String,
35+
tmp: &Bound<'_, PyList>,
36+
) -> PyResult<i64>{
37+
let raw_ptr = bound_fs.as_ptr();
38+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
39+
let fs = unsafe{ &*fs_ptr};
40+
let writer = fs.append(path)?;
41+
let arr = writer.pos();
42+
43+
tmp.set_item(0, arr)?;
44+
Ok(FFIUtils::into_raw_ptr(writer))
45+
}
46+
47+
#[pyfunction]
48+
pub fn python_io_curvine_curvine_native_write(
49+
bound_writer: &Bound<'_, LibFsWriter>,
50+
buf: i64,
51+
len: i32,
52+
) -> PyResult<()>{
53+
let raw_ptr = bound_writer.as_ptr();
54+
let writer_ptr = raw_ptr.cast::<LibFsWriter>();
55+
let writer = unsafe{ &mut *writer_ptr};
56+
let raw_vec = RawVec::from_raw(buf as *mut u8, len as usize);
57+
let buf = DataSlice::MemSlice(raw_vec);
58+
writer.write(buf)?;
59+
60+
Ok(())
61+
}
62+
63+
#[pyfunction]
64+
pub fn python_io_curvine_curvine_native_flush(bound_writer: &Bound<'_, LibFsWriter>) -> PyResult<()>{
65+
let raw_ptr = bound_writer.as_ptr();
66+
let writer_ptr = raw_ptr.cast::<LibFsWriter>();
67+
let writer = unsafe{ &mut *writer_ptr};
68+
writer.flush()?;
69+
Ok(())
70+
}
71+
72+
#[pyfunction]
73+
pub unsafe fn python_io_curvine_curvine_native_close_writer(bound_writer: &Bound<'_, LibFsWriter>) -> PyResult<()>{
74+
let raw_ptr = bound_writer.as_ptr();
75+
let writer_ptr = raw_ptr.cast::<LibFsWriter>();
76+
let writer = &mut *writer_ptr;
77+
writer.complete()?;
78+
FFIUtils::free_raw_ptr(writer_ptr);
79+
Ok(())
80+
}
81+
82+
#[pyfunction]
83+
pub fn python_io_curvine_curvine_native_open(
84+
bound_fs: &Bound<'_, PythonFilesystem>,
85+
path: String,
86+
tmp: &Bound<'_, PyList>,
87+
) -> PyResult<i64>{
88+
let raw_ptr = bound_fs.as_ptr();
89+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
90+
let fs = unsafe{ &*fs_ptr};
91+
let reader = fs.open(path)?;
92+
let arr = reader.len();
93+
tmp.set_item(0, arr)?;
94+
Ok(FFIUtils::into_raw_ptr(reader))
95+
}
96+
97+
#[pyfunction]
98+
pub fn python_io_curvine_curvine_native_read(
99+
bound_reader: &Bound<'_, LibFsReader>,
100+
tmp: &Bound<'_, PyList>,
101+
) -> PyResult<()>{
102+
let raw_ptr = bound_reader.as_ptr();
103+
let reader_ptr = raw_ptr.cast::<LibFsReader>();
104+
let reader = unsafe{ &mut *reader_ptr};
105+
let bytes = reader.read()?;
106+
107+
let bytes_ptr = bytes.as_ptr() as i64;
108+
let bytes_len = bytes.len() as i64;
109+
tmp.set_item(0, bytes_ptr)?;
110+
tmp.set_item(0, bytes_len)?;
111+
112+
Ok(())
113+
}
114+
115+
#[pyfunction]
116+
pub fn python_io_curvine_curvine_native_seek(
117+
bound_reader: &Bound<'_, LibFsReader>,
118+
pos: i64,
119+
) -> PyResult<()>{
120+
let raw_ptr = bound_reader.as_ptr();
121+
let reader_ptr = raw_ptr.cast::<LibFsReader>();
122+
let reader = unsafe{ &mut *reader_ptr};
123+
reader.seek(pos)?;
124+
Ok(())
125+
}
126+
127+
#[pyfunction]
128+
pub unsafe fn python_io_curvine_curvine_native_close_reader(bound_reader: &Bound<'_, LibFsReader>) -> PyResult<()>{
129+
let raw_ptr = bound_reader.as_ptr();
130+
let reader_ptr = raw_ptr.cast::<LibFsReader>();
131+
let reader = &mut *reader_ptr;
132+
reader.complete()?;
133+
FFIUtils::free_raw_ptr(reader_ptr);
134+
Ok(())
135+
}
136+
137+
#[pyfunction]
138+
pub unsafe fn python_io_curvine_curvine_native_close_filesystem(bound_fs: &Bound<'_, PythonFilesystem>) -> PyResult<()>{
139+
let raw_ptr = bound_fs.as_ptr();
140+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
141+
FFIUtils::free_raw_ptr(fs_ptr);
142+
Ok(())
143+
}
144+
145+
#[pyfunction]
146+
pub fn python_io_curvine_curvine_native_mkdir(
147+
bound_fs: &Bound<'_, PythonFilesystem>,
148+
path: String,
149+
create_parent: bool,
150+
) -> PyResult<bool>{
151+
let raw_ptr = bound_fs.as_ptr();
152+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
153+
let fs = unsafe{ &*fs_ptr};
154+
let is_success = fs.mkdir(path, create_parent)?;
155+
Ok(is_success)
156+
}
157+
158+
#[pyfunction]
159+
pub fn python_io_curvine_curvine_native_get_file_status<'py>(
160+
bound_fs: &Bound<'py, PythonFilesystem>,
161+
path: String,
162+
py: Python<'py>,
163+
) -> PyResult<Bound<'py, PyBytes>>{
164+
let raw_ptr = bound_fs.as_ptr();
165+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
166+
let fs = unsafe{ &*fs_ptr};
167+
let status = fs.get_file_status(path)?;
168+
let bytes_data = status.freeze().to_vec();
169+
Ok(PyBytes::new(py, &bytes_data))
170+
}
171+
172+
#[pyfunction]
173+
pub fn python_io_curvine_curvine_native_list_status<'py>(
174+
bound_fs: &Bound<'_, PythonFilesystem>,
175+
path: String,
176+
py: Python<'py>,
177+
) -> PyResult<Bound<'py, PyBytes>>{
178+
let raw_ptr = bound_fs.as_ptr();
179+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
180+
let fs = unsafe{ &*fs_ptr};
181+
let status = fs.list_status(path)?;
182+
let bytes_data = status.freeze().to_vec();
183+
Ok(PyBytes::new(py, &bytes_data))
184+
}
185+
186+
#[pyfunction]
187+
pub fn python_io_curvine_curvine_native_rename(
188+
bound_fs: &Bound<'_, PythonFilesystem>,
189+
src: String,
190+
dst: String,
191+
) -> PyResult<bool>{
192+
let raw_ptr = bound_fs.as_ptr();
193+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
194+
let fs = unsafe{ &*fs_ptr};
195+
let is_rename = fs.rename(src, dst)?;
196+
Ok(is_rename)
197+
}
198+
199+
#[pyfunction]
200+
pub fn python_io_curvine_curvine_native_delete(
201+
bound_fs: &Bound<'_, PythonFilesystem>,
202+
path: String,
203+
recursive: bool,
204+
) -> PyResult<()>{
205+
let raw_ptr = bound_fs.as_ptr();
206+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
207+
let fs = unsafe{ &*fs_ptr};
208+
fs.delete(path, recursive)?;
209+
Ok(())
210+
}
211+
212+
#[pyfunction]
213+
pub fn python_io_curvine_curvine_native_get_master_info<'py>(
214+
bound_fs: &Bound<'_, PythonFilesystem>,
215+
py: Python<'py>,
216+
) -> PyResult<Bound<'py, PyBytes>>{
217+
let raw_ptr = bound_fs.as_ptr();
218+
let fs_ptr = raw_ptr.cast::<PythonFilesystem>();
219+
let fs = unsafe{ &*fs_ptr};
220+
let status = fs.get_master_info()?;
221+
let bytes_data = status.freeze().to_vec();
222+
Ok(PyBytes::new(py, &bytes_data))
223+
}
224+
225+
#[pymodule]
226+
fn curvine_libsdk(m: &Bound<'_, PyModule>) -> PyResult<()>{
227+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_new_filesystem, m)?)?;
228+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_create, m)?)?;
229+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_append, m)?)?;
230+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_write, m)?)?;
231+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_flush, m)?)?;
232+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_close_writer, m)?)?;
233+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_open, m)?)?;
234+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_read, m)?)?;
235+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_seek, m)?)?;
236+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_close_reader, m)?)?;
237+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_close_filesystem, m)?)?;
238+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_mkdir, m)?)?;
239+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_get_file_status, m)?)?;
240+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_list_status, m)?)?;
241+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_rename, m)?)?;
242+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_delete, m)?)?;
243+
m.add_function(wrap_pyfunction!(python_io_curvine_curvine_native_get_master_info, m)?)?;
244+
245+
Ok(())
246+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use crate::{FilesystemConf, LibFilesystem, LibFsReader, LibFsWriter};
2+
use bytes::BytesMut;
3+
use curvine_common::FsResult;
4+
use pyo3::prelude::*;
5+
6+
7+
//Define the PythonFilesystem struct
8+
#[pyclass]
9+
pub struct PythonFilesystem {
10+
inner: LibFilesystem,
11+
}
12+
13+
//Implement methods for PythonFilesystem
14+
impl PythonFilesystem{
15+
//New PythonFilesystem
16+
pub fn new(conf: String) -> FsResult<Self>{
17+
let fs_conf = FilesystemConf::from_str(conf)?;
18+
let cluster_conf = fs_conf.into_cluster_conf()?;
19+
20+
let inner = LibFilesystem::new(cluster_conf)?;
21+
Ok(Self{inner})
22+
}
23+
24+
//Create file
25+
pub fn create(
26+
&self,
27+
path: String,
28+
overwrite: bool,
29+
) -> FsResult<LibFsWriter>{
30+
self.inner.create(path, overwrite)
31+
}
32+
33+
//Append content
34+
pub fn append(&self, path: String) -> FsResult<LibFsWriter>{
35+
self.inner.append(path)
36+
}
37+
38+
//Create a directory
39+
pub fn mkdir(
40+
&self,
41+
path: String,
42+
create_parent: bool,
43+
) -> FsResult<bool>{
44+
self.inner
45+
.mkdir(path, create_parent)
46+
}
47+
48+
//Open file
49+
pub fn open(&self, path: String) -> FsResult<LibFsReader>{
50+
self.inner.open(path)
51+
}
52+
53+
//Get file status
54+
pub fn get_file_status(&self, path: String) -> FsResult<BytesMut>{ //之后要考虑这样传递(直接返回所有权)有没有问题
55+
let status = self.inner.get_status(path)?;
56+
Ok(status)
57+
58+
}
59+
60+
//List file system
61+
pub fn list_status(&self, path: String) -> FsResult<BytesMut>{
62+
let status = self.inner.list_status(path)?;
63+
Ok(status)
64+
}
65+
66+
//Rename file
67+
pub fn rename(&self, src: String, dst: String) -> FsResult<bool>{
68+
self.inner.rename(src,dst)
69+
}
70+
71+
//Delete file
72+
pub fn delete(&self, path: String, recursive: bool) -> FsResult<()>{
73+
self.inner.delete(path, recursive)
74+
}
75+
76+
//Get master information
77+
pub fn get_master_info(&self) -> FsResult<BytesMut>{
78+
let status = self.inner.get_master_info()?;
79+
Ok(status)
80+
}
81+
}
82+
83+

0 commit comments

Comments
 (0)