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
#![doc(html_root_url = "https://docs.rs/cql_nullable_f64/0.1.0")]
use std::fs::{ File, OpenOptions };
use std::io::{ Read, Write, Cursor, SeekFrom, Seek };
use byteorder::{ ReadBytesExt, WriteBytesExt, LittleEndian };
use cql_model::{ CqlType, CqlWritable, CqlReadable, CqlStreamReadable };
const HAS_VALUE_FLAG: u8 = 1;
const NULL_FLAG: u8 = 0;
const CONTENT_SIZE: usize = 8;
const HAS_VALUE_SIZE: usize = 1;
pub struct NullableF64;
impl CqlType for NullableF64 {
type ValueType = Option<f64>;
const VALUE_SIZE: usize = HAS_VALUE_SIZE + CONTENT_SIZE;
}
impl CqlWritable for NullableF64 {
fn write_to_db(db_location: &str, value_location: u64, input_value: Self::ValueType) {
let mut file = OpenOptions::new().write(true).open(db_location).unwrap();
file.seek(SeekFrom::Start(value_location * Self::VALUE_SIZE as u64)).unwrap();
match input_value {
None => {
file.write(&[NULL_FLAG; HAS_VALUE_SIZE]).unwrap();
}
Some(value) => {
file.write(&[HAS_VALUE_FLAG; HAS_VALUE_SIZE]).unwrap();
let mut wtr = vec![];
wtr.write_f64::<LittleEndian>(value).unwrap();
file.write(&wtr).unwrap();
}
}
}
}
impl CqlReadable for NullableF64 {
fn read_from_db(db_location: &str, value_location: u64) -> Self::ValueType {
let mut file = File::open(&db_location).unwrap();
file.seek(SeekFrom::Start(value_location * Self::VALUE_SIZE as u64)).unwrap();
let mut null_buffer = [0; HAS_VALUE_SIZE];
file.read(&mut null_buffer).unwrap();
if null_buffer[0] == NULL_FLAG {
return None
}
let mut value_buffer = [0; CONTENT_SIZE];
file.read(&mut value_buffer).unwrap();
let mut rdr = Cursor::new(value_buffer);
Some(rdr.read_f64::<LittleEndian>().unwrap())
}
}
impl CqlStreamReadable for NullableF64 {
fn read_to_stream(db_location: &str, stream: &mut dyn Write, value_location: u64, n_values: u64) {
let mut file = File::open(&db_location).unwrap();
file.seek(SeekFrom::Start(value_location * Self::VALUE_SIZE as u64)).unwrap();
for _i in 0..n_values {
let mut buffer = [0; Self::VALUE_SIZE];
file.read(&mut buffer).unwrap();
stream.write(&mut buffer).unwrap();
}
stream.flush().unwrap();
}
}
pub fn unpack_stream<F>(stream: &mut Cursor<Vec<u8>>, n_values: usize, mut res: F) where F: FnMut(usize, Option<f64>) {
for index in 0..n_values {
let mut null_buffer = [0; HAS_VALUE_SIZE];
let n_bytes_read = stream.read(&mut null_buffer).unwrap();
if n_bytes_read == 0 {
break;
}
let mut value_buffer = [0; CONTENT_SIZE];
stream.read(&mut value_buffer).unwrap();
if null_buffer[0] == NULL_FLAG {
res(index, None);
} else {
let mut rdr = Cursor::new(value_buffer);
res(index, Some(rdr.read_f64::<LittleEndian>().unwrap()));
}
}
}