/
collection.rs
53 lines (43 loc) · 1.38 KB
/
collection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use crate::bucket::Bucket;
struct BucketCollectionInner {
buckets_byid: HashMap<Vec<u8>, Arc<Bucket>>,
}
pub struct BucketCollection {
inner: RwLock<BucketCollectionInner>,
}
impl BucketCollection {
pub fn new() -> BucketCollection {
BucketCollection {
inner: RwLock::new(BucketCollectionInner {
buckets_byid: HashMap::new(),
}),
}
}
pub fn add_bucket(&mut self, bucket: Bucket) -> Arc<Bucket> {
let bucket_arc = Arc::new(bucket);
let mut inner_lock = self.inner.write().unwrap();
let inner = &mut *inner_lock;
inner
.buckets_byid
.insert(bucket_arc.get_id(), bucket_arc.clone());
bucket_arc
}
pub fn get_bucket_byid(&self, bucket_id: &[u8]) -> Option<Arc<Bucket>> {
let inner_lock = self.inner.read().unwrap();
(*inner_lock).buckets_byid.get(bucket_id).cloned()
}
pub fn get_bucket_one(&self) -> Option<Arc<Bucket>> {
let inner_lock = self.inner.read().unwrap();
(*inner_lock)
.buckets_byid
.iter()
.next()
.map(|x| x.1)
.cloned()
}
// pub fn drop_bucket(&mut self, bucket: Arc<Bucket>) -> Option<Arc<Bucket>> {
// self.buckets_byid.remove(bucket.get_id().as_slice())
// }
}