Skip to content

Commit 993864d

Browse files
replace stringstream with format
1 parent b450973 commit 993864d

File tree

18 files changed

+170
-256
lines changed

18 files changed

+170
-256
lines changed

apis/python/src/tiledbsoma/common.cc

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* This file defines common functions for the SOMA PyBind layer.
1212
*/
1313

14+
#include <format>
15+
1416
#include "common.h"
1517

1618
using namespace pybind11::literals; // to bring in the `_a` literal
@@ -160,13 +162,8 @@ tiledb_datatype_t np_to_tdb_dtype(py::dtype type) {
160162
if (kind.is(py::str("U")))
161163
TPY_ERROR_LOC("[np_to_tdb_dtype] UTF-32 encoded strings are not supported");
162164

163-
// No std::format in C++17, and, including logger/fmt headers
164-
// is tetchy here.
165-
std::stringstream ss;
166-
ss << "[np_to_tdb_dtype] Could not handle numpy dtype of kind '";
167-
ss << kind.operator std::string();
168-
ss << "'";
169-
TPY_ERROR_LOC(ss.str());
165+
TPY_ERROR_LOC(
166+
std::format("[np_to_tdb_dtype] Could not handle numpy dtype of kind '{}'", kind.operator std::string()));
170167
}
171168

172169
bool is_tdb_str(tiledb_datatype_t type) {
@@ -269,12 +266,11 @@ void set_metadata(SOMAObject& soma_object, const std::string& key, py::array val
269266

270267
break;
271268
default:
272-
// No std::format in C++17, and, including logger/fmt headers
273-
// is tetchy here.
274-
std::stringstream ss;
275-
ss << "[set_metadata] Unsupported string encoding '" << tiledb::impl::type_to_str(value_type)
276-
<< "' for key '" << key << "'";
277-
throw TileDBSOMAError(ss.str());
269+
throw TileDBSOMAError(
270+
std::format(
271+
"[set_metadata] Unsupported string encoding '{}' for key '{}'",
272+
tiledb::impl::type_to_str(value_type),
273+
key));
278274
}
279275
}
280276

apis/python/src/tiledbsoma/soma_vfs.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "common.h"
1616

17+
#include <format>
1718
#include <streambuf>
1819

1920
namespace libtiledbsomacpp {
@@ -47,10 +48,7 @@ class SOMAFileHandle {
4748
, buf_(SOMAFilebuf(vfs_))
4849
, ctx_(ctx) {
4950
if (buf_.open(uri, openmode_) == nullptr) {
50-
// No std::format in C++17, and fmt::format is overkill here
51-
std::stringstream ss;
52-
ss << "URI " << uri << " is not a valid URI";
53-
TPY_ERROR_LOC(ss.str());
51+
TPY_ERROR_LOC(std::format("URI {} is not a valid URI", uri));
5452
}
5553
}
5654

apis/r/src/metadata.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <Rcpp/Lighter> // for R interface to C++
2-
#include <sstream>
2+
#include <format>
33

44
#include <nanoarrow/r.h> // for C/C++ interface to Arrow (via header exported from the R package)
55
#include <RcppInt64> // for fromInteger64
@@ -193,17 +193,13 @@ void set_metadata(
193193
if (type == "character") {
194194
const tiledb_datatype_t value_type = TILEDB_STRING_UTF8;
195195
std::string value = Rcpp::as<std::string>(valuesxp);
196-
std::stringstream ss;
197-
ss << "[set_metadata] key " << key << " value " << value << " is_array " << is_array << " type " << type;
198-
tdbs::LOG_DEBUG(ss.str());
196+
tdbs::LOG_DEBUG(std::format("[set_metadata] key {} value {} is_array {} type {}", key, value, is_array, type));
199197
soup->set_metadata(key, value_type, value.length(), (void*)value.c_str(), true);
200198
} else if (type == "integer64") {
201199
const tiledb_datatype_t value_type = TILEDB_INT64;
202200
double dv = Rcpp::as<double>(valuesxp);
203201
int64_t value = Rcpp::fromInteger64(dv);
204-
std::stringstream ss;
205-
ss << "[set_metadata] key " << key << " value " << value << " is_array " << is_array << " type " << type;
206-
tdbs::LOG_DEBUG(ss.str());
202+
tdbs::LOG_DEBUG(std::format("[set_metadata] key {} value {} is_array {} type {}", key, value, is_array, type));
207203
soup->set_metadata(key, value_type, 1, (void*)&value, true);
208204
} else {
209205
Rcpp::stop("Unsupported type '%s'", type);

apis/r/src/query_condition.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#include <Rcpp.h> // for R interface to C++
2-
#include <nanoarrow/r.h> // for C interface to Arrow (via R package)
3-
#include <RcppInt64> // for fromInteger64
1+
#include <Rcpp.h> // for R interface to C++
2+
#include <nanoarrow/r.h> // for C interface to Arrow (via R package)
3+
#include <RcppInt64> // for fromInteger64
4+
#include <format>
45
#include <nanoarrow/nanoarrow.hpp> // for C/C++ interface to Arrow
5-
#include <sstream>
66

77
// we currently get deprecation warnings by default which are noisy
88
#ifndef TILEDB_NO_API_DEPRECATION_WARNINGS
@@ -141,9 +141,7 @@ void libtiledbsoma_query_condition_from_triple(
141141

142142
} else if (arrow_type_name == "timestamp_s") {
143143
int64_t v = static_cast<int64_t>(Rcpp::as<double>(condition_value));
144-
std::stringstream ss;
145-
ss << "ts3 " << v;
146-
tdbs::LOG_DEBUG(ss.str());
144+
tdbs::LOG_DEBUG(std::format("ts3 {}", v));
147145
uint64_t cond_val_size = sizeof(int64_t);
148146
query_cond->init(attr_name, (void*)&v, cond_val_size, op);
149147

apis/r/src/rinterface.cpp

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#include <Rcpp.h> // for R interface to C++
2-
#include <nanoarrow/r.h> // for C interface to Arrow (via R package)
3-
#include <RcppInt64> // for fromInteger64
1+
#include <Rcpp.h> // for R interface to C++
2+
#include <nanoarrow/r.h> // for C interface to Arrow (via R package)
3+
#include <RcppInt64> // for fromInteger64
4+
#include <format>
45
#include <nanoarrow/nanoarrow.hpp> // for C/C++ interface to Arrow
5-
#include <sstream>
66
#include <type_traits>
77

88
// we currently get deprecation warnings by default which are noisy
@@ -67,16 +67,12 @@ SEXP soma_array_reader(
6767

6868
// shared pointer to SOMAContext from external pointer wrapper
6969
std::shared_ptr<tdbs::SOMAContext> somactx = ctxxp->ctxptr;
70-
std::stringstream ss;
71-
ss << "[soma_array_reader] Reading from " << uri;
72-
tdbs::LOG_INFO(ss.str());
70+
tdbs::LOG_INFO(std::format("[soma_array_reader] Reading from {}", uri));
7371

7472
std::vector<std::string> column_names = {};
7573
if (!colnames.isNull()) { // If we have column names, select them
7674
column_names = Rcpp::as<std::vector<std::string>>(colnames);
77-
std::stringstream ss;
78-
ss << "[soma_array_reader] Selecting " << column_names.size() << " columns";
79-
tdbs::LOG_DEBUG(ss.str());
75+
tdbs::LOG_DEBUG(std::format("[soma_array_reader] Selecting {} columns", column_names.size()));
8076
}
8177

8278
auto tdb_result_order = get_tdb_result_order(result_order);
@@ -85,9 +81,7 @@ SEXP soma_array_reader(
8581
std::optional<tdbs::TimestampRange> tsrng = makeTimestampRange(timestamprange);
8682
if (timestamprange.isNotNull()) {
8783
Rcpp::DatetimeVector vec(timestamprange);
88-
std::stringstream ss;
89-
ss << "[soma_array_reader] timestamp range (" << vec[0] << ", " << vec[1] << ")";
90-
tdbs::LOG_DEBUG(ss.str());
84+
tdbs::LOG_DEBUG(std::format("[soma_array_reader] timestamp range ({}, {})", vec[0], vec[1]));
9185
}
9286

9387
// Read selected columns from the uri (return is unique_ptr<SOMAArray>)
@@ -104,10 +98,12 @@ SEXP soma_array_reader(
10498
tiledb::Domain domain = schema->domain();
10599
std::vector<tiledb::Dimension> dims = domain.dimensions();
106100
for (auto& dim : dims) {
107-
std::stringstream ss;
108-
ss << "[soma_array_reader] Dimension " << dim.name() << " type " << tiledb::impl::to_str(dim.type())
109-
<< " extent " << dim.tile_extent_to_str();
110-
tdbs::LOG_INFO(ss.str());
101+
tdbs::LOG_INFO(
102+
std::format(
103+
"[soma_array_reader] Dimension {} type {} extent {}",
104+
dim.name(),
105+
tiledb::impl::to_str(dim.type()),
106+
dim.tile_extent_to_str()));
111107
name2dim.emplace(std::make_pair(dim.name(), std::make_shared<tiledb::Dimension>(dim)));
112108
}
113109

@@ -143,12 +139,12 @@ SEXP soma_array_reader(
143139
"or using iterated partial reads.",
144140
uri);
145141
}
146-
{
147-
std::stringstream ss;
148-
ss << "[soma_array_reader] Read complete with " << sr_data->get()->num_rows() << " rows and "
149-
<< sr_data->get()->names().size() << " cols";
150-
tdbs::LOG_INFO(ss.str());
151-
}
142+
tdbs::LOG_INFO(
143+
std::format(
144+
"[soma_array_reader] Read complete with {} rows and {} cols",
145+
sr_data->get()->num_rows(),
146+
sr_data->get()->names().size()));
147+
152148
const std::vector<std::string> names = sr_data->get()->names();
153149
auto ncol = names.size();
154150
// Schema first
@@ -166,11 +162,8 @@ SEXP soma_array_reader(
166162

167163
arr->length = 0; // initial value
168164
for (size_t i = 0; i < ncol; i++) {
169-
{
170-
std::stringstream ss;
171-
ss << "[soma_array_reader] Accessing '" << names[i] << "' at pos " << i;
172-
tdbs::LOG_INFO(ss.str());
173-
}
165+
tdbs::LOG_INFO(std::format("[soma_array_reader] Accessing '{}' at pos {}", names[i], i));
166+
174167
// now buf is a shared_ptr to ColumnBuffer
175168
auto buf = sr_data->get()->at(names[i]);
176169

@@ -182,16 +175,13 @@ SEXP soma_array_reader(
182175
// pp.first.get(), sizeof(ArrowArray));
183176
ArrowArrayMove(pp.first.get(), arr->children[i]);
184177
ArrowSchemaMove(pp.second.get(), sch->children[i]);
185-
{
186-
std::stringstream ss;
187-
ss << "[soma_array_reader] Incoming name " << std::string(pp.second->name) << " length "
188-
<< pp.first->length;
189-
tdbs::LOG_INFO(ss.str());
190-
}
178+
179+
tdbs::LOG_INFO(
180+
std::format(
181+
"[soma_array_reader] Incoming name {} length {}", std::string(pp.second->name), pp.first->length));
182+
191183
if (pp.first->length > arr->length) {
192-
std::stringstream ss;
193-
ss << "[soma_array_reader] Setting array length to " << pp.first->length;
194-
tdbs::LOG_DEBUG(ss.str());
184+
tdbs::LOG_DEBUG(std::format("[soma_array_reader] Setting array length to {}", pp.first->length));
195185
arr->length = pp.first->length;
196186
}
197187
}
@@ -437,10 +427,12 @@ SEXP c_schema(const std::string& uri, Rcpp::XPtr<somactx_wrap_t> ctxxp) {
437427
exitIfError(ArrowSchemaAllocateChildren(sch, lib_retval->n_children), "Bad schema children alloc");
438428

439429
for (size_t i = 0; i < static_cast<size_t>(lib_retval->n_children); i++) {
440-
std::stringstream ss;
441-
ss << "[c_schema] Accessing name '" << std::string(lib_retval->children[i]->name) << "' format '"
442-
<< std::string(lib_retval->children[i]->format) << "' at position " << i;
443-
tdbs::LOG_INFO(ss.str());
430+
tdbs::LOG_INFO(
431+
std::format(
432+
"[c_schema] Accessing name '{}' format '{}' at position {}",
433+
lib_retval->children[i]->name,
434+
lib_retval->children[i]->format,
435+
i));
444436
ArrowSchemaMove(lib_retval->children[i], sch->children[i]);
445437
}
446438

apis/r/src/riterator.cpp

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <nanoarrow/nanoarrow.h>
88
#include <nanoarrow/r.h> // for C interface to Arrow (via R package nanoarrow)
99
#include <RcppInt64> // for fromInteger64
10-
#include <sstream>
10+
#include <format>
1111

1212
#include <tiledb/tiledb>
1313
#if TILEDB_VERSION_MAJOR == 2 && TILEDB_VERSION_MINOR >= 4
@@ -91,9 +91,7 @@ Rcpp::XPtr<tdbs::ManagedQuery> mq_setup(
9191
tdbs::LOG_SET_LEVEL(loglevel);
9292
}
9393

94-
std::stringstream ss;
95-
ss << "[mq_setup] Setting up " << uri;
96-
tdbs::LOG_DEBUG(ss.str());
94+
tdbs::LOG_DEBUG(std::format("[mq_setup] Setting up {}", uri));
9795

9896
std::string_view name = "unnamed";
9997
std::vector<std::string> column_names = {};
@@ -123,10 +121,13 @@ Rcpp::XPtr<tdbs::ManagedQuery> mq_setup(
123121
tiledb::Domain domain = schema->domain();
124122
std::vector<tiledb::Dimension> dims = domain.dimensions();
125123
for (auto& dim : dims) {
126-
std::stringstream ss;
127-
ss << "[mq_setup] Dimension '" << dim.name() << "' type " << tiledb::impl::to_str(dim.type()) << " domain "
128-
<< dim.domain_to_str() << " extent " << dim.tile_extent_to_str();
129-
tdbs::LOG_DEBUG(ss.str());
124+
tdbs::LOG_DEBUG(
125+
std::format(
126+
"[mq_setup] Dimension '{}' type {} domain {} extent {}",
127+
dim.name(),
128+
tiledb::impl::to_str(dim.type()),
129+
dim.domain_to_str(),
130+
dim.tile_extent_to_str()));
130131
name2dim.emplace(std::make_pair(dim.name(), std::make_shared<tiledb::Dimension>(dim)));
131132
}
132133

@@ -203,19 +204,14 @@ SEXP mq_next(Rcpp::XPtr<tdbs::ManagedQuery> mq) {
203204
check_xptr_tag<tdbs::ManagedQuery>(mq);
204205

205206
if (mq_complete(mq)) {
206-
std::stringstream ss;
207-
ss << "[mq_next] complete " << mq->is_complete(true) << " num_cells " << mq->total_num_cells();
208-
tdbs::LOG_TRACE(ss.str());
207+
tdbs::LOG_TRACE(
208+
std::format("[mq_next] complete {} num_cells {}", mq->is_complete(true), mq->total_num_cells()));
209209
return create_empty_arrow_table();
210210
}
211211

212212
auto mq_data = mq->read_next();
213-
{
214-
std::stringstream ss;
215-
ss << "[mq_next] Read " << mq_data->get()->num_rows() << " rows and " << mq_data->get()->names().size()
216-
<< " cols";
217-
tdbs::LOG_DEBUG(ss.str());
218-
}
213+
tdbs::LOG_DEBUG(
214+
std::format("[mq_next] Read {} rows and {} cols", mq_data->get()->num_rows(), mq_data->get()->names().size()));
219215

220216
if (!mq_data) {
221217
tdbs::LOG_TRACE("[mq_next] complete - mq_data read no data");
@@ -240,11 +236,8 @@ SEXP mq_next(Rcpp::XPtr<tdbs::ManagedQuery> mq) {
240236
arr->length = 0; // initial value
241237

242238
for (size_t i = 0; i < ncol; i++) {
243-
{
244-
std::stringstream ss;
245-
ss << "[mq_next] Accessing " << names[i] << " at " << i;
246-
tdbs::LOG_TRACE(ss.str());
247-
}
239+
tdbs::LOG_TRACE(std::format("[mq_next] Accessing {} at {}", names[i], i));
240+
248241
// now buf is a shared_ptr to ColumnBuffer
249242
auto buf = mq_data->get()->at(names[i]);
250243

@@ -255,16 +248,12 @@ SEXP mq_next(Rcpp::XPtr<tdbs::ManagedQuery> mq) {
255248
ArrowSchemaMove(pp.second.get(), sch->children[i]);
256249

257250
if (pp.first->length > arr->length) {
258-
std::stringstream ss;
259-
ss << "[soma_array_reader] Setting array length to " << pp.first->length;
260-
tdbs::LOG_DEBUG(ss.str());
251+
tdbs::LOG_DEBUG(std::format("[soma_array_reader] Setting array length to {}", pp.first->length));
261252
arr->length = pp.first->length;
262253
}
263254
}
264255

265-
std::stringstream ss;
266-
ss << "[mq_next] Exporting chunk with " << arr->length << " rows";
267-
tdbs::LOG_DEBUG(ss.str());
256+
tdbs::LOG_DEBUG(std::format("[mq_next] Exporting chunk with {} rows", arr->length));
268257
// Nanoarrow special: stick schema into xptr tag to return single SEXP
269258
array_xptr_set_schema(arrayxp, schemaxp); // embed schema in array
270259
return arrayxp;
@@ -284,8 +273,11 @@ void mq_set_dim_points(Rcpp::XPtr<tdbs::ManagedQuery> mq, std::string dim, Rcpp:
284273

285274
std::vector<int64_t> vec = Rcpp::fromInteger64(points);
286275
mq->select_points<int64_t>(dim, vec);
287-
std::stringstream ss;
288-
ss << "[mq_set_dim_points] Set on dim '" << dim << "' for " << points.length() << " points, first two are "
289-
<< vec[0] << " and " << vec[1];
290-
tdbs::LOG_DEBUG(ss.str());
276+
tdbs::LOG_DEBUG(
277+
std::format(
278+
"[mq_set_dim_points] Set on dim '{}' for {} points, first two are {} and {}",
279+
dim,
280+
points.length(),
281+
vec[0],
282+
vec[1]));
291283
}

0 commit comments

Comments
 (0)