engula_journal/grpc/
journal.rs

1// Copyright 2021 The Engula Authors.
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 super::{
16    client::Client,
17    proto::{CreateStreamRequest, DeleteStreamRequest},
18    stream::Stream,
19};
20use crate::{async_trait, Result};
21
22#[derive(Clone)]
23pub struct Journal {
24    client: Client,
25}
26
27impl Journal {
28    pub async fn connect(addr: &str) -> Result<Journal> {
29        let endpoint = format!("http://{}", addr);
30        let client = Client::connect(&endpoint).await?;
31        Ok(Journal { client })
32    }
33}
34
35#[async_trait]
36impl crate::Journal for Journal {
37    type Stream = Stream;
38
39    async fn stream(&self, name: &str) -> Result<Stream> {
40        Ok(Stream::new(self.client.clone(), name.to_owned()))
41    }
42
43    async fn create_stream(&self, name: &str) -> Result<Stream> {
44        let input = CreateStreamRequest {
45            stream: name.to_owned(),
46        };
47        self.client.create_stream(input).await?;
48        self.stream(name).await
49    }
50
51    async fn delete_stream(&self, name: &str) -> Result<()> {
52        let input = DeleteStreamRequest {
53            stream: name.to_owned(),
54        };
55        self.client.delete_stream(input).await?;
56        Ok(())
57    }
58}