Skip to main content

engula_transactor/
server.rs

1// Copyright 2022 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 engula_apis::*;
16use engula_cooperator::Cooperator;
17use engula_supervisor::Supervisor;
18use tonic::{Request, Response};
19
20use crate::Result;
21
22pub struct Server {
23    supervisor: Supervisor,
24    cooperator: Cooperator,
25}
26
27impl Default for Server {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33impl Server {
34    pub fn new() -> Self {
35        let supervisor = Supervisor::new();
36        let cooperator = Cooperator::new(supervisor.clone());
37        Self {
38            supervisor,
39            cooperator,
40        }
41    }
42
43    pub fn into_service(self) -> engula_server::EngulaServer<Self> {
44        engula_server::EngulaServer::new(self)
45    }
46}
47
48#[tonic::async_trait]
49impl engula_server::Engula for Server {
50    async fn txn(&self, req: Request<TxnRequest>) -> Result<Response<TxnResponse>> {
51        let req = req.into_inner();
52        let res = self.cooperator.txn(req).await?;
53        Ok(Response::new(res))
54    }
55
56    async fn database(&self, req: Request<DatabaseRequest>) -> Result<Response<DatabaseResponse>> {
57        let req = req.into_inner();
58        let res = self.supervisor.database(req).await?;
59        Ok(Response::new(res))
60    }
61
62    async fn collection(
63        &self,
64        req: Request<CollectionRequest>,
65    ) -> Result<Response<CollectionResponse>> {
66        let req = req.into_inner();
67        let res = self.supervisor.collection(req).await?;
68        Ok(Response::new(res))
69    }
70}