1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use bytes::BufMut;

use crate::record::{ReadingBuf, SerializableRecord, StaticRecord, UnsafeBuf, UnsafeRead};

/// ZooKeeper node stat.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Stat {
    /// The zxid of the change that caused this znode to be created.
    pub czxid: i64,

    /// The zxid of the change that last modified this znode.
    pub mzxid: i64,

    /// The zxid of the change that last modified children of this znode.
    pub pzxid: i64,

    /// The time in milliseconds from epoch when this znode was created.
    pub ctime: i64,

    /// The time in milliseconds from epoch when this znode was last modified.
    pub mtime: i64,

    /// The number of changes to the data of this znode.
    pub version: i32,

    /// The number of changes to the children of this znode.
    pub cversion: i32,

    /// The number of changes to the ACL of this znode.
    pub aversion: i32,

    /// The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.
    pub ephemeral_owner: i64,

    /// The length of the data field of this znode.
    pub data_length: i32,

    /// The number of children of this znode.
    pub num_children: i32,
}

impl SerializableRecord for Stat {
    fn serialize(&self, buf: &mut dyn BufMut) {
        buf.put_i64(self.czxid);
        buf.put_i64(self.mzxid);
        buf.put_i64(self.ctime);
        buf.put_i64(self.mtime);
        buf.put_i32(self.version);
        buf.put_i32(self.cversion);
        buf.put_i32(self.aversion);
        buf.put_i64(self.ephemeral_owner);
        buf.put_i32(self.data_length);
        buf.put_i32(self.num_children);
        buf.put_i64(self.pzxid);
    }
}

impl StaticRecord for Stat {
    fn record_len() -> usize {
        return 68;
    }
}

impl UnsafeRead<'_> for Stat {
    type Error = std::convert::Infallible;

    unsafe fn read(buf: &mut ReadingBuf) -> Result<Self, Self::Error> {
        let czxid = buf.get_unchecked_i64();
        let mzxid = buf.get_unchecked_i64();
        let ctime = buf.get_unchecked_i64();
        let mtime = buf.get_unchecked_i64();
        let version = buf.get_unchecked_i32();
        let cversion = buf.get_unchecked_i32();
        let aversion = buf.get_unchecked_i32();
        let ephemeral_owner = buf.get_unchecked_i64();
        let data_length = buf.get_unchecked_i32();
        let num_children = buf.get_unchecked_i32();
        let pzxid = buf.get_unchecked_i64();
        return Ok(Stat {
            czxid,
            mzxid,
            ctime,
            mtime,
            version,
            cversion,
            aversion,
            ephemeral_owner,
            data_length,
            num_children,
            pzxid,
        });
    }
}

#[cfg(test)]
mod tests {
    use rand::distributions::Standard;
    use rand::{self, Rng};

    use super::Stat;
    use crate::record::{self, DeserializableRecord, SerializableRecord, StaticRecord};

    #[test]
    fn test_insufficient_buf() {
        let rng = rand::thread_rng();
        let data: Vec<u8> = rng.sample_iter(Standard).take(Stat::record_len() - 1).collect();
        let mut buf = data.as_slice();
        let err = Stat::deserialize(&mut buf).unwrap_err();
        assert_eq!(err, record::InsufficientBuf);
    }

    #[test]
    fn test_serde() {
        let mut rng = rand::thread_rng();
        let stat = Stat {
            czxid: rng.gen(),
            mzxid: rng.gen(),
            ctime: rng.gen(),
            mtime: rng.gen(),
            version: rng.gen(),
            cversion: rng.gen(),
            aversion: rng.gen(),
            ephemeral_owner: rng.gen(),
            data_length: rng.gen(),
            num_children: rng.gen(),
            pzxid: rng.gen(),
        };
        let mut data = Vec::new();
        stat.serialize(&mut data);
        assert_eq!(data.len(), Stat::record_len());
        let mut buf = data.as_slice();
        let deserialized = Stat::deserialize(&mut buf).unwrap();
        assert_eq!(deserialized, stat);
        assert!(buf.is_empty());
    }
}