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
use escape_string::split_one;
use crate::row_format::*;
use byteorder::ByteOrder;
pub fn add_from_stream<R: std::io::BufRead>(
tx: &mut crate::CreateTx,
db: &crate::DatabaseReader,
format: &str, input: &mut R,
timestamp_format: Option<&str>,
nocheck: bool,
) -> Result<(), crate::WriteFailure>
{
let row_format = parse_row_format(format);
let mut line = String::new();
let mut row_data = vec!();
let mut key_format_identified = String::new();
while 0 != input.read_line(&mut line).unwrap()
{
let tail = line.trim_end();
if tail.is_empty() { continue; }
let (key, tail) = split_one(&tail).unwrap();
let (timestamp, tail) = split_one(&tail).unwrap();
let ts: Timestamp;
if let Some(f) = timestamp_format.as_ref()
{
let n = chrono::NaiveDateTime::parse_from_str(×tamp, f)
.expect("parsing timestamp according to format");
ts = n.timestamp_nanos() as Timestamp;
}
else
{
ts = timestamp.parse().expect("parsing timestamp");
}
let (values, _tail) = split_one(&tail).unwrap();
row_format.to_stored_format(ts, &values, &mut row_data)
.unwrap();
if !nocheck && key_format_identified != key
{
if let Some(record) = db.get(&key).next()
{
if record.format() != format
{
return Err(crate::WriteFailure::HeterogeneousFormats(
key.to_string(),
record.format().to_owned(),
format.to_owned()
));
}
}
key_format_identified = key.to_string();
}
tx.add_record(&key, format, &row_data)?;
row_data.clear();
line.clear();
}
Ok(())
}
pub fn add_from_stream_with_fmt<R: std::io::BufRead>(
tx: &mut crate::CreateTx,
db: &crate::DatabaseReader,
input: &mut R,
timestamp_format: Option<&str>,
nocheck: bool,
) -> Result<(), crate::WriteFailure>
{
let mut line = String::new();
let mut row_data = vec!();
let mut key_format_identified = String::new();
while 0 != input.read_line(&mut line).unwrap()
{
let tail = line.trim_end();
if tail.is_empty() { continue; }
let (key, tail) = split_one(&tail).unwrap();
let (timestamp, tail) = split_one(&tail).unwrap();
let ts: Timestamp;
if let Some(f) = timestamp_format.as_ref()
{
let n = chrono::NaiveDateTime::parse_from_str(×tamp, f)
.expect("parsing timestamp according to format");
ts = n.timestamp_nanos() as Timestamp;
}
else
{
ts = timestamp.parse().expect("parsing timestamp");
}
let (format, values) = split_one(&tail).unwrap();
let row_format = parse_row_format(&format);
row_format.to_stored_format(ts, &values, &mut row_data)
.unwrap();
if !nocheck && key_format_identified != key
{
if let Some(record) = db.get(&key).next()
{
if record.format() != format
{
return Err(crate::WriteFailure::HeterogeneousFormats(
key.to_string(),
record.format().to_owned(),
format.to_string()
));
}
}
key_format_identified = key.to_string();
}
tx.add_record(&key, &format, &row_data)?;
row_data.clear();
line.clear();
}
Ok(())
}
pub fn print_record<W: std::io::Write>(
record: &crate::record::OwnedRecord,
out: &mut W,
) -> std::io::Result<()>
{
let fmt = parse_row_format(record.format());
let key = record.key();
let ts = &record.value()[0..8];
let value = &record.value()[8..];
let ts: u64 = byteorder::BigEndian::read_u64(ts);
let ts = chrono::NaiveDateTime::from_timestamp(
(ts/1_000_000_000) as i64, (ts%1_000_000_000) as u32
);
write!(out, "{}\t{}\t", escape_string::escape(key), ts)?;
fmt.to_protocol_format(value, out)
}
pub fn print_record_with_fmt<W: std::io::Write>(
record: &crate::record::OwnedRecord,
timestamp_format: &str,
out: &mut W,
) -> std::io::Result<()>
{
let fmt_string = record.format();
let fmt = parse_row_format(fmt_string);
let key = record.key();
let ts = &record.value()[0..8];
let value = &record.value()[8..];
let ts: u64 = byteorder::BigEndian::read_u64(ts);
let ts = chrono::NaiveDateTime::from_timestamp(
(ts/1_000_000_000) as i64, (ts%1_000_000_000) as u32
);
write!(
out, "{}\t{}\t{}\t",
escape_string::escape(key),
ts.format(timestamp_format),
fmt_string,
)?;
fmt.to_protocol_format(value, out)
}
pub fn print_record_nanos<W: std::io::Write>(
record: &crate::record::OwnedRecord,
out: &mut W,
) -> std::io::Result<()>
{
let fmt = parse_row_format(record.format());
let key = record.key();
let ts = &record.value()[0..8];
let value = &record.value()[8..];
let ts: u64 = byteorder::BigEndian::read_u64(ts);
write!(out, "{}\t{}\t", escape_string::escape(key), ts)?;
fmt.to_protocol_format(value, out)
}