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
use std::marker::PhantomData; use std::net::Ipv4Addr; use std::net::Ipv6Addr; use std::sync::Arc; use super::column_data::ColumnData; use super::ColumnFrom; use crate::binary::Encoder; use crate::binary::ReadEx; use crate::errors::Result; use crate::types::column::column_data::BoxColumnData; use crate::types::column::nullable::NullableColumnData; use crate::types::column::ColumnWrapper; use crate::types::SqlType; use crate::types::Value; use crate::types::ValueRef; pub(crate) trait IpVersion: Copy + Sync + Send + 'static { fn sql_type() -> SqlType; fn size() -> usize; fn push(inner: &mut Vec<u8>, value: Value); fn get(inner: &[u8], index: usize) -> ValueRef; } #[derive(Copy, Clone)] pub(crate) struct Ipv4; #[derive(Copy, Clone)] pub(crate) struct Ipv6; #[derive(Copy, Clone)] pub(crate) struct Uuid; impl IpVersion for Ipv4 { #[inline(always)] fn sql_type() -> SqlType { SqlType::Ipv4 } #[inline(always)] fn size() -> usize { 4 } #[inline(always)] fn push(inner: &mut Vec<u8>, value: Value) { if let Value::Ipv4(v) = value { inner.extend(&v); } else { panic!(); } } #[inline(always)] fn get(inner: &[u8], index: usize) -> ValueRef { let mut v: [u8; 4] = Default::default(); v.copy_from_slice(&inner[index * 4..(index + 1) * 4]); ValueRef::Ipv4(v) } } impl IpVersion for Ipv6 { #[inline(always)] fn sql_type() -> SqlType { SqlType::Ipv6 } #[inline(always)] fn size() -> usize { 16 } #[inline(always)] fn push(inner: &mut Vec<u8>, value: Value) { if let Value::Ipv6(v) = value { inner.extend(&v); } else { panic!(); } } #[inline(always)] fn get(inner: &[u8], index: usize) -> ValueRef { let mut v: [u8; 16] = Default::default(); v.copy_from_slice(&inner[index * 16..(index + 1) * 16]); ValueRef::Ipv6(v) } } impl IpVersion for Uuid { #[inline(always)] fn sql_type() -> SqlType { SqlType::Uuid } #[inline(always)] fn size() -> usize { 16 } #[inline(always)] fn push(inner: &mut Vec<u8>, value: Value) { if let Value::Uuid(v) = value { inner.extend(&v); } else { panic!(); } } #[inline(always)] fn get(inner: &[u8], index: usize) -> ValueRef { let mut v: [u8; 16] = Default::default(); v.copy_from_slice(&inner[index * 16..(index + 1) * 16]); ValueRef::Uuid(v) } } impl ColumnFrom for Vec<Ipv4Addr> { fn column_from<W: ColumnWrapper>(data: Self) -> W::Wrapper { let mut inner = Vec::with_capacity(data.len()); for ip in data { let mut buffer = ip.octets(); buffer.reverse(); inner.extend(&buffer); } W::wrap(IpColumnData::<Ipv4> { inner, phantom: PhantomData }) } } impl ColumnFrom for Vec<Ipv6Addr> { fn column_from<W: ColumnWrapper>(data: Self) -> W::Wrapper { let mut inner = Vec::with_capacity(data.len()); for ip in data { inner.extend(&ip.octets()); } W::wrap(IpColumnData::<Ipv6> { inner, phantom: PhantomData }) } } impl ColumnFrom for Vec<uuid::Uuid> { fn column_from<W: ColumnWrapper>(data: Self) -> W::Wrapper { let mut inner = Vec::with_capacity(data.len()); for uuid in data { let mut buffer = *uuid.as_bytes(); buffer[..8].reverse(); buffer[8..].reverse(); inner.extend(&buffer); } W::wrap(IpColumnData::<Uuid> { inner, phantom: PhantomData }) } } impl ColumnFrom for Vec<Option<Ipv4Addr>> { fn column_from<W: ColumnWrapper>(source: Self) -> <W as ColumnWrapper>::Wrapper { let n = source.len(); let mut inner: Vec<u8> = Vec::with_capacity(n * 4); let mut nulls: Vec<u8> = Vec::with_capacity(n); for ip in source { match ip { None => { inner.extend(&[0; 4]); nulls.push(1); } Some(ip) => { let mut buffer = ip.octets(); buffer.reverse(); inner.extend(&buffer); nulls.push(0); } } } let inner = Arc::new(IpColumnData::<Ipv4> { inner, phantom: PhantomData }); let data = NullableColumnData { inner, nulls }; W::wrap(data) } } impl ColumnFrom for Vec<Option<Ipv6Addr>> { fn column_from<W: ColumnWrapper>(source: Self) -> <W as ColumnWrapper>::Wrapper { let n = source.len(); let mut inner: Vec<u8> = Vec::with_capacity(n * 16); let mut nulls: Vec<u8> = Vec::with_capacity(n); for ip in source { match ip { None => { inner.extend(&[0; 16]); nulls.push(1); } Some(ip) => { inner.extend(&ip.octets()); nulls.push(0); } } } let inner = Arc::new(IpColumnData::<Ipv6> { inner, phantom: PhantomData }); let data = NullableColumnData { inner, nulls }; W::wrap(data) } } impl ColumnFrom for Vec<Option<uuid::Uuid>> { fn column_from<W: ColumnWrapper>(source: Self) -> <W as ColumnWrapper>::Wrapper { let n = source.len(); let mut inner: Vec<u8> = Vec::with_capacity(n * 16); let mut nulls: Vec<u8> = Vec::with_capacity(n); for uuid in source { match uuid { None => { inner.extend(&[0; 16]); nulls.push(1); } Some(uuid) => { let mut buffer = *uuid.as_bytes(); buffer[..8].reverse(); buffer[8..].reverse(); inner.extend(&buffer); nulls.push(0); } } } let inner = Arc::new(IpColumnData::<Uuid> { inner, phantom: PhantomData }); let data = NullableColumnData { inner, nulls }; W::wrap(data) } } pub(crate) struct IpColumnData<V: IpVersion> { pub(crate) inner: Vec<u8>, pub(crate) phantom: PhantomData<V> } impl<V: IpVersion> IpColumnData<V> { pub fn with_capacity(capacity: usize) -> Self { Self { inner: vec![0; capacity * V::size()], phantom: PhantomData } } pub(crate) fn load<R: ReadEx>(reader: &mut R, size: usize) -> Result<Self> { let mut inner = vec![0; size * V::size()]; reader.read_bytes(inner.as_mut())?; Ok(Self { inner, phantom: PhantomData }) } } impl<V: IpVersion> ColumnData for IpColumnData<V> { fn sql_type(&self) -> SqlType { V::sql_type() } fn save(&self, encoder: &mut Encoder, start: usize, end: usize) { let start_index = start * V::size(); let end_index = end * V::size(); let slice: &[u8] = &self.inner.as_ref(); encoder.write_bytes(&slice[start_index..end_index]); } fn len(&self) -> usize { self.inner.len() / V::size() } fn push(&mut self, value: Value) { V::push(&mut self.inner, value) } fn at(&self, index: usize) -> ValueRef { V::get(&self.inner, index) } fn clone_instance(&self) -> BoxColumnData { Box::new(Self { inner: self.inner.clone(), phantom: PhantomData }) } unsafe fn get_internal(&self, pointers: &[*mut *const u8], level: u8) -> Result<()> { assert_eq!(level, 0); *pointers[0] = &self.inner as *const Vec<u8> as *const u8; Ok(()) } }