cloud_util/
storage.rs

1// Copyright Rivtower Technologies LLC.
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 cita_cloud_proto::client::{InterceptedSvc, StorageClientTrait};
16use cita_cloud_proto::retry::RetryClient;
17use cita_cloud_proto::status_code::StatusCodeEnum;
18use cita_cloud_proto::storage::storage_service_client::StorageServiceClient;
19use cita_cloud_proto::storage::{Content, ExtKey};
20
21pub async fn store_data(
22    client: RetryClient<StorageServiceClient<InterceptedSvc>>,
23    region: u32,
24    key: Vec<u8>,
25    value: Vec<u8>,
26) -> StatusCodeEnum {
27    let content = Content { region, key, value };
28    match client.store(content.clone()).await {
29        Ok(code) => StatusCodeEnum::from(code),
30        Err(e) => {
31            warn!("store_data({:?}) failed: {}", content, e.to_string());
32            StatusCodeEnum::StorageServerNotReady
33        }
34    }
35}
36
37pub async fn load_data(
38    client: RetryClient<StorageServiceClient<InterceptedSvc>>,
39    region: u32,
40    key: Vec<u8>,
41) -> Result<Vec<u8>, StatusCodeEnum> {
42    let ext_key = ExtKey { region, key };
43    let value = client.load(ext_key.clone()).await.map_err(|e| {
44        warn!("load_data({:?}) failed: {}", ext_key, e.to_string());
45        StatusCodeEnum::StorageServerNotReady
46    })?;
47
48    StatusCodeEnum::from(value.status.ok_or(StatusCodeEnum::NoneStatusCode)?).is_success()?;
49    Ok(value.value)
50}