Skip to content

Commit 7adba24

Browse files
authored
Close tdbMatrix and tdbMatrixWithIds Array's when we have nothing left to read (#466)
1 parent 5db39ff commit 7adba24

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

src/include/detail/linalg/tdb_matrix.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ class tdbBlockedMatrix : public MatrixBase {
114114
// size_t pending_row_offset{0};
115115
// size_t pending_col_offset{0};
116116

117+
size_t get_elements_to_load() const {
118+
return std::min(load_blocksize_, last_col_ - last_resident_col_);
119+
}
120+
117121
public:
118122
tdbBlockedMatrix(tdbBlockedMatrix&& rhs) = default;
119123

@@ -324,11 +328,11 @@ class tdbBlockedMatrix : public MatrixBase {
324328
}
325329

326330
size_t dimension = last_row_ - first_row_;
327-
auto elements_to_load =
328-
std::min(load_blocksize_, last_col_ - last_resident_col_);
331+
auto elements_to_load = get_elements_to_load();
329332

330333
// Return if we're at the end
331334
if (elements_to_load == 0 || dimension == 0) {
335+
array_->close();
332336
return false;
333337
}
334338

@@ -363,6 +367,10 @@ class tdbBlockedMatrix : public MatrixBase {
363367
throw std::runtime_error("Query status is not complete");
364368
}
365369

370+
if (get_elements_to_load() == 0) {
371+
array_->close();
372+
}
373+
366374
num_loads_++;
367375
return true;
368376
}

src/include/detail/linalg/tdb_matrix_with_ids.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class tdbBlockedMatrixWithIds
167167
bool load() {
168168
scoped_timer _{tdb_func__ + " " + this->ids_uri_};
169169
if (!Base::load()) {
170+
ids_array_->close();
170171
return false;
171172
}
172173

@@ -214,6 +215,10 @@ class tdbBlockedMatrixWithIds
214215
throw std::runtime_error("Query status for IDs is not complete");
215216
}
216217

218+
if (this->get_elements_to_load() == 0) {
219+
ids_array_->close();
220+
}
221+
217222
return true;
218223
}
219224
}; // tdbBlockedMatrixWithIds

src/include/test/unit_tdb_matrix.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ TEMPLATE_TEST_CASE("constructors", "[tdb_matrix]", float, uint8_t) {
5959
write_matrix(ctx, X, tmp_matrix_uri);
6060

6161
auto Y = tdbColMajorMatrix<TestType>(ctx, tmp_matrix_uri);
62-
Y.load();
62+
CHECK(Y.load() == true);
63+
for (int i = 0; i < 5; ++i) {
64+
CHECK(Y.load() == false);
65+
}
6366

6467
auto Z = tdbColMajorMatrix<TestType>(std::move(Y));
6568

src/include/test/unit_tdb_matrix_with_ids.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ TEMPLATE_TEST_CASE(
6363

6464
auto Y = tdbColMajorMatrixWithIds<TestType, TestType>(
6565
ctx, tmp_matrix_uri, tmp_ids_uri);
66-
Y.load();
66+
CHECK(Y.load() == true);
67+
for (int i = 0; i < 5; ++i) {
68+
CHECK(Y.load() == false);
69+
}
6770
CHECK(num_vectors(Y) == num_vectors(X));
6871
CHECK(dimensions(Y) == dimensions(X));
6972
CHECK(std::equal(

0 commit comments

Comments
 (0)