|
| 1 | +use alloc::borrow::Cow; |
| 2 | +use alloc::string::String; |
| 3 | +use alloc::string::ToString; |
| 4 | +use alloc::vec::Vec; |
| 5 | +use serde::Deserialize; |
| 6 | + |
| 7 | +use super::BucketPriority; |
| 8 | +use super::Checksum; |
| 9 | + |
| 10 | +use crate::util::{deserialize_optional_string_to_i64, deserialize_string_to_i64}; |
| 11 | + |
| 12 | +/// While we would like to always borrow strings for efficiency, that's not consistently possible. |
| 13 | +/// With the JSON decoder, borrowing from input data is only possible when the string contains no |
| 14 | +/// escape sequences (otherwise, the string is not a direct view of input data and we need an |
| 15 | +/// internal copy). |
| 16 | +type SyncLineStr<'a> = Cow<'a, str>; |
| 17 | + |
| 18 | +#[derive(Deserialize, Debug)] |
| 19 | +pub struct Checkpoint<'a> { |
| 20 | + #[serde(deserialize_with = "deserialize_string_to_i64")] |
| 21 | + pub last_op_id: i64, |
| 22 | + #[serde(default)] |
| 23 | + #[serde(deserialize_with = "deserialize_optional_string_to_i64")] |
| 24 | + pub write_checkpoint: Option<i64>, |
| 25 | + #[serde(borrow)] |
| 26 | + pub buckets: Vec<BucketChecksum<'a>>, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Deserialize, Debug)] |
| 30 | +pub struct BucketChecksum<'a> { |
| 31 | + #[serde(borrow)] |
| 32 | + pub bucket: SyncLineStr<'a>, |
| 33 | + pub checksum: Checksum, |
| 34 | + #[serde(default)] |
| 35 | + pub priority: Option<BucketPriority>, |
| 36 | + #[serde(default)] |
| 37 | + pub count: Option<i64>, |
| 38 | + // #[serde(default)] |
| 39 | + // #[serde(deserialize_with = "deserialize_optional_string_to_i64")] |
| 40 | + // pub last_op_id: Option<i64>, |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Deserialize, Debug)] |
| 44 | +pub struct DataLine<'a> { |
| 45 | + #[serde(borrow)] |
| 46 | + pub bucket: SyncLineStr<'a>, |
| 47 | + pub data: Vec<OplogEntry<'a>>, |
| 48 | + // #[serde(default)] |
| 49 | + // pub has_more: bool, |
| 50 | + // #[serde(default, borrow)] |
| 51 | + // pub after: Option<SyncLineStr<'a>>, |
| 52 | + // #[serde(default, borrow)] |
| 53 | + // pub next_after: Option<SyncLineStr<'a>>, |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Deserialize, Debug)] |
| 57 | +pub struct OplogEntry<'a> { |
| 58 | + pub checksum: Checksum, |
| 59 | + #[serde(deserialize_with = "deserialize_string_to_i64")] |
| 60 | + pub op_id: i64, |
| 61 | + pub op: OpType, |
| 62 | + #[serde(default, borrow)] |
| 63 | + pub object_id: Option<SyncLineStr<'a>>, |
| 64 | + #[serde(default, borrow)] |
| 65 | + pub object_type: Option<SyncLineStr<'a>>, |
| 66 | + #[serde(default, borrow)] |
| 67 | + pub subkey: Option<SyncLineStr<'a>>, |
| 68 | + #[serde(default, borrow)] |
| 69 | + pub data: Option<OplogData<'a>>, |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Debug)] |
| 73 | +pub enum OplogData<'a> { |
| 74 | + /// A string encoding a well-formed JSON object representing values of the row. |
| 75 | + Json { data: Cow<'a, str> }, |
| 76 | + // BsonDocument { data: Cow<'a, [u8]> }, |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)] |
| 80 | +pub enum OpType { |
| 81 | + CLEAR, |
| 82 | + MOVE, |
| 83 | + PUT, |
| 84 | + REMOVE, |
| 85 | +} |
| 86 | + |
| 87 | +impl<'a, 'de: 'a> Deserialize<'de> for OplogData<'a> { |
| 88 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 89 | + where |
| 90 | + D: serde::Deserializer<'de>, |
| 91 | + { |
| 92 | + // For now, we will always get oplog data as a string. In the future, there may be the |
| 93 | + // option of the sync service sending BSON-encoded data lines too, but that's not relevant |
| 94 | + // for now. |
| 95 | + return Ok(OplogData::Json { |
| 96 | + data: Deserialize::deserialize(deserializer)?, |
| 97 | + }); |
| 98 | + } |
| 99 | +} |
0 commit comments