From 42c8e3daa85b52063966877bf59f626f2b88d90c Mon Sep 17 00:00:00 2001 From: Agisilaos Kounelis <36283973+kounelisagis@users.noreply.github.com> Date: Sat, 2 Mar 2024 00:02:25 +0200 Subject: [PATCH] Fix Filestore unable to read sizes smaller than offset (#1907) * Fix filestore unable to read sizes smaller than offset * add test for filestore using small buffer --- tiledb/filestore.py | 2 +- tiledb/tests/test_filestore.py | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tiledb/filestore.py b/tiledb/filestore.py index b3d49cf57c..ced8561459 100644 --- a/tiledb/filestore.py +++ b/tiledb/filestore.py @@ -67,7 +67,7 @@ def read(self, offset: int = 0, size: int = -1) -> bytes: if size == -1: size = len(self) - size = max(size - offset, 0) + size = min(size, len(self) - offset) return lt.Filestore._buffer_export( self._ctx, diff --git a/tiledb/tests/test_filestore.py b/tiledb/tests/test_filestore.py index dd8e3da3e2..bde45452aa 100644 --- a/tiledb/tests/test_filestore.py +++ b/tiledb/tests/test_filestore.py @@ -35,12 +35,33 @@ def test_buffer(self, capfd): schema.attr(0).dump() assert_captured(capfd, "Type: BLOB") - data = b"buffer" - fs = tiledb.Filestore(path) fs.write(data) assert bytes(data) == fs.read() + def test_small_buffer(self, capfd): + path = self.path("test_small_buffer") + # create a 4 byte array + data = b"abcd" + + fs = tiledb.Filestore(path) + + with self.assertRaises(tiledb.TileDBError): + fs.write(data) + + schema = tiledb.ArraySchema.from_file() + tiledb.Array.create(path, schema) + + assert schema.attr(0).name == "contents" + assert schema.attr(0).dtype == np.bytes_ + + schema.attr(0).dump() + assert_captured(capfd, "Type: BLOB") + + fs = tiledb.Filestore(path) + fs.write(data) + assert data[3:4] == fs.read(offset=3, size=1) + def test_uri(self, text_fname): path = self.path("test_uri") schema = tiledb.ArraySchema.from_file(text_fname)