Skip to main content

mito2/sst/parquet/
helper.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::ops::Range;
16use std::time::Instant;
17
18use bytes::Bytes;
19use common_telemetry::trace;
20use object_store::ObjectStore;
21
22const FETCH_PARALLELISM: usize = 8;
23pub(crate) const MERGE_GAP: usize = 512 * 1024;
24
25/// Asynchronously fetches byte ranges from an object store.
26///
27/// * `FETCH_PARALLELISM` - The number of concurrent fetch operations.
28/// * `MERGE_GAP` - The maximum gap size (in bytes) to merge small byte ranges for optimized fetching.
29pub async fn fetch_byte_ranges(
30    file_path: &str,
31    object_store: ObjectStore,
32    ranges: &[Range<u64>],
33) -> object_store::Result<Vec<Bytes>> {
34    fetch_byte_ranges_with_gap(file_path, object_store, ranges, MERGE_GAP).await
35}
36
37pub(crate) async fn fetch_byte_ranges_with_gap(
38    file_path: &str,
39    object_store: ObjectStore,
40    ranges: &[Range<u64>],
41    gap: usize,
42) -> object_store::Result<Vec<Bytes>> {
43    let total_size = ranges.iter().map(|r| r.end - r.start).sum::<u64>();
44    let start = Instant::now();
45
46    let result = object_store
47        .reader_with(file_path)
48        .concurrent(FETCH_PARALLELISM)
49        .gap(gap)
50        .await?
51        .fetch(ranges.to_vec())
52        .await?
53        .into_iter()
54        .map(|buf| buf.to_bytes())
55        .collect::<Vec<_>>();
56
57    trace!(
58        "Fetch {} bytes from '{}' in object store, cost: {:?}",
59        total_size,
60        file_path,
61        start.elapsed()
62    );
63
64    Ok(result)
65}