td_client/
table_import.rs

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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use flate2::write::GzEncoder;
use flate2::Compression;
use rmp::encode::*;
use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io;
use tempdir::TempDir;

pub struct TableImportWritableChunk {
    elms_in_row: Option<(u32, u32)>,
    file_path: String,
    tmp_dir: TempDir,
    write: GzEncoder<File>,
}

#[allow(dead_code)]
pub struct TableImportReadableChunk {
    pub file_path: String,
    tmp_dir: TempDir,
}

#[derive(Debug, Clone)]
pub struct UnmatchElementNumsError(Option<(u32, u32)>);

impl fmt::Display for UnmatchElementNumsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            Some((capacity, added)) => write!(
                f,
                "The number of elements in the row is unexpeceted. capacity:{}, added:{}",
                capacity, added
            ),
            None => write!(f, "Not initialized yet"),
        }
    }
}

impl Error for UnmatchElementNumsError {}

#[derive(Debug)]
pub enum TableImportChunkError {
    IOError(io::Error),
    UnmatchElementNums(UnmatchElementNumsError),
    UnexpectedError(String),
    MsgpackValueWriteError(ValueWriteError),
}

impl From<UnmatchElementNumsError> for TableImportChunkError {
    fn from(err: UnmatchElementNumsError) -> Self {
        TableImportChunkError::UnmatchElementNums(err)
    }
}

impl From<ValueWriteError> for TableImportChunkError {
    fn from(err: ValueWriteError) -> Self {
        TableImportChunkError::MsgpackValueWriteError(err)
    }
}

impl From<io::Error> for TableImportChunkError {
    fn from(err: io::Error) -> Self {
        TableImportChunkError::IOError(err)
    }
}

impl fmt::Display for TableImportChunkError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            TableImportChunkError::IOError(ref x) => write!(f, "{}", x),
            TableImportChunkError::UnmatchElementNums(ref x) => write!(f, "{}", x),
            TableImportChunkError::UnexpectedError(ref x) => write!(f, "{}", x),
            TableImportChunkError::MsgpackValueWriteError(ref x) => write!(f, "{}", x),
        }
    }
}

impl Error for TableImportChunkError {}

impl TableImportWritableChunk {
    pub fn new() -> Result<TableImportWritableChunk, TableImportChunkError> {
        // let uuid =  Uuid::new_v4().hyphenated().to_string();
        // let tmp_dir = TempDir::new(format!("td-client-rust-{}", uuid).as_str())?;
        let tmp_dir = TempDir::new("td-client-rust")?;
        let tmp_file_path = tmp_dir.path().join("msgpack.gz");
        let file_path = tmp_file_path
            .to_str()
            .ok_or(TableImportChunkError::UnexpectedError(format!(
                "Failed to convert path to string: {:?}",
                tmp_file_path
            )))?
            .to_string();
        let file = File::create(file_path.clone())?;
        let write = GzEncoder::new(file, Compression::default());
        Ok(TableImportWritableChunk {
            elms_in_row: None,
            file_path: file_path,
            tmp_dir: tmp_dir,
            write: write,
        })
    }

    fn check_elm_number(&self) -> Result<(), TableImportChunkError> {
        match self.elms_in_row {
            Some((capacity, added)) => {
                if capacity != added {
                    Err(UnmatchElementNumsError(Some((capacity, added))))?
                }
            }
            None => (),
        };
        Ok(())
    }

    pub fn next_row(&mut self, len: u32) -> Result<(), TableImportChunkError> {
        self.check_elm_number()?;
        write_map_len(&mut self.write, len)?;
        self.elms_in_row = Some((len, 0));
        Ok(())
    }

    fn incr_elms_in_row(&mut self) -> Result<(), UnmatchElementNumsError> {
        match self.elms_in_row {
            Some((capacity, added)) => {
                let new_added = added + 1;
                if capacity < new_added {
                    Err(UnmatchElementNumsError(Some((capacity, new_added))))?
                } else {
                    self.elms_in_row = Some((capacity, new_added));
                    Ok(())
                }
            }
            None => Err(UnmatchElementNumsError(None))?,
        }
    }

    pub fn write_key_and_array_header(
        &mut self,
        key: &str,
        len: u32,
    ) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_array_len(&mut self.write, len)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_bin(
        &mut self,
        key: &str,
        data: &[u8],
    ) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_bin(&mut self.write, data)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_bool(
        &mut self,
        key: &str,
        val: bool,
    ) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_bool(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_ext_meta(
        &mut self,
        key: &str,
        len: u32,
        typeid: i8,
    ) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_ext_meta(&mut self.write, len, typeid)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_f32(&mut self, key: &str, val: f32) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_f32(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_f64(&mut self, key: &str, val: f64) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_f64(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_i16(&mut self, key: &str, val: i16) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_i16(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_i32(&mut self, key: &str, val: i32) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_i32(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_i64(&mut self, key: &str, val: i64) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_i64(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_i8(&mut self, key: &str, val: i8) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_i8(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_map_len(
        &mut self,
        key: &str,
        len: u32,
    ) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_map_len(&mut self.write, len)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_nfix(&mut self, key: &str, val: i8) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_nfix(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_nil(&mut self, key: &str) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_nil(&mut self.write)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_pfix(&mut self, key: &str, val: u8) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_pfix(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_sint(&mut self, key: &str, val: i64) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_sint(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_sint_eff(
        &mut self,
        key: &str,
        val: i64,
    ) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_sint(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_str(
        &mut self,
        key: &str,
        data: &str,
    ) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_str(&mut self.write, data)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_u16(&mut self, key: &str, val: u16) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_u16(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_u32(&mut self, key: &str, val: u32) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_u32(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_u64(&mut self, key: &str, val: u64) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_u64(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_u8(&mut self, key: &str, val: u8) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_u8(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn write_key_and_uint(&mut self, key: &str, val: u64) -> Result<(), TableImportChunkError> {
        write_str(&mut self.write, key)?;
        write_uint(&mut self.write, val)?;
        self.incr_elms_in_row()?;
        Ok(())
    }

    pub fn close(self) -> Result<TableImportReadableChunk, TableImportChunkError> {
        self.check_elm_number()?;
        self.write.finish()?;
        Ok(TableImportReadableChunk {
            file_path: self.file_path,
            tmp_dir: self.tmp_dir,
        })
    }
}