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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use crate::component::datatype::DataType;
use crate::index::tree::NodeType;
use crate::storage::bytescoder::BytesCoder;

trait IndexPage {
    fn new(
        pid: u32,
        capacity: usize,
        node_type: NodeType,
        key_type: DataType,
        ptr_size: usize,
        key_size: usize,
        row_ptr_size: Option<usize>,
    ) -> Self;
}

trait FilePage {
    fn new(pid: u32, block_length: usize) -> Self;
}

struct IndexInternalPage {
    header: HeaderBytes,
    content: ContentBytes,
}

struct IndexLeafPage {
    header: HeaderBytes,
    content: ContentBytes,
}

struct DataFilePage {
    header: HeaderBytes,
    content: ContentBytes,
}

trait Header {
    fn to_bytes(&self) -> HeaderBytes;
    fn from_bytes(header_bytes: &HeaderBytes) -> Self;
}

pub const FILE_HEADER_SIZE: usize = 20;
struct FileHeader {
    pid: u32,
    capacity: usize,
    block_length: usize,
}

pub const INDEX_INTERNAL_HEADER_SIZE: usize = 20;
struct IndexInternalHeader {
    pid: u32,
    capacity: usize,
    node_type: NodeType,
    key_type: DataType,
    ptr_size: usize,
    key_size: usize,
}

pub const INDEX_LEAF_HEADER_SIZE: usize = 20;
struct IndexLeafHeader {
    pid: u32,
    capacity: usize,
    node_type: NodeType,
    key_type: DataType,
    ptr_size: usize,
    key_size: usize,
    row_ptr_size: usize,
}

type Bytes = Vec<u8>;
struct HeaderBytes(Bytes);
struct ContentBytes(Bytes);

impl IndexPage for IndexInternalPage {
    fn new(
        pid: u32,
        capacity: usize,
        node_type: NodeType,
        key_type: DataType,
        ptr_size: usize,
        key_size: usize,
        row_ptr_size: Option<usize>,
    ) -> Self {
        let header = IndexInternalHeader {
            pid,
            capacity,
            node_type,
            key_type,
            ptr_size,
            key_size,
        };

        let content = Vec::with_capacity(key_size * capacity + ptr_size * (capacity + 1));

        Self {
            header: header.to_bytes(),
            content: ContentBytes(content),
        }
    }
}

impl IndexPage for IndexLeafPage {
    fn new(
        pid: u32,
        capacity: usize,
        node_type: NodeType,
        key_type: DataType,
        ptr_size: usize,
        key_size: usize,
        row_ptr_size: Option<usize>,
    ) -> Self {
        let row_ptr_size = row_ptr_size.unwrap();
        let header = IndexLeafHeader {
            pid,
            capacity,
            node_type,
            key_type,
            ptr_size,
            key_size,
            row_ptr_size,
        };

        let content = Vec::with_capacity((key_size + row_ptr_size) * capacity + ptr_size * 2);

        Self {
            header: header.to_bytes(),
            content: ContentBytes(content),
        }
    }
}

impl FilePage for DataFilePage {
    fn new(pid: u32, block_length: usize) -> Self {
        let capacity = get_file_capacity(&block_length);

        let header = FileHeader {
            pid,
            capacity: capacity.clone(),
            block_length: block_length.clone(),
        };

        let content = Vec::with_capacity(block_length * capacity);

        Self {
            header: header.to_bytes(),
            content: ContentBytes(content),
        }
    }
}

fn get_file_capacity(block_length: &usize) -> usize {
    let page_size = match dotenv!("PAGE_SIZE").parse::<usize>() {
        Ok(s) => s,
        Err(_) => 4096,
    };

    (page_size - FILE_HEADER_SIZE) / block_length
}

impl Header for FileHeader {
    fn to_bytes(&self) -> HeaderBytes {
        let mut bytes: Bytes = vec![];
        bytes.extend_from_slice(&BytesCoder::attr_to_bytes(&DataType::Int, &self.pid.to_string()).unwrap());
        bytes.extend_from_slice(&BytesCoder::attr_to_bytes(&DataType::Int, &self.capacity.to_string()).unwrap());
        bytes.extend_from_slice(&BytesCoder::attr_to_bytes(&DataType::Int, &self.block_length.to_string()).unwrap());

        HeaderBytes(bytes)
    }

    fn from_bytes(header_bytes: &HeaderBytes) -> Self {
        let bytes = &header_bytes.0;
        let pid = BytesCoder::bytes_to_attr(&DataType::Int, &bytes[0..4])
            .unwrap()
            .parse::<u32>()
            .unwrap();
        let capacity = BytesCoder::bytes_to_attr(&DataType::Int, &bytes[4..8])
            .unwrap()
            .parse::<usize>()
            .unwrap();
        let block_length = BytesCoder::bytes_to_attr(&DataType::Int, &bytes[8..12])
            .unwrap()
            .parse::<usize>()
            .unwrap();
        Self {
            pid,
            capacity,
            block_length,
        }
    }
}

impl Header for IndexInternalHeader {
    fn to_bytes(&self) -> HeaderBytes {
        let mut bytes: Bytes = vec![];
        // TODO
        HeaderBytes(bytes)
    }

    fn from_bytes(header_bytes: &HeaderBytes) -> Self {
        // TODO
        let bytes = &header_bytes.0;
        let pid = 0;
        let capacity = 0;
        let node_type = NodeType::Internal;
        let key_type = DataType::Int;
        let ptr_size = 0;
        let key_size = 0;
        Self {
            pid,
            capacity,
            node_type,
            key_type,
            ptr_size,
            key_size,
        }
    }
}

impl Header for IndexLeafHeader {
    fn to_bytes(&self) -> HeaderBytes {
        let mut bytes: Bytes = vec![];
        // TODO
        HeaderBytes(bytes)
    }

    fn from_bytes(header_bytes: &HeaderBytes) -> Self {
        // TODO
        let bytes = &header_bytes.0;
        let pid = 0;
        let capacity = 0;
        let node_type = NodeType::Leaf;
        let key_type = DataType::Int;
        let ptr_size = 0;
        let key_size = 0;
        let row_ptr_size = 0;
        Self {
            pid,
            capacity,
            node_type,
            key_type,
            ptr_size,
            key_size,
            row_ptr_size,
        }
    }
}

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

    #[test]
    pub fn test_file_header() {
        let header = FileHeader {
            pid: 1,
            capacity: 101,
            block_length: 128,
        };

        let header_bytes = header.to_bytes();
        let header = FileHeader::from_bytes(&header_bytes);

        assert_eq!(header.pid, 1);
        assert_eq!(header.capacity, 101);
        assert_eq!(header.block_length, 128);
    }

    #[test]
    pub fn test_create_file_page() {
        let _file_page = DataFilePage::new(0, 128);
    }
}