simple_db_rust/btree/
tuple.rs

1use crate::field::*;
2use std::{
3    fmt::{self},
4    usize,
5};
6
7#[derive(Debug, Default)]
8pub struct Tuple {
9    pub scheme: TupleScheme,
10    pub fields: Vec<IntField>,
11}
12
13impl Tuple {
14    pub fn new(scheme: TupleScheme, bytes: &[u8]) -> Tuple {
15        let mut cells: Vec<IntField> = Vec::new();
16        let mut start: usize = 0;
17        let mut end: usize = 0;
18        for field in &scheme.fields {
19            match field.field_type {
20                Type::INT => {
21                    end += get_type_length(field.field_type);
22                    let cell_bytes = &bytes[start..end];
23
24                    let mut bytes_array = [0; 4];
25                    for i in 0..4 {
26                        bytes_array[i] = cell_bytes[i];
27                    }
28                    let value = i32::from_be_bytes(bytes_array);
29
30                    cells.push(IntField::new(value));
31
32                    start = end;
33                }
34            }
35        }
36        Tuple {
37            scheme,
38            fields: cells,
39        }
40    }
41
42    pub fn new_default_tuple(scheme: TupleScheme, _width: i32) -> Tuple {
43        let mut cells: Vec<IntField> = Vec::new();
44        for field in &scheme.fields {
45            match field.field_type {
46                Type::INT => {
47                    cells.push(IntField::new(0));
48                }
49            }
50        }
51        Tuple {
52            scheme,
53            fields: cells,
54        }
55    }
56
57    pub fn new_btree_tuple(value: i32, width: i32) -> Tuple {
58        let scheme = simple_int_tuple_scheme(width, "");
59        let _bytes = [0];
60        let mut tuple = Tuple::new_default_tuple(scheme, width);
61        for i in 0..tuple.fields.len() {
62            tuple.set_field(i, IntField::new(value));
63        }
64        tuple
65    }
66
67    pub fn set_field(&mut self, i: usize, c: IntField) {
68        self.fields[i] = c;
69    }
70
71    pub fn get_field(&self, i: usize) -> IntField {
72        self.fields[i]
73    }
74
75    pub fn clone(&self) -> Tuple {
76        Tuple {
77            scheme: self.scheme.clone(),
78            fields: self.fields.to_vec(),
79        }
80    }
81}
82
83impl PartialEq for Tuple {
84    fn eq(&self, other: &Self) -> bool {
85        if self.scheme != other.scheme {
86            return false;
87        }
88
89        for (i, field) in self.fields.iter().enumerate() {
90            if field != &other.fields[i] {
91                return false;
92            }
93        }
94
95        return true;
96    }
97}
98
99impl Eq for Tuple {}
100
101impl fmt::Display for Tuple {
102    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
103        let mut content: String = "{".to_owned();
104        for cell in &self.fields {
105            let cell_str = format!("{}, ", cell.value);
106            content.push_str(&cell_str);
107        }
108        content = content[..content.len() - 2].to_string();
109        content.push_str(&"}");
110        write!(f, "{}", content,)
111    }
112}
113
114#[derive(Debug)]
115pub struct TupleScheme {
116    pub fields: Vec<FieldItem>,
117}
118
119impl PartialEq for TupleScheme {
120    fn eq(&self, other: &Self) -> bool {
121        let matching = self
122            .fields
123            .iter()
124            .zip(&other.fields)
125            .filter(|&(a, b)| a == b)
126            .count();
127        self.fields.len() == matching
128    }
129}
130
131impl TupleScheme {
132    pub fn merge(scheme1: TupleScheme, scheme2: TupleScheme) -> TupleScheme {
133        let mut new_scheme = TupleScheme {
134            ..Default::default()
135        };
136
137        for f in scheme1.fields {
138            new_scheme.fields.push(f);
139        }
140        for f in scheme2.fields {
141            new_scheme.fields.push(f);
142        }
143
144        new_scheme
145    }
146
147    /**
148    get tuple size in bytes
149    */
150    pub fn get_size(&self) -> usize {
151        self.fields.len() * 4
152    }
153}
154
155impl Clone for TupleScheme {
156    fn clone(&self) -> Self {
157        Self {
158            fields: self.fields.to_vec(),
159        }
160    }
161}
162
163impl Default for TupleScheme {
164    fn default() -> TupleScheme {
165        TupleScheme { fields: Vec::new() }
166    }
167}
168
169pub fn simple_int_tuple_scheme(width: i32, name_prefix: &str) -> TupleScheme {
170    let mut fields: Vec<FieldItem> = Vec::new();
171    for i in 0..width {
172        let field = FieldItem {
173            field_name: format!("{}-{}", name_prefix, i),
174            field_type: Type::INT,
175        };
176        fields.push(field);
177    }
178
179    TupleScheme { fields: fields }
180}
181
182#[cfg(test)]
183mod tests {
184    use log::info;
185
186    use crate::util::init_log;
187
188    use super::*;
189
190    #[test]
191    fn test_tuple_clone() {
192        init_log();
193
194        let tuple = Tuple::new_btree_tuple(35, 2);
195        info!("tuple: {}", tuple);
196        let new_tuple = tuple.clone();
197        info!("new tuple: {}", new_tuple);
198    }
199}