Skip to content

Commit fc5dc8a

Browse files
committed
Address PR feedback
1 parent b712e6d commit fc5dc8a

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/io/vss_store.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,42 @@ impl VssStore {
2828
Self { client, store_id, runtime }
2929
}
3030

31-
fn build_key(&self, namespace: &str, sub_namespace: &str, key: &str) -> io::Result<String> {
32-
if key.is_empty() {
33-
return Err(Error::new(ErrorKind::Other, "Empty key is not allowed"));
34-
}
35-
if namespace.is_empty() {
31+
fn build_key(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> io::Result<String> {
32+
if primary_namespace.is_empty() {
3633
Ok(key.to_string())
3734
} else {
38-
Ok(format!("{}#{}#{}", namespace, sub_namespace, key))
35+
Ok(format!("{}#{}#{}", primary_namespace, secondary_namespace, key))
3936
}
4037
}
4138

42-
fn split_key(&self, key: &str) -> io::Result<(String, String, String)> {
43-
let parts: Vec<&str> = key.split('#').collect();
44-
match parts.as_slice() {
45-
[namespace, sub_namespace, actual_key] => {
46-
Ok((namespace.to_string(), sub_namespace.to_string(), actual_key.to_string()))
47-
}
48-
_ => Err(Error::new(ErrorKind::InvalidData, "Invalid key format")),
39+
fn extract_key(&self, unified_key: &str) -> io::Result<String> {
40+
let mut parts = unified_key.splitn(3, '#');
41+
let (_, _) = (parts.next(), parts.next()); // Discard primary_namespace & secondary_namespace
42+
match parts.next() {
43+
Some(actual_key) => Ok(actual_key.to_string()),
44+
None => Err(Error::new(ErrorKind::InvalidData, "Invalid key format")),
4945
}
5046
}
5147

52-
async fn list_all_keys(&self, namespace: &str, sub_namespace: &str) -> io::Result<Vec<String>> {
48+
async fn list_all_keys(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result<Vec<String>> {
5349
let mut page_token = None;
5450
let mut keys = vec![];
55-
let key_prefix = format!("{}#{}", namespace, sub_namespace);
51+
let key_prefix = format!("{}#{}", primary_namespace, secondary_namespace);
5652
while page_token != Some("".to_string()) {
5753
let request = ListKeyVersionsRequest {
58-
store_id: self.store_id.to_string(),
59-
key_prefix: Some(key_prefix.to_string()),
54+
store_id: self.store_id.clone(),
55+
key_prefix: Some(key_prefix.clone()),
6056
page_token,
6157
page_size: None,
6258
};
6359

6460
let response = self.client.list_key_versions(&request).await.map_err(|e| {
65-
let msg = format!("Failed to list keys in {}/{}: {}", namespace, sub_namespace, e);
61+
let msg = format!("Failed to list keys in {}/{}: {}", primary_namespace, secondary_namespace, e);
6662
Error::new(ErrorKind::Other, msg)
6763
})?;
6864

6965
for kv in response.key_versions {
70-
keys.push(self.split_key(&kv.key)?.2);
66+
keys.push(self.extract_key(&kv.key)?);
7167
}
7268
page_token = response.next_page_token;
7369
}
@@ -76,11 +72,11 @@ impl VssStore {
7672
}
7773

7874
impl KVStore for VssStore {
79-
fn read(&self, namespace: &str, sub_namespace: &str, key: &str) -> io::Result<Vec<u8>> {
80-
check_namespace_key_validity(namespace, sub_namespace, Some(key), "read")?;
75+
fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> io::Result<Vec<u8>> {
76+
check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "read")?;
8177
let request = GetObjectRequest {
8278
store_id: self.store_id.to_string(),
83-
key: self.build_key(namespace, sub_namespace, key)?,
79+
key: self.build_key(primary_namespace, secondary_namespace, key)?,
8480
};
8581

8682
let resp =
@@ -89,28 +85,28 @@ impl KVStore for VssStore {
8985
VssError::NoSuchKeyError(..) => {
9086
let msg = format!(
9187
"Failed to read as key could not be found: {}/{}/{}. Details: {}",
92-
namespace, sub_namespace, key, e
88+
primary_namespace, secondary_namespace, key, e
9389
);
9490
Error::new(ErrorKind::NotFound, msg)
9591
}
9692
_ => {
9793
let msg = format!(
9894
"Failed to read from key {}/{}/{}: {}",
99-
namespace, sub_namespace, key, e
95+
primary_namespace, secondary_namespace, key, e
10096
);
10197
Error::new(ErrorKind::Other, msg)
10298
}
10399
})?;
104100
Ok(resp.value.unwrap().value)
105101
}
106102

107-
fn write(&self, namespace: &str, sub_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> {
108-
check_namespace_key_validity(namespace, sub_namespace, Some(key), "write")?;
103+
fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> {
104+
check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "write")?;
109105
let request = PutObjectRequest {
110106
store_id: self.store_id.to_string(),
111107
global_version: None,
112108
transaction_items: vec![KeyValue {
113-
key: self.build_key(namespace, sub_namespace, key)?,
109+
key: self.build_key(primary_namespace, secondary_namespace, key)?,
114110
version: -1,
115111
value: buf.to_vec(),
116112
}],
@@ -121,7 +117,7 @@ impl KVStore for VssStore {
121117
.map_err(|e| {
122118
let msg = format!(
123119
"Failed to write to key {}/{}/{}: {}",
124-
namespace, sub_namespace, key, e
120+
primary_namespace, secondary_namespace, key, e
125121
);
126122
Error::new(ErrorKind::Other, msg)
127123
})?;
@@ -130,13 +126,13 @@ impl KVStore for VssStore {
130126
}
131127

132128
fn remove(
133-
&self, namespace: &str, sub_namespace: &str, key: &str, _lazy: bool,
129+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, _lazy: bool,
134130
) -> io::Result<()> {
135-
check_namespace_key_validity(namespace, sub_namespace, Some(key), "remove")?;
131+
check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "remove")?;
136132
let request = DeleteObjectRequest {
137133
store_id: self.store_id.to_string(),
138134
key_value: Some(KeyValue {
139-
key: self.build_key(namespace, sub_namespace, key)?,
135+
key: self.build_key(primary_namespace, secondary_namespace, key)?,
140136
version: -1,
141137
value: vec![],
142138
}),
@@ -145,22 +141,22 @@ impl KVStore for VssStore {
145141
tokio::task::block_in_place(|| self.runtime.block_on(self.client.delete_object(&request)))
146142
.map_err(|e| {
147143
let msg =
148-
format!("Failed to delete key {}/{}/{}: {}", namespace, sub_namespace, key, e);
144+
format!("Failed to delete key {}/{}/{}: {}", primary_namespace, secondary_namespace, key, e);
149145
Error::new(ErrorKind::Other, msg)
150146
})?;
151147
Ok(())
152148
}
153149

154-
fn list(&self, namespace: &str, sub_namespace: &str) -> io::Result<Vec<String>> {
155-
check_namespace_key_validity(namespace, sub_namespace, None, "list")?;
150+
fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result<Vec<String>> {
151+
check_namespace_key_validity(primary_namespace, secondary_namespace, None, "list")?;
156152

157153
let keys = tokio::task::block_in_place(|| {
158-
self.runtime.block_on(self.list_all_keys(namespace, sub_namespace))
154+
self.runtime.block_on(self.list_all_keys(primary_namespace, secondary_namespace))
159155
})
160156
.map_err(|e| {
161157
let msg = format!(
162158
"Failed to retrieve keys in namespace: {}/{} : {}",
163-
namespace, sub_namespace, e
159+
primary_namespace, secondary_namespace, e
164160
);
165161
Error::new(ErrorKind::Other, msg)
166162
})?;

0 commit comments

Comments
 (0)