MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / append_session_turn

Method append_session_turn

atomic-core/src/pristine/txn/write/mod.rs:455–497  ·  view source on GitHub ↗

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],
    )

Source from the content-addressed store, hash-verified

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,

Callers 2

Calls 11

encode_session_turn_keyFunction · 0.85
session_turn_namespaceFunction · 0.85
epoch_millisFunction · 0.85
is_noneMethod · 0.80
emit_turn_kgMethod · 0.80
to_bytesMethod · 0.45
insertMethod · 0.45
as_sliceMethod · 0.45
as_bytesMethod · 0.45
as_strMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected