mirror of
https://gitlab.com/famedly/conduit.git
synced 2025-06-27 16:35:59 +00:00
improvement: copy watcher deduplication from sqlite
This commit is contained in:
parent
c9070df6c0
commit
5f64e24a6e
1 changed files with 23 additions and 25 deletions
|
@ -1,11 +1,11 @@
|
||||||
use super::super::Config;
|
use super::super::Config;
|
||||||
use crate::{utils, Result};
|
use crate::{utils, Result};
|
||||||
|
|
||||||
use std::{future::Future, pin::Pin, sync::Arc};
|
use std::{future::Future, pin::Pin, sync::Arc};
|
||||||
|
use std::{
|
||||||
|
collections::{hash_map, HashMap},
|
||||||
|
sync::RwLock};
|
||||||
use super::{DatabaseEngine, Tree};
|
use super::{DatabaseEngine, Tree};
|
||||||
|
use tokio::sync::watch;
|
||||||
use std::{collections::HashMap, sync::RwLock};
|
|
||||||
|
|
||||||
pub struct Engine {
|
pub struct Engine {
|
||||||
rocks: rocksdb::DBWithThreadMode<rocksdb::MultiThreaded>,
|
rocks: rocksdb::DBWithThreadMode<rocksdb::MultiThreaded>,
|
||||||
|
@ -15,7 +15,7 @@ pub struct Engine {
|
||||||
pub struct RocksDbEngineTree<'a> {
|
pub struct RocksDbEngineTree<'a> {
|
||||||
db: Arc<Engine>,
|
db: Arc<Engine>,
|
||||||
name: &'a str,
|
name: &'a str,
|
||||||
watchers: RwLock<HashMap<Vec<u8>, Vec<tokio::sync::oneshot::Sender<()>>>>,
|
watchers: RwLock<HashMap<Vec<u8>, (watch::Sender<()>, watch::Receiver<()>)>>,
|
||||||
write_lock: RwLock<()>,
|
write_lock: RwLock<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,6 +102,12 @@ impl Tree for RocksDbEngineTree<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
||||||
|
let lock = self.write_lock.read().unwrap();
|
||||||
|
|
||||||
|
let result = self.db.rocks.put_cf(self.cf(), key, value)?;
|
||||||
|
|
||||||
|
drop(lock);
|
||||||
|
|
||||||
let watchers = self.watchers.read().unwrap();
|
let watchers = self.watchers.read().unwrap();
|
||||||
let mut triggered = Vec::new();
|
let mut triggered = Vec::new();
|
||||||
|
|
||||||
|
@ -116,19 +122,11 @@ impl Tree for RocksDbEngineTree<'_> {
|
||||||
if !triggered.is_empty() {
|
if !triggered.is_empty() {
|
||||||
let mut watchers = self.watchers.write().unwrap();
|
let mut watchers = self.watchers.write().unwrap();
|
||||||
for prefix in triggered {
|
for prefix in triggered {
|
||||||
if let Some(txs) = watchers.remove(prefix) {
|
if let Some(tx) = watchers.remove(prefix) {
|
||||||
for tx in txs {
|
let _ = tx.0.send(());
|
||||||
let _ = tx.send(());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
let lock = self.write_lock.read().unwrap();
|
|
||||||
|
|
||||||
let result = self.db.rocks.put_cf(self.cf(), key, value)?;
|
|
||||||
|
|
||||||
drop(lock);
|
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
@ -219,18 +217,18 @@ impl Tree for RocksDbEngineTree<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
|
||||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
let mut rx = match self.watchers.write().unwrap().entry(prefix.to_vec()) {
|
||||||
|
hash_map::Entry::Occupied(o) => o.get().1.clone(),
|
||||||
self.watchers
|
hash_map::Entry::Vacant(v) => {
|
||||||
.write()
|
let (tx, rx) = tokio::sync::watch::channel(());
|
||||||
.unwrap()
|
v.insert((tx, rx.clone()));
|
||||||
.entry(prefix.to_vec())
|
rx
|
||||||
.or_default()
|
}
|
||||||
.push(tx);
|
};
|
||||||
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
// Tx is never destroyed
|
// Tx is never destroyed
|
||||||
rx.await.unwrap();
|
rx.changed().await.unwrap();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue