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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
mod manager; // mod subject_graph; // mod topo_subject_head_iter; use slab::*; use subject::*; use memorefhead::MemoRefHead; use error::RetrieveError; use index::IndexFixed; use self::manager::ContextManager; use std::ops::Deref; use std::fmt; use std::collections::HashMap; use std::sync::{Mutex, RwLock, Arc, Weak}; #[derive(Clone)] pub struct Context(pub Arc<ContextInner>); impl Deref for Context { type Target = ContextInner; fn deref(&self) -> &ContextInner { &*self.0 } } pub struct ContextInner { pub slab: Slab, pub root_index: RwLock<Option<IndexFixed>>, /// For compaction of the subject_heads manager: Mutex<ContextManager>, /// For active subjects / subject subscription management subjects: RwLock<HashMap<SubjectId, WeakSubject>>, } #[derive(Clone)] pub struct WeakContext(Weak<ContextInner>); #[derive(Clone)] pub enum ContextRef { Weak(WeakContext), Strong(Context), } impl ContextRef { pub fn get_context<'a>(&'a self) -> Context { match self { &ContextRef::Weak(ref c) => { c.upgrade().expect("Sanity error. Weak context has been dropped") } &ContextRef::Strong(ref c) => c.clone(), } } } impl Context { pub fn new(slab: &Slab) -> Context { let new_self = Context(Arc::new(ContextInner { slab: slab.clone(), root_index: RwLock::new(None), manager: Mutex::new(ContextManager::new()), subjects: RwLock::new(HashMap::new()), })); // Typically subjects, and the indexes that use them, have a hard link to their originating // contexts. This is useful because we want to make sure the context (and associated slab) // stick around until we're done with them // The root index is a bit of a special case however, because the context needs to have a hard link to it, // as it must use the index directly. Therefore I need to make sure it doesn't have a hard link back to me. // This shouldn't be a problem, because the index is private, and not subject to direct use, so the context // should outlive it. let seed = slab.get_root_index_seed().expect("Uninitialized slab"); let index = IndexFixed::new_from_memorefhead(ContextRef::Weak(new_self.weak()), 5, seed); *new_self.root_index.write().unwrap() = Some(index); new_self } pub fn insert_into_root_index(&self, subject_id: SubjectId, subject: &Subject) { if let Some(ref index) = *self.root_index.write().unwrap() { index.insert(subject_id, subject); } else { panic!("no root index") } } // Add MemoRefs to this context // // pub fn add (&self, mut memorefs: Vec<MemoRef>) { // for memoref in memorefs.drain(..) { // if let Some(subject_id) = memoref.subject_id { // let relation_links = // let mut manager = self.manager.write().unwrap(); // manager.set_subject_head(subject_id, &memoref, &self.slab); // } // } // } // /// Retrieves a subject by ID from this context only if it is currently resedent fn get_subject_if_resident(&self, subject_id: SubjectId) -> Option<Subject> { if let Some(weaksub) = self.subjects.read().unwrap().get(&subject_id) { if let Some(subject) = weaksub.upgrade() { // NOTE: In theory we shouldn't need to apply the current context // to this subject, as it shouldddd have already happened return Some(subject); } } None } /// Retrive a Subject from the root index by ID pub fn get_subject_by_id(&self, subject_id: SubjectId) -> Result<Subject, RetrieveError> { match *self.root_index.read().unwrap() { Some(ref index) => index.get(subject_id), None => Err(RetrieveError::IndexNotInitialized), } } /// Retrieve a subject for a known MemoRefHead – ususally used for relationship traversal. /// Any relevant context will also be applied when reconstituting the relevant subject to ensure that our consistency model invariants are met pub fn get_subject_with_head(&self, subject_id: SubjectId, mut head: MemoRefHead) -> Result<Subject, RetrieveError> { // println!("# Context.get_subject_with_head({},{:?})", subject_id, head.memo_ids() ); if head.len() == 0 { return Err(RetrieveError::InvalidMemoRefHead); } let maybe_head = { // Don't want to hold the lock while calling head.apply, as it could request a memo from a remote slab, and we'd deadlock if let Some(ref head) = self.manager.lock().unwrap().get_head(subject_id) { Some((*head).clone()) } else { None } }; if let Some(relevant_context_head) = maybe_head { // println!("# \\ Relevant context head is ({:?})", relevant_context_head.memo_ids() ); head.apply(&relevant_context_head, &self.slab); } else { // println!("# \\ No relevant head found in context"); } match self.get_subject_if_resident(subject_id) { Some(ref mut subject) => { subject.apply_head(&head); return Ok(subject.clone()); } None => {} } // NOTE: Subject::reconstitute calls back to Context.subscribe_subject() // so we need to release the mutex prior to this let subject = Subject::reconstitute(ContextRef::Strong(self.clone()), head); return Ok(subject); } /// Subscribes a resident subject struct to relevant updates from this context /// Used by the subject constructor pub fn subscribe_subject(&self, subject: &Subject) { // println!("Context.subscribe_subject({})", subject.id ); { self.subjects.write().unwrap().insert(subject.id, subject.weak()); } self.slab.subscribe_subject(subject.id, self); } /// Unsubscribes the subject from further updates. Used by Subject.drop /// ( Temporarily defeated due to deadlocks. TODO ) pub fn unsubscribe_subject(&self, subject_id: SubjectId) { // println!("# Context.unsubscribe_subject({})", subject_id); // let _ = subject_id; self.subjects.write().unwrap().remove(&subject_id); // BUG/TODO: Temporarily disabled unsubscription // 1. Because it was causing deadlocks on the context AND slab mutexes // when the thread in the test case happened to drop the subject // when we were busy doing apply_subject_head, which locks context, // and is called by slab – so clearly this is untenable // 2. It was always sort of a hack that the subject was managing subscriptions // in this way anyways. Lets put together a more final version of the subscriptions // before we bother with fixing unsubscription // // { // let mut shared = self.inner.shared.lock().unwrap(); // shared.subjects.remove( &subject_id ); // } // // self.inner.slab.unsubscribe_subject(subject_id, self); // println!("# Context.unsubscribe_subject({}) - FINISHED", subject_id); // } /// Called by the Slab whenever memos matching one of our subscriptions comes in, or by the Subject when an edit is made pub fn apply_subject_head(&self, subject_id: SubjectId, apply_head: &MemoRefHead, notify_subject: bool) { // println!("Context.apply_subject_head({}, {:?}) ", subject_id, head.memo_ids() ); // NOTE: In all liklihood, there is significant room to optimize this. // We're applying heads to heads redundantly // QUESTION: Should we be updating our query context here? // not sure if this should happen implicitly or require explicit context exchange // I think there's a pretty strong argument for implicit, but I want to think // about this a bit more before I say yes for certain. // // ANSWER: It occurs to me that we're only getting subject heads from the slab which we expressly // subscribed to, so this strengthens the case quite a bit { let head: MemoRefHead = if let Some(mut head) = { self.manager.lock().unwrap().get_head(subject_id) } { head.apply(apply_head, &self.slab); head.clone() } else { apply_head.clone() }; let relation_links = head.project_all_relation_links(&self.slab); { self.manager .lock() .unwrap() .set_subject_head(subject_id, relation_links, head.clone()); } if notify_subject { if let Some(ref subject) = self.get_subject_if_resident(subject_id) { subject.apply_head(&head); } } } } // Magically transport subject heads into another context in the same process. // This is a temporary hack for testing purposes until such time as proper context exchange is enabled // QUESTION: should context exchanges be happening constantly, but often ignored? or requested? Probably the former, // sent based on an interval and/or compaction ( which would also likely be based on an interval and/or present context size) pub fn hack_send_context(&self, other: &Self) -> usize { self.compress(); let manager = self.manager.lock().unwrap(); let from_slabref = self.slab.my_ref.clone_for_slab(&other.slab); let mut memoref_count = 0; for subject_head in manager.subject_head_iter() { memoref_count += subject_head.head.len(); other.apply_subject_head(subject_head.subject_id, &subject_head.head .clone_for_slab(&from_slabref, &other.slab, false), true); // HACK inside a hack - manually updating the remote subject is cheating, but necessary for now because subjects // have a separate MRH versus the context } memoref_count } pub fn get_subject_head(&self, subject_id: SubjectId) -> Option<MemoRefHead> { if let Some(ref head) = self.manager.lock().unwrap().get_head(subject_id) { Some((*head).clone()) } else { None } } pub fn get_subject_head_memo_ids(&self, subject_id: SubjectId) -> Vec<MemoId> { if let Some(head) = self.get_subject_head(subject_id) { head.memo_ids() } else { vec![] } } pub fn cmp(&self, other: &Self) -> bool { // stable way: &*(self.0) as *const _ != &*(other.0) as *const _ // unstable way: // Arc::ptr_eq(&self.inner,&other.inner) } pub fn weak(&self) -> WeakContext { WeakContext(Arc::downgrade(&self.0)) } // Putting this on hold for now // // pub fn topo_subject_head_iter (&self) -> TopoSubjectHeadIter { // TopoSubjectHeadIter::new( &self ) // } // // Subject A -> B -> E // \-> C -> F // \-> D -> G // // Steps: // 1. iterate over context subject heads, starting with leaves, working to the root // NOTE: This may not form a contiguous tree, as we're dealing with memos // which have been delivered from other slabs too, not just local edits // NOTE: We can actually have referential cycles here, because a subject // is not just a DAG of Memos, but rather the projection of a DAG *plus* whatever // is in our context. If we tried to continuously materialize such a structure, // it would generate an infinite number of memos - so we'll need to break cycles. // 2. Materialize each subject head in ascending topological order // 3. If any other context subject heads reference the subject head materialized // Issue a relation edit referencing it (ensuring that it gets added to the context) // and drop the materialized subject head from the context. // 4. Continue until the list is exhausted, or a cycle is detected // // subject_relation_map: // E: [] // B: [E] // A: [B] // etc /// Attempt to compress the present query context. /// We do this by issuing Relation memos for any subject heads which reference other subject heads presently in the query context. /// Then we can remove the now-referenced subject heads, and repeat the process in a topological fashion, confident that these /// referenced subject heads will necessarily be included in subsequent projection as a result. pub fn compress(&self) { // TODO: conditionalize this on the basis of the present context size // Iterate the contextualized subject heads in reverse topological order for subject_head in { self.manager.lock().unwrap().subject_head_iter() } { // TODO: implement MemoRefHead.conditionally_materialize such that the materialization threshold is selected dynamically. // It shold almost certainly not materialize with a single edit since the last FullyMaterialized memo // head.conditionally_materialize( &self.slab ); if subject_head.from_subject_ids.len() > 0 { // OK, somebody is pointing to us, so lets issue an edit for them // to point to the new materialized memo for their relevant relations self.repoint_subject_relations(subject_head.subject_id, subject_head.head, subject_head.from_subject_ids); // NOTE: In order to remove a subject head from the context, we must ensure that // ALL referencing subject heads in the context get repointed. It's not enough to just do one // Now that we know they are pointing to the new materialized MemoRefHead, // and that the resident subject struct we have is already updated, we can // remove this subject MemoRefHead from the context head, because subsequent // index/graph traversals should find this updated parent. // // When trying to materialize/compress fully (not that we'll want to do this often), // this would continue all the way to the root index node, and we should be left // with a very small context head } } } fn repoint_subject_relations(&self, _to_subject_id: SubjectId, _to_head: MemoRefHead, _from_subject_ids: Vec<SubjectId>) { unimplemented!() } pub fn is_fully_materialized(&self) -> bool { for subject_head in self.manager.lock().unwrap().subject_head_iter() { if !subject_head.head.is_fully_materialized(&self.slab) { return false; } } return true; } } impl Drop for ContextInner { fn drop(&mut self) { // println!("# ContextShared.drop"); } } impl fmt::Debug for Context { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_struct("ContextShared") .field("subject_heads", &self.manager.lock().unwrap().subject_ids() ) // TODO: restore Debug for WeakSubject //.field("subjects", &self.subjects) .finish() } } impl WeakContext { pub fn upgrade(&self) -> Option<Context> { match self.0.upgrade() { Some(i) => Some(Context(i)), None => None, } } pub fn cmp(&self, other: &WeakContext) -> bool { if let Some(context) = self.upgrade() { if let Some(other) = other.upgrade() { // stable way: &*(context.0) as *const _ != &*(other.0) as *const _ // unstable way: // Arc::ptr_eq(&context.inner,&other.inner) } else { false } } else { false } } }