2018-03-09 12:07:45 +00:00
|
|
|
// Copyright 2018 Mozilla
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
|
|
|
|
use std::sync::{
|
|
|
|
Arc,
|
2018-03-13 11:25:33 +00:00
|
|
|
Weak,
|
|
|
|
};
|
2018-03-19 17:09:38 +00:00
|
|
|
|
2018-03-13 11:25:33 +00:00
|
|
|
use std::sync::mpsc::{
|
|
|
|
channel,
|
|
|
|
Receiver,
|
|
|
|
RecvError,
|
|
|
|
Sender,
|
2018-03-09 12:07:45 +00:00
|
|
|
};
|
2018-03-19 17:09:38 +00:00
|
|
|
|
2018-03-09 12:07:45 +00:00
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
use indexmap::{
|
|
|
|
IndexMap,
|
|
|
|
};
|
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
use mentat_core::{
|
|
|
|
Entid,
|
|
|
|
Schema,
|
|
|
|
TypedValue,
|
|
|
|
};
|
|
|
|
|
|
|
|
use mentat_tx::entities::{
|
|
|
|
OpType,
|
|
|
|
};
|
|
|
|
|
|
|
|
use errors::{
|
|
|
|
Result,
|
2018-03-13 11:25:33 +00:00
|
|
|
};
|
|
|
|
|
2018-03-09 12:07:45 +00:00
|
|
|
use types::{
|
|
|
|
AttributeSet,
|
|
|
|
};
|
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
use watcher::TransactWatcher;
|
|
|
|
|
2018-03-09 12:07:45 +00:00
|
|
|
pub struct TxObserver {
|
2018-03-19 17:09:38 +00:00
|
|
|
notify_fn: Arc<Box<Fn(&str, IndexMap<&Entid, &AttributeSet>) + Send + Sync>>,
|
2018-03-09 12:07:45 +00:00
|
|
|
attributes: AttributeSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TxObserver {
|
2018-03-19 17:09:38 +00:00
|
|
|
pub fn new<F>(attributes: AttributeSet, notify_fn: F) -> TxObserver where F: Fn(&str, IndexMap<&Entid, &AttributeSet>) + 'static + Send + Sync {
|
2018-03-09 12:07:45 +00:00
|
|
|
TxObserver {
|
2018-03-13 11:25:33 +00:00
|
|
|
notify_fn: Arc::new(Box::new(notify_fn)),
|
2018-03-09 12:07:45 +00:00
|
|
|
attributes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
pub fn applicable_reports<'r>(&self, reports: &'r IndexMap<Entid, AttributeSet>) -> IndexMap<&'r Entid, &'r AttributeSet> {
|
|
|
|
reports.into_iter()
|
|
|
|
.filter(|&(_txid, attrs)| !self.attributes.is_disjoint(attrs))
|
|
|
|
.collect()
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
fn notify(&self, key: &str, reports: IndexMap<&Entid, &AttributeSet>) {
|
2018-03-13 11:25:33 +00:00
|
|
|
(*self.notify_fn)(key, reports);
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 11:25:33 +00:00
|
|
|
pub trait Command {
|
|
|
|
fn execute(&mut self);
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 11:25:33 +00:00
|
|
|
pub struct TxCommand {
|
2018-03-19 17:09:38 +00:00
|
|
|
reports: IndexMap<Entid, AttributeSet>,
|
2018-03-13 11:25:33 +00:00
|
|
|
observers: Weak<IndexMap<String, Arc<TxObserver>>>,
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 11:25:33 +00:00
|
|
|
impl TxCommand {
|
2018-03-19 17:09:38 +00:00
|
|
|
fn new(observers: &Arc<IndexMap<String, Arc<TxObserver>>>, reports: IndexMap<Entid, AttributeSet>) -> Self {
|
2018-03-13 11:25:33 +00:00
|
|
|
TxCommand {
|
2018-03-09 12:07:45 +00:00
|
|
|
reports,
|
2018-03-13 11:25:33 +00:00
|
|
|
observers: Arc::downgrade(observers),
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 11:25:33 +00:00
|
|
|
impl Command for TxCommand {
|
|
|
|
fn execute(&mut self) {
|
|
|
|
self.observers.upgrade().map(|observers| {
|
|
|
|
for (key, observer) in observers.iter() {
|
|
|
|
let applicable_reports = observer.applicable_reports(&self.reports);
|
|
|
|
if !applicable_reports.is_empty() {
|
|
|
|
observer.notify(&key, applicable_reports);
|
|
|
|
}
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TxObservationService {
|
2018-03-13 11:25:33 +00:00
|
|
|
observers: Arc<IndexMap<String, Arc<TxObserver>>>,
|
|
|
|
executor: Option<Sender<Box<Command + Send>>>,
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TxObservationService {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
TxObservationService {
|
2018-03-13 11:25:33 +00:00
|
|
|
observers: Arc::new(IndexMap::new()),
|
|
|
|
executor: None,
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-13 11:25:33 +00:00
|
|
|
|
2018-03-09 12:07:45 +00:00
|
|
|
// For testing purposes
|
|
|
|
pub fn is_registered(&self, key: &String) -> bool {
|
|
|
|
self.observers.contains_key(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register(&mut self, key: String, observer: Arc<TxObserver>) {
|
2018-03-13 11:25:33 +00:00
|
|
|
Arc::make_mut(&mut self.observers).insert(key, observer);
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deregister(&mut self, key: &String) {
|
2018-03-13 11:25:33 +00:00
|
|
|
Arc::make_mut(&mut self.observers).remove(key);
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_observers(&self) -> bool {
|
|
|
|
!self.observers.is_empty()
|
|
|
|
}
|
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
pub fn in_progress_did_commit(&mut self, txes: IndexMap<Entid, AttributeSet>) {
|
2018-04-03 16:18:22 +00:00
|
|
|
// Don't spawn a thread only to say nothing.
|
|
|
|
if !self.has_observers() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
let executor = self.executor.get_or_insert_with(|| {
|
|
|
|
let (tx, rx): (Sender<Box<Command + Send>>, Receiver<Box<Command + Send>>) = channel();
|
|
|
|
let mut worker = CommandExecutor::new(rx);
|
2018-03-09 12:07:45 +00:00
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
thread::spawn(move || {
|
|
|
|
worker.main();
|
|
|
|
});
|
2018-03-13 11:25:33 +00:00
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
tx
|
|
|
|
});
|
2018-03-13 11:25:33 +00:00
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
let cmd = Box::new(TxCommand::new(&self.observers, txes));
|
|
|
|
executor.send(cmd).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 11:25:33 +00:00
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
impl Drop for TxObservationService {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.executor = None;
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 11:25:33 +00:00
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
pub struct InProgressObserverTransactWatcher {
|
|
|
|
collected_attributes: AttributeSet,
|
|
|
|
pub txes: IndexMap<Entid, AttributeSet>,
|
|
|
|
}
|
2018-03-13 11:25:33 +00:00
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
impl InProgressObserverTransactWatcher {
|
|
|
|
pub fn new() -> InProgressObserverTransactWatcher {
|
|
|
|
InProgressObserverTransactWatcher {
|
|
|
|
collected_attributes: Default::default(),
|
|
|
|
txes: Default::default(),
|
2018-03-13 11:25:33 +00:00
|
|
|
}
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
2018-03-13 11:25:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 17:09:38 +00:00
|
|
|
impl TransactWatcher for InProgressObserverTransactWatcher {
|
|
|
|
fn datom(&mut self, _op: OpType, _e: Entid, a: Entid, _v: &TypedValue) {
|
|
|
|
self.collected_attributes.insert(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn done(&mut self, t: &Entid, _schema: &Schema) -> Result<()> {
|
|
|
|
let collected_attributes = ::std::mem::replace(&mut self.collected_attributes, Default::default());
|
|
|
|
self.txes.insert(*t, collected_attributes);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 11:25:33 +00:00
|
|
|
struct CommandExecutor {
|
2018-03-19 17:09:38 +00:00
|
|
|
receiver: Receiver<Box<Command + Send>>,
|
2018-03-13 11:25:33 +00:00
|
|
|
}
|
2018-03-09 12:07:45 +00:00
|
|
|
|
2018-03-13 11:25:33 +00:00
|
|
|
impl CommandExecutor {
|
|
|
|
fn new(rx: Receiver<Box<Command + Send>>) -> Self {
|
|
|
|
CommandExecutor {
|
2018-03-19 17:09:38 +00:00
|
|
|
receiver: rx,
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
2018-03-13 11:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main(&mut self) {
|
|
|
|
loop {
|
2018-03-19 17:09:38 +00:00
|
|
|
match self.receiver.recv() {
|
2018-03-13 11:25:33 +00:00
|
|
|
Err(RecvError) => {
|
2018-04-03 16:18:22 +00:00
|
|
|
// "The recv operation can only fail if the sending half of a channel (or
|
|
|
|
// sync_channel) is disconnected, implying that no further messages will ever be
|
|
|
|
// received."
|
|
|
|
// No need to log here.
|
2018-03-13 11:25:33 +00:00
|
|
|
return
|
|
|
|
},
|
2018-03-09 12:07:45 +00:00
|
|
|
|
2018-03-13 11:25:33 +00:00
|
|
|
Ok(mut cmd) => {
|
|
|
|
cmd.execute()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 12:07:45 +00:00
|
|
|
}
|
|
|
|
}
|