Skip to content

Commit 52ffd0f

Browse files
authored
chore: Rename Segment to SegmentSpec (#2715)
I was reading through the writing code and I think these names are clearer, we use `segments/segment` a bunch for different things, so this is an attempt to make things more distinct.
1 parent 1df5528 commit 52ffd0f

File tree

24 files changed

+179
-166
lines changed

24 files changed

+179
-166
lines changed

vortex-datafusion/src/persistent/cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use vortex_array::aliases::DefaultHashBuilder;
1010
use vortex_array::stats::{Precision, Stat};
1111
use vortex_dtype::DType;
1212
use vortex_error::{VortexError, VortexResult, vortex_err};
13-
use vortex_file::{Footer, Segment, VortexOpenOptions};
13+
use vortex_file::{Footer, SegmentSpec, VortexOpenOptions};
1414
use vortex_io::ObjectStoreReadAt;
1515
use vortex_layout::LayoutRegistry;
1616
use vortex_layout::segments::SegmentId;
@@ -39,7 +39,7 @@ impl From<&ObjectMeta> for Key {
3939

4040
/// Approximate the in-memory size of a layout
4141
fn estimate_layout_size(footer: &Footer) -> usize {
42-
let segments_size = footer.segment_map().len() * size_of::<Segment>();
42+
let segments_size = footer.segment_map().len() * size_of::<SegmentSpec>();
4343
let stats_size = footer
4444
.statistics()
4545
.iter()

vortex-file/src/footer/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Footer {
2222
ctx: ArrayContext,
2323
layout_ctx: LayoutContext,
2424
layout: Layout,
25-
segments: Arc<[Segment]>,
25+
segments: Arc<[SegmentSpec]>,
2626
statistics: Option<Arc<[StatsSet]>>,
2727
}
2828

@@ -36,7 +36,7 @@ impl Footer {
3636
ctx: ArrayContext,
3737
layout_ctx: LayoutContext,
3838
root_layout: Layout,
39-
segments: Arc<[Segment]>,
39+
segments: Arc<[SegmentSpec]>,
4040
statistics: Option<Arc<[StatsSet]>>,
4141
) -> Self {
4242
// Note this assertion is `<=` since we allow zero-length segments
@@ -108,7 +108,10 @@ impl Footer {
108108
let fb_segments = fb
109109
.segments()
110110
.ok_or_else(|| vortex_err!("Footer missing segments"))?;
111-
let segments = fb_segments.iter().map(Segment::try_from).try_collect()?;
111+
let segments = fb_segments
112+
.iter()
113+
.map(SegmentSpec::try_from)
114+
.try_collect()?;
112115

113116
let statistics = fb
114117
.statistics()
@@ -144,7 +147,7 @@ impl Footer {
144147
}
145148

146149
/// Returns the segment map of the file.
147-
pub fn segment_map(&self) -> &Arc<[Segment]> {
150+
pub fn segment_map(&self) -> &Arc<[SegmentSpec]> {
148151
&self.segments
149152
}
150153

@@ -168,7 +171,7 @@ impl Footer {
168171
pub(crate) fn flatbuffer_writer<'a>(
169172
ctx: ArrayContext,
170173
layout: Layout,
171-
segments: Arc<[Segment]>,
174+
segments: Arc<[SegmentSpec]>,
172175
statistics: Option<Arc<[StatsSet]>>,
173176
) -> impl WriteFlatBuffer<Target<'a> = fb::Footer<'a>> + FlatBufferRoot {
174177
FooterFlatBufferWriter {
@@ -183,7 +186,7 @@ impl Footer {
183186
pub(crate) struct FooterFlatBufferWriter {
184187
ctx: ArrayContext,
185188
layout: Layout,
186-
segments: Arc<[Segment]>,
189+
segments: Arc<[SegmentSpec]>,
187190
statistics: Option<Arc<[StatsSet]>>,
188191
}
189192

@@ -200,7 +203,7 @@ impl WriteFlatBuffer for FooterFlatBufferWriter {
200203
let layout_ctx = LayoutContext::empty();
201204
let layout = self.layout.write_flatbuffer(fbb, &layout_ctx);
202205

203-
let segments = fbb.create_vector_from_iter(self.segments.iter().map(fb::Segment::from));
206+
let segments = fbb.create_vector_from_iter(self.segments.iter().map(fb::SegmentSpec::from));
204207
let statistics = self
205208
.statistics
206209
.as_ref()

vortex-file/src/footer/postscript.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use flatbuffers::Follow;
22
use vortex_error::{VortexError, vortex_err};
33
use vortex_flatbuffers::{FlatBufferRoot, ReadFlatBuffer, WriteFlatBuffer, footer as fb};
44

5-
use crate::footer::segment::Segment;
5+
use crate::footer::segment::SegmentSpec;
66

77
/// Captures the layout information of a Vortex file.
88
pub(crate) struct Postscript {
9-
pub(crate) dtype: Option<Segment>,
10-
pub(crate) footer: Segment,
9+
pub(crate) dtype: Option<SegmentSpec>,
10+
pub(crate) footer: SegmentSpec,
1111
}
1212

1313
impl FlatBufferRoot for Postscript {}
@@ -19,8 +19,8 @@ impl WriteFlatBuffer for Postscript {
1919
&self,
2020
fbb: &mut flatbuffers::FlatBufferBuilder<'fb>,
2121
) -> flatbuffers::WIPOffset<Self::Target<'fb>> {
22-
let dtype = self.dtype.as_ref().map(fb::Segment::from);
23-
let footer = fb::Segment::from(&self.footer);
22+
let dtype = self.dtype.as_ref().map(fb::SegmentSpec::from);
23+
let footer = fb::SegmentSpec::from(&self.footer);
2424
fb::Postscript::create(
2525
fbb,
2626
&fb::PostscriptArgs {
@@ -39,8 +39,8 @@ impl ReadFlatBuffer for Postscript {
3939
fb: &<Self::Source<'buf> as Follow<'buf>>::Inner,
4040
) -> Result<Self, Self::Error> {
4141
Ok(Self {
42-
dtype: fb.dtype().map(Segment::try_from).transpose()?,
43-
footer: Segment::try_from(
42+
dtype: fb.dtype().map(SegmentSpec::try_from).transpose()?,
43+
footer: SegmentSpec::try_from(
4444
fb.footer()
4545
.ok_or_else(|| vortex_err!("Postscript missing footer segment"))?,
4646
)?,

vortex-file/src/footer/segment.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ use vortex_flatbuffers::footer as fb;
44

55
/// The location of a segment within a Vortex file.
66
#[derive(Clone, Debug)]
7-
pub struct Segment {
7+
pub struct SegmentSpec {
88
pub offset: u64,
99
pub length: u32,
1010
pub alignment: Alignment,
1111
}
1212

13-
impl From<&Segment> for fb::Segment {
14-
fn from(value: &Segment) -> Self {
15-
fb::Segment::new(value.offset, value.length, value.alignment.exponent(), 0, 0)
13+
impl From<&SegmentSpec> for fb::SegmentSpec {
14+
fn from(value: &SegmentSpec) -> Self {
15+
fb::SegmentSpec::new(value.offset, value.length, value.alignment.exponent(), 0, 0)
1616
}
1717
}
1818

19-
impl TryFrom<&fb::Segment> for Segment {
19+
impl TryFrom<&fb::SegmentSpec> for SegmentSpec {
2020
type Error = VortexError;
2121

22-
fn try_from(value: &fb::Segment) -> Result<Self, Self::Error> {
22+
fn try_from(value: &fb::SegmentSpec) -> Result<Self, Self::Error> {
2323
Ok(Self {
2424
offset: value.offset(),
2525
length: value.length(),

vortex-file/src/generic.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use vortex_layout::scan::ScanDriver;
1919
use vortex_layout::segments::{AsyncSegmentReader, SegmentEvent, SegmentId, SegmentStream};
2020
use vortex_metrics::{Counter, VortexMetrics};
2121

22-
use crate::footer::{Footer, Segment};
22+
use crate::footer::{Footer, SegmentSpec};
2323
use crate::segments::channel::SegmentChannel;
2424
use crate::segments::{InMemorySegmentCache, SegmentCache};
2525
use crate::{FileType, VortexOpenOptions};
@@ -344,20 +344,20 @@ where
344344
#[derive(Debug)]
345345
struct SegmentRequest {
346346
id: SegmentId,
347-
location: Segment,
347+
spec: SegmentSpec,
348348
cancel_handle: oneshot::Receiver<()>,
349349
}
350350

351351
impl SegmentRequest {
352-
fn new(id: SegmentId, location: Segment, cancel_handle: oneshot::Receiver<()>) -> Self {
352+
fn new(id: SegmentId, spec: SegmentSpec, cancel_handle: oneshot::Receiver<()>) -> Self {
353353
Self {
354354
id,
355-
location,
355+
spec,
356356
cancel_handle,
357357
}
358358
}
359359
fn range(&self) -> Range<u64> {
360-
self.location.offset..self.location.offset + self.location.length as u64
360+
self.spec.offset..self.spec.offset + self.spec.length as u64
361361
}
362362
}
363363

@@ -366,7 +366,7 @@ async fn filter_with_cache(
366366
cache: Arc<dyn SegmentCache>,
367367
inflight_segments: InflightSegments,
368368
) -> Option<SegmentRequest> {
369-
match cache.get(request.id, request.location.alignment).await {
369+
match cache.get(request.id, request.spec.alignment).await {
370370
Ok(None) => {}
371371
Ok(Some(buffer)) => {
372372
inflight_segments.complete(request.id, Ok(buffer));
@@ -389,7 +389,7 @@ struct CoalescedSegmentRequest {
389389
/// The range of the file to read.
390390
pub(crate) byte_range: Range<u64>,
391391
/// The original segment requests, ordered by segment ID.
392-
pub(crate) requests: Vec<(SegmentId, Segment)>,
392+
pub(crate) requests: Vec<(SegmentId, SegmentSpec)>,
393393
}
394394

395395
#[derive(Default)]
@@ -430,7 +430,7 @@ impl CoalescedCancellationHandle {
430430
async fn evaluate<R: VortexReadAt>(
431431
read: R,
432432
request: CoalescedSegmentRequest,
433-
segment_map: Arc<[Segment]>,
433+
segment_map: Arc<[SegmentSpec]>,
434434
segment_cache: Arc<dyn SegmentCache>,
435435
inflight_segments: InflightSegments,
436436
) -> VortexResult<()> {
@@ -516,7 +516,7 @@ fn coalesce(
516516
// to ensure correct alignment for all coalesced buffers.
517517
alignment: requests
518518
.first()
519-
.map(|r| r.location.alignment)
519+
.map(|r| r.spec.alignment)
520520
.unwrap_or(Alignment::none()),
521521
byte_range: range.clone(),
522522
requests: vec![],
@@ -531,9 +531,9 @@ fn coalesce(
531531
.collect::<Vec<_>>();
532532

533533
for req in requests {
534-
let idx = fetch_ranges.partition_point(|v| v.start <= req.location.offset) - 1;
534+
let idx = fetch_ranges.partition_point(|v| v.start <= req.spec.offset) - 1;
535535
let (ref mut request, ref mut cancellation) = coalesced[idx];
536-
request.requests.push((req.id, req.location));
536+
request.requests.push((req.id, req.spec));
537537
cancellation.push(req.cancel_handle);
538538
}
539539

vortex-file/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ mod writer;
105105
use std::sync::{Arc, LazyLock};
106106

107107
pub use file::*;
108-
pub use footer::{Footer, Segment};
108+
pub use footer::{Footer, SegmentSpec};
109109
pub use forever_constant::*;
110110
pub use generic::*;
111111
pub use memory::*;

vortex-file/src/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use vortex_layout::segments::{AsyncSegmentReader, SegmentId, SegmentStream};
99
use vortex_metrics::VortexMetrics;
1010

1111
use crate::segments::SegmentCache;
12-
use crate::{FileType, Footer, Segment, VortexOpenOptions};
12+
use crate::{FileType, Footer, SegmentSpec, VortexOpenOptions};
1313

1414
/// A Vortex file that is backed by an in-memory buffer.
1515
///
@@ -60,7 +60,7 @@ impl ScanDriver for InMemoryVortexFile {
6060
#[async_trait]
6161
impl AsyncSegmentReader for InMemoryVortexFile {
6262
async fn get(&self, id: SegmentId) -> VortexResult<ByteBuffer> {
63-
let segment: &Segment = self
63+
let segment: &SegmentSpec = self
6464
.footer
6565
.segment_map()
6666
.get(*id as usize)

vortex-file/src/open.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use vortex_layout::segments::SegmentId;
1515
use vortex_layout::{LayoutRegistry, LayoutRegistryExt};
1616
use vortex_metrics::VortexMetrics;
1717

18-
use crate::footer::{Footer, Postscript, Segment};
18+
use crate::footer::{Footer, Postscript, SegmentSpec};
1919
use crate::segments::{NoOpSegmentCache, SegmentCache};
2020
use crate::{DEFAULT_REGISTRY, EOF_SIZE, MAGIC_BYTES, MAX_FOOTER_SIZE, VERSION, VortexFile};
2121

@@ -272,7 +272,7 @@ impl<F: FileType> VortexOpenOptions<F> {
272272
&self,
273273
initial_offset: u64,
274274
initial_read: &ByteBuffer,
275-
dtype: Segment,
275+
dtype: SegmentSpec,
276276
) -> VortexResult<DType> {
277277
let offset = usize::try_from(dtype.offset - initial_offset)?;
278278
let sliced_buffer =
@@ -287,7 +287,7 @@ impl<F: FileType> VortexOpenOptions<F> {
287287
&self,
288288
initial_offset: u64,
289289
initial_read: &ByteBuffer,
290-
segment: Segment,
290+
segment: SegmentSpec,
291291
dtype: DType,
292292
) -> VortexResult<Footer> {
293293
let offset = usize::try_from(segment.offset - initial_offset)?;

vortex-file/src/segments/writer.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ use vortex_error::{VortexResult, vortex_err};
33
use vortex_io::VortexWrite;
44
use vortex_layout::segments::{SegmentId, SegmentWriter};
55

6-
use crate::footer::Segment;
6+
use crate::footer::SegmentSpec;
77

88
/// A segment writer that holds buffers in memory until they are flushed by a writer.
99
#[derive(Default)]
1010
pub(crate) struct BufferedSegmentWriter {
11-
segments: Vec<Vec<ByteBuffer>>,
11+
/// A Vec byte buffers for segments
12+
segments_buffers: Vec<Vec<ByteBuffer>>,
1213
next_id: SegmentId,
1314
}
1415

1516
impl SegmentWriter for BufferedSegmentWriter {
1617
fn put(&mut self, data: &[ByteBuffer]) -> SegmentId {
17-
self.segments.push(data.to_vec());
18+
self.segments_buffers.push(data.to_vec());
1819
let id = self.next_id;
1920
self.next_id = SegmentId::from(*self.next_id + 1);
2021
id
@@ -25,10 +26,10 @@ impl BufferedSegmentWriter {
2526
/// Flush the segments to the provided async writer.
2627
pub async fn flush_async<W: VortexWrite>(
2728
&mut self,
28-
write: &mut futures::io::Cursor<W>,
29-
segments: &mut Vec<Segment>,
29+
writer: &mut futures::io::Cursor<W>,
30+
segment_descriptions: &mut Vec<SegmentSpec>,
3031
) -> VortexResult<()> {
31-
for buffers in self.segments.drain(..) {
32+
for buffers in self.segments_buffers.drain(..) {
3233
// The API requires us to write these buffers contiguously. Therefore, we can only
3334
// respect the alignment of the first one.
3435
// Don't worry, in most cases the caller knows what they're doing and will align the
@@ -39,22 +40,22 @@ impl BufferedSegmentWriter {
3940
.unwrap_or_else(Alignment::none);
4041

4142
// Add any padding required to align the segment.
42-
let offset = write.position();
43+
let offset = writer.position();
4344
let padding = offset.next_multiple_of(*alignment as u64) - offset;
4445
if padding > 0 {
45-
write
46+
writer
4647
.write_all(ByteBuffer::zeroed(padding as usize))
4748
.await?;
4849
}
49-
let offset = write.position();
50+
let offset = writer.position();
5051

5152
for buffer in buffers {
52-
write.write_all(buffer).await?;
53+
writer.write_all(buffer).await?;
5354
}
5455

55-
segments.push(Segment {
56+
segment_descriptions.push(SegmentSpec {
5657
offset,
57-
length: u32::try_from(write.position() - offset)
58+
length: u32::try_from(writer.position() - offset)
5859
.map_err(|_| vortex_err!("segment length exceeds maximum u32"))?,
5960
alignment,
6061
});

0 commit comments

Comments
 (0)