You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.rs 2.1 kB

5 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //! The operator API is a framework to implement dora operators.
  2. //! The implemented operator will be managed by `dora`.
  3. //!
  4. //! This framework enable us to make optimisation and provide advanced features.
  5. //! It is the recommended way of using `dora`.
  6. //!
  7. //! An operator requires to be registered and implement the `DoraOperator` trait.
  8. //! It is composed of an `on_event` method that defines the behaviour
  9. //! of the operator when there is an event such as receiving an input for example.
  10. //!
  11. //! Try it out with:
  12. //!
  13. //! ```bash
  14. //! dora new op --kind operator
  15. //! ```
  16. //!
  17. #![warn(unsafe_op_in_unsafe_fn)]
  18. #![allow(clippy::missing_safety_doc)]
  19. pub use dora_arrow_convert::*;
  20. pub use dora_operator_api_macros::register_operator;
  21. pub use dora_operator_api_types as types;
  22. pub use types::DoraStatus;
  23. use types::{
  24. Metadata, Output, SendOutput,
  25. arrow::{self, array::Array},
  26. };
  27. pub mod raw;
  28. #[derive(Debug)]
  29. #[non_exhaustive]
  30. pub enum Event<'a> {
  31. Input { id: &'a str, data: ArrowData },
  32. InputParseError { id: &'a str, error: String },
  33. InputClosed { id: &'a str },
  34. Stop,
  35. }
  36. pub trait DoraOperator: Default {
  37. #[allow(clippy::result_unit_err)] // we use a () error type only for testing
  38. fn on_event(
  39. &mut self,
  40. event: &Event,
  41. output_sender: &mut DoraOutputSender,
  42. ) -> Result<DoraStatus, String>;
  43. }
  44. pub struct DoraOutputSender<'a>(&'a SendOutput);
  45. impl DoraOutputSender<'_> {
  46. /// Send an output from the operator:
  47. /// - `id` is the `output_id` as defined in your dataflow.
  48. /// - `data` is the data that should be sent
  49. pub fn send(&mut self, id: String, data: impl Array) -> Result<(), String> {
  50. let (data_array, schema) =
  51. arrow::ffi::to_ffi(&data.into_data()).map_err(|err| err.to_string())?;
  52. let result = self.0.send_output.call(Output {
  53. id: id.into(),
  54. data_array,
  55. schema,
  56. metadata: Metadata {
  57. open_telemetry_context: String::new().into(), // TODO
  58. },
  59. });
  60. result.into_result()
  61. }
  62. }