Skip to content

Commit bea859b

Browse files
authored
Refactor C++ group code and add more Vamana unit tests (#363)
1 parent b7aabc2 commit bea859b

File tree

4 files changed

+35
-49
lines changed

4 files changed

+35
-49
lines changed

src/include/index/index_group.h

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ class base_index_group {
9696
tiledb::Context cached_ctx_;
9797
std::string group_uri_;
9898
TemporalPolicy temporal_policy_{TimeTravel, 0};
99-
size_t index_timestamp_{0};
100-
size_t group_timestamp_{0};
101-
size_t timetravel_index_{0};
99+
size_t base_array_timestamp_{0};
100+
size_t history_index_{0};
102101

103102
// std::reference_wrapper<const index_type> index_;
104103
std::string version_;
@@ -151,7 +150,7 @@ class base_index_group {
151150
* @param ctx
152151
*/
153152
void init_for_open() {
154-
if (!exists(cached_ctx_)) {
153+
if (!exists()) {
155154
throw std::runtime_error(
156155
"Group uri " + std::string(group_uri_) + " does not exist.");
157156
}
@@ -196,25 +195,22 @@ class base_index_group {
196195
if (size(metadata_.ingestion_timestamps_) == 0) {
197196
throw std::runtime_error("No ingestion timestamps found.");
198197
}
199-
if (index_timestamp_ == 0) {
200-
index_timestamp_ = metadata_.ingestion_timestamps_.back();
201-
temporal_policy_ = TemporalPolicy{TimeTravel, index_timestamp_};
198+
if (base_array_timestamp_ == 0) {
199+
base_array_timestamp_ = metadata_.ingestion_timestamps_.back();
200+
temporal_policy_ = TemporalPolicy{TimeTravel, base_array_timestamp_};
202201
}
203202

204203
auto timestamp_bound = std::lower_bound(
205204
begin(metadata_.ingestion_timestamps_),
206205
end(metadata_.ingestion_timestamps_),
207-
index_timestamp_);
206+
base_array_timestamp_);
208207
if (timestamp_bound == end(metadata_.ingestion_timestamps_)) {
209208
// We may try to load the index at a timestamp beyond the latest ingestion
210209
// timestamp. In this case, use the last timestamp.
211210
timestamp_bound = end(metadata_.ingestion_timestamps_) - 1;
212211
}
213-
timetravel_index_ =
212+
history_index_ =
214213
std::distance(begin(metadata_.ingestion_timestamps_), timestamp_bound);
215-
216-
// @todo Or index_timestamp_?
217-
group_timestamp_ = metadata_.ingestion_timestamps_[timetravel_index_];
218214
}
219215

220216
/**
@@ -233,16 +229,15 @@ class base_index_group {
233229
* @param version
234230
*/
235231
void open_for_write() {
236-
if (exists(cached_ctx_)) {
232+
if (exists()) {
237233
/** Load the current group metadata */
238234
init_for_open();
239235
if (!metadata_.ingestion_timestamps_.empty() &&
240-
index_timestamp_ < metadata_.ingestion_timestamps_.back()) {
236+
base_array_timestamp_ < metadata_.ingestion_timestamps_.back()) {
241237
throw std::runtime_error(
242-
"Requested write timestamp " + std::to_string(index_timestamp_) +
243-
" is not greater than " +
238+
"Requested write timestamp " +
239+
std::to_string(base_array_timestamp_) + " is not greater than " +
244240
std::to_string(metadata_.ingestion_timestamps_.back()));
245-
group_timestamp_ = index_timestamp_;
246241
}
247242
} else {
248243
/** Create a new group */
@@ -308,7 +303,7 @@ class base_index_group {
308303
: cached_ctx_(ctx)
309304
, group_uri_(uri)
310305
, temporal_policy_(temporal_policy)
311-
, index_timestamp_(temporal_policy.timestamp_end())
306+
, base_array_timestamp_(temporal_policy.timestamp_end())
312307
, version_(version)
313308
, opened_for_(rw) {
314309
switch (opened_for_) {
@@ -347,8 +342,8 @@ class base_index_group {
347342
* @brief Test whether the group exists or not.
348343
* @param ctx
349344
*/
350-
bool exists(const tiledb::Context& ctx) const {
351-
return tiledb::Object::object(ctx, group_uri_).type() ==
345+
bool exists() const {
346+
return tiledb::Object::object(cached_ctx_, group_uri_).type() ==
352347
tiledb::Object::Type::Group;
353348
}
354349

@@ -362,16 +357,12 @@ class base_index_group {
362357
return tiledb::Object::remove(ctx, group_uri_);
363358
}
364359

365-
/**************************************************************************
366-
* Getters for read and write timestamps and sizes
367-
**************************************************************************/
368-
369360
/** Temporary until time traveling is implemented */
370361
auto get_previous_ingestion_timestamp() const {
371362
return metadata_.ingestion_timestamps_.back();
372363
}
373364
auto get_ingestion_timestamp() const {
374-
return metadata_.ingestion_timestamps_[timetravel_index_];
365+
return metadata_.ingestion_timestamps_[history_index_];
375366
}
376367
auto append_ingestion_timestamp(size_t timestamp) {
377368
metadata_.ingestion_timestamps_.push_back(timestamp);
@@ -380,14 +371,11 @@ class base_index_group {
380371
return metadata_.ingestion_timestamps_;
381372
}
382373

383-
/*
384-
* Base size information
385-
*/
386374
auto get_previous_base_size() const {
387375
return metadata_.base_sizes_.back();
388376
}
389377
auto get_base_size() const {
390-
return metadata_.base_sizes_[timetravel_index_];
378+
return metadata_.base_sizes_[history_index_];
391379
}
392380
auto append_base_size(size_t size) {
393381
metadata_.base_sizes_.push_back(size);
@@ -410,40 +398,34 @@ class base_index_group {
410398
metadata_.dimension_ = dim;
411399
}
412400

413-
auto get_timetravel_index() const {
414-
return timetravel_index_;
401+
auto get_history_index() const {
402+
return history_index_;
415403
}
416404

417-
/**************************************************************************
418-
* Getters for names and uris
419-
**************************************************************************/
420-
421405
[[nodiscard]] auto ids_uri() const {
422406
return array_key_to_uri("ids_array_name");
423407
}
424408
[[nodiscard]] auto ids_array_name() const {
425409
return array_key_to_array_name("ids_array_name");
426410
}
411+
427412
[[nodiscard]] const std::reference_wrapper<const tiledb::Context> cached_ctx()
428413
const {
429414
return cached_ctx_;
430415
}
431416
[[nodiscard]] std::reference_wrapper<const tiledb::Context> cached_ctx() {
432417
return cached_ctx_;
433418
}
434-
[[nodiscard]] auto group_timestamp() const {
435-
return group_timestamp_;
436-
}
437419

438420
/**************************************************************************
439421
* Helpful functions for debugging, testing, etc
440422
**************************************************************************/
441423

442424
auto set_ingestion_timestamp(size_t timestamp) {
443-
metadata_.ingestion_timestamps_[timetravel_index_] = timestamp;
425+
metadata_.ingestion_timestamps_[history_index_] = timestamp;
444426
}
445427
auto set_base_size(size_t size) {
446-
metadata_.base_sizes_[timetravel_index_] = size;
428+
metadata_.base_sizes_[history_index_] = size;
447429
}
448430

449431
auto set_last_ingestion_timestamp(size_t timestamp) {
@@ -485,7 +467,7 @@ class base_index_group {
485467
*
486468
* @param msg Optional message to print before the dump.
487469
*/
488-
auto dump(const std::string& msg = "") {
470+
auto dump(const std::string& msg = "") const {
489471
if (!empty(msg)) {
490472
std::cout << "-------------------------------------------------------\n";
491473
std::cout << "# " + msg << std::endl;
@@ -502,6 +484,10 @@ class base_index_group {
502484
}
503485
std::cout << *name << " " << member.uri() << std::endl;
504486
}
487+
std::cout << "version_: " << version_ << std::endl;
488+
std::cout << "history_index_: " << history_index_ << std::endl;
489+
std::cout << "base_array_timestamp_: " << base_array_timestamp_
490+
<< std::endl;
505491
std::cout << "-------------------------------------------------------\n";
506492
std::cout << "# Metadata:" << std::endl;
507493
std::cout << "-------------------------------------------------------\n";

src/include/index/ivf_flat_group.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class ivf_flat_index_group
109109
return metadata_.partition_history_.back();
110110
}
111111
auto get_num_partitions() const {
112-
return metadata_.partition_history_[this->timetravel_index_];
112+
return metadata_.partition_history_[this->history_index_];
113113
}
114114
auto append_num_partitions(size_t size) {
115115
metadata_.partition_history_.push_back(size);
@@ -119,7 +119,7 @@ class ivf_flat_index_group
119119
}
120120

121121
auto set_num_partitions(size_t size) {
122-
metadata_.partition_history_[this->timetravel_index_] = size;
122+
metadata_.partition_history_[this->history_index_] = size;
123123
}
124124
auto set_last_num_partitions(size_t size) {
125125
metadata_.partition_history_.back() = size;

src/include/index/vamana_group.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class vamana_index_group : public base_index_group<vamana_index_group<Index>> {
132132
return metadata_.num_edges_history_.back();
133133
}
134134
auto get_num_edges() const {
135-
return metadata_.num_edges_history_[this->timetravel_index_];
135+
return metadata_.num_edges_history_[this->history_index_];
136136
}
137137
auto append_num_edges(size_t size) {
138138
metadata_.num_edges_history_.push_back(size);
@@ -141,7 +141,7 @@ class vamana_index_group : public base_index_group<vamana_index_group<Index>> {
141141
return metadata_.num_edges_history_;
142142
}
143143
auto set_num_edges(size_t size) {
144-
metadata_.num_edges_history_[this->timetravel_index_] = size;
144+
metadata_.num_edges_history_[this->history_index_] = size;
145145
}
146146
auto set_last_num_edges(size_t size) {
147147
metadata_.num_edges_history_.back() = size;

src/include/test/unit_api_vamana_index.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ TEST_CASE(
588588
adjacency_row_index_type_type>(ctx, index_uri);
589589
CHECK(typed_index.group().get_dimension() == dimensions);
590590
CHECK(typed_index.group().get_temp_size() == 0);
591-
CHECK(typed_index.group().get_timetravel_index() == 0);
591+
CHECK(typed_index.group().get_history_index() == 0);
592592

593593
CHECK(typed_index.group().get_base_size() == 0);
594594
CHECK(typed_index.group().get_ingestion_timestamp() == 0);
@@ -650,7 +650,7 @@ TEST_CASE(
650650
adjacency_row_index_type_type>(ctx, index_uri);
651651
CHECK(typed_index.group().get_dimension() == dimensions);
652652
CHECK(typed_index.group().get_temp_size() == 0);
653-
CHECK(typed_index.group().get_timetravel_index() == 0);
653+
CHECK(typed_index.group().get_history_index() == 0);
654654

655655
CHECK(typed_index.group().get_base_size() == 4);
656656
CHECK(typed_index.group().get_ingestion_timestamp() == 99);
@@ -714,7 +714,7 @@ TEST_CASE(
714714
adjacency_row_index_type_type>(ctx, index_uri);
715715
CHECK(typed_index.group().get_dimension() == dimensions);
716716
CHECK(typed_index.group().get_temp_size() == 0);
717-
CHECK(typed_index.group().get_timetravel_index() == 1);
717+
CHECK(typed_index.group().get_history_index() == 1);
718718

719719
CHECK(typed_index.group().get_base_size() == 5);
720720
CHECK(typed_index.group().get_ingestion_timestamp() == 100);
@@ -770,7 +770,7 @@ TEST_CASE(
770770
adjacency_row_index_type_type>(ctx, index_uri, temporal_policy);
771771
CHECK(typed_index.group().get_dimension() == dimensions);
772772
CHECK(typed_index.group().get_temp_size() == 0);
773-
CHECK(typed_index.group().get_timetravel_index() == 0);
773+
CHECK(typed_index.group().get_history_index() == 0);
774774

775775
CHECK(typed_index.group().get_base_size() == 4);
776776
CHECK(typed_index.group().get_ingestion_timestamp() == 99);

0 commit comments

Comments
 (0)