|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use serde::{ |
| 4 | + Deserialize, |
| 5 | + Serialize, |
| 6 | +}; |
| 7 | +use sync_types::Timestamp; |
| 8 | +use value::{ |
| 9 | + codegen_convex_serialization, |
| 10 | + InternalId, |
| 11 | +}; |
| 12 | + |
| 13 | +use crate::bootstrap_model::index::search_index::{ |
| 14 | + index_snapshot::SerializedFragmentedSearchSegment, |
| 15 | + FragmentedSearchSegment, |
| 16 | +}; |
| 17 | + |
| 18 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 19 | +#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))] |
| 20 | +pub struct TextIndexBackfillState { |
| 21 | + pub segments: Vec<FragmentedSearchSegment>, |
| 22 | + // None at the start of backfill, then set after the first backfill iteration. |
| 23 | + pub cursor: Option<TextBackfillCursor>, |
| 24 | +} |
| 25 | + |
| 26 | +impl TextIndexBackfillState { |
| 27 | + pub fn new() -> Self { |
| 28 | + Self { |
| 29 | + segments: vec![], |
| 30 | + cursor: None, |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 36 | +#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))] |
| 37 | +pub struct TextBackfillCursor { |
| 38 | + pub cursor: InternalId, |
| 39 | + pub backfill_snapshot_ts: Timestamp, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Serialize, Deserialize)] |
| 43 | +pub struct SerializedTextBackfillCursor { |
| 44 | + pub document_cursor: String, |
| 45 | + pub backfill_snapshot_ts: i64, |
| 46 | +} |
| 47 | + |
| 48 | +impl From<TextBackfillCursor> for SerializedTextBackfillCursor { |
| 49 | + fn from(value: TextBackfillCursor) -> Self { |
| 50 | + Self { |
| 51 | + document_cursor: value.cursor.to_string(), |
| 52 | + backfill_snapshot_ts: value.backfill_snapshot_ts.into(), |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl TryFrom<SerializedTextBackfillCursor> for TextBackfillCursor { |
| 58 | + type Error = anyhow::Error; |
| 59 | + |
| 60 | + fn try_from(value: SerializedTextBackfillCursor) -> Result<Self, Self::Error> { |
| 61 | + Ok(Self { |
| 62 | + cursor: InternalId::from_str(&value.document_cursor)?, |
| 63 | + backfill_snapshot_ts: Timestamp::try_from(value.backfill_snapshot_ts)?, |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Serialize, Deserialize)] |
| 69 | +pub struct SerializedTextIndexBackfillState { |
| 70 | + segments: Option<Vec<SerializedFragmentedSearchSegment>>, |
| 71 | + cursor: Option<SerializedTextBackfillCursor>, |
| 72 | +} |
| 73 | + |
| 74 | +impl TryFrom<TextIndexBackfillState> for SerializedTextIndexBackfillState { |
| 75 | + type Error = anyhow::Error; |
| 76 | + |
| 77 | + fn try_from(backfill_state: TextIndexBackfillState) -> Result<Self, Self::Error> { |
| 78 | + Ok(SerializedTextIndexBackfillState { |
| 79 | + segments: Some( |
| 80 | + backfill_state |
| 81 | + .segments |
| 82 | + .into_iter() |
| 83 | + .map(|s| s.try_into()) |
| 84 | + .collect::<anyhow::Result<Vec<_>>>()?, |
| 85 | + ), |
| 86 | + cursor: backfill_state |
| 87 | + .cursor |
| 88 | + .map(|cursor| cursor.try_into()) |
| 89 | + .transpose()?, |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl TryFrom<SerializedTextIndexBackfillState> for TextIndexBackfillState { |
| 95 | + type Error = anyhow::Error; |
| 96 | + |
| 97 | + fn try_from(serialized: SerializedTextIndexBackfillState) -> Result<Self, Self::Error> { |
| 98 | + Ok(TextIndexBackfillState { |
| 99 | + segments: serialized |
| 100 | + .segments |
| 101 | + .unwrap_or_default() |
| 102 | + .into_iter() |
| 103 | + .map(|s| s.try_into()) |
| 104 | + .collect::<anyhow::Result<Vec<_>>>()?, |
| 105 | + cursor: serialized |
| 106 | + .cursor |
| 107 | + .map(TextBackfillCursor::try_from) |
| 108 | + .transpose()?, |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +codegen_convex_serialization!(TextIndexBackfillState, SerializedTextIndexBackfillState); |
0 commit comments