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],
)
| 362 | /// Append a turn without touching prior rows when the existing session was |
| 363 | /// already written with the current canonical schema. |
| 364 | fn append_session_turn( |
| 365 | &mut self, |
| 366 | mut record: crate::change::session::SessionRecord, |
| 367 | turn: crate::change::session::SessionTurn, |
| 368 | existing_turns: &[crate::change::session::SessionTurn], |
| 369 | ) -> PristineResult<()> { |
| 370 | use crate::change::session::{encode_session_turn_key, session_turn_namespace}; |
| 371 | |
| 372 | let key = |
| 373 | encode_session_turn_key(session_turn_namespace(&record.session_id), turn.turn_number); |
| 374 | { |
| 375 | let mut turns = self.txn.open_table(SESSION_TURNS)?; |
| 376 | let mut reverse = self.txn.open_table(SESSION_PROVENANCE)?; |
| 377 | let bytes = turn.to_bytes(); |
| 378 | turns.insert(&key, bytes.as_slice())?; |
| 379 | reverse.insert(turn.provenance_hash.as_bytes(), &key)?; |
| 380 | } |
| 381 | |
| 382 | if record.first_provenance.is_none() { |
| 383 | record.first_provenance = Some(turn.provenance_hash); |
| 384 | } |
| 385 | record.latest_provenance = Some(turn.provenance_hash); |
| 386 | record.turn_count = turn.turn_number.saturating_add(1); |
| 387 | record.started_at = record.started_at.min(turn.timestamp); |
| 388 | { |
| 389 | let mut sessions = self.txn.open_table(SESSIONS)?; |
| 390 | let bytes = record.to_bytes(); |
| 391 | sessions.insert(record.session_id.as_str(), bytes.as_slice())?; |
| 392 | } |
| 393 | |
| 394 | let previous_turn_number = turn.previous_provenance.and_then(|hash| { |
| 395 | existing_turns |
| 396 | .iter() |
| 397 | .find(|existing| existing.provenance_hash == hash) |
| 398 | .map(|existing| existing.turn_number) |
| 399 | .or_else(|| (hash == turn.provenance_hash).then_some(turn.turn_number)) |
| 400 | }); |
| 401 | self.emit_turn_kg(&record, &turn, previous_turn_number) |
| 402 | } |
| 403 | |
| 404 | fn index_session_turn_candidate( |
| 405 | &mut self, |
no test coverage detected