|
| 1 | +/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. |
| 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 | +==============================================================================*/ |
| 15 | + |
| 16 | +//! Opaque, URL-safe blob keys. |
| 17 | +
|
| 18 | +use serde::{Deserialize, Serialize}; |
| 19 | +use std::borrow::Cow; |
| 20 | +use std::convert::TryFrom; |
| 21 | +use std::fmt::Display; |
| 22 | +use std::str::FromStr; |
| 23 | + |
| 24 | +use crate::types::Step; |
| 25 | + |
| 26 | +const BASE_64_CONFIG: base64::Config = base64::URL_SAFE_NO_PAD; |
| 27 | + |
| 28 | +/// Unique identifier for a blob. |
| 29 | +/// |
| 30 | +/// Blob keys are returned by the `ReadBlobSequences` RPC, and can be dereferenced via the |
| 31 | +/// `ReadBlob` RPC. |
| 32 | +/// |
| 33 | +/// Blob keys implement [`Display`] and [`FromStr`], which should be used for encoding and |
| 34 | +/// decoding, respectively. The `Display` format of a blob key is URL-safe. |
| 35 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 36 | +pub struct BlobKey<'a> { |
| 37 | + pub experiment_id: Cow<'a, str>, |
| 38 | + pub run: Cow<'a, str>, |
| 39 | + pub tag: Cow<'a, str>, |
| 40 | + pub step: Step, |
| 41 | + pub index: usize, |
| 42 | +} |
| 43 | + |
| 44 | +/// Helper struct to encode `BlobKey`s as tuples (rather than objects with named keys) and to use |
| 45 | +/// portable integers over the wire. |
| 46 | +#[derive(Debug, Serialize, Deserialize)] |
| 47 | +struct WireBlobKey<'a>(&'a str, &'a str, &'a str, i64, u64); |
| 48 | + |
| 49 | +/// An error returned when parsing a `BlobKey`. |
| 50 | +#[derive(Debug, thiserror::Error)] |
| 51 | +pub enum ParseBlobKeyError { |
| 52 | + #[error("invalid base-64: {}", .0)] |
| 53 | + BadBase64(base64::DecodeError), |
| 54 | + #[error("invalid JSON: {}", .0)] |
| 55 | + BadJson(serde_json::Error), |
| 56 | + #[error("index does not fit in memory on this system: {} > {}", .0, usize::MAX)] |
| 57 | + BadIndex(u64), |
| 58 | +} |
| 59 | + |
| 60 | +impl<'a> FromStr for BlobKey<'a> { |
| 61 | + type Err = ParseBlobKeyError; |
| 62 | + |
| 63 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 64 | + let buf = base64::decode_config(s, BASE_64_CONFIG).map_err(ParseBlobKeyError::BadBase64)?; |
| 65 | + let WireBlobKey(experiment_id, run, tag, step, index) = |
| 66 | + serde_json::from_slice(&buf).map_err(ParseBlobKeyError::BadJson)?; |
| 67 | + let index = usize::try_from(index).map_err(|_| ParseBlobKeyError::BadIndex(index))?; |
| 68 | + Ok(BlobKey { |
| 69 | + experiment_id: Cow::Owned(experiment_id.into()), |
| 70 | + run: Cow::Owned(run.into()), |
| 71 | + tag: Cow::Owned(tag.into()), |
| 72 | + step: Step(step), |
| 73 | + index, |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl<'a> Display for BlobKey<'a> { |
| 79 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 80 | + use base64::display::Base64Display; |
| 81 | + let wire = WireBlobKey( |
| 82 | + &self.experiment_id, |
| 83 | + &self.run, |
| 84 | + &self.tag, |
| 85 | + self.step.0, |
| 86 | + self.index as u64, |
| 87 | + ); |
| 88 | + let json = |
| 89 | + serde_json::to_string(&wire).expect("wire blob keys should always be serializable"); |
| 90 | + Base64Display::with_config(json.as_bytes(), BASE_64_CONFIG).fmt(f) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use super::*; |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_roundtrip() { |
| 100 | + let key = BlobKey { |
| 101 | + experiment_id: Cow::Borrowed("123"), |
| 102 | + run: Cow::Owned("mnist".to_string()), |
| 103 | + tag: Cow::Borrowed("input_image"), |
| 104 | + step: Step(777), |
| 105 | + index: 123, |
| 106 | + }; |
| 107 | + assert_eq!(key.to_string().parse::<BlobKey>().unwrap(), key); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test_no_padding() { |
| 112 | + for eid_length in 0..10 { |
| 113 | + let key = BlobKey { |
| 114 | + experiment_id: Cow::Owned("x".repeat(eid_length)), |
| 115 | + run: Cow::Borrowed("run"), |
| 116 | + tag: Cow::Borrowed("tag"), |
| 117 | + step: Step(0), |
| 118 | + index: 0, |
| 119 | + }; |
| 120 | + let encoded = key.to_string(); |
| 121 | + assert!( |
| 122 | + !encoded.ends_with('='), |
| 123 | + "encoded form should not end with '=': {:?} => {:?}", |
| 124 | + key, |
| 125 | + encoded, |
| 126 | + ); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_bad_base64() { |
| 132 | + match "???".parse::<BlobKey>().unwrap_err() { |
| 133 | + ParseBlobKeyError::BadBase64(_) => (), |
| 134 | + other => panic!("expected BadBase64(_), got {:?}", other), |
| 135 | + }; |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_bad_json() { |
| 140 | + match "AAAAAA".parse::<BlobKey>().unwrap_err() { |
| 141 | + ParseBlobKeyError::BadJson(_) => (), |
| 142 | + other => panic!("expected BadJson(_), got {:?}", other), |
| 143 | + }; |
| 144 | + } |
| 145 | +} |
0 commit comments