|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::error::Result; |
| 19 | +use crate::plugin::udf::UDFPluginManager; |
| 20 | +use libloading::Library; |
| 21 | +use std::any::Any; |
| 22 | +use std::env; |
| 23 | +use std::sync::Arc; |
| 24 | + |
| 25 | +/// plugin manager |
| 26 | +pub mod plugin_manager; |
| 27 | +/// udf plugin |
| 28 | +pub mod udf; |
| 29 | + |
| 30 | +/// CARGO_PKG_VERSION |
| 31 | +pub static CORE_VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 32 | +/// RUSTC_VERSION |
| 33 | +pub static RUSTC_VERSION: &str = env!("RUSTC_VERSION"); |
| 34 | + |
| 35 | +/// Top plugin trait |
| 36 | +pub trait Plugin { |
| 37 | + /// Returns the plugin as [`Any`](std::any::Any) so that it can be |
| 38 | + /// downcast to a specific implementation. |
| 39 | + fn as_any(&self) -> &dyn Any; |
| 40 | +} |
| 41 | + |
| 42 | +/// The enum of Plugin |
| 43 | +#[derive(PartialEq, std::cmp::Eq, std::hash::Hash, Copy, Clone)] |
| 44 | +pub enum PluginEnum { |
| 45 | + /// UDF/UDAF plugin |
| 46 | + UDF, |
| 47 | +} |
| 48 | + |
| 49 | +impl PluginEnum { |
| 50 | + /// new a struct which impl the PluginRegistrar trait |
| 51 | + pub fn init_plugin_manager(&self) -> Box<dyn PluginRegistrar> { |
| 52 | + match self { |
| 53 | + PluginEnum::UDF => Box::new(UDFPluginManager::default()), |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Every plugin need a PluginDeclaration |
| 59 | +#[derive(Copy, Clone)] |
| 60 | +pub struct PluginDeclaration { |
| 61 | + /// Rust doesn’t have a stable ABI, meaning different compiler versions can generate incompatible code. |
| 62 | + /// For these reasons, the UDF plug-in must be compiled using the same version of rustc as datafusion. |
| 63 | + pub rustc_version: &'static str, |
| 64 | + |
| 65 | + /// core version of the plugin. The plugin's core_version need same as plugin manager. |
| 66 | + pub core_version: &'static str, |
| 67 | + |
| 68 | + /// One of PluginEnum |
| 69 | + pub plugin_type: unsafe extern "C" fn() -> PluginEnum, |
| 70 | +} |
| 71 | + |
| 72 | +/// Plugin Registrar , Every plugin need implement this trait |
| 73 | +pub trait PluginRegistrar: Send + Sync + 'static { |
| 74 | + /// # Safety |
| 75 | + /// load plugin from library |
| 76 | + unsafe fn load(&mut self, library: Arc<Library>) -> Result<()>; |
| 77 | + |
| 78 | + /// Returns the plugin as [`Any`](std::any::Any) so that it can be |
| 79 | + /// downcast to a specific implementation. |
| 80 | + fn as_any(&self) -> &dyn Any; |
| 81 | +} |
| 82 | + |
| 83 | +/// Declare a plugin's PluginDeclaration. |
| 84 | +/// |
| 85 | +/// # Notes |
| 86 | +/// |
| 87 | +/// This works by automatically generating an `extern "C"` function named `get_plugin_type` with a |
| 88 | +/// pre-defined signature and symbol name. And then generating a PluginDeclaration. |
| 89 | +/// Therefore you will only be able to declare one plugin per library. |
| 90 | +#[macro_export] |
| 91 | +macro_rules! declare_plugin { |
| 92 | + ($plugin_type:expr) => { |
| 93 | + #[no_mangle] |
| 94 | + pub extern "C" fn get_plugin_type() -> $crate::plugin::PluginEnum { |
| 95 | + $plugin_type |
| 96 | + } |
| 97 | + |
| 98 | + #[no_mangle] |
| 99 | + pub static plugin_declaration: $crate::plugin::PluginDeclaration = |
| 100 | + $crate::plugin::PluginDeclaration { |
| 101 | + rustc_version: $crate::plugin::RUSTC_VERSION, |
| 102 | + core_version: $crate::plugin::CORE_VERSION, |
| 103 | + plugin_type: get_plugin_type, |
| 104 | + }; |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +/// get the plugin dir |
| 109 | +pub fn plugin_dir() -> String { |
| 110 | + let current_exe_dir = match env::current_exe() { |
| 111 | + Ok(exe_path) => exe_path.display().to_string(), |
| 112 | + Err(_e) => "".to_string(), |
| 113 | + }; |
| 114 | + |
| 115 | + // If current_exe_dir contain `deps` the root dir is the parent dir |
| 116 | + // eg: /Users/xxx/workspace/rust/rust_plugin_sty/target/debug/deps/plugins_app-067452b3ff2af70e |
| 117 | + // the plugin dir is /Users/xxx/workspace/rust/rust_plugin_sty/target/debug/deps |
| 118 | + // else eg: /Users/xxx/workspace/rust/rust_plugin_sty/target/debug/plugins_app |
| 119 | + // the plugin dir is /Users/xxx/workspace/rust/rust_plugin_sty/target/debug/ |
| 120 | + if current_exe_dir.contains("/deps/") { |
| 121 | + let i = current_exe_dir.find("/deps/").unwrap(); |
| 122 | + String::from(¤t_exe_dir.as_str()[..i + 6]) |
| 123 | + } else { |
| 124 | + let i = current_exe_dir.rfind('/').unwrap(); |
| 125 | + String::from(¤t_exe_dir.as_str()[..i]) |
| 126 | + } |
| 127 | +} |
0 commit comments