Built-in I/O backends¶
Import these backends from guideloom.io and pass an instance as read_interface
to a storage constructor. They control how shard bytes are read; they do not
change the stored format or the write path. All three provide synchronous and
asynchronous reader factories.
| Backend | Read behavior | Availability |
|---|---|---|
SeekIO |
Opens, seeks, and reads into a writable buffer for each request. Default when read_interface is omitted. |
All supported platforms. |
MmapIO |
Keeps a read-only memory map and copies each requested range into a writable buffer. | All supported platforms. |
PosixPreadIO |
Keeps a file descriptor and reads directly into a writable buffer at an explicit offset using os.preadv. |
POSIX with os.preadv; check is_supported(). |
For PosixPreadIO(advice=True), check is_supported(advice=True) first. This also
requires posix_fadvise and requests POSIX_FADV_RANDOM when opening files.
Choose a backend¶
This example falls back to the portable default when positional reads are unavailable:
from pathlib import Path
from tempfile import TemporaryDirectory
import numpy as np
from guideloom import StreamNumpyFormat, StreamStorage
from guideloom.io import PosixPreadIO, SeekIO
backend = PosixPreadIO() if PosixPreadIO.is_supported() else SeekIO()
with TemporaryDirectory() as directory:
storage = StreamStorage(
Path(directory),
StreamNumpyFormat(np.dtype([("tokens", "<i4")])),
read_interface=backend,
)
with storage.sync_writer() as writer:
writer.append(0, {"tokens": np.arange(8, dtype=np.int32)})
with storage.sync_reader() as reader:
assert reader.read(2, 4)["tokens"].tolist() == [2, 3, 4, 5]
See performance tips for tuning. To implement another backend, see custom I/O backends and the I/O reader interfaces.
Backend API¶
sync_reader() and async_reader() return reader constructors, which accept a
file path. Storage opens and closes those readers for you. The returned readers
expose filesize() and read(offset, size) through the
reader interfaces.
guideloom.io.SeekIO
¶
Bases: IOReaderAbstract[Path]
Read shard bytes by seeking within the file for each read.
This backend is supported on every platform. Storage uses it when read_interface is omitted.
is_supported() -> bool
staticmethod
¶
Return whether this backend can be constructed.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Always True. |
sync_reader() -> Callable[[pathlib.Path], _SeekIOSync]
¶
Return a constructor for a synchronous reader of one file.
Returns:
| Type | Description |
|---|---|
Callable[[Path], _SeekIOSync]
|
Callable[[pathlib.Path], IOSyncReaderAbstract]: Called with the file path. |
async_reader() -> Callable[[pathlib.Path], _SeekIOAsync]
¶
Return a constructor for an asynchronous reader of one file.
Returns:
| Type | Description |
|---|---|
Callable[[Path], _SeekIOAsync]
|
Callable[[pathlib.Path], IOAsyncReaderAbstract]: Called with the file path. |
guideloom.io.MmapIO
¶
Bases: IOReaderAbstract[Path]
Read shard bytes from a read-only memory map.
This backend is supported on every platform. Each read copies the requested range out of the map.
is_supported() -> bool
staticmethod
¶
Return whether this backend can be constructed.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Always True. |
sync_reader() -> Callable[[pathlib.Path], _MmapIOSync]
¶
Return a constructor for a synchronous reader of one file.
Returns:
| Type | Description |
|---|---|
Callable[[Path], _MmapIOSync]
|
Callable[[pathlib.Path], IOSyncReaderAbstract]: Called with the file path. |
async_reader() -> Callable[[pathlib.Path], _MmapIOAsync]
¶
Return a constructor for an asynchronous reader of one file.
Returns:
| Type | Description |
|---|---|
Callable[[Path], _MmapIOAsync]
|
Callable[[pathlib.Path], IOAsyncReaderAbstract]: Called with the file path. |
guideloom.io.PosixPreadIO
¶
Bases: IOReaderAbstract[Path]
Read shard bytes with os.preadv.
Available on POSIX platforms that provide os.preadv.
__init__(*, advice: bool = False) -> None
¶
Create a preadv backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
advice
|
bool
|
When True, call |
False
|
Raises:
| Type | Description |
|---|---|
OSError
|
If |
is_supported(*, advice: bool = False) -> bool
staticmethod
¶
Return whether this platform can construct PosixPreadIO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
advice
|
bool
|
Also require |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True when |
sync_reader() -> Callable[[pathlib.Path], _PosixPreadIOSync]
¶
Return a constructor for a synchronous reader of one file.
The constructor receives the advice setting from this backend.
Returns:
| Type | Description |
|---|---|
Callable[[Path], _PosixPreadIOSync]
|
Callable[[pathlib.Path], IOSyncReaderAbstract]: Called with the file path. |
async_reader() -> Callable[[pathlib.Path], _PosixPreadIOAsync]
¶
Return a constructor for an asynchronous reader of one file.
The constructor receives the advice setting from this backend.
Returns:
| Type | Description |
|---|---|
Callable[[Path], _PosixPreadIOAsync]
|
Callable[[pathlib.Path], IOAsyncReaderAbstract]: Called with the file path. |