Skip to content

Commit 883866f

Browse files
authored
Fix tiledb direct URIs in C++ group's (#332)
1 parent 1d3997c commit 883866f

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

apis/python/test/test_cloud.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ def test_cloud_vamana(self):
9393
mode=Mode.LOCAL,
9494
)
9595

96-
# TODO(paris): Fix error from loading this URI and then re-enable, and add the rest of the test.
97-
# tiledb_index_uri = groups.info(index_uri).tiledb_uri
98-
# vs.vamana_index.VamanaIndex(uri=tiledb_index_uri)
96+
tiledb_index_uri = groups.info(index_uri).tiledb_uri
97+
vs.vamana_index.VamanaIndex(
98+
uri=tiledb_index_uri, config=tiledb.cloud.Config().dict()
99+
)
99100

100101
def test_cloud_ivf_flat(self):
101102
source_uri = "tiledb://TileDB-Inc/sift_10k"

src/include/index/index_group.h

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,15 @@ class base_index_group {
111111

112112
std::unordered_map<std::string, std::string> array_key_to_array_name_;
113113

114-
/** Check validity of key name */
115-
constexpr bool is_valid_key_name(const std::string& key_name) const noexcept {
116-
return valid_array_keys_.contains(key_name);
117-
}
118-
119-
/** Check validity of array name */
120-
constexpr bool is_valid_array_name(
121-
const std::string& array_name) const noexcept {
122-
return valid_array_names_.contains(array_name);
123-
}
114+
// Maps from the array name (not the key) to the URI of the array. Should be
115+
// used to get array URI's because the group_uri_ may be of the form
116+
// `tiledb://foo/edc4656a-3f45-43a1-8ee5-fa692a015c53` which cannot have the
117+
// array name added as a suffix.
118+
std::unordered_map<std::string, std::string> array_name_to_uri_;
124119

125120
/** Lookup an array name given an array key */
126121
constexpr auto array_key_to_array_name(const std::string& array_key) const {
127-
if (!is_valid_key_name(array_key)) {
122+
if (!valid_array_keys_.contains(array_key)) {
128123
throw std::runtime_error("Invalid array key: " + array_key);
129124
}
130125
return array_key_to_array_name_from_map(
@@ -140,29 +135,12 @@ class base_index_group {
140135
valid_array_keys_.insert(array_key);
141136
valid_array_names_.insert(array_name);
142137
array_key_to_array_name_[array_key] = array_name;
138+
array_name_to_uri_[array_name] =
139+
array_name_to_uri(group_uri_, array_name);
143140
}
144141
static_cast<group_type*>(this)->append_valid_array_names_impl();
145142
}
146143

147-
/**
148-
* @brief Add an array to the group.
149-
*
150-
* @param array_name
151-
*
152-
* @todo Could have type of array set here instead of by Index. Might be
153-
* better to have it set in conjunction with array being set?
154-
*/
155-
auto init_array_for_create(const std::string& array_name) {
156-
if (!is_valid_array_name(array_name)) {
157-
throw std::runtime_error(
158-
"Invalid array name in add_array: " + array_name);
159-
}
160-
161-
std::filesystem::path uri = array_name_to_uri(array_name);
162-
163-
return uri;
164-
}
165-
166144
/**
167145
* Open group for reading. If version_ is not set, the group will be opened
168146
* with whichever version is found in the metadata. If version_ is set,
@@ -201,10 +179,17 @@ class base_index_group {
201179
if (!name || name->empty()) {
202180
throw std::runtime_error("Name is empty.");
203181
}
204-
if (!is_valid_array_name(*name)) {
182+
auto uri = member.uri();
183+
if (uri.empty()) {
184+
throw std::runtime_error("Uri is empty.");
185+
}
186+
187+
if (!valid_array_names_.contains(*name)) {
205188
throw std::runtime_error(
206189
"Invalid array name in group: " + std::string(*name));
207190
}
191+
192+
array_name_to_uri_[*name] = uri;
208193
}
209194
}
210195

@@ -278,17 +263,16 @@ class base_index_group {
278263
static_cast<group_type*>(this)->create_default_impl(cfg);
279264
}
280265

281-
/** Convert an array name to a uri. */
282-
constexpr std::string array_name_to_uri(
283-
const std::string& array_name) const noexcept {
284-
return array_name_to_uri(group_uri_, array_name);
285-
}
286-
287266
/** Convert an array key to a uri. */
288267
constexpr std::string array_key_to_uri(const std::string& array_key) const {
289-
return (std::filesystem::path{group_uri_} /
290-
std::filesystem::path{array_key_to_array_name(array_key)})
291-
.string();
268+
auto name = array_key_to_array_name(array_key);
269+
if (array_name_to_uri_.find(name) == array_name_to_uri_.end()) {
270+
throw std::runtime_error(
271+
"Invalid key when getting the URI: " + array_key +
272+
". Name does not exist: " + name);
273+
}
274+
275+
return array_name_to_uri_.at(name);
292276
}
293277

294278
public:

src/include/index/ivf_flat_group.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class ivf_flat_index_group
6262
// using Base::Base;
6363

6464
using Base::array_key_to_array_name_;
65+
using Base::array_name_to_uri_;
6566
using Base::cached_ctx_;
6667
using Base::group_uri_;
6768
using Base::metadata_;
@@ -97,6 +98,8 @@ class ivf_flat_index_group
9798
valid_array_keys_.insert(array_key);
9899
valid_array_names_.insert(array_name);
99100
array_key_to_array_name_[array_key] = array_name;
101+
array_name_to_uri_[array_name] =
102+
array_name_to_uri(group_uri_, array_name);
100103
}
101104
}
102105

src/include/index/vamana_group.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class vamana_index_group : public base_index_group<vamana_index_group<Index>> {
8484
// using Base::Base;
8585

8686
using Base::array_key_to_array_name_;
87+
using Base::array_name_to_uri_;
8788
using Base::cached_ctx_;
8889
using Base::group_uri_;
8990
using Base::metadata_;
@@ -120,6 +121,8 @@ class vamana_index_group : public base_index_group<vamana_index_group<Index>> {
120121
valid_array_keys_.insert(array_key);
121122
valid_array_names_.insert(array_name);
122123
array_key_to_array_name_[array_key] = array_name;
124+
array_name_to_uri_[array_name] =
125+
array_name_to_uri(group_uri_, array_name);
123126
}
124127
}
125128

0 commit comments

Comments
 (0)