Skip to content

Commit 097f155

Browse files
Add tiledb_fragment_info_dump_str C API and replace FragmentInfo::dump(FILE*) with operator<< overload. (#5266)
After #5026, the only `dump` API that remains without a string counterpart is on `FragmentInfo`. This PR adds the `tiledb_fragment_info_dump_str` C API and replaces `FragmentInfo::dump(FILE*)` with an `operator<<` overload. [sc-50605] --- TYPE: C_API | CPP_API DESC: Add `tiledb_fragment_info_dump_str` C API and replace `FragmentInfo::dump(FILE*)` with `operator<<` overload. --------- Co-authored-by: Theodore Tsirpanis <[email protected]>
1 parent fd24448 commit 097f155

File tree

7 files changed

+125
-46
lines changed

7 files changed

+125
-46
lines changed

tiledb/doxygen/source/c-api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,8 @@ Fragment Info
772772
:project: TileDB-C
773773
.. doxygenfunction:: tiledb_fragment_info_dump
774774
:project: TileDB-C
775+
.. doxygenfunction:: tiledb_fragment_info_dump_str
776+
:project: TileDB-C
775777

776778
Experimental
777779
-------------

tiledb/sm/c_api/tiledb.cc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3916,7 +3916,29 @@ int32_t tiledb_fragment_info_dump(
39163916
tiledb_ctx_t* ctx, const tiledb_fragment_info_t* fragment_info, FILE* out) {
39173917
if (sanity_check(ctx, fragment_info) == TILEDB_ERR)
39183918
return TILEDB_ERR;
3919-
fragment_info->fragment_info_->dump(out);
3919+
3920+
std::stringstream ss;
3921+
ss << *fragment_info->fragment_info_;
3922+
size_t r = fwrite(ss.str().c_str(), sizeof(char), ss.str().size(), out);
3923+
if (r != ss.str().size()) {
3924+
throw CAPIException("Error writing fragment info to output stream");
3925+
}
3926+
3927+
return TILEDB_OK;
3928+
}
3929+
3930+
int32_t tiledb_fragment_info_dump_str(
3931+
tiledb_ctx_t* ctx,
3932+
const tiledb_fragment_info_t* fragment_info,
3933+
tiledb_string_t** out) {
3934+
if (sanity_check(ctx, fragment_info) == TILEDB_ERR)
3935+
return TILEDB_ERR;
3936+
ensure_output_pointer_is_valid(out);
3937+
3938+
std::stringstream ss;
3939+
ss << *fragment_info->fragment_info_;
3940+
*out = tiledb_string_handle_t::make_handle(ss.str());
3941+
39203942
return TILEDB_OK;
39213943
}
39223944

@@ -6086,6 +6108,15 @@ CAPI_INTERFACE(
60866108
ctx, fragment_info, out);
60876109
}
60886110

6111+
CAPI_INTERFACE(
6112+
fragment_info_dump_str,
6113+
tiledb_ctx_t* ctx,
6114+
const tiledb_fragment_info_t* fragment_info,
6115+
tiledb_string_handle_t** out) {
6116+
return api_entry<tiledb::api::tiledb_fragment_info_dump_str>(
6117+
ctx, fragment_info, out);
6118+
}
6119+
60896120
/* ********************************* */
60906121
/* EXPERIMENTAL APIs */
60916122
/* ********************************* */

tiledb/sm/c_api/tiledb.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,6 +3742,30 @@ TILEDB_EXPORT int32_t tiledb_fragment_info_dump(
37423742
const tiledb_fragment_info_t* fragment_info,
37433743
FILE* out) TILEDB_NOEXCEPT;
37443744

3745+
/**
3746+
* Dumps the fragment info in ASCII format in the selected string output.
3747+
*
3748+
* The output string handle must be freed by the user after use.
3749+
*
3750+
* **Example:**
3751+
*
3752+
* @code{.c}
3753+
* tiledb_string_t* tdb_string;
3754+
* tiledb_fragment_info_dump_str(ctx, fragment_info, &tdb_string);
3755+
* // Use the string
3756+
* tiledb_string_free(&tdb_string);
3757+
* @endcode
3758+
*
3759+
* @param[in] ctx The TileDB context.
3760+
* @param[in] fragment_info The fragment info object.
3761+
* @param[out] out The output string.
3762+
* @return `TILEDB_OK` for success and `TILEDB_ERR` for error.
3763+
*/
3764+
TILEDB_EXPORT capi_return_t tiledb_fragment_info_dump_str(
3765+
tiledb_ctx_t* ctx,
3766+
const tiledb_fragment_info_t* fragment_info,
3767+
tiledb_string_t** out) TILEDB_NOEXCEPT;
3768+
37453769
#ifdef __cplusplus
37463770
}
37473771
#endif

tiledb/sm/cpp_api/fragment_info.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ class FragmentInfo {
101101
return impl::convert_to_string(&name).value();
102102
}
103103

104+
/** Returns the context that the fragment info belongs to. */
105+
const Context& context() const {
106+
return ctx_;
107+
}
108+
104109
/**
105110
* Retrieves the non-empty domain of the fragment with the given index
106111
* on the given dimension index.
@@ -403,17 +408,20 @@ class FragmentInfo {
403408
return std::string(uri_c);
404409
}
405410

411+
#ifndef TILEDB_REMOVE_DEPRECATIONS
406412
/**
407413
* Dumps the fragment info in an ASCII representation to an output.
408414
*
409415
* @param out (Optional) File to dump output to. Defaults to `nullptr`
410416
* which will lead to selection of `stdout`.
411417
*/
418+
TILEDB_DEPRECATED
412419
void dump(FILE* out = nullptr) const {
413420
auto& ctx = ctx_.get();
414421
ctx.handle_error(
415422
tiledb_fragment_info_dump(ctx.ptr().get(), fragment_info_.get(), out));
416423
}
424+
#endif
417425

418426
/** Returns the C TileDB context object. */
419427
std::shared_ptr<tiledb_fragment_info_t> ptr() const {
@@ -435,6 +443,22 @@ class FragmentInfo {
435443
std::shared_ptr<tiledb_fragment_info_t> fragment_info_;
436444
};
437445

446+
/* ********************************* */
447+
/* MISC */
448+
/* ********************************* */
449+
450+
/** Get a string representation of fragment info. */
451+
inline std::ostream& operator<<(std::ostream& os, const FragmentInfo& fi) {
452+
tiledb_string_t* tdb_string;
453+
auto& ctx = fi.context();
454+
ctx.handle_error(tiledb_fragment_info_dump_str(
455+
ctx.ptr().get(), fi.ptr().get(), &tdb_string));
456+
457+
os << impl::convert_to_string(&tdb_string).value();
458+
459+
return os;
460+
}
461+
438462
} // namespace tiledb
439463

440464
#endif // TILEDB_CPP_API_FRAGMENT_INFO_H

tiledb/sm/fragment/fragment_info.cc

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,6 @@ void FragmentInfo::expand_anterior_ndrange(
8484
domain.expand_ndrange(range, &anterior_ndrange_);
8585
}
8686

87-
void FragmentInfo::dump(FILE* out) const {
88-
ensure_loaded();
89-
if (out == nullptr)
90-
out = stdout;
91-
92-
auto fragment_num = this->fragment_num();
93-
94-
std::stringstream ss;
95-
ss << "- Fragment num: " << fragment_num << "\n";
96-
ss << "- Unconsolidated metadata num: " << unconsolidated_metadata_num_
97-
<< "\n";
98-
ss << "- To vacuum num: " << to_vacuum_.size() << "\n";
99-
100-
if (!to_vacuum_.empty()) {
101-
ss << "- To vacuum URIs:\n";
102-
for (const auto& v : to_vacuum_)
103-
ss << " > " << v.c_str() << "\n";
104-
}
105-
106-
fprintf(out, "%s", ss.str().c_str());
107-
108-
for (uint32_t fid = 0; fid < fragment_num; ++fid) {
109-
auto meta = single_fragment_info_vec_[fid].meta();
110-
auto dim_types = meta->dim_types();
111-
fprintf(out, "- Fragment #%u:\n", fid + 1);
112-
single_fragment_info_vec_[fid].dump(dim_types, out);
113-
}
114-
}
115-
11687
Status FragmentInfo::get_dense(uint32_t fid, int32_t* dense) const {
11788
ensure_loaded();
11889
if (dense == nullptr)
@@ -1203,3 +1174,31 @@ Status FragmentInfo::replace(
12031174
}
12041175

12051176
} // namespace tiledb::sm
1177+
1178+
std::ostream& operator<<(
1179+
std::ostream& os, const tiledb::sm::FragmentInfo& fragment_info) {
1180+
auto fragment_num = fragment_info.fragment_num();
1181+
1182+
os << "- Fragment num: " << fragment_num << "\n";
1183+
os << "- Unconsolidated metadata num: "
1184+
<< fragment_info.unconsolidated_metadata_num() << "\n";
1185+
1186+
auto to_vacuum = fragment_info.to_vacuum();
1187+
os << "- To vacuum num: " << to_vacuum.size() << "\n";
1188+
1189+
if (!to_vacuum.empty()) {
1190+
os << "- To vacuum URIs:\n";
1191+
for (const auto& v : to_vacuum)
1192+
os << " > " << v.c_str() << "\n";
1193+
}
1194+
1195+
auto single_fragment_info_vec = fragment_info.single_fragment_info_vec();
1196+
for (uint32_t fid = 0; fid < fragment_num; ++fid) {
1197+
auto meta = single_fragment_info_vec[fid].meta();
1198+
auto dim_types = meta->dim_types();
1199+
os << "- Fragment #" << fid + 1 << ":\n";
1200+
single_fragment_info_vec[fid].dump_single_fragment_info(os, dim_types);
1201+
}
1202+
1203+
return os;
1204+
}

tiledb/sm/fragment/fragment_info.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,7 @@ class FragmentInfo {
485485
} // namespace tiledb
486486

487487
#endif // TILEDB_FRAGMENT_INFO_H
488+
489+
/** Converts the fragment info into a string representation. */
490+
std::ostream& operator<<(
491+
std::ostream& os, const tiledb::sm::FragmentInfo& fragment_info);

tiledb/sm/fragment/single_fragment_info.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,20 @@ class SingleFragmentInfo {
119119
}
120120

121121
/** Dumps the single fragment info in ASCII format in the selected output. */
122-
void dump(const std::vector<Datatype>& dim_types, FILE* out) const {
123-
if (out == nullptr)
124-
out = stdout;
125-
126-
std::stringstream ss;
127-
ss << " > URI: " << uri_.c_str() << "\n";
128-
ss << " > Schema name: " << array_schema_name_ << "\n";
129-
ss << " > Type: " << (sparse_ ? "sparse" : "dense") << "\n";
130-
ss << " > Non-empty domain: " << non_empty_domain_str(dim_types).c_str()
122+
void dump_single_fragment_info(
123+
std::ostream& os, const std::vector<Datatype>& dim_types) const {
124+
os << " > URI: " << uri_.c_str() << "\n";
125+
os << " > Schema name: " << array_schema_name_ << "\n";
126+
os << " > Type: " << (sparse_ ? "sparse" : "dense") << "\n";
127+
os << " > Non-empty domain: " << non_empty_domain_str(dim_types).c_str()
131128
<< "\n";
132-
ss << " > Size: " << fragment_size_ << "\n";
133-
ss << " > Cell num: " << cell_num_ << "\n";
134-
ss << " > Timestamp range: [" << timestamp_range_.first << ", "
129+
os << " > Size: " << fragment_size_ << "\n";
130+
os << " > Cell num: " << cell_num_ << "\n";
131+
os << " > Timestamp range: [" << timestamp_range_.first << ", "
135132
<< timestamp_range_.second << "]\n";
136-
ss << " > Format version: " << version_ << "\n";
137-
ss << " > Has consolidated metadata: "
133+
os << " > Format version: " << version_ << "\n";
134+
os << " > Has consolidated metadata: "
138135
<< (has_consolidated_footer_ ? "yes" : "no") << "\n";
139-
140-
fprintf(out, "%s", ss.str().c_str());
141136
}
142137

143138
/** Returns `true` if the fragment is sparse. */

0 commit comments

Comments
 (0)