|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::sync::Arc; |
| 16 | + |
| 17 | +use async_channel::Receiver; |
| 18 | +use async_channel::Sender; |
| 19 | +use databend_common_catalog::table_context::TableContext; |
| 20 | +use databend_common_exception::ErrorCode; |
| 21 | +use databend_common_exception::Result; |
| 22 | +use databend_common_expression::BlockMetaInfoPtr; |
| 23 | +use databend_common_expression::DataBlock; |
| 24 | +use databend_common_pipeline_core::processors::InputPort; |
| 25 | +use databend_common_pipeline_core::processors::OutputPort; |
| 26 | +use databend_common_pipeline_core::processors::ProcessorPtr; |
| 27 | +use databend_common_pipeline_sinks::AsyncSink; |
| 28 | +use databend_common_pipeline_sinks::AsyncSinker; |
| 29 | +use databend_common_pipeline_sources::AsyncSource; |
| 30 | +use databend_common_pipeline_sources::AsyncSourcer; |
| 31 | + |
| 32 | +pub struct BroadcastSourceProcessor { |
| 33 | + pub receiver: Receiver<BlockMetaInfoPtr>, |
| 34 | +} |
| 35 | + |
| 36 | +impl BroadcastSourceProcessor { |
| 37 | + pub fn create( |
| 38 | + ctx: Arc<dyn TableContext>, |
| 39 | + receiver: Receiver<BlockMetaInfoPtr>, |
| 40 | + output_port: Arc<OutputPort>, |
| 41 | + ) -> Result<ProcessorPtr> { |
| 42 | + AsyncSourcer::create(ctx, output_port, Self { receiver }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[async_trait::async_trait] |
| 47 | +impl AsyncSource for BroadcastSourceProcessor { |
| 48 | + const NAME: &'static str = "BroadcastSource"; |
| 49 | + const SKIP_EMPTY_DATA_BLOCK: bool = false; |
| 50 | + |
| 51 | + #[async_backtrace::framed] |
| 52 | + async fn generate(&mut self) -> Result<Option<DataBlock>> { |
| 53 | + let received = self.receiver.recv().await; |
| 54 | + match received { |
| 55 | + Ok(meta) => Ok(Some(DataBlock::empty_with_meta(meta))), |
| 56 | + Err(_) => { |
| 57 | + // The channel is closed, we should return None to stop generating |
| 58 | + Ok(None) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +pub struct BroadcastSinkProcessor { |
| 65 | + received: Vec<BlockMetaInfoPtr>, |
| 66 | + sender: Sender<BlockMetaInfoPtr>, |
| 67 | +} |
| 68 | + |
| 69 | +impl BroadcastSinkProcessor { |
| 70 | + pub fn create(input: Arc<InputPort>, sender: Sender<BlockMetaInfoPtr>) -> Result<ProcessorPtr> { |
| 71 | + Ok(ProcessorPtr::create(AsyncSinker::create(input, Self { |
| 72 | + received: vec![], |
| 73 | + sender, |
| 74 | + }))) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[async_trait::async_trait] |
| 79 | +impl AsyncSink for BroadcastSinkProcessor { |
| 80 | + const NAME: &'static str = "BroadcastSink"; |
| 81 | + |
| 82 | + async fn on_finish(&mut self) -> Result<()> { |
| 83 | + self.sender.close(); |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | + |
| 87 | + async fn consume(&mut self, mut data_block: DataBlock) -> Result<bool> { |
| 88 | + let meta = data_block |
| 89 | + .take_meta() |
| 90 | + .ok_or_else(|| ErrorCode::Internal("Cannot downcast meta to BroadcastMeta"))?; |
| 91 | + self.sender |
| 92 | + .send(meta) |
| 93 | + .await |
| 94 | + .map_err(|_| ErrorCode::Internal("BroadcastSinkProcessor send error"))?; |
| 95 | + Ok(false) |
| 96 | + } |
| 97 | +} |
0 commit comments