Skip to main content

common_meta/ddl/create_flow/
metadata.rs

1// Copyright 2023 Greptime Team
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
15use crate::ddl::create_flow::CreateFlowProcedure;
16use crate::error::Result;
17use crate::key::table_name::TableNameKey;
18
19impl CreateFlowProcedure {
20    /// Allocates the [FlowId].
21    pub(crate) async fn allocate_flow_id(&mut self) -> Result<()> {
22        // TODO(weny, ruihang): We don't support the partitions. It's always be 1, now.
23        let partitions = 1;
24        let (flow_id, peers) = self
25            .context
26            .flow_metadata_allocator
27            .create(partitions)
28            .await?;
29        self.data.flow_id = Some(flow_id);
30        self.data.peers = peers;
31
32        Ok(())
33    }
34
35    /// Collects source table ids and keeps track of missing tables.
36    pub(crate) async fn collect_source_tables(&mut self) -> Result<()> {
37        let keys = self
38            .data
39            .task
40            .source_table_names
41            .iter()
42            .map(|name| TableNameKey::new(&name.catalog_name, &name.schema_name, &name.table_name))
43            .collect::<Vec<_>>();
44
45        let source_table_ids = self
46            .context
47            .table_metadata_manager
48            .table_name_manager()
49            .batch_get(keys)
50            .await?;
51
52        let mut resolved = Vec::with_capacity(self.data.task.source_table_names.len());
53        let mut unresolved = Vec::new();
54
55        for (name, table_id) in self
56            .data
57            .task
58            .source_table_names
59            .iter()
60            .zip(source_table_ids)
61        {
62            match table_id {
63                Some(table_id) => resolved.push(table_id.table_id()),
64                None => unresolved.push(name.clone()),
65            }
66        }
67
68        self.data.source_table_ids = resolved;
69        self.data.unresolved_source_table_names = unresolved;
70        Ok(())
71    }
72}