Skip to main content

fluss/rpc/message/
create_database.rs

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
18use crate::metadata::DatabaseDescriptor;
19use crate::{impl_read_version_type, impl_write_version_type, proto};
20
21use crate::error::Result as FlussResult;
22use crate::proto::CreateDatabaseResponse;
23use crate::rpc::api_key::ApiKey;
24use crate::rpc::api_version::ApiVersion;
25use crate::rpc::frame::ReadError;
26use crate::rpc::frame::WriteError;
27use crate::rpc::message::{ReadVersionedType, RequestBody, WriteVersionedType};
28
29use bytes::{Buf, BufMut};
30use prost::Message;
31
32#[derive(Debug)]
33pub struct CreateDatabaseRequest {
34    pub inner_request: proto::CreateDatabaseRequest,
35}
36
37impl CreateDatabaseRequest {
38    pub fn new(
39        database_name: &str,
40        database_descriptor: Option<&DatabaseDescriptor>,
41        ignore_if_exists: bool,
42    ) -> FlussResult<Self> {
43        let database_json = if let Some(descriptor) = database_descriptor {
44            Some(descriptor.to_json_bytes()?)
45        } else {
46            None
47        };
48
49        Ok(CreateDatabaseRequest {
50            inner_request: proto::CreateDatabaseRequest {
51                database_name: database_name.to_string(),
52                ignore_if_exists,
53                database_json,
54            },
55        })
56    }
57}
58
59impl RequestBody for CreateDatabaseRequest {
60    type ResponseBody = CreateDatabaseResponse;
61
62    const API_KEY: ApiKey = ApiKey::CreateDatabase;
63
64    const REQUEST_VERSION: ApiVersion = ApiVersion(0);
65}
66
67impl_write_version_type!(CreateDatabaseRequest);
68impl_read_version_type!(CreateDatabaseResponse);