use std::mem::size_of;
pub trait ByteSize {
fn est_bytes(&self) -> usize;
}
use crate::{Bitmask, Buffer, Vec64};
impl<T> ByteSize for Vec64<T> {
#[inline]
fn est_bytes(&self) -> usize {
self.capacity() * size_of::<T>()
}
}
impl<T> ByteSize for Buffer<T> {
#[inline]
fn est_bytes(&self) -> usize {
self.capacity() * size_of::<T>()
}
}
impl ByteSize for Bitmask {
#[inline]
fn est_bytes(&self) -> usize {
self.bits.est_bytes()
}
}
use crate::{BooleanArray, CategoricalArray, FloatArray, IntegerArray, StringArray};
impl<T> ByteSize for IntegerArray<T> {
#[inline]
fn est_bytes(&self) -> usize {
let data_bytes = self.data.est_bytes();
let mask_bytes = self.null_mask.as_ref().map_or(0, |m| m.est_bytes());
data_bytes + mask_bytes
}
}
impl<T> ByteSize for FloatArray<T> {
#[inline]
fn est_bytes(&self) -> usize {
let data_bytes = self.data.est_bytes();
let mask_bytes = self.null_mask.as_ref().map_or(0, |m| m.est_bytes());
data_bytes + mask_bytes
}
}
impl<T> ByteSize for StringArray<T> {
#[inline]
fn est_bytes(&self) -> usize {
let data_bytes = self.data.est_bytes();
let offsets_bytes = self.offsets.est_bytes();
let mask_bytes = self.null_mask.as_ref().map_or(0, |m| m.est_bytes());
data_bytes + offsets_bytes + mask_bytes
}
}
impl<T: crate::traits::type_unions::Integer> ByteSize for CategoricalArray<T> {
#[inline]
fn est_bytes(&self) -> usize {
let data_bytes = self.data.est_bytes();
let unique_values_bytes = self.unique_values.est_bytes();
let mask_bytes = self.null_mask.as_ref().map_or(0, |m| m.est_bytes());
data_bytes + unique_values_bytes + mask_bytes
}
}
impl<T> ByteSize for BooleanArray<T> {
#[inline]
fn est_bytes(&self) -> usize {
let data_bytes = self.data.est_bytes();
let mask_bytes = self.null_mask.as_ref().map_or(0, |m| m.est_bytes());
data_bytes + mask_bytes
}
}
#[cfg(feature = "datetime")]
use crate::DatetimeArray;
#[cfg(feature = "datetime")]
impl<T> ByteSize for DatetimeArray<T> {
#[inline]
fn est_bytes(&self) -> usize {
let data_bytes = self.data.est_bytes();
let mask_bytes = self.null_mask.as_ref().map_or(0, |m| m.est_bytes());
data_bytes + mask_bytes
}
}
use crate::{NumericArray, TextArray};
impl ByteSize for NumericArray {
fn est_bytes(&self) -> usize {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(arr) => arr.est_bytes(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(arr) => arr.est_bytes(),
NumericArray::Int32(arr) => arr.est_bytes(),
NumericArray::Int64(arr) => arr.est_bytes(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(arr) => arr.est_bytes(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(arr) => arr.est_bytes(),
NumericArray::UInt32(arr) => arr.est_bytes(),
NumericArray::UInt64(arr) => arr.est_bytes(),
NumericArray::Float32(arr) => arr.est_bytes(),
NumericArray::Float64(arr) => arr.est_bytes(),
NumericArray::Null => 0,
}
}
}
impl ByteSize for TextArray {
fn est_bytes(&self) -> usize {
match self {
TextArray::String32(arr) => arr.est_bytes(),
#[cfg(feature = "large_string")]
TextArray::String64(arr) => arr.est_bytes(),
#[cfg(feature = "default_categorical_8")]
TextArray::Categorical8(arr) => arr.est_bytes(),
#[cfg(feature = "extended_categorical")]
TextArray::Categorical16(arr) => arr.est_bytes(),
#[cfg(any(not(feature = "default_categorical_8"), feature = "extended_categorical"))]
TextArray::Categorical32(arr) => arr.est_bytes(),
#[cfg(feature = "extended_categorical")]
TextArray::Categorical64(arr) => arr.est_bytes(),
TextArray::Null => 0,
}
}
}
#[cfg(feature = "datetime")]
use crate::TemporalArray;
#[cfg(feature = "datetime")]
impl ByteSize for TemporalArray {
fn est_bytes(&self) -> usize {
match self {
TemporalArray::Datetime32(arr) => arr.est_bytes(),
TemporalArray::Datetime64(arr) => arr.est_bytes(),
TemporalArray::Null => 0,
}
}
}
use crate::Array;
impl ByteSize for Array {
fn est_bytes(&self) -> usize {
match self {
Array::NumericArray(arr) => arr.est_bytes(),
Array::TextArray(arr) => arr.est_bytes(),
#[cfg(feature = "datetime")]
Array::TemporalArray(arr) => arr.est_bytes(),
Array::BooleanArray(arr) => arr.est_bytes(),
Array::Null => 0,
}
}
}
use crate::{Field, FieldArray, Table};
impl ByteSize for Field {
#[inline]
fn est_bytes(&self) -> usize {
self.name.capacity()
}
}
impl ByteSize for FieldArray {
#[inline]
fn est_bytes(&self) -> usize {
self.field.est_bytes() + self.array.est_bytes()
}
}
impl ByteSize for Table {
fn est_bytes(&self) -> usize {
self.cols.iter().map(|col| col.est_bytes()).sum()
}
}
#[cfg(feature = "views")]
use crate::{ArrayV, TableV};
#[cfg(feature = "views")]
impl ByteSize for ArrayV {
fn est_bytes(&self) -> usize {
let full_len = self.array.len();
let full_bytes = self.array.est_bytes();
if full_len > 0 {
(full_bytes * self.len()) / full_len
} else {
0
}
}
}
#[cfg(feature = "views")]
impl ByteSize for TableV {
fn est_bytes(&self) -> usize {
self.cols.iter().map(|col| col.est_bytes()).sum()
}
}
#[cfg(all(feature = "chunked", feature = "views"))]
use crate::{SuperArrayV, SuperTableV};
#[cfg(all(feature = "chunked", feature = "views"))]
impl ByteSize for SuperArrayV {
fn est_bytes(&self) -> usize {
self.slices.iter().map(|slice| slice.est_bytes()).sum()
}
}
#[cfg(all(feature = "chunked", feature = "views"))]
impl ByteSize for SuperTableV {
fn est_bytes(&self) -> usize {
self.slices.iter().map(|slice| slice.est_bytes()).sum()
}
}
#[cfg(feature = "matrix")]
use crate::Matrix;
#[cfg(feature = "matrix")]
impl ByteSize for Matrix {
fn est_bytes(&self) -> usize {
self.data.est_bytes()
}
}
#[cfg(feature = "cube")]
use crate::Cube;
#[cfg(feature = "cube")]
impl ByteSize for Cube {
fn est_bytes(&self) -> usize {
self.tables.iter().map(|tbl| tbl.est_bytes()).sum()
}
}
#[cfg(feature = "chunked")]
use crate::SuperArray;
#[cfg(feature = "chunked")]
impl ByteSize for SuperArray {
fn est_bytes(&self) -> usize {
self.chunks().iter().map(|chunk| chunk.est_bytes()).sum()
}
}
#[cfg(feature = "chunked")]
use crate::SuperTable;
#[cfg(feature = "chunked")]
impl ByteSize for SuperTable {
fn est_bytes(&self) -> usize {
self.batches.iter().map(|batch| batch.est_bytes()).sum()
}
}
#[cfg(feature = "value_type")]
use crate::Value;
#[cfg(feature = "value_type")]
#[cfg(feature = "scalar_type")]
use crate::Scalar;
#[cfg(feature = "value_type")]
#[cfg(feature = "scalar_type")]
impl ByteSize for Scalar {
#[inline]
fn est_bytes(&self) -> usize {
match self {
Scalar::String32(s) => s.capacity(),
#[cfg(feature = "large_string")]
Scalar::String64(s) => s.capacity(),
_ => 0, }
}
}
#[cfg(feature = "value_type")]
impl ByteSize for Value {
fn est_bytes(&self) -> usize {
match self {
#[cfg(feature = "scalar_type")]
Value::Scalar(s) => s.est_bytes(),
Value::Array(arr) => arr.est_bytes(),
#[cfg(feature = "views")]
Value::ArrayView(av) => av.est_bytes(),
Value::Table(tbl) => tbl.est_bytes(),
#[cfg(feature = "views")]
Value::TableView(tv) => tv.est_bytes(),
#[cfg(feature = "chunked")]
Value::SuperArray(sa) => sa.est_bytes(),
#[cfg(all(feature = "chunked", feature = "views"))]
Value::SuperArrayView(sav) => sav.est_bytes(),
#[cfg(feature = "chunked")]
Value::SuperTable(st) => st.est_bytes(),
#[cfg(all(feature = "chunked", feature = "views"))]
Value::SuperTableView(stv) => stv.est_bytes(),
Value::FieldArray(fa) => fa.est_bytes(),
#[cfg(feature = "matrix")]
Value::Matrix(m) => m.est_bytes(),
#[cfg(feature = "cube")]
Value::Cube(c) => c.est_bytes(),
Value::VecValue(vec) => {
vec.iter().map(|v| v.est_bytes()).sum::<usize>()
+ vec.capacity() * size_of::<Value>() }
Value::BoxValue(boxed) => boxed.est_bytes(),
Value::ArcValue(arc) => arc.est_bytes(),
Value::Tuple2(tuple) => tuple.0.est_bytes() + tuple.1.est_bytes(),
Value::Tuple3(tuple) => tuple.0.est_bytes() + tuple.1.est_bytes() + tuple.2.est_bytes(),
Value::Tuple4(tuple) => {
tuple.0.est_bytes()
+ tuple.1.est_bytes()
+ tuple.2.est_bytes()
+ tuple.3.est_bytes()
}
Value::Tuple5(tuple) => {
tuple.0.est_bytes()
+ tuple.1.est_bytes()
+ tuple.2.est_bytes()
+ tuple.3.est_bytes()
+ tuple.4.est_bytes()
}
Value::Tuple6(tuple) => {
tuple.0.est_bytes()
+ tuple.1.est_bytes()
+ tuple.2.est_bytes()
+ tuple.3.est_bytes()
+ tuple.4.est_bytes()
+ tuple.5.est_bytes()
}
Value::Custom(_) => {
size_of::<std::sync::Arc<dyn crate::traits::custom_value::CustomValue>>()
}
}
}
}