Skip to content

Commit 3adaf18

Browse files
committed
trying out Mutex instead of Cell
1 parent cb4a9f8 commit 3adaf18

File tree

3 files changed

+53
-42
lines changed

3 files changed

+53
-42
lines changed

packages/storage/src/append_store.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! This is achieved by storing each item in a separate storage entry. A special key is reserved
55
//! for storing the length of the collection so far.
66
use std::any::type_name;
7-
use std::cell::Cell;
7+
use std::sync::Mutex;
88
use std::{convert::TryInto};
99
use std::marker::PhantomData;
1010

@@ -26,7 +26,7 @@ pub struct AppendStore<'a, T, Ser = Bincode2>
2626
/// needed if any suffixes were added to the original namespace.
2727
/// therefore it is not necessarily same as the namespace.
2828
prefix: Option<Vec<u8>>,
29-
length: Cell<Option<u32>>,
29+
length: Mutex<Option<u32>>,
3030
item_type: PhantomData<T>,
3131
serialization_type: PhantomData<Ser>,
3232
}
@@ -37,7 +37,7 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser>{
3737
Self {
3838
namespace: prefix,
3939
prefix: None,
40-
length: Cell::new(None),
40+
length: Mutex::new(None),
4141
item_type: PhantomData,
4242
serialization_type: PhantomData,
4343
}
@@ -53,7 +53,7 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser>{
5353
Self {
5454
namespace: self.namespace,
5555
prefix: Some(prefix),
56-
length: Cell::new(None),
56+
length: Mutex::new(None),
5757
item_type: self.item_type,
5858
serialization_type: self.serialization_type,
5959
}
@@ -63,17 +63,18 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser>{
6363
impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
6464
/// gets the length from storage, and otherwise sets it to 0
6565
pub fn get_len<S: ReadonlyStorage>(&self, storage: &S) -> StdResult<u32> {
66-
match self.length.get() {
66+
let mut may_len = self.length.lock().unwrap();
67+
match *may_len {
6768
Some(len) => { Ok(len) },
6869
None => {
6970
let len_key = [self.as_slice(), LEN_KEY].concat();
7071
if let Some(len_vec) = storage.get(&len_key) {
7172
let len_bytes = len_vec.as_slice().try_into().map_err(|err| StdError::parse_err("u32", err))?;
7273
let len = u32::from_be_bytes(len_bytes);
73-
self.length.set(Some(len));
74+
*may_len = Some(len);
7475
Ok(len)
7576
} else {
76-
self.length.set(Some(0));
77+
*may_len = Some(0);
7778
Ok(0)
7879
}
7980
},
@@ -98,8 +99,10 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> AppendStore<'a, T, Ser> {
9899
/// Set the length of the collection
99100
fn set_len<S: Storage>(&self, storage: &mut S, len: u32) {
100101
let len_key = [self.as_slice(), LEN_KEY].concat();
101-
self.length.set(Some(len));
102102
storage.set(&len_key, &len.to_be_bytes());
103+
104+
let mut may_len = self.length.lock().unwrap();
105+
*may_len = Some(len);
103106
}
104107
/// Clear the collection
105108
pub fn clear<S: Storage>(&self, storage: &mut S) {
@@ -342,29 +345,29 @@ mod tests {
342345
let mut storage = MockStorage::new();
343346
let append_store: AppendStore<i32> = AppendStore::new(b"test");
344347

345-
assert_eq!(append_store.length, Cell::new(None));
348+
assert!(append_store.length.lock().unwrap().eq(&None));
346349
assert_eq!(append_store.get_len(&mut storage)?, 0);
347-
assert_eq!(append_store.length, Cell::new(Some(0)));
350+
assert!(append_store.length.lock().unwrap().eq(&Some(0)));
348351

349352
append_store.push(&mut storage, &1234)?;
350353
append_store.push(&mut storage, &2143)?;
351354
append_store.push(&mut storage, &3412)?;
352355
append_store.push(&mut storage, &4321)?;
353-
assert_eq!(append_store.length, Cell::new(Some(4)));
356+
assert!(append_store.length.lock().unwrap().eq(&Some(4)));
354357
assert_eq!(append_store.get_len(&mut storage)?, 4);
355358

356359
assert_eq!(append_store.pop(&mut storage), Ok(4321));
357360
assert_eq!(append_store.pop(&mut storage), Ok(3412));
358-
assert_eq!(append_store.length, Cell::new(Some(2)));
361+
assert!(append_store.length.lock().unwrap().eq(&Some(2)));
359362
assert_eq!(append_store.get_len(&mut storage)?, 2);
360363

361364
assert_eq!(append_store.pop(&mut storage), Ok(2143));
362365
assert_eq!(append_store.pop(&mut storage), Ok(1234));
363-
assert_eq!(append_store.length, Cell::new(Some(0)));
366+
assert!(append_store.length.lock().unwrap().eq(&Some(0)));
364367
assert_eq!(append_store.get_len(&mut storage)?, 0);
365368

366369
assert!(append_store.pop(&mut storage).is_err());
367-
assert_eq!(append_store.length, Cell::new(Some(0)));
370+
assert!(append_store.length.lock().unwrap().eq(&Some(0)));
368371
assert_eq!(append_store.get_len(&mut storage)?, 0);
369372

370373
Ok(())

packages/storage/src/deque_store.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! A special key is reserved for storing the length of the collection so far.
66
//! Another special key is reserved for storing the offset of the collection.
77
use std::any::type_name;
8-
use std::cell::Cell;
8+
use std::sync::Mutex;
99
use std::{convert::TryInto};
1010
use std::marker::PhantomData;
1111

@@ -28,8 +28,8 @@ pub struct DequeStore<'a, T, Ser = Bincode2>
2828
/// needed if any suffixes were added to the original namespace.
2929
/// therefore it is not necessarily same as the namespace.
3030
prefix: Option<Vec<u8>>,
31-
length: Cell<Option<u32>>,
32-
offset: Cell<Option<u32>>,
31+
length: Mutex<Option<u32>>,
32+
offset: Mutex<Option<u32>>,
3333
item_type: PhantomData<T>,
3434
serialization_type: PhantomData<Ser>,
3535
}
@@ -40,8 +40,8 @@ impl<'a, 'b, T: Serialize + DeserializeOwned, Ser: Serde> DequeStore<'a, T, Ser>
4040
Self {
4141
namespace: prefix,
4242
prefix: None,
43-
length: Cell::new(None),
44-
offset: Cell::new(None),
43+
length: Mutex::new(None),
44+
offset: Mutex::new(None),
4545
item_type: PhantomData,
4646
serialization_type: PhantomData,
4747
}
@@ -57,8 +57,8 @@ impl<'a, 'b, T: Serialize + DeserializeOwned, Ser: Serde> DequeStore<'a, T, Ser>
5757
Self {
5858
namespace: self.namespace,
5959
prefix: Some(prefix),
60-
length: Cell::new(None),
61-
offset: Cell::new(None),
60+
length: Mutex::new(None),
61+
offset: Mutex::new(None),
6262
item_type: self.item_type,
6363
serialization_type: self.serialization_type,
6464
}
@@ -68,12 +68,13 @@ impl<'a, 'b, T: Serialize + DeserializeOwned, Ser: Serde> DequeStore<'a, T, Ser>
6868
impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> DequeStore<'a, T, Ser> {
6969
/// gets the length from storage, and otherwise sets it to 0
7070
pub fn get_len<S: ReadonlyStorage>(&self, storage: &S) -> StdResult<u32> {
71-
match self.length.get() {
71+
let mut may_len = self.length.lock().unwrap();
72+
match *may_len {
7273
Some(len) => { Ok(len) },
7374
None => {
7475
match self._get_u32(storage, LEN_KEY) {
7576
Ok(len) => {
76-
self.length.set(Some(len));
77+
*may_len = Some(len);
7778
Ok(len)
7879
},
7980
Err(e) => { Err(e) },
@@ -83,12 +84,13 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> DequeStore<'a, T, Ser> {
8384
}
8485
/// gets the offset from storage, and otherwise sets it to 0
8586
pub fn get_off<S: ReadonlyStorage>(&self, storage: &S) -> StdResult<u32> {
86-
match self.offset.get() {
87+
let mut may_off = self.offset.lock().unwrap();
88+
match *may_off {
8789
Some(len) => { Ok(len) },
8890
None => {
8991
match self._get_u32(storage, OFFSET_KEY) {
9092
Ok(len) => {
91-
self.offset.set(Some(len));
93+
*may_off = Some(len);
9294
Ok(len)
9395
},
9496
Err(e) => { Err(e) },
@@ -128,12 +130,14 @@ impl<'a, T: Serialize + DeserializeOwned, Ser: Serde> DequeStore<'a, T, Ser> {
128130
}
129131
/// Set the length of the collection
130132
fn set_len<S: Storage>(&self, storage: &mut S, len: u32) {
131-
self.length.set(Some(len));
133+
let mut may_len = self.length.lock().unwrap();
134+
*may_len = Some(len);
132135
self._set_u32(storage, LEN_KEY, len)
133136
}
134137
/// Set the offset of the collection
135138
fn set_off<S: Storage>(&self, storage: &mut S, off: u32) {
136-
self.offset.set(Some(off));
139+
let mut may_off = self.offset.lock().unwrap();
140+
*may_off = Some(off);
137141
self._set_u32(storage, OFFSET_KEY, off)
138142
}
139143
/// Set the length or offset of the collection

packages/storage/src/keymap.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::any::type_name;
2-
use std::cell::{Cell};
2+
use std::sync::Mutex;
33
use std::{convert::TryInto};
44
use std::marker::PhantomData;
55

@@ -39,7 +39,7 @@ pub struct Keymap<'a, K, T, Ser = Bincode2>
3939
/// needed if any suffixes were added to the original namespace.
4040
/// therefore it is not necessarily same as the namespace.
4141
prefix: Option<Vec<u8>>,
42-
length: Cell<Option<u32>>,
42+
length: Mutex<Option<u32>>,
4343
key_type: PhantomData<K>,
4444
item_type: PhantomData<T>,
4545
serialization_type: PhantomData<Ser>,
@@ -51,7 +51,7 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser:
5151
Self {
5252
namespace: prefix,
5353
prefix: None,
54-
length: Cell::new(None),
54+
length: Mutex::new(None),
5555
key_type: PhantomData,
5656
item_type: PhantomData,
5757
serialization_type: PhantomData,
@@ -68,7 +68,7 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser:
6868
Self {
6969
namespace: self.namespace,
7070
prefix: Some(prefix),
71-
length: Cell::new(None),
71+
length: Mutex::new(None),
7272
key_type: self.key_type,
7373
item_type: self.item_type,
7474
serialization_type: self.serialization_type,
@@ -87,17 +87,18 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser:
8787
}
8888
/// get total number of objects saved
8989
pub fn get_len<S: ReadonlyStorage>(&self, storage: &S) -> StdResult<u32> {
90-
match self.length.get() {
90+
let mut may_len = self.length.lock().unwrap();
91+
match *may_len {
9192
Some(length) => { Ok(length) },
9293
None => {
9394
let len_key = [self.as_slice(), MAP_LENGTH].concat();
9495
if let Some(len_vec) = storage.get(&len_key) {
9596
let len_bytes = len_vec.as_slice().try_into().map_err(|err| StdError::parse_err("u32", err))?;
9697
let len = u32::from_be_bytes(len_bytes);
97-
self.length.set(Some(len));
98+
*may_len = Some(len);
9899
Ok(len)
99100
} else {
100-
self.length.set(Some(0));
101+
*may_len = Some(0);
101102
Ok(0)
102103
}
103104
},
@@ -111,7 +112,10 @@ impl<'a, K: Serialize + DeserializeOwned, T: Serialize + DeserializeOwned, Ser:
111112
fn set_len<S: Storage>(&self, storage: &mut S, len: u32) -> StdResult<()> {
112113
let len_key = [self.as_slice(), MAP_LENGTH].concat();
113114
storage.set(&len_key, &len.to_be_bytes());
114-
self.length.set(Some(len));
115+
116+
let mut may_len = self.length.lock().unwrap();
117+
*may_len = Some(len);
118+
115119
Ok(())
116120
}
117121
/// Used to get the indexes stored in the given page number
@@ -1262,36 +1266,36 @@ mod tests {
12621266
number: 1111,
12631267
};
12641268

1265-
assert_eq!(keymap.length, Cell::new(None));
1269+
assert!(keymap.length.lock().unwrap().eq(&None));
12661270
assert_eq!(keymap.get_len(&storage)?, 0);
1267-
assert_eq!(keymap.length, Cell::new(Some(0)));
1271+
assert!(keymap.length.lock().unwrap().eq(&Some(0)));
12681272

12691273
let key1 = "k1".to_string();
12701274
let key2 = "k2".to_string();
12711275

12721276
keymap.insert(&mut storage, &key1, foo1.clone())?;
12731277
assert_eq!(keymap.get_len(&storage)?, 1);
1274-
assert_eq!(keymap.length, Cell::new(Some(1)));
1278+
assert!(keymap.length.lock().unwrap().eq(&Some(1)));
12751279

12761280
// add another item
12771281
keymap.insert(&mut storage, &key2, foo2.clone())?;
12781282
assert_eq!(keymap.get_len(&storage)?, 2);
1279-
assert_eq!(keymap.length, Cell::new(Some(2)));
1283+
assert!(keymap.length.lock().unwrap().eq(&Some(2)));
12801284

12811285
// remove item and check length
12821286
keymap.remove(&mut storage, &key1)?;
12831287
assert_eq!(keymap.get_len(&storage)?, 1);
1284-
assert_eq!(keymap.length, Cell::new(Some(1)));
1288+
assert!(keymap.length.lock().unwrap().eq(&Some(1)));
12851289

12861290
// override item (should not change length)
12871291
keymap.insert(&mut storage, &key2, foo1)?;
12881292
assert_eq!(keymap.get_len(&storage)?, 1);
1289-
assert_eq!(keymap.length, Cell::new(Some(1)));
1293+
assert!(keymap.length.lock().unwrap().eq(&Some(1)));
12901294

12911295
// remove item and check length
12921296
keymap.remove(&mut storage, &key2)?;
12931297
assert_eq!(keymap.get_len(&storage)?, 0);
1294-
assert_eq!(keymap.length, Cell::new(Some(0)));
1298+
assert!(keymap.length.lock().unwrap().eq(&Some(0)));
12951299

12961300
Ok(())
12971301
}

0 commit comments

Comments
 (0)