Get or create the counter handle for `db_name`. The returned `Arc ` is stable for the process lifetime.
(&self, db_name: &str)
| 63 | /// |
| 64 | /// The returned `Arc<DatabaseCounters>` is stable for the process lifetime. |
| 65 | pub fn get_or_create(&self, db_name: &str) -> Arc<DatabaseCounters> { |
| 66 | // Fast path: read lock. |
| 67 | { |
| 68 | let r = self.counters.read().unwrap_or_else(|p| p.into_inner()); |
| 69 | if let Some(c) = r.get(db_name) { |
| 70 | return Arc::clone(c); |
| 71 | } |
| 72 | } |
| 73 | // Slow path: write lock. |
| 74 | let mut w = self.counters.write().unwrap_or_else(|p| p.into_inner()); |
| 75 | w.entry(db_name.to_string()) |
| 76 | .or_insert_with(|| Arc::new(DatabaseCounters::default())) |
| 77 | .clone() |
| 78 | } |
| 79 | |
| 80 | /// Increment the QPS counter for `db_name` by 1. |
| 81 | pub fn record_qps(&self, db_name: &str) { |