Skip to content

Commit ca8f898

Browse files
committed
Address @cjpatton's comments and remove debug printing
1 parent 540deb7 commit ca8f898

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

crates/ct_worker/src/ctlog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ mod tests {
17511751
}
17521752

17531753
impl LockBackend for TestLockBackend {
1754-
const PART_SIZE: usize = 100;
1754+
const MAX_PART_BYTES: usize = 100;
17551755
const MAX_PARTS: usize = 10;
17561756
async fn put_multipart(&self, key: &str, value: &[u8]) -> worker::Result<()> {
17571757
self.put(key, value).await

crates/ct_worker/src/lib.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl MemoryCache {
280280

281281
// Compare-and-swap backend.
282282
trait LockBackend {
283-
const PART_SIZE: usize;
283+
const MAX_PART_BYTES: usize;
284284
const MAX_PARTS: usize;
285285
async fn put_multipart(&self, key: &str, value: &[u8]) -> Result<()>;
286286
async fn get_multipart(&self, key: &str) -> Result<Vec<u8>>;
@@ -290,20 +290,19 @@ trait LockBackend {
290290
}
291291

292292
impl LockBackend for State {
293-
// Stated limit is 2MB, but actual limit seems to be between 2.1 and 2.2MB.
293+
// DO value size limit is 2MB.
294294
// https://developers.cloudflare.com/durable-objects/platform/limits/
295-
const PART_SIZE: usize = 2_000_000;
295+
const MAX_PART_BYTES: usize = 1 << 21;
296296
// KV API supports putting and getting up to 128 values at a time. If this
297297
// is ever exceeded, the Worker has already run out of memory.
298298
// https://developers.cloudflare.com/durable-objects/api/storage-api/#kv-api
299299
const MAX_PARTS: usize = 128;
300300

301301
// Write a value to DO storage in multiple parts, each limited to
302-
// `PART_SIZE` bytes. Also write a manifest file that includes the total
303-
// length of the value and a checksum.
302+
// `MAX_PART_BYTES` bytes. Also write a manifest file that includes the
303+
// total length of the value and a checksum.
304304
async fn put_multipart(&self, key: &str, value: &[u8]) -> Result<()> {
305-
log::warn!("put_multipart of size {}", value.len());
306-
if value.len() > Self::PART_SIZE * Self::MAX_PARTS {
305+
if value.len() > Self::MAX_PART_BYTES * Self::MAX_PARTS {
307306
return Err("value too large".into());
308307
}
309308
let len_bytes = u32::try_from(value.len())
@@ -315,11 +314,11 @@ impl LockBackend for State {
315314
.collect::<Vec<u8>>();
316315
self.storage().put(key, manifest).await?;
317316

318-
let obj = js_sys::Object::new();
319317
// Encode keys suffixes as two hex digits so they'll be in the correct
320-
// when sorted in increasing order of UTF-8 encodings.
318+
// order when sorted in increasing order of UTF-8 encodings.
321319
let key_iter = (0..).map(|i| format!("{key}_{i:02x}"));
322-
for (k, v) in key_iter.zip(value.chunks(Self::PART_SIZE)) {
320+
let obj = js_sys::Object::new();
321+
for (k, v) in key_iter.zip(value.chunks(Self::MAX_PART_BYTES)) {
323322
let value = js_sys::Uint8Array::new_with_length(
324323
u32::try_from(v.len()).map_err(|_| "u32 conversion failed")?,
325324
);
@@ -345,11 +344,11 @@ impl LockBackend for State {
345344
let checksum: [u8; 32] = manifest[4..4 + 32]
346345
.try_into()
347346
.map_err(|_| "slice conversion failed")?;
348-
if len > Self::PART_SIZE * Self::MAX_PARTS {
347+
if len > Self::MAX_PART_BYTES * Self::MAX_PARTS {
349348
return Err("value too large".into());
350349
}
351350
let mut result = Vec::with_capacity(len);
352-
let keys = (0..((len - 1 + Self::PART_SIZE) / Self::PART_SIZE))
351+
let keys = (0..len.div_ceil(Self::MAX_PART_BYTES))
353352
.map(|i| format!("{key}_{i:02x}"))
354353
.collect::<Vec<_>>();
355354
// Keys in the map are sorted in increasing order of UTF-8 encodings, so we
@@ -360,12 +359,6 @@ impl LockBackend for State {
360359
result.extend(serde_wasm_bindgen::from_value::<Vec<u8>>(value?)?);
361360
}
362361

363-
let list = self.storage().list().await?;
364-
for key in list.keys() {
365-
let key = key?.as_string().ok_or("key wasn't a string")?;
366-
log::warn!("keys: {key}");
367-
}
368-
369362
if checksum != *Sha256::digest(&result) {
370363
return Err("checksum failed".into());
371364
}

0 commit comments

Comments
 (0)