Append a turn without touching prior rows when the existing session was already written with the current canonical schema.
(
&mut self,
mut record: crate::change::session::SessionRecord,
turn: crate::change::session::SessionTurn,
existing_turns: &[crate::change::session::SessionTurn],
)
| 453 | /// Append a turn without touching prior rows when the existing session was |
| 454 | /// already written with the current canonical schema. |
| 455 | fn append_session_turn( |
| 456 | &mut self, |
| 457 | mut record: crate::change::session::SessionRecord, |
| 458 | turn: crate::change::session::SessionTurn, |
| 459 | existing_turns: &[crate::change::session::SessionTurn], |
| 460 | ) -> PristineResult<()> { |
| 461 | use crate::change::session::{encode_session_turn_key, session_turn_namespace}; |
| 462 | |
| 463 | let key = |
| 464 | encode_session_turn_key(session_turn_namespace(&record.session_id), turn.turn_number); |
| 465 | { |
| 466 | let mut turns = self.txn.open_table(SESSION_TURNS)?; |
| 467 | let mut reverse = self.txn.open_table(SESSION_PROVENANCE)?; |
| 468 | let bytes = turn.to_bytes(); |
| 469 | turns.insert(&key, bytes.as_slice())?; |
| 470 | reverse.insert(turn.provenance_hash.as_bytes(), &key)?; |
| 471 | } |
| 472 | |
| 473 | if record.first_provenance.is_none() { |
| 474 | record.first_provenance = Some(turn.provenance_hash); |
| 475 | } |
| 476 | record.latest_provenance = Some(turn.provenance_hash); |
| 477 | record.turn_count = turn.turn_number.saturating_add(1); |
| 478 | if crate::types::timestamp::epoch_millis(turn.timestamp) |
| 479 | < crate::types::timestamp::epoch_millis(record.started_at) |
| 480 | { |
| 481 | record.started_at = turn.timestamp; |
| 482 | } |
| 483 | { |
| 484 | let mut sessions = self.txn.open_table(SESSIONS)?; |
| 485 | let bytes = record.to_bytes(); |
| 486 | sessions.insert(record.session_id.as_str(), bytes.as_slice())?; |
| 487 | } |
| 488 | |
| 489 | let previous_turn_number = turn.previous_provenance.and_then(|hash| { |
| 490 | existing_turns |
| 491 | .iter() |
| 492 | .find(|existing| existing.provenance_hash == hash) |
| 493 | .map(|existing| existing.turn_number) |
| 494 | .or_else(|| (hash == turn.provenance_hash).then_some(turn.turn_number)) |
| 495 | }); |
| 496 | self.emit_turn_kg(&record, &turn, previous_turn_number) |
| 497 | } |
| 498 | |
| 499 | fn index_session_turn_candidate( |
| 500 | &mut self, |
no test coverage detected