datafusion_execution/spill_file.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use bytes::Bytes;
19use datafusion_common::Result;
20use futures::Stream;
21use std::path::Path;
22use std::pin::Pin;
23use std::sync::Arc;
24
25/// Abstraction over a spill file backend.
26/// Implementations handle their own quota enforcement and blocking concerns.
27pub trait SpillFile: Send + Sync {
28 /// Returns the OS path if this is a local file, None otherwise.
29 fn path(&self) -> Option<&Path> {
30 None
31 }
32
33 /// Returns current size in bytes if cheaply available.
34 fn size(&self) -> Option<u64>;
35
36 /// Returns file contents as an async stream of byte chunks.
37 fn read_stream(&self) -> Result<Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>>;
38
39 /// Opens a writer for appending data to this file.
40 fn open_writer(&self) -> Result<Box<dyn SpillWriter>>;
41}
42
43/// Writer for spill file backends.
44pub trait SpillWriter: std::io::Write + Send {
45 /// Intended for close/sync/commit operations.
46 fn finish(&mut self) -> Result<()>;
47}
48
49/// Factory for creating spill files.
50pub trait TempFileFactory: Send + Sync {
51 fn create_temp_file(&self, description: &str) -> Result<Arc<dyn SpillFile>>;
52}