Skip to content

Commit ec8e89a

Browse files
committed
update comments, renaming
1 parent 07d81ef commit ec8e89a

File tree

2 files changed

+65
-84
lines changed

2 files changed

+65
-84
lines changed

src/service/pom.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl crate::server::MyService {
2525
{
2626
return Err(Status::permission_denied(""));
2727
}
28-
// prepare information for starting the instance
28+
// prepare CLI param to start PO instance
2929
if self.core_uri.is_none() {
3030
return Err(Status::internal("core_uri not found."));
3131
}
@@ -37,8 +37,8 @@ impl crate::server::MyService {
3737
let instance_id = Uuid::new_v4();
3838
let protocol_name: &str = &request.get_ref().protocol_name;
3939
// check protocol_name
40-
let file_name = Path::new(&protocol_name).file_name();
41-
if file_name.is_none() || file_name.unwrap() != protocol_name {
40+
let protocol_name_parsed = Path::new(&protocol_name).file_name();
41+
if protocol_name_parsed.is_none() || protocol_name_parsed.unwrap() != protocol_name {
4242
return Err(Status::invalid_argument("protocol_name is invalid."));
4343
}
4444
// create protocols directory if not exist
@@ -52,14 +52,14 @@ impl crate::server::MyService {
5252
let protocol_package_dir = Path::new(&colink_home)
5353
.join("protocols")
5454
.join(protocol_name);
55-
let file_lock = get_file_lock(&colink_home, protocol_name)?;
56-
let file_lock = tokio::task::spawn_blocking(move || {
57-
file_lock.lock_exclusive().unwrap();
58-
file_lock
55+
let protocol_package_lock = get_file_lock(&colink_home, protocol_name)?;
56+
let protocol_package_lock = tokio::task::spawn_blocking(move || {
57+
protocol_package_lock.lock_exclusive().unwrap();
58+
protocol_package_lock
5959
})
6060
.await
6161
.unwrap();
62-
// use a closure to catch errors and unlock the lock after this closure
62+
// use a closure to prevent locking forever caused by errors
6363
let res = async {
6464
// read running instances in user storage
6565
let running_instances_key = format!("protocol_operator_groups:{}", protocol_name);
@@ -83,19 +83,19 @@ impl crate::server::MyService {
8383
}
8484
} else {
8585
return Err(Status::aborted(format!(
86-
"Protocol {} has running instances.",
86+
"Protocol {} has running instances and cannot be upgraded.",
8787
protocol_name
8888
)));
8989
}
9090
}
91-
// fetch protocol package from inventory if not exist
91+
// fetch protocol package from inventory if protocol package folder does not exist
9292
let colink_toml_path = protocol_package_dir.join("colink.toml");
9393
if std::fs::metadata(&colink_toml_path).is_err() {
9494
match fetch_protocol_from_inventory(protocol_name, &colink_home).await {
9595
Ok(_) => {}
9696
Err(err) => {
9797
return Err(Status::not_found(&format!(
98-
"protocol {} not found: {}",
98+
"protocol {} not found from inventory: {}",
9999
protocol_name, err
100100
)));
101101
}
@@ -191,7 +191,7 @@ impl crate::server::MyService {
191191
Ok::<(), Status>(())
192192
}
193193
.await;
194-
file_lock.unlock()?;
194+
protocol_package_lock.unlock()?;
195195
res?;
196196
Ok(Response::new(ProtocolOperatorInstanceId {
197197
instance_id: instance_id.to_string(),
@@ -243,10 +243,10 @@ impl crate::server::MyService {
243243
let protocol_name = String::from_utf8(protocol_name).unwrap();
244244
let running_instances_key = format!("protocol_operator_groups:{}", protocol_name);
245245
let colink_home = self.get_colink_home()?;
246-
let file_lock = get_file_lock(&colink_home, &protocol_name)?;
247-
let file_lock = tokio::task::spawn_blocking(move || {
248-
file_lock.lock_exclusive().unwrap();
249-
file_lock
246+
let protocol_package_lock = get_file_lock(&colink_home, &protocol_name)?;
247+
let protocol_package_lock = tokio::task::spawn_blocking(move || {
248+
protocol_package_lock.lock_exclusive().unwrap();
249+
protocol_package_lock
250250
})
251251
.await
252252
.unwrap();
@@ -267,7 +267,7 @@ impl crate::server::MyService {
267267
Ok::<(), Status>(())
268268
}
269269
.await;
270-
file_lock.unlock()?;
270+
protocol_package_lock.unlock()?;
271271
res?;
272272
Ok(Response::new(Empty::default()))
273273
}

src/storage/basic.rs

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,20 @@ impl crate::storage::common::Storage for BasicStorage {
3333
return Err(format!("Key name already exists: {}", user_id_key_name));
3434
}
3535
} else {
36-
_update_dir(&mut maps, user_id, key_name);
36+
_maintain_prefixes_along_path(&mut maps, user_id, key_name);
3737
}
3838
maps.key_path_to_value
3939
.insert(key_path_created.clone(), value.to_vec());
4040
maps.key_name_to_timestamp
4141
.insert(user_id_key_name, timestamp);
42-
let prefix = get_prefix(key_name);
43-
let keys_directory = format!("{}::{}", user_id, prefix);
44-
let contains_key = maps.prefix_to_key_paths.contains_key(&keys_directory);
45-
if contains_key {
46-
let key_list_set = maps.prefix_to_key_paths.get_mut(&keys_directory).unwrap();
47-
key_list_set.0.insert(key_path_created.clone());
48-
key_list_set.1.insert(key_path_created.clone());
49-
} else {
50-
let mut key_list_set = (HashSet::new(), HashSet::new());
51-
key_list_set.0.insert(key_path_created.clone());
52-
key_list_set.1.insert(key_path_created.clone());
53-
maps.prefix_to_key_paths
54-
.insert(keys_directory, key_list_set);
55-
}
42+
let key_name_prefix = get_prefix(key_name);
43+
let prefix = format!("{}::{}", user_id, key_name_prefix);
44+
let prefix_set = maps
45+
.prefix_to_key_paths
46+
.entry(prefix)
47+
.or_insert((HashSet::new(), HashSet::new()));
48+
prefix_set.0.insert(key_path_created.clone());
49+
prefix_set.1.insert(key_path_created.clone());
5650
Ok(key_path_created)
5751
}
5852

@@ -102,11 +96,11 @@ impl crate::storage::common::Storage for BasicStorage {
10296
let res = maps.prefix_to_key_paths.get(&format!("{}:", prefix));
10397
Ok(match res {
10498
None => vec![],
105-
Some(key_list_set) => {
99+
Some(prefix_set) => {
106100
if include_history {
107-
Vec::from_iter(key_list_set.1.clone())
101+
Vec::from_iter(prefix_set.1.clone())
108102
} else {
109-
Vec::from_iter(key_list_set.0.clone())
103+
Vec::from_iter(prefix_set.0.clone())
110104
}
111105
}
112106
})
@@ -124,31 +118,25 @@ impl crate::storage::common::Storage for BasicStorage {
124118
.insert(user_id_key_name.clone(), timestamp)
125119
.unwrap_or(0_i64);
126120
if old_timestamp == 0 {
127-
_update_dir(&mut maps, user_id, key_name);
121+
_maintain_prefixes_along_path(&mut maps, user_id, key_name);
128122
}
129-
let prefix = get_prefix(key_name);
130-
let keys_directory = format!("{}::{}", user_id, prefix);
131-
let contains_key = maps.prefix_to_key_paths.contains_key(&keys_directory);
132-
if contains_key {
133-
let key_list_set = maps.prefix_to_key_paths.get_mut(&keys_directory).unwrap();
134-
if old_timestamp != 0
135-
&& key_list_set
136-
.0
137-
.contains(&format!("{}@{}", user_id_key_name, old_timestamp))
138-
{
139-
key_list_set
140-
.0
141-
.remove(&format!("{}@{}", user_id_key_name, old_timestamp));
142-
}
143-
key_list_set.0.insert(key_path_created.clone());
144-
key_list_set.1.insert(key_path_created.clone());
145-
} else {
146-
let mut key_list_set = (HashSet::new(), HashSet::new());
147-
key_list_set.0.insert(key_path_created.clone());
148-
key_list_set.1.insert(key_path_created.clone());
149-
maps.prefix_to_key_paths
150-
.insert(keys_directory, key_list_set);
123+
let key_name_prefix = get_prefix(key_name);
124+
let prefix = format!("{}::{}", user_id, key_name_prefix);
125+
let prefix_set = maps
126+
.prefix_to_key_paths
127+
.entry(prefix)
128+
.or_insert((HashSet::new(), HashSet::new()));
129+
if old_timestamp != 0
130+
&& prefix_set
131+
.0
132+
.contains(&format!("{}@{}", user_id_key_name, old_timestamp))
133+
{
134+
prefix_set
135+
.0
136+
.remove(&format!("{}@{}", user_id_key_name, old_timestamp));
151137
}
138+
prefix_set.0.insert(key_path_created.clone());
139+
prefix_set.1.insert(key_path_created.clone());
152140
Ok(key_path_created)
153141
}
154142

@@ -167,17 +155,16 @@ impl crate::storage::common::Storage for BasicStorage {
167155
.key_name_to_timestamp
168156
.insert(user_id_key_name.clone(), timestamp)
169157
.unwrap_or(0_i64);
170-
let prefix = get_prefix(key_name);
171-
let keys_directory = format!("{}::{}", user_id, prefix);
172-
let contains_key = maps.prefix_to_key_paths.contains_key(&keys_directory);
173-
if contains_key {
174-
let key_list_set = maps.prefix_to_key_paths.get_mut(&keys_directory).unwrap();
158+
let key_name_prefix = get_prefix(key_name);
159+
let prefix = format!("{}::{}", user_id, key_name_prefix);
160+
if maps.prefix_to_key_paths.contains_key(&prefix) {
161+
let prefix_set = maps.prefix_to_key_paths.get_mut(&prefix).unwrap();
175162
if old_timestamp != 0
176-
&& key_list_set
163+
&& prefix_set
177164
.0
178165
.contains(&format!("{}@{}", user_id_key_name, old_timestamp))
179166
{
180-
key_list_set
167+
prefix_set
181168
.0
182169
.remove(&format!("{}@{}", user_id_key_name, old_timestamp));
183170
}
@@ -189,24 +176,18 @@ impl crate::storage::common::Storage for BasicStorage {
189176
}
190177
}
191178

192-
fn _update_dir(maps: &mut StorageMap, user_id: &str, key_name: &str) {
193-
let split: Vec<&str> = key_name.split(':').collect();
194-
let mut path1 = user_id.to_string() + "::";
195-
let mut path2 = user_id.to_string() + "::" + split[0];
196-
for i in 0..split.len() - 1 {
197-
let contains_key = maps.prefix_to_key_paths.contains_key(&path1);
198-
let value = path2.clone() + "@0";
199-
if contains_key {
200-
let key_list_set = maps.prefix_to_key_paths.get_mut(&path1).unwrap();
201-
key_list_set.0.insert(value.clone());
202-
key_list_set.1.insert(value);
203-
} else {
204-
let mut key_list_set = (HashSet::new(), HashSet::new());
205-
key_list_set.0.insert(value.clone());
206-
key_list_set.1.insert(value);
207-
maps.prefix_to_key_paths.insert(path1, key_list_set);
208-
}
209-
path1 = path2.clone() + ":";
210-
path2 = path2 + ":" + split[i + 1];
179+
fn _maintain_prefixes_along_path(maps: &mut StorageMap, user_id: &str, key_name: &str) {
180+
let splits: Vec<&str> = key_name.split(':').collect();
181+
let mut current_path = user_id.to_string() + "::";
182+
let mut next_path = user_id.to_string() + "::" + splits[0];
183+
for i in 0..splits.len() - 1 {
184+
let current_set = maps
185+
.prefix_to_key_paths
186+
.entry(current_path)
187+
.or_insert((HashSet::new(), HashSet::new()));
188+
current_set.0.insert(next_path.clone() + "@0");
189+
current_set.1.insert(next_path.clone() + "@0");
190+
current_path = next_path.clone() + ":";
191+
next_path = next_path + ":" + splits[i + 1];
211192
}
212193
}

0 commit comments

Comments
 (0)