| 338 | |
| 339 | impl<'a> KgMutTxnT for WriteTxn<'a> { |
| 340 | fn upsert_kg_node(&mut self, node: &KgNode) -> PristineResult<()> { |
| 341 | if !self.kg_writes_enabled()? { |
| 342 | return Ok(()); |
| 343 | } |
| 344 | if node.types.is_empty() |
| 345 | || node |
| 346 | .types |
| 347 | .iter() |
| 348 | .any(|iri| crate::kg::ontology::Class::from_iri(iri).is_none()) |
| 349 | { |
| 350 | return Err(PristineError::Serialization { |
| 351 | message: "node must have canonical asserted classes".into(), |
| 352 | }); |
| 353 | } |
| 354 | let old_types: Vec<String> = self |
| 355 | .txn |
| 356 | .open_multimap_table(KG_TYPES)? |
| 357 | .remove_all(node.id.as_str())? |
| 358 | .map(|row| row.map(|row| row.value().to_owned())) |
| 359 | .collect::<Result<_, _>>()?; |
| 360 | for class in old_types { |
| 361 | self.txn |
| 362 | .open_multimap_table(KG_TYPES_REV)? |
| 363 | .remove(class.as_str(), node.id.as_str())?; |
| 364 | } |
| 365 | for class in &node.types { |
| 366 | self.txn |
| 367 | .open_multimap_table(KG_TYPES)? |
| 368 | .insert(node.id.as_str(), class.as_str())?; |
| 369 | self.txn |
| 370 | .open_multimap_table(KG_TYPES_REV)? |
| 371 | .insert(class.as_str(), node.id.as_str())?; |
| 372 | } |
| 373 | // Replacement semantics are important: old ID/label/summary tokens |
| 374 | // must disappear before the same node ID is re-indexed. |
| 375 | self.remove_kg_fts_entries(&node.id)?; |
| 376 | |
| 377 | let mut table = self.txn.open_table(KG_NODES)?; |
| 378 | let bytes = serde_json::to_vec(node).map_err(|e| PristineError::Serialization { |
| 379 | message: e.to_string(), |
| 380 | })?; |
| 381 | table.insert(node.id.as_str(), bytes.as_slice())?; |
| 382 | drop(table); |
| 383 | |
| 384 | self.add_kg_fts_entries(&node.id, &kg_node_fts_text(node))?; |
| 385 | |
| 386 | Ok(()) |
| 387 | } |
| 388 | |
| 389 | fn upsert_kg_edge(&mut self, edge: &KgEdge) -> PristineResult<()> { |
| 390 | if !self.kg_writes_enabled()? { |