|
| 1 | +import io |
| 2 | +from typing import Optional, Type, TYPE_CHECKING, Union |
| 3 | +from types import TracebackType |
| 4 | +import warnings |
| 5 | + |
| 6 | +import tiledb.cc as lt |
| 7 | +from .ctx import default_ctx |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from .libtiledb import Ctx, Config |
| 11 | + |
| 12 | + |
| 13 | +class VFS(lt.VFS): |
| 14 | + """TileDB VFS class |
| 15 | +
|
| 16 | + Encapsulates the TileDB VFS module instance with a specific configuration (config). |
| 17 | +
|
| 18 | + :param tiledb.Ctx ctx: The TileDB Context |
| 19 | + :param config: Override `ctx` VFS configurations with updated values in config. |
| 20 | + :type config: tiledb.Config or dict |
| 21 | +
|
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, config: Union["Config", dict] = None, ctx: "Ctx" = None): |
| 25 | + self._ctx = ctx or default_ctx() |
| 26 | + cctx = lt.Context(self._ctx.__capsule__(), False) |
| 27 | + |
| 28 | + if config: |
| 29 | + from .libtiledb import Config |
| 30 | + |
| 31 | + if isinstance(config, Config): |
| 32 | + config = config.dict() |
| 33 | + else: |
| 34 | + try: |
| 35 | + config = dict(config) |
| 36 | + except: |
| 37 | + raise ValueError("`config` argument must be of type Config or dict") |
| 38 | + |
| 39 | + ccfg = lt.Config(config) |
| 40 | + super().__init__(cctx, ccfg) |
| 41 | + else: |
| 42 | + super().__init__(cctx) |
| 43 | + |
| 44 | + def open(self, uri: str, mode: str = "rb"): |
| 45 | + """Opens a VFS file resource for reading / writing / appends at URI |
| 46 | +
|
| 47 | + If the file did not exist upon opening, a new file is created. |
| 48 | +
|
| 49 | + :param str uri: URI of VFS file resource |
| 50 | + :param mode str: 'rb' for opening the file to read, 'wb' to write, 'ab' to append |
| 51 | + :rtype: FileHandle |
| 52 | + :return: TileDB FileIO |
| 53 | + :raises TypeError: cannot convert `uri` to unicode string |
| 54 | + :raises ValueError: invalid mode |
| 55 | + :raises: :py:exc:`tiledb.TileDBError` |
| 56 | +
|
| 57 | + """ |
| 58 | + return FileIO(self, uri, mode) |
| 59 | + |
| 60 | + def close(self, file: lt.FileHandle): |
| 61 | + """Closes a VFS FileHandle object |
| 62 | +
|
| 63 | + :param FileIO file: An opened VFS FileIO |
| 64 | + :rtype: FileIO |
| 65 | + :return: closed VFS FileHandle |
| 66 | + :raises: :py:exc:`tiledb.TileDBError` |
| 67 | +
|
| 68 | + """ |
| 69 | + if isinstance(file, FileIO): |
| 70 | + warnings.warn( |
| 71 | + f"`tiledb.VFS().open` now returns a a FileIO object. Use " |
| 72 | + "`FileIO.close`.", |
| 73 | + DeprecationWarning, |
| 74 | + ) |
| 75 | + file.close() |
| 76 | + return file |
| 77 | + |
| 78 | + def write(self, file: lt.FileHandle, buff: Union[str, bytes]): |
| 79 | + """Writes buffer to opened VFS FileHandle |
| 80 | +
|
| 81 | + :param FileHandle file: An opened VFS FileHandle in 'w' mode |
| 82 | + :param buff: a Python object that supports the byte buffer protocol |
| 83 | + :raises TypeError: cannot convert buff to bytes |
| 84 | + :raises: :py:exc:`tiledb.TileDBError` |
| 85 | +
|
| 86 | + """ |
| 87 | + if isinstance(file, FileIO): |
| 88 | + warnings.warn( |
| 89 | + f"`tiledb.VFS().open` now returns a a FileIO object. Use " |
| 90 | + "`FileIO.write`.", |
| 91 | + DeprecationWarning, |
| 92 | + ) |
| 93 | + if isinstance(buff, str): |
| 94 | + buff = buff.encode() |
| 95 | + file.write(buff) |
| 96 | + |
| 97 | + def read(self, file: lt.FileHandle, offset: int, nbytes: int): |
| 98 | + """Read nbytes from an opened VFS FileHandle at a given offset |
| 99 | +
|
| 100 | + :param FileHandle file: An opened VFS FileHandle in 'r' mode |
| 101 | + :param int offset: offset position in bytes to read from |
| 102 | + :param int nbytes: number of bytes to read |
| 103 | + :rtype: :py:func:`bytes` |
| 104 | + :return: read bytes |
| 105 | + :raises: :py:exc:`tiledb.TileDBError` |
| 106 | +
|
| 107 | + """ |
| 108 | + if isinstance(file, FileIO): |
| 109 | + warnings.warn( |
| 110 | + f"`tiledb.VFS().open` now returns a a FileIO object. Use " |
| 111 | + "`FileIO.seek` and `FileIO.read`.", |
| 112 | + DeprecationWarning, |
| 113 | + ) |
| 114 | + return file.read(nbytes) |
| 115 | + |
| 116 | + if nbytes == 0: |
| 117 | + return b"" |
| 118 | + |
| 119 | + return file.read(offset, nbytes) |
| 120 | + |
| 121 | + def supports(self, scheme: str) -> bool: |
| 122 | + """Returns true if the given URI scheme (storage backend) is supported |
| 123 | +
|
| 124 | + :param str scheme: scheme component of a VFS resource URI (ex. 'file' / 'hdfs' / 's3') |
| 125 | + :rtype: bool |
| 126 | + :return: True if the linked libtiledb version supports the storage backend, False otherwise |
| 127 | + :raises ValueError: VFS storage backend is not supported |
| 128 | +
|
| 129 | + """ |
| 130 | + if scheme == "file": |
| 131 | + return True |
| 132 | + |
| 133 | + scheme_to_fs_type = { |
| 134 | + "s3": lt.FileSystem.S3, |
| 135 | + "azure": lt.FileSystem.AZURE, |
| 136 | + "gcs": lt.FileSystem.GCS, |
| 137 | + "hdfs": lt.FileSystem.HDFS, |
| 138 | + } |
| 139 | + |
| 140 | + if scheme not in scheme_to_fs_type: |
| 141 | + raise ValueError(f"Unsupported VFS scheme '{scheme}://'") |
| 142 | + |
| 143 | + cctx = lt.Context(self._ctx.__capsule__(), False) |
| 144 | + return cctx.is_supported_fs(scheme_to_fs_type[scheme]) |
| 145 | + |
| 146 | + |
| 147 | +class FileIO(io.RawIOBase): |
| 148 | + def __init__(self, vfs: VFS, uri: str, mode: str = "rb"): |
| 149 | + self._vfs = vfs |
| 150 | + |
| 151 | + str_to_vfs_mode = { |
| 152 | + "rb": lt.VFSMode.READ, |
| 153 | + "wb": lt.VFSMode.WRITE, |
| 154 | + "ab": lt.VFSMode.APPEND, |
| 155 | + } |
| 156 | + if mode not in str_to_vfs_mode: |
| 157 | + raise ValueError(f"invalid mode {mode}") |
| 158 | + self._mode = mode |
| 159 | + |
| 160 | + self._fh = lt.FileHandle( |
| 161 | + self._vfs.ctx(), self._vfs, uri, str_to_vfs_mode[self._mode] |
| 162 | + ) |
| 163 | + self._offset = 0 |
| 164 | + self._nbytes = 0 |
| 165 | + |
| 166 | + if self._mode == "rb": |
| 167 | + try: |
| 168 | + self._nbytes = vfs.file_size(uri) |
| 169 | + except: |
| 170 | + raise IOError(f"URI {uri} is not a valid file") |
| 171 | + |
| 172 | + def __len__(self): |
| 173 | + return self._nbytes |
| 174 | + |
| 175 | + def __enter__(self): |
| 176 | + return self |
| 177 | + |
| 178 | + def __exit__( |
| 179 | + self, |
| 180 | + exc_type: Optional[Type[BaseException]], |
| 181 | + exc_val: Optional[BaseException], |
| 182 | + exc_tb: Optional[TracebackType], |
| 183 | + ) -> bool: |
| 184 | + self.flush() |
| 185 | + |
| 186 | + @property |
| 187 | + def mode(self): |
| 188 | + return self._mode |
| 189 | + |
| 190 | + def readable(self): |
| 191 | + return self._mode == "rb" |
| 192 | + |
| 193 | + def writable(self): |
| 194 | + return self._mode != "rb" |
| 195 | + |
| 196 | + @property |
| 197 | + def closed(self): |
| 198 | + return self._fh.closed |
| 199 | + |
| 200 | + def seekable(self): |
| 201 | + return True |
| 202 | + |
| 203 | + def flush(self): |
| 204 | + self._fh.flush() |
| 205 | + |
| 206 | + def seek(self, offset: int, whence: int = 0): |
| 207 | + if not isinstance(offset, int): |
| 208 | + raise TypeError( |
| 209 | + f"Offset must be an integer or None (got {safe_repr(offset)})" |
| 210 | + ) |
| 211 | + if whence == 0: |
| 212 | + if offset < 0: |
| 213 | + raise ValueError( |
| 214 | + "offset must be a positive or zero value when SEEK_SET" |
| 215 | + ) |
| 216 | + self._offset = offset |
| 217 | + elif whence == 1: |
| 218 | + self._offset += offset |
| 219 | + elif whence == 2: |
| 220 | + self._offset = self._nbytes + offset |
| 221 | + else: |
| 222 | + raise ValueError("whence must be equal to SEEK_SET, SEEK_START, SEEK_END") |
| 223 | + if self._offset < 0: |
| 224 | + self._offset = 0 |
| 225 | + elif self._offset > self._nbytes: |
| 226 | + self._offset = self._nbytes |
| 227 | + |
| 228 | + return self._offset |
| 229 | + |
| 230 | + def tell(self): |
| 231 | + return self._offset |
| 232 | + |
| 233 | + def read(self, size: int = -1): |
| 234 | + if not isinstance(size, int): |
| 235 | + raise TypeError(f"size must be an integer or None (got {safe_repr(size)})") |
| 236 | + if not self.readable(): |
| 237 | + raise IOError("Cannot read from write-only FileIO handle") |
| 238 | + if self.closed: |
| 239 | + raise IOError("Cannot read from closed FileIO handle") |
| 240 | + |
| 241 | + nbytes_left = self._nbytes - self._offset |
| 242 | + nbytes = nbytes_left if size < 0 or size > nbytes_left else size |
| 243 | + if nbytes == 0: |
| 244 | + return b"" |
| 245 | + |
| 246 | + buff = self._fh.read(self._offset, nbytes) |
| 247 | + self._offset += nbytes |
| 248 | + return buff |
| 249 | + |
| 250 | + def write(self, buff: bytes): |
| 251 | + if not self.writable(): |
| 252 | + raise IOError("cannot write to read-only FileIO handle") |
| 253 | + if isinstance(buff, str): |
| 254 | + buff = buff.encode() |
| 255 | + nbytes = len(buff) |
| 256 | + self._fh.write(buff) |
| 257 | + self._nbytes += nbytes |
| 258 | + self._offset += nbytes |
| 259 | + return nbytes |
0 commit comments