Skip to content

Commit b965950

Browse files
authored
Switch to cpp11 (#585)
1 parent d3324f1 commit b965950

11 files changed

+118
-139
lines changed

DESCRIPTION

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Imports:
2727
lifecycle,
2828
methods,
2929
prettyunits,
30-
Rcpp,
3130
rlang (>= 1.1.0),
3231
tibble
3332
Suggests:
@@ -43,8 +42,8 @@ Suggests:
4342
withr
4443
LinkingTo:
4544
cli,
46-
rapidjsonr,
47-
Rcpp
45+
cpp11,
46+
rapidjsonr
4847
Config/Needs/website: tidyverse/tidytemplate
4948
Config/testthat/edition: 3
5049
Config/testthat/parallel: TRUE
@@ -53,7 +52,6 @@ Encoding: UTF-8
5352
Roxygen: list(markdown = TRUE)
5453
RoxygenNote: 7.2.3
5554
Collate:
56-
'RcppExports.R'
5755
'bigrquery-package.R'
5856
'bq-auth.R'
5957
'bq-dataset.R'
@@ -72,6 +70,7 @@ Collate:
7270
'bq-test.R'
7371
'camelCase.R'
7472
'connections-page.R'
73+
'cpp11.R'
7574
'dbi-driver.R'
7675
'dbi-connection.R'
7776
'dbi-result.R'

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ exportMethods(show)
152152
import(DBI)
153153
import(methods)
154154
import(rlang, except = unbox)
155-
importFrom(Rcpp,sourceCpp)
156155
importFrom(bit64,integer64)
157156
importFrom(gargle,token_fetch)
158157
importFrom(httr,DELETE)

R/RcppExports.R

Lines changed: 0 additions & 15 deletions
This file was deleted.

R/bigrquery-package.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ the <- new_environment()
2323
#' @importFrom bit64 integer64
2424
#' @importFrom jsonlite unbox
2525
#' @importFrom lifecycle deprecated
26-
#' @importFrom Rcpp sourceCpp
2726
#' @importFrom tibble tibble
2827
## usethis namespace: end
2928
NULL

R/cpp11.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Generated by cpp11: do not edit by hand
2+
3+
bq_parse <- function(meta_s, data_s) {
4+
.Call(`_bigrquery_bq_parse`, meta_s, data_s)
5+
}
6+
7+
bq_field_init <- function(json, value) {
8+
.Call(`_bigrquery_bq_field_init`, json, value)
9+
}
10+
11+
bq_parse_files <- function(schema_path, file_paths, n, quiet) {
12+
.Call(`_bigrquery_bq_parse_files`, schema_path, file_paths, n, quiet)
13+
}

src/BqField.cpp

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
// [[Rcpp::depends(rapidjsonr)]]
2-
#include <Rcpp.h>
1+
#include <cpp11.hpp>
2+
33
#include <rapidjson/document.h>
44
#include <rapidjson/istreamwrapper.h>
55
#include "rapidjson/filereadstream.h"
66
#include <cli/progress.h>
77
#include "integer64.h"
88
#include "base64.h"
99

10+
#include <vector>
1011
#include <ctime>
1112
#include <cstdio>
1213
#include <climits>
@@ -27,7 +28,7 @@ int64_t parse_int64(const char* x) {
2728
// this will throw an exception with the appropriate error message
2829
// if the package is not installed
2930
void check_namespace(const char* pkg, const char* bq_type) {
30-
Rcpp::Function checkNamespaceFun("bq_check_namespace", "bigrquery");
31+
auto checkNamespaceFun = cpp11::package("bigrquery")["bq_check_namespace"];
3132
checkNamespaceFun(pkg, bq_type);
3233
}
3334

@@ -96,7 +97,7 @@ class BqField {
9697

9798
BqField(const rapidjson::Value& field) {
9899
if (!field.IsObject()) {
99-
Rcpp::stop("Invalid field spec");
100+
cpp11::stop("Invalid field spec");
100101
}
101102

102103
name_ = field["name"].GetString();
@@ -118,40 +119,44 @@ class BqField {
118119

119120
SEXP vectorInit(int n, bool array) const {
120121
if (array) {
121-
return Rcpp::List(n);
122+
return cpp11::writable::list(n);
122123
}
123124

124125
switch(type_) {
125126
case BQ_INTEGER: {
126-
Rcpp::DoubleVector out(n);
127+
cpp11::writable::doubles out(n);
127128
out.attr("class") = "integer64";
128129
return out;
129130
}
130131
case BQ_FLOAT:
131-
return Rcpp::DoubleVector(n);
132+
return cpp11::writable::doubles(n);
132133
case BQ_BOOLEAN:
133-
return Rcpp::LogicalVector(n);
134+
return cpp11::writable::logicals(n);
134135
case BQ_STRING:
135-
return Rcpp::CharacterVector(n);
136-
case BQ_TIMESTAMP:
137-
return Rcpp::DatetimeVector(n, "UTC");
136+
return cpp11::writable::strings(n);
137+
case BQ_TIMESTAMP: {
138+
cpp11::writable::doubles out(n);
139+
out.attr("class") = {"POSIXct", "POSIXt"};
140+
out.attr("tzone") = "UTC";
141+
return out;
142+
}
138143
case BQ_RECORD:
139-
return Rcpp::List(n);
144+
return cpp11::writable::list(n);
140145
case BQ_GEOGRAPHY: {
141146
check_namespace("wk", "GEOGRAPHY");
142-
Rcpp::CharacterVector out(n);
143-
out.attr("class") = Rcpp::CharacterVector::create("wk_wkt", "wk_vctr");
147+
cpp11::writable::strings out(n);
148+
out.attr("class") = {"wk_wkt", "wk_vctr"};
144149
return out;
145150
}
146151
case BQ_BYTES: {
147152
check_namespace("blob", "BYTES");
148-
Rcpp::List out(n);
149-
out.attr("class") = Rcpp::CharacterVector::create("blob", "vctrs_list_of", "vctrs_vctr", "list");
150-
out.attr("ptype") = Rcpp::RawVector::create();
153+
cpp11::writable::list out(n);
154+
out.attr("class") = {"blob", "vctrs_list_of", "vctrs_vctr", "list"};
155+
out.attr("ptype") = cpp11::writable::raws((R_xlen_t) 0);
151156
return out;
152157
}
153158
default: {
154-
Rcpp::CharacterVector out(n);
159+
cpp11::writable::strings out(n);
155160
out.attr("bq_type") = type_str_;
156161
return out;
157162
}
@@ -165,10 +170,10 @@ class BqField {
165170
void vectorSet(SEXP x, int i, const rapidjson::Value& v, bool array) const {
166171
if (array && type_ != BQ_RECORD) {
167172
if (!v.IsArray())
168-
Rcpp::stop("Not an array [1]");
173+
cpp11::stop("Not an array [1]");
169174

170175
int n = v.Size();
171-
Rcpp::RObject out = vectorInit(n, false);
176+
cpp11::sexp out = vectorInit(n, false);
172177
for (int j = 0; j < n; ++j) {
173178
vectorSet(out, j, v[j]["v"], false);
174179
}
@@ -196,7 +201,7 @@ class BqField {
196201
case BQ_UNKNOWN:
197202
case BQ_STRING:
198203
if (v.IsString()) {
199-
Rcpp::RObject chr = Rf_mkCharLenCE(v.GetString(), v.GetStringLength(), CE_UTF8);
204+
cpp11::sexp chr = Rf_mkCharLenCE(v.GetString(), v.GetStringLength(), CE_UTF8);
200205
SET_STRING_ELT(x, i, chr);
201206
} else {
202207
SET_STRING_ELT(x, i, NA_STRING);
@@ -207,17 +212,15 @@ class BqField {
207212
break;
208213
case BQ_GEOGRAPHY:
209214
if (v.IsString()) {
210-
Rcpp::RObject chr = Rf_mkCharLenCE(v.GetString(), v.GetStringLength(), CE_UTF8);
215+
cpp11::sexp chr = Rf_mkCharLenCE(v.GetString(), v.GetStringLength(), CE_UTF8);
211216
SET_STRING_ELT(x, i, chr);
212217
} else {
213218
SET_STRING_ELT(x, i, NA_STRING);
214219
}
215220
break;
216221
case BQ_BYTES:
217222
if (v.IsString()) {
218-
Rcpp::RawVector chr(v.GetStringLength());
219-
memcpy(&(chr[0]), v.GetString(), v.GetStringLength());
220-
SET_VECTOR_ELT(x, i, base64_decode(chr));
223+
SET_VECTOR_ELT(x, i, base64_decode(v.GetString(), v.GetStringLength()));
221224
} else {
222225
SET_VECTOR_ELT(x, i, R_NilValue);
223226
}
@@ -228,9 +231,8 @@ class BqField {
228231
SEXP recordValue(const rapidjson::Value& v) const {
229232
int p = fields_.size();
230233

231-
Rcpp::List out(p);
232-
Rcpp::CharacterVector names(p);
233-
out.attr("names") = names;
234+
cpp11::writable::list out(p);
235+
cpp11::writable::strings names(p);
234236

235237
if (!array_) {
236238
if (!v.IsObject())
@@ -239,14 +241,14 @@ class BqField {
239241
const rapidjson::Value& f = v["f"];
240242
// f is array of fields
241243
if (!f.IsArray())
242-
Rcpp::stop("Not array [2]");
244+
cpp11::stop("Not array [2]");
243245

244246
for (int j = 0; j < p; ++j) {
245247
const BqField& field = fields_[j];
246248
const rapidjson::Value& vs = f[j]["v"];
247249

248250
int n = (field.array_) ? vs.Size() : 1;
249-
Rcpp::RObject col = field.vectorInit(n, false);
251+
cpp11::sexp col = field.vectorInit(n, false);
250252

251253
if (field.array_) {
252254
for (int i = 0; i < n; ++i) {
@@ -267,23 +269,26 @@ class BqField {
267269
out[j] = field.vectorInit(n);
268270
names[j] = field.name_;
269271
}
270-
out.attr("class") = Rcpp::CharacterVector::create("tbl_df", "tbl", "data.frame");
271-
out.attr("row.names") = Rcpp::IntegerVector::create(NA_INTEGER, -n);
272+
out.attr("class") = {"tbl_df", "tbl", "data.frame"};
273+
out.attr("row.names") = {NA_INTEGER, -n};
272274

273-
if (n == 0)
275+
if (n == 0) {
276+
out.attr("names") = names;
274277
return out;
278+
}
275279

276280
for (int i = 0; i < n; ++i) {
277281
const rapidjson::Value& f = v[i]["v"]["f"];
278282
if (!f.IsArray())
279-
Rcpp::stop("Not an array [3]");
283+
cpp11::stop("Not an array [3]");
280284

281285
for (int j = 0; j < p; ++j) {
282286
fields_[j].vectorSet(out[j], i, f[j]["v"]);
283287
}
284288
}
285289
}
286290

291+
out.attr("names") = names;
287292
return out;
288293
}
289294

@@ -306,24 +311,24 @@ std::vector<BqField> bq_fields_parse(const rapidjson::Value& meta) {
306311
return fields;
307312
}
308313

309-
Rcpp::List bq_fields_init(const std::vector<BqField>& fields, int n) {
314+
cpp11::list bq_fields_init(const std::vector<BqField>& fields, int n) {
310315
int p = fields.size();
311316

312-
Rcpp::List out(p);
313-
Rcpp::CharacterVector names(p);
317+
cpp11::writable::list out(p);
318+
cpp11::writable::strings names(p);
314319
for (int j = 0; j < p; ++j) {
315320
out[j] = fields[j].vectorInit(n);
316321
names[j] = fields[j].name();
317322
};
318-
out.attr("class") = Rcpp::CharacterVector::create("tbl_df", "tbl", "data.frame");
323+
out.attr("class") = {"tbl_df", "tbl", "data.frame"};
319324
out.attr("names") = names;
320-
out.attr("row.names") = Rcpp::IntegerVector::create(NA_INTEGER, -n);
325+
out.attr("row.names") = {NA_INTEGER, -n};
321326

322327
return out;
323328
}
324329

325330
int bq_fields_set(const rapidjson::Value& data,
326-
Rcpp::List out,
331+
cpp11::writable::list out,
327332
const std::vector<BqField>& fields,
328333
int offset
329334
) {
@@ -345,7 +350,7 @@ int bq_fields_set(const rapidjson::Value& data,
345350
return n;
346351
}
347352

348-
// [[Rcpp::export]]
353+
[[cpp11::register]]
349354
SEXP bq_parse(std::string meta_s, std::string data_s) {
350355
rapidjson::Document meta_d;
351356
meta_d.Parse(&meta_s[0]);
@@ -356,19 +361,19 @@ SEXP bq_parse(std::string meta_s, std::string data_s) {
356361

357362
int n = (values_d.HasMember("rows")) ? values_d["rows"].Size() : 0;
358363

359-
Rcpp::List out = bq_fields_init(fields, n);
364+
cpp11::writable::list out = bq_fields_init(fields, n);
360365
bq_fields_set(values_d, out, fields, 0);
361366

362367
return out;
363368
}
364369

365-
// [[Rcpp::export]]
370+
[[cpp11::register]]
366371
SEXP bq_field_init(std::string json, std::string value = "") {
367372
rapidjson::Document d1;
368373
d1.Parse(&json[0]);
369374

370375
BqField field(d1);
371-
Rcpp::RObject out = field.vectorInit(1);
376+
cpp11::sexp out = field.vectorInit(1);
372377

373378
if (value != "") {
374379
rapidjson::Document d2;
@@ -380,7 +385,7 @@ SEXP bq_field_init(std::string json, std::string value = "") {
380385
return out;
381386
}
382387

383-
// [[Rcpp::export]]
388+
[[cpp11::register]]
384389
SEXP bq_parse_files(std::string schema_path,
385390
std::vector<std::string> file_paths,
386391
int n,
@@ -393,7 +398,7 @@ SEXP bq_parse_files(std::string schema_path,
393398
schema_doc.ParseStream(schema_stream_w);
394399

395400
std::vector<BqField> fields = bq_fields_parse(schema_doc);
396-
Rcpp::List out = bq_fields_init(fields, n);
401+
cpp11::writable::list out = bq_fields_init(fields, n);
397402

398403
std::vector<std::string>::const_iterator it = file_paths.begin(),
399404
it_end = file_paths.end();
@@ -414,15 +419,15 @@ SEXP bq_parse_files(std::string schema_path,
414419

415420
if (values_doc.HasParseError()) {
416421
UNPROTECT(2);
417-
Rcpp::stop("Failed to parse '%s'", *it);
422+
cpp11::stop("Failed to parse '%s'", it->c_str());
418423
fclose(values_file);
419424
}
420425

421426
total_seen += bq_fields_set(values_doc, out, fields, total_seen);
422427
if (!quiet) {
423428
if (CLI_SHOULD_TICK) cli_progress_add(pb, 1);
424429
} else {
425-
Rcpp::checkUserInterrupt();
430+
cpp11::check_user_interrupt();
426431
};
427432

428433
fclose(values_file);
@@ -433,7 +438,7 @@ SEXP bq_parse_files(std::string schema_path,
433438

434439
if (total_seen != n) {
435440
// Matches the error thrown from R if the first "test balloon" chunk is short.
436-
Rcpp::stop("%d rows were requested, but only %d rows were received.\n Leave `page_size` unspecified or use an even smaller value.", n, total_seen);
441+
cpp11::stop("%d rows were requested, but only %d rows were received.\n Leave `page_size` unspecified or use an even smaller value.", n, total_seen);
437442
}
438443

439444
return out;

src/Makevars

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)