Skip to content

Commit 2020051

Browse files
authored
change range to u32 (#15)
* change range to u32 * remove conversions
1 parent 1936d72 commit 2020051

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

cas_client/src/remote_client.rs

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use std::io::{Cursor, Write};
33
use anyhow::anyhow;
44
use bytes::Buf;
55
use cas::key::Key;
6-
use cas_types::{
7-
QueryChunkResponse, QueryReconstructionResponse, UploadXorbResponse
8-
};
6+
use cas_types::{QueryChunkResponse, QueryReconstructionResponse, UploadXorbResponse};
97
use reqwest::{StatusCode, Url};
108
use serde::{de::DeserializeOwned, Serialize};
119

@@ -85,7 +83,9 @@ impl Client for RemoteClient {
8583

8684
impl RemoteClient {
8785
pub async fn from_config(endpoint: String) -> Self {
88-
Self { client: CASAPIClient::new(&endpoint) }
86+
Self {
87+
client: CASAPIClient::new(&endpoint),
88+
}
8989
}
9090
}
9191

@@ -103,10 +103,11 @@ impl Default for CASAPIClient {
103103

104104
impl CASAPIClient {
105105
pub fn new(endpoint: &str) -> Self {
106-
let client = reqwest::Client::builder()
107-
.build()
108-
.unwrap();
109-
Self { client, endpoint: endpoint.to_string() }
106+
let client = reqwest::Client::builder().build().unwrap();
107+
Self {
108+
client,
109+
endpoint: endpoint.to_string(),
110+
}
110111
}
111112

112113
pub async fn exists(&self, key: &Key) -> Result<bool> {
@@ -162,11 +163,11 @@ impl CASAPIClient {
162163

163164
let mut writer = Cursor::new(Vec::new());
164165

165-
let (_,_) = CasObject::serialize(
166+
let (_, _) = CasObject::serialize(
166167
&mut writer,
167-
&key.hash,
168+
&key.hash,
168169
contents,
169-
&chunk_boundaries.into_iter().map(|x| x as u32).collect()
170+
&chunk_boundaries.into_iter().map(|x| x as u32).collect(),
170171
)?;
171172

172173
debug!("Upload: POST to {url:?} for {key:?}");
@@ -178,18 +179,25 @@ impl CASAPIClient {
178179
let response_parsed: UploadXorbResponse = serde_json::from_reader(response_body.reader())?;
179180

180181
Ok(response_parsed.was_inserted)
181-
}
182+
}
182183

183184
/// Reconstruct a file and write to writer.
184-
pub async fn write_file<W: Write>(&self, file_id: &MerkleHash, writer: &mut W) -> Result<usize> {
185-
185+
pub async fn write_file<W: Write>(
186+
&self,
187+
file_id: &MerkleHash,
188+
writer: &mut W,
189+
) -> Result<usize> {
186190
// get manifest of xorbs to download
187191
let manifest = self.reconstruct_file(file_id).await?;
188192

189193
self.reconstruct(manifest, writer).await
190194
}
191195

192-
async fn reconstruct<W: Write>(&self, reconstruction_response: QueryReconstructionResponse, writer: &mut W) -> Result<usize> {
196+
async fn reconstruct<W: Write>(
197+
&self,
198+
reconstruction_response: QueryReconstructionResponse,
199+
writer: &mut W,
200+
) -> Result<usize> {
193201
let info = reconstruction_response.reconstruction;
194202
let total_len = info.iter().fold(0, |acc, x| acc + x.unpacked_length);
195203
let futs = info.into_iter().map(|term| {
@@ -212,19 +220,21 @@ impl CASAPIClient {
212220

213221
/// Reconstruct the file
214222
async fn reconstruct_file(&self, file_id: &MerkleHash) -> Result<QueryReconstructionResponse> {
215-
let url = Url::parse(&format!("{}/reconstruction/{}", self.endpoint, file_id.hex()))?;
216-
223+
let url = Url::parse(&format!(
224+
"{}/reconstruction/{}",
225+
self.endpoint,
226+
file_id.hex()
227+
))?;
228+
217229
let response = self.client.get(url).send().await?;
218230
let response_body = response.bytes().await?;
219-
let response_parsed: QueryReconstructionResponse = serde_json::from_reader(response_body.reader())?;
220-
231+
let response_parsed: QueryReconstructionResponse =
232+
serde_json::from_reader(response_body.reader())?;
233+
221234
Ok(response_parsed)
222235
}
223236

224-
pub async fn shard_query_chunk(
225-
&self,
226-
key: &Key,
227-
) -> Result<QueryChunkResponse> {
237+
pub async fn shard_query_chunk(&self, key: &Key) -> Result<QueryChunkResponse> {
228238
let url = Url::parse(&format!("{}/chunk/{key}", self.endpoint))?;
229239
let response = self.client.get(url).send().await?;
230240
let response_body = response.bytes().await?;
@@ -259,7 +269,7 @@ async fn get_one(term: &CASReconstructionTerm) -> Result<Bytes> {
259269
let mut readseek = Cursor::new(xorb_bytes.to_vec());
260270

261271
let cas_object = CasObject::deserialize(&mut readseek)?;
262-
let data = cas_object.get_range(&mut readseek, term.range.start as u32, term.range.end as u32)?;
272+
let data = cas_object.get_range(&mut readseek, term.range.start, term.range.end)?;
263273

264274
Ok(Bytes::from(data))
265275
}
@@ -289,13 +299,16 @@ mod tests {
289299
// Assert
290300
assert!(result.is_ok());
291301
}
292-
293-
fn gen_dummy_xorb(num_chunks: u32, uncompressed_chunk_size: u32, randomize_chunk_sizes: bool) -> (DataHash, Vec<u8>, Vec<u64>) {
302+
303+
fn gen_dummy_xorb(
304+
num_chunks: u32,
305+
uncompressed_chunk_size: u32,
306+
randomize_chunk_sizes: bool,
307+
) -> (DataHash, Vec<u8>, Vec<u64>) {
294308
let mut contents = Vec::new();
295309
let mut chunks: Vec<Chunk> = Vec::new();
296310
let mut chunk_boundaries = Vec::with_capacity(num_chunks as usize);
297311
for _idx in 0..num_chunks {
298-
299312
let chunk_size: u32 = if randomize_chunk_sizes {
300313
let mut rng = rand::thread_rng();
301314
rng.gen_range(1024..=uncompressed_chunk_size)
@@ -305,7 +318,10 @@ mod tests {
305318

306319
let bytes = gen_random_bytes(chunk_size);
307320

308-
chunks.push(Chunk { hash: merklehash::compute_data_hash(&bytes), length: bytes.len() });
321+
chunks.push(Chunk {
322+
hash: merklehash::compute_data_hash(&bytes),
323+
length: bytes.len(),
324+
});
309325

310326
contents.extend(bytes);
311327
chunk_boundaries.push(contents.len() as u64);
@@ -324,6 +340,6 @@ mod tests {
324340
let mut rng = rand::thread_rng();
325341
let mut data = vec![0u8; uncompressed_chunk_size as usize];
326342
rng.fill(&mut data[..]);
327-
data
343+
data
328344
}
329345
}

cas_types/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub struct UploadXorbResponse {
1414

1515
#[derive(Debug, Serialize, Deserialize, Clone)]
1616
pub struct Range {
17-
pub start: u64,
18-
pub end: u64,
17+
pub start: u32,
18+
pub end: u32,
1919
}
2020

2121
#[derive(Debug, Serialize, Deserialize, Clone)]

0 commit comments

Comments
 (0)