aboutsummaryrefslogtreecommitdiffstats
path: root/log_db/src/record.rs
blob: ab9f8dc6cf121431d4d3bec30a6bd4f55696c45e (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use super::*;

#[derive(Debug, Clone)]
pub struct Record {
    pub values: Vec<Value>,
    pub tombstone: bool,
}

impl Record {
    pub fn serialize(&self) -> Vec<u8> {
        let mut bytes = Vec::new();

        if self.tombstone {
            bytes.extend(&[B_TOMBSTONE]);
        } else {
            bytes.extend(&[B_LIVE]);
        }

        for value in &self.values {
            bytes.extend(value.serialize());
        }
        bytes
    }

    pub fn deserialize(bytes: &[u8]) -> Record {
        assert!(bytes.len() > 0);

        let mut values = Vec::new();

        let tombstone = bytes[0] == B_TOMBSTONE;

        let mut start = 1;
        while start < bytes.len() {
            let (rv, consumed) = Value::deserialize(&bytes[start..]);
            values.push(rv);
            start += consumed;
        }
        Record { values, tombstone }
    }

    pub fn from(values: &[Value]) -> Record {
        Record {
            values: values.to_vec(),
            tombstone: false,
        }
    }

    pub fn at(&self, index: usize) -> &Value {
        &self.values[index]
    }

    pub fn validate<Field: Eq>(&self, schema: &[(Field, Type)]) -> DBResult<()> {
        // If there are more values than schema fields, it's an error.
        if self.values.len() > schema.len() {
            return Err(DBError::ValidationError(format!(
                "Record has more fields ({}) than expected by the schema ({})",
                self.values.len(),
                schema.len()
            )));
        }

        for (i, (_, typ)) in schema.iter().enumerate() {
            match self.values.get(i) {
                Some(value) => {
                    if !Self::value_matches_type(value, typ) {
                        return Err(DBError::ValidationError(format!(
                            "Record field {} has incorrect type: {:?}, expected {:?}",
                            i, value, typ.primitive
                        )));
                    }
                }
                // If the field is missing from the record...
                None => {
                    // ...it's allowed only if the schema says the field is nullable.
                    if !typ.nullable {
                        return Err(DBError::ValidationError(format!(
                            "Record is missing field expected by the schema ({:?}) at index {}",
                            typ, i
                        )));
                    }
                }
            }
        }

        Ok(())
    }

    fn value_matches_type(value: &Value, typ: &Type) -> bool {
        match (value, typ) {
            (Value::Null, Type { nullable: true, .. }) => true,
            (
                Value::Int(_),
                Type {
                    primitive: PrimitiveType::Int,
                    ..
                },
            ) => true,
            (
                Value::String(_),
                Type {
                    primitive: PrimitiveType::String,
                    ..
                },
            ) => true,
            (
                Value::Bytes(_),
                Type {
                    primitive: PrimitiveType::Bytes,
                    ..
                },
            ) => true,
            _ => false,
        }
    }
}

/// A trait that describes how to convert a data structure into a database record and vice versa.
pub trait Recordable {
    /// The field type of the data structure implementing the `Recordable` trait.
    type Field: Eq + Clone + Debug;
    /// Define the schema of the instance implementing the `Recordable` trait.
    fn schema() -> Vec<(Self::Field, Type)>;
    /// Define the primary key of the instance implementing the `Recordable` trait.
    fn primary_key() -> Self::Field;
    /// Define the secondary keys of the instance implementing the `Recordable` trait.
    fn secondary_keys() -> Vec<Self::Field> {
        Vec::new()
    }

    /// Convert the data structure implementing the `Recordable` trait into a vector of database values.
    fn into_record(self) -> Vec<Value>;
    /// Convert a vector of database values into the data structure implementing the `Recordable` trait.
    fn from_record(record: Vec<Value>) -> Self;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_record_serialize_deserialize() {
        let record = Record {
            values: vec![
                Value::Int(1),
                Value::String("hello".to_string()),
                Value::Bytes(vec![0, 1, 2, 3]),
            ],
            tombstone: true,
        };

        let serialized = record.serialize();
        let deserialized = Record::deserialize(&serialized);
        let reserialized = deserialized.serialize();

        assert_eq!(serialized.len(), reserialized.len());
        assert_eq!(record.values, deserialized.values);
    }
}

#[derive(Clone, Debug)]
pub enum TxEntry {
    Upsert { record: Record },
    Delete { record: Record },
}