Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace dump functions with operator<< overloads #5026

Merged
merged 30 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
76dbda4
Add dump functions
kounelisagis May 30, 2024
2c08b88
Rename the API to the existing one
kounelisagis May 30, 2024
2e50554
Remove duplicate code
kounelisagis May 30, 2024
a8d2f26
Merge dev
kounelisagis May 30, 2024
f30a995
Change basic dump functions to operator<< overloads
kounelisagis Jun 1, 2024
67d7ed4
Fix comments
kounelisagis Jun 11, 2024
276c406
Merge branch 'dev' into agis/add-dump-functions-with-str-argument
kounelisagis Jun 11, 2024
7baa8fd
Fix merge errors
kounelisagis Jun 11, 2024
a8c64ee
Merge branch 'dev' into agis/add-dump-functions-with-str-argument
kounelisagis Jun 11, 2024
be6edf6
Merge branch 'dev' into agis/add-dump-functions-with-str-argument
kounelisagis Jun 12, 2024
c0ebda3
Fix windows
kounelisagis Jun 12, 2024
6dcde59
Fix some comments
kounelisagis Jun 14, 2024
74f5443
Fix some more comments
kounelisagis Jun 14, 2024
dc416e7
Merge branch 'dev' into agis/add-dump-functions-with-str-argument
kounelisagis Jun 14, 2024
cb844d5
add check for nullptr FILE*
kounelisagis Jun 14, 2024
24d2d54
Fixes
kounelisagis Jun 14, 2024
68207f7
Some fixes
kounelisagis Jun 17, 2024
a9d93a2
more fixes
kounelisagis Jun 18, 2024
e76c49b
Merge branch 'dev' into agis/add-dump-functions-with-str-argument
kounelisagis Jun 18, 2024
0b729d9
Some linting
kounelisagis Jun 18, 2024
e92f3da
Fixes
kounelisagis Jun 19, 2024
70b094a
ensure_cstream_handle_is_valid + fixes
kounelisagis Jun 20, 2024
da04c93
Merge branch 'dev' into agis/add-dump-functions-with-str-argument
kounelisagis Jun 20, 2024
d032811
Adjust the use of internal vs. external identifiers and headers. In p…
eric-hughes-tiledb Jul 3, 2024
49f77ed
clang-format
eric-hughes-tiledb Jul 3, 2024
0f8a70f
Fix a consequence of header changes.
eric-hughes-tiledb Jul 3, 2024
3d74fde
More of it
eric-hughes-tiledb Jul 3, 2024
7d71079
Document new dump_str APIs
kounelisagis Jul 4, 2024
bde9480
Add tiledb_dimension_dump_str
kounelisagis Jul 4, 2024
44399c2
Merge branch 'dev' into agis/add-dump-functions-with-str-argument
kounelisagis Jul 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/regression/targets/sc-38300.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void create_array(const std::string& array_uri) {
void dump_schema(const std::string& array_uri) {
Context ctx;
Array array(ctx, array_uri, TILEDB_READ);
array.schema().dump();
std::cout << array.schema();
ArrayExperimental::load_all_enumerations(ctx, array);
array.schema().dump();
std::cout << array.schema();
}
21 changes: 2 additions & 19 deletions test/src/unit-cppapi-enumerations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -674,27 +674,10 @@ CPPEnumerationFx::~CPPEnumerationFx() {

template <typename T>
void CPPEnumerationFx::check_dump(const T& val) {
FILE* handle = fopen(dump_name.c_str(), "w");
REQUIRE(handle != nullptr);
val.dump(handle);
fclose(handle);

std::stringstream ss;
ss << val;

// Scoped in an anonymous block to ensure that rstream closes before
// we attempt to delete the file for cleanup.
{
std::ifstream rstream(dump_name);
if (rstream.is_open()) {
ss << rstream.rdbuf();
}
}

auto data = ss.str();
auto iter = data.find("Enumeration");
REQUIRE(iter != std::string::npos);

vfs_.remove_file(dump_name);
REQUIRE(ss.str().find("Enumeration") != std::string::npos);
}

void CPPEnumerationFx::create_array(bool with_empty_enumeration) {
Expand Down
16 changes: 3 additions & 13 deletions test/src/unit-cppapi-fill_values.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,13 @@
using namespace tiledb;

void check_dump(const Attribute& attr, const std::string& gold_out) {
FILE* gold_fout = fopen("gold_fout.txt", "w");
fwrite(gold_out.c_str(), sizeof(char), gold_out.size(), gold_fout);
fclose(gold_fout);
FILE* fout = fopen("fout.txt", "w");
attr.dump(fout);
fclose(fout);
#ifdef _WIN32
CHECK(!system("FC gold_fout.txt fout.txt > nul"));
#else
CHECK(!system("diff gold_fout.txt fout.txt"));
#endif
std::stringstream ss;
ss << attr;
CHECK(gold_out == ss.str());

// Clean up
Context ctx;
VFS vfs(ctx);
CHECK_NOTHROW(vfs.remove_file("gold_fout.txt"));
CHECK_NOTHROW(vfs.remove_file("fout.txt"));
}

void create_array_1d(
Expand Down
44 changes: 40 additions & 4 deletions tiledb/api/c_api/attribute/attribute_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2023 TileDB, Inc.
* @copyright Copyright (c) 2023-2024 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -135,7 +135,28 @@ int32_t tiledb_attribute_get_cell_size(
int32_t tiledb_attribute_dump(
const tiledb_attribute_handle_t* attr, FILE* out) {
ensure_attribute_is_valid(attr);
attr->dump(out);
ensure_cstream_handle_is_valid(out);

std::stringstream ss;
ss << *attr;
kounelisagis marked this conversation as resolved.
Show resolved Hide resolved
size_t r = fwrite(ss.str().c_str(), sizeof(char), ss.str().size(), out);
if (r != ss.str().size()) {
throw CAPIException(
"Error writing attribute " + attr->name() + " to output stream");
}

return TILEDB_OK;
}

int32_t tiledb_attribute_dump_str(
const tiledb_attribute_handle_t* attr, tiledb_string_handle_t** out) {
ensure_attribute_is_valid(attr);
ensure_output_pointer_is_valid(out);

std::stringstream ss;
ss << *attr;
*out = tiledb_string_handle_t::make_handle(ss.str());
kounelisagis marked this conversation as resolved.
Show resolved Hide resolved

return TILEDB_OK;
}

Expand Down Expand Up @@ -186,7 +207,7 @@ capi_return_t tiledb_attribute_set_enumeration_name(
}

capi_return_t tiledb_attribute_get_enumeration_name(
tiledb_attribute_t* attr, tiledb_string_t** name) {
tiledb_attribute_t* attr, tiledb_string_handle_t** name) {
ensure_attribute_is_valid(attr);
ensure_output_pointer_is_valid(name);

Expand All @@ -203,6 +224,12 @@ capi_return_t tiledb_attribute_get_enumeration_name(

} // namespace tiledb::api

std::ostream& operator<<(
std::ostream& os, const tiledb_attribute_handle_t& attr) {
os << *attr.attr_;
return os;
}

using tiledb::api::api_entry_context;

CAPI_INTERFACE(
Expand Down Expand Up @@ -308,6 +335,15 @@ CAPI_INTERFACE(
return api_entry_context<tiledb::api::tiledb_attribute_dump>(ctx, attr, out);
}

CAPI_INTERFACE(
attribute_dump_str,
tiledb_ctx_t* ctx,
const tiledb_attribute_handle_t* attr,
tiledb_string_handle_t** out) {
return api_entry_context<tiledb::api::tiledb_attribute_dump_str>(
ctx, attr, out);
}

CAPI_INTERFACE(
attribute_set_fill_value,
tiledb_ctx_t* ctx,
Expand Down Expand Up @@ -365,7 +401,7 @@ CAPI_INTERFACE(
attribute_get_enumeration_name,
tiledb_ctx_t* ctx,
tiledb_attribute_t* attr,
tiledb_string_t** name) {
tiledb_string_handle_t** name) {
return api_entry_context<tiledb::api::tiledb_attribute_get_enumeration_name>(
ctx, attr, name);
}
25 changes: 25 additions & 0 deletions tiledb/api/c_api/attribute/attribute_api_external.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,31 @@ TILEDB_EXPORT int32_t tiledb_attribute_dump(
const tiledb_attribute_t* attr,
FILE* out) TILEDB_NOEXCEPT;

/**
* Dumps the contents of an Attribute in ASCII form to the selected string
* output.
*
* The output string handle must be freed by the user after use.
*
* **Example:**
*
* @code{.c}
* tiledb_string_t* tdb_string;
* tiledb_attribute_dump_str(ctx, attr, &tdb_string);
* // Use the string
* tiledb_string_free(&tdb_string);
* @endcode
*
* @param ctx The TileDB context.
* @param attr The attribute.
* @param out The output string.
* @return `TILEDB_OK` for success and `TILEDB_ERR` for error.
*/
TILEDB_EXPORT int32_t tiledb_attribute_dump_str(
tiledb_ctx_t* ctx,
const tiledb_attribute_t* attr,
tiledb_string_t** out) TILEDB_NOEXCEPT;

/**
* Sets the default fill value for the input attribute. This value will
* be used for the input attribute whenever querying (1) an empty cell in
Expand Down
11 changes: 4 additions & 7 deletions tiledb/api/c_api/attribute/attribute_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2023 TileDB, Inc.
* @copyright Copyright (c) 2023-2024 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,7 +33,6 @@
#ifndef TILEDB_CAPI_ATTRIBUTE_INTERNAL_H
#define TILEDB_CAPI_ATTRIBUTE_INTERNAL_H

#include "attribute_api_external.h"
#include "tiledb/api/c_api_support/handle/handle.h"
#include "tiledb/common/common.h"
#include "tiledb/sm/array_schema/attribute.h"
Expand Down Expand Up @@ -90,7 +89,7 @@ struct tiledb_attribute_handle_t
/**
* Copy the underlying attribute object.
*/
[[nodiscard]] attribute_type copy_attribute() {
[[nodiscard]] attribute_type copy_attribute() const {
return attr_;
}

Expand Down Expand Up @@ -190,13 +189,11 @@ struct tiledb_attribute_handle_t
[[nodiscard]] std::optional<std::string> get_enumeration_name() const {
return attr_->get_enumeration_name();
};

/**
* Facade for `Attribute` function
kounelisagis marked this conversation as resolved.
Show resolved Hide resolved
*/
void dump(FILE* out) const {
attr_->dump(out);
}
friend std::ostream& operator<<(
std::ostream& os, const tiledb_attribute_handle_t& attr);
};

namespace tiledb::api {
Expand Down
2 changes: 1 addition & 1 deletion tiledb/api/c_api/attribute/test/unit_capi_attribute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "../../../c_api_test_support/testsupport_capi_context.h"
#include "../../../c_api_test_support/testsupport_capi_datatype.h"
#include "../../filter_list/filter_list_api_internal.h"
#include "../attribute_api_internal.h"
#include "../attribute_api_external.h"
using namespace tiledb::api::test_support;

TEST_CASE(
Expand Down
1 change: 0 additions & 1 deletion tiledb/api/c_api/context/context_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/

#include "../config/config_api_internal.h"
#include "../filesystem/filesystem_api_external.h"
#include "context_api_external.h"
#include "context_api_internal.h"
#include "tiledb/api/c_api_support/c_api_support.h"
Expand Down
21 changes: 18 additions & 3 deletions tiledb/api/c_api/dimension/dimension_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2023 TileDB, Inc.
* @copyright Copyright (c) 2023-2024 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -40,7 +40,7 @@
namespace tiledb::api {

int32_t tiledb_dimension_alloc(
tiledb_ctx_t* ctx,
tiledb_ctx_handle_t* ctx,
const char* name,
tiledb_datatype_t type,
const void* dim_domain,
Expand Down Expand Up @@ -138,12 +138,27 @@ int32_t tiledb_dimension_get_tile_extent(

int32_t tiledb_dimension_dump(const tiledb_dimension_t* dim, FILE* out) {
kounelisagis marked this conversation as resolved.
Show resolved Hide resolved
ensure_dimension_is_valid(dim);
dim->dump(out);
ensure_cstream_handle_is_valid(out);

std::stringstream ss;
ss << *dim;
size_t r = fwrite(ss.str().c_str(), sizeof(char), ss.str().size(), out);
if (r != ss.str().size()) {
throw CAPIException(
"Error writing dimension " + dim->name() + " to output stream");
}

return TILEDB_OK;
}

} // namespace tiledb::api

std::ostream& operator<<(
std::ostream& os, const tiledb_dimension_handle_t& dim) {
os << *dim.dimension_;
return os;
}

using tiledb::api::api_entry_context;

CAPI_INTERFACE(
Expand Down
8 changes: 3 additions & 5 deletions tiledb/api/c_api/dimension/dimension_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* The MIT License
*
* @copyright Copyright (c) 2023 TileDB, Inc.
* @copyright Copyright (c) 2023-2024 TileDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -34,7 +34,6 @@
#define TILEDB_CAPI_DIMENSION_INTERNAL_H

#include <memory>
#include "dimension_api_external.h"
#include "tiledb/api/c_api_support/handle/handle.h"
#include "tiledb/common/common.h"
#include "tiledb/common/memory_tracker.h"
Expand Down Expand Up @@ -127,9 +126,8 @@ struct tiledb_dimension_handle_t
return dimension_->tile_extent();
}

inline void dump(FILE* out) const {
dimension_->dump(out);
}
friend std::ostream& operator<<(
std::ostream& os, const tiledb_dimension_handle_t& dim);
};

namespace tiledb::api {
Expand Down
37 changes: 36 additions & 1 deletion tiledb/api/c_api/domain/domain_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "../../c_api_support/c_api_support.h"
#include "../dimension/dimension_api_internal.h"
#include "../string/string_api_internal.h"
#include "domain_api_external.h"
#include "domain_api_internal.h"
#include "tiledb/common/memory_tracker.h"
Expand Down Expand Up @@ -142,12 +143,37 @@ int32_t tiledb_domain_has_dimension(

int32_t tiledb_domain_dump(const tiledb_domain_t* domain, FILE* out) {
ensure_domain_is_valid(domain);
domain->dump(out);
ensure_cstream_handle_is_valid(out);

std::stringstream ss;
ss << *domain;
size_t r = fwrite(ss.str().c_str(), sizeof(char), ss.str().size(), out);
if (r != ss.str().size()) {
throw CAPIException("Error writing domain to output stream");
}

return TILEDB_OK;
}

int32_t tiledb_domain_dump_str(
const tiledb_domain_t* domain, tiledb_string_handle_t** out) {
ensure_domain_is_valid(domain);
ensure_output_pointer_is_valid(out);

std::stringstream ss;
ss << *domain;
*out = tiledb_string_handle_t::make_handle(ss.str());
kounelisagis marked this conversation as resolved.
Show resolved Hide resolved
return TILEDB_OK;
}

} // namespace tiledb::api

std::ostream& operator<<(
std::ostream& os, const tiledb_domain_handle_t& domain) {
os << *domain.domain_;
return os;
}

using tiledb::api::api_entry_context;

CAPI_INTERFACE(domain_alloc, tiledb_ctx_t* ctx, tiledb_domain_t** domain) {
Expand Down Expand Up @@ -220,3 +246,12 @@ CAPI_INTERFACE(
domain_dump, tiledb_ctx_t* ctx, const tiledb_domain_t* domain, FILE* out) {
return api_entry_context<tiledb::api::tiledb_domain_dump>(ctx, domain, out);
}

CAPI_INTERFACE(
domain_dump_str,
tiledb_ctx_t* ctx,
const tiledb_domain_t* domain,
tiledb_string_handle_t** out) {
return api_entry_context<tiledb::api::tiledb_domain_dump_str>(
ctx, domain, out);
}
Loading
Loading