Skip to content

Commit

Permalink
Move EstimateMemory trait into own memory.rs file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefffrey committed Aug 21, 2024
1 parent 2db87db commit 38ceeca
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 24 deletions.
6 changes: 2 additions & 4 deletions src/arrow_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ use snafu::{ensure, ResultExt};

use crate::{
error::{IoSnafu, Result, UnexpectedSnafu},
memory::EstimateMemory,
proto,
writer::{
column::EstimateMemory,
stripe::{StripeInformation, StripeWriter},
},
writer::stripe::{StripeInformation, StripeWriter},
};

/// Construct an [`ArrowWriter`] to encode [`RecordBatch`]es into a single
Expand Down
5 changes: 1 addition & 4 deletions src/encoding/byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

use bytes::{BufMut, BytesMut};

use crate::{
error::Result,
writer::column::{EstimateMemory, PrimitiveValueEncoder},
};
use crate::{error::Result, memory::EstimateMemory, writer::column::PrimitiveValueEncoder};
use std::io::Read;

use super::util::read_u8;
Expand Down
3 changes: 2 additions & 1 deletion src/encoding/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use snafu::ResultExt;

use crate::{
error::{self, Result},
writer::column::{EstimateMemory, PrimitiveValueEncoder},
memory::EstimateMemory,
writer::column::PrimitiveValueEncoder,
};

/// Generically represent f32 and f64.
Expand Down
5 changes: 1 addition & 4 deletions src/encoding/rle_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ use std::{io::Read, marker::PhantomData};

use bytes::BytesMut;

use crate::{
error::Result,
writer::column::{EstimateMemory, PrimitiveValueEncoder},
};
use crate::{error::Result, memory::EstimateMemory, writer::column::PrimitiveValueEncoder};

use self::{
delta::{read_delta_values, write_fixed_delta, write_varying_delta},
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod async_arrow_reader;
mod column;
mod encoding;
pub mod error;
mod memory;
pub mod projection;
mod proto;
pub mod reader;
Expand Down
23 changes: 23 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

/// Estimating memory usage is important when writing files, as we finish
/// writing a stripe according to a set size threshold.
pub trait EstimateMemory {
/// Approximate current memory usage in bytes.
fn estimate_memory_size(&self) -> usize;
}
8 changes: 1 addition & 7 deletions src/writer/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,12 @@ use crate::{
byte::ByteRleWriter, float::FloatValueEncoder, rle_v2::RleWriterV2, SignedEncoding,
},
error::Result,
memory::EstimateMemory,
writer::StreamType,
};

use super::{ColumnEncoding, PresentStreamEncoder, Stream};

/// Used to help determine when to finish writing a stripe once a certain
/// size threshold has been reached.
pub trait EstimateMemory {
/// Approximate current memory usage in bytes.
fn estimate_memory_size(&self) -> usize;
}

/// Encodes a specific column for a stripe. Will encode to an internal memory
/// buffer until it is finished, in which case it returns the stream bytes to
/// be serialized to a writer.
Expand Down
4 changes: 2 additions & 2 deletions src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use std::fmt::Debug;
use arrow::{array::BooleanBufferBuilder, buffer::NullBuffer};
use bytes::Bytes;

use crate::{encoding::byte::ByteRleWriter, proto};
use crate::{encoding::byte::ByteRleWriter, memory::EstimateMemory, proto};

use self::column::{EstimateMemory, PrimitiveValueEncoder};
use self::column::PrimitiveValueEncoder;

pub mod column;
pub mod stripe;
Expand Down
5 changes: 3 additions & 2 deletions src/writer/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ use prost::Message;
use snafu::ResultExt;

use crate::error::{IoSnafu, Result};
use crate::memory::EstimateMemory;
use crate::proto;

use super::column::{
ByteStripeEncoder, ColumnStripeEncoder, DoubleStripeEncoder, EstimateMemory,
FloatStripeEncoder, Int16StripeEncoder, Int32StripeEncoder, Int64StripeEncoder,
ByteStripeEncoder, ColumnStripeEncoder, DoubleStripeEncoder, FloatStripeEncoder,
Int16StripeEncoder, Int32StripeEncoder, Int64StripeEncoder,
};
use super::{ColumnEncoding, StreamType};

Expand Down

0 comments on commit 38ceeca

Please sign in to comment.