Skip to content

Commit

Permalink
WIP - Implement PyTxn
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Jan 29, 2024
1 parent 3923bf5 commit b6dc393
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/communicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ impl PyCommunicator {
todo!();

// future_into_py(py, async move {
// let mut client = client.lock().await;
// result.map(|_| ()).map_err(|e| Error(e).into())
// let mut client = client.lock().await;
// result.map(|_| ()).map_err(|e| Error(e).into())
// })
}

Expand All @@ -121,8 +121,9 @@ impl PyCommunicator {
todo!();

// future_into_py(py, async move {
// let mut client = client.lock().await;
// result.map(|_| ()).map_err(|e| Error(e).into())
// let mut client = client.lock().await;
// client.txn(txn)
// result.map(|_| ()).map_err(|e| Error(e).into())
// })
}

Expand All @@ -131,8 +132,8 @@ impl PyCommunicator {
todo!();

// future_into_py(py, async move {
// let mut client = client.lock().await;
// result.map(|_| ()).map_err(|e| Error(e).into())
// let mut client = client.lock().await;
// result.map(|_| ()).map_err(|e| Error(e).into())
// })
}

Expand Down
48 changes: 48 additions & 0 deletions src/compare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use etcd_client::{Compare, CompareOp};
use pyo3::prelude::*;
use pyo3::pyclass::CompareOp as PyO3CompareOp;

// #[derive(Clone)]
// #[pyclass(name = "CompareOp")]
// pub struct PyCompareOp(CompareOp);

// #[pymethods]
// impl PyCompareOp {
// #[classattr]
// const EQUAL: Self = Self(CompareOp::Equal);
// #[classattr]
// const GREATER: Self = Self(CompareOp::Greater);
// #[classattr]
// const LESS: Self = Self(CompareOp::Less);
// #[classattr]
// const NOT_EQUAL: Self = Self(CompareOp::NotEqual);

// pub fn __repr__(&self) -> String {
// match self.0 {
// CompareOp::Equal => "EQUAL".to_owned(),
// CompareOp::Greater => "GREATER".to_owned(),
// CompareOp::Less => "LESS".to_owned(),
// CompareOp::NotEqual => "NOT_EQUAL".to_owned(),
// }
// }

// pub fn __richcmp__(&self, py: Python, rhs: &PyCompareOp, op: PyO3CompareOp) -> PyObject {
// match op {
// PyO3CompareOp::Eq => (self.0 == rhs.0).into_py(py),
// PyO3CompareOp::Ne => (self.0 != rhs.0).into_py(py),
// _ => py.NotImplemented(),
// }
// }
// }

#[derive(Clone)]
#[pyclass(name = "Compare")]
pub struct PyCompare(pub Compare);

#[pymethods]
impl PyCompare {
// fn when(&self) {
// self.inner.
// }

}
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
mod client;
mod communicator;
mod compare;
mod condvar;
mod error;
mod event;
mod request_generator;
mod stream;
mod transaction;
mod utils;
mod watch;
mod request_generator;
mod transaction_action;

use client::PyClient;
use communicator::PyCommunicator;
use condvar::PyCondVar;
use error::ClientError;
use event::{PyEvent, PyEventType};
use pyo3::prelude::*;
use transaction::PyTxn;
use watch::PyWatch;

#[pymodule]
Expand All @@ -25,6 +27,7 @@ fn etcd_client(py: Python, module: &PyModule) -> PyResult<()> {
module.add_class::<PyEventType>()?;
module.add_class::<PyWatch>()?;
module.add_class::<PyCondVar>()?;
module.add_class::<PyTxn>()?;

module.add("ClientError", py.get_type::<ClientError>())?;
Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/request_generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct PyEventStream {
stream: WatchStream,
events: Vec<PyEvent>,
index: usize,
once: bool
once: bool,
}

impl PyEventStream {
Expand Down
50 changes: 50 additions & 0 deletions src/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use etcd_client::{DeleteOptions, GetOptions, PutOptions, Txn, TxnOp};
use pyo3::prelude::*;

use crate::compare::PyCompare;

#[derive(Clone)]
#[pyclass(name = "TxnOp")]
pub struct PyTxnOp(TxnOp);

#[pymethods]
impl PyTxnOp {
#[staticmethod]
fn get(key: String) -> PyResult<Self> {
let options = GetOptions::new();
Ok(PyTxnOp(TxnOp::get(key, Some(options))))
}
#[staticmethod]
fn put(key: String, value: String) -> PyResult<Self> {
let options = PutOptions::new();
Ok(PyTxnOp(TxnOp::put(key, value, Some(options))))
}

#[staticmethod]
fn delete(key: String) -> PyResult<Self> {
let options = DeleteOptions::new();
Ok(PyTxnOp(TxnOp::delete(key, Some(options))))
}
}

#[derive(Clone)]
#[pyclass(name = "Txn")]
pub struct PyTxn(Txn);

#[pymethods]
impl PyTxn {
fn when(&self, compares: Vec<PyCompare>) -> PyResult<Self> {
let compares = compares.into_iter().map(|c| c.0).collect::<Vec<_>>();
Ok(PyTxn(self.0.clone().when(compares)))
}

fn and_then(&self, operations: Vec<PyTxnOp>) -> PyResult<Self> {
let operations = operations.into_iter().map(|c| c.0).collect::<Vec<_>>();
Ok(PyTxn(self.0.clone().and_then(operations)))
}

fn or_else(&self, operations: Vec<PyTxnOp>) -> PyResult<Self> {
let operations = operations.into_iter().map(|c| c.0).collect::<Vec<_>>();
Ok(PyTxn(self.0.clone().or_else(operations)))
}
}
Empty file removed src/transaction_action.rs
Empty file.
2 changes: 1 addition & 1 deletion src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl PyWatch {
}

result
},
}
None => return Err(PyStopAsyncIteration::new_err(())),
}?;

Expand Down

0 comments on commit b6dc393

Please sign in to comment.