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

Introduce CBOR format support for REST payloads #169

Merged
merged 7 commits into from
Aug 12, 2019

Conversation

bpintea
Copy link
Collaborator

@bpintea bpintea commented Aug 7, 2019

This PR adds basic support for CBOR encapsulation, as an alternative
to JSON.

tinycbor library is employed for parsing/encoding CBOR, at its today's
master version (latest 0.5.2 release plus a few fixes).

The format to use is connection-specific and configured in the
connection string.

The introduction stops short of supporting encoding/decoding of the
parameters and result sets values: it will allow building a
non-parameterized query and decoding the response object (either with a
result-set or with an error), but without unpacking the values in the
result set of the latter. (The rest of the functionality will be part of
subsequent PRs.)
Server version querying is also carried over CBOR.

All communication is done either over JSON or CBOR, although the driver
will carry on if it receives a JSON response to a CBOR request (or the
other way around). However, driver-generated "fake" responses to catalog
queries are always JSON-formatted (makes maintenance of text responses
easier).

The PR also enhances the support of the Elasticsearch-formatted
errors (both JSON and CBOR) in that the "error" parameter will be parsed
if of a map type (generally an ES/SQL error) or passed on as is, if of a
string type. The previous behavior was to abandon parsing if "error"
wasn't a map and present the entire error answer to the user; this
wouldn't work well with a CBOR object now.

bpintea added 6 commits August 7, 2019 12:42
git-subtree-dir: libs/tinycbor
git-subtree-split: d2dd95cb8841d88d5a801e3ef9c328fd6200e7bd
With this commit CMake will add the tinycbor files to the sources to be
compiled into the driver.
CMake will exclude two files:
- open_memstream.c that won't be compiled by MSVC
  (WITHOUT_OPEN_MEMSTREAM compilation flag doesn't exclude its source);
- cborparser.c that will be copied (and patched) under the building
  folder.

The patching of the later file above adds one function that allows
copy-free extraction of text/byte strings.
This commit adds basic support for CBOR encapsulation, as an alternative
to JSON.

The format to use is connection-specific and configured in the
connection string.

The introduction stops short of supporting encoding/decoding of the
parameters and result sets values: it will allow building a
non-parameterized query and decoding the response object (either with a
result-set or with an error), but without unpacking the values in the
result set of the latter.
Server version querying is also carried over CBOR.

All communication is done either over JSON or CBOR, although the driver
will carry on if it receives a JSON response to a CBOR request (or the
other way around). However, driver-generated "fake" responses to catalog
queries are always JSON-formatted (makes maintenance of text responses
easier).

The commit also enhances the support of the Elasticsearch-formatted
errors (both JSON and CBOR) in that the "error" parameter will be parsed
if of a map type (generally an ES/SQL error) or passed on as is, if of a
string type. The previous behavior was to abandon parsing if "error"
wasn't a map and present the entire error answer to the user; this
wouldn't work well with a CBOR object now.
- the define is not yet used, though.
- add to the repo the files necessary to generate the legal notices and
reports.
@bpintea bpintea added >feature Applicable to PRs adding new functionality v8.0.0-alpha1 v7.4.0 labels Aug 7, 2019
@bpintea bpintea requested review from droberts195 and edsavage August 7, 2019 12:37
@edsavage edsavage self-assigned this Aug 7, 2019
Copy link
Collaborator

@edsavage edsavage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM: I've commented on a few nits - sorry!

Also, at some point it would be good to see test cases exercising CBOR queries etc.

driver/connect.c Outdated
resp.cnt, LCSTR(&resp));
goto err;
if (is_json) {
if (! parse_es_version_json(dbc, &rsp_body, &es_ver, &state)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the return codes from parse_es_version_json and parse_es_version_cbor could be assigned to a variable, this would allow for the deduplication of the error handling and assignment of the version number to n

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've compacted the code here.

driver/queries.c Outdated
rec->name = col_name->cnt ? *col_name : EMPTY_WSTR;

assert(! rec->es_type);
/* lookup the DBC-cashed ES type */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: cached

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed.

driver/queries.c Outdated
}
}
if (rec->es_type) {
/* copy fileds pre-calculated at DB connect time */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: fields

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed.

driver/queries.c Outdated
n = type_wstr.cnt < wbuff_cnt ? type_wstr.cnt : wbuff_cnt;
wmemcpy(wbuff, type_wstr.str, n);
pos = n;
if (sizeof(": ") - 1 + pos < wbuff_cnt) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: assign ": " to a named variable

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, implemented the change.

};
const char fmt_outer_keys[] = "ON";
const char fmt_outer_keys[] = "UN";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Was this change intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it changes parser's matching on key types, having it accept any ("unknown" type) objects.

if ((res = cbor_value_skip_tag(it)) != CborNoError) {
return res;
}
/* is current key is a string, get its name */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed.

if (res != CborNoError) {
return res;
}
// TODO: binary search on ordered keys?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

- reducing code duplication on srv. version checking;
- fixing a couple of comment typos;
- assigning const strings to named vars.
@bpintea
Copy link
Collaborator Author

bpintea commented Aug 9, 2019

Also, at some point it would be good to see test cases exercising CBOR queries etc.

For sure. They will be added with the next PRs.

@bpintea bpintea merged commit 992f0e0 into elastic:master Aug 12, 2019
@bpintea bpintea deleted the feat/pack_cbor_basic branch August 12, 2019 11:57
@bpintea bpintea mentioned this pull request Aug 16, 2019
5 tasks
bpintea added a commit that referenced this pull request Aug 28, 2019
* Squashed 'libs/tinycbor/' content from commit d2dd95c

git-subtree-dir: libs/tinycbor
git-subtree-split: d2dd95cb8841d88d5a801e3ef9c328fd6200e7bd

* add tinycbor library to the project

With this commit CMake will add the tinycbor files to the sources to be
compiled into the driver.
CMake will exclude two files:
- open_memstream.c that won't be compiled by MSVC
  (WITHOUT_OPEN_MEMSTREAM compilation flag doesn't exclude its source);
- cborparser.c that will be copied (and patched) under the building
  folder.

The patching of the later file above adds one function that allows
copy-free extraction of text/byte strings.

* introduce CBOR format support for REST payloads

This commit adds basic support for CBOR encapsulation, as an alternative
to JSON.

The format to use is connection-specific and configured in the
connection string.

The introduction stops short of supporting encoding/decoding of the
parameters and result sets values: it will allow building a
non-parameterized query and decoding the response object (either with a
result-set or with an error), but without unpacking the values in the
result set of the latter.
Server version querying is also carried over CBOR.

All communication is done either over JSON or CBOR, although the driver
will carry on if it receives a JSON response to a CBOR request (or the
other way around). However, driver-generated "fake" responses to catalog
queries are always JSON-formatted (makes maintenance of text responses
easier).

The commit also enhances the support of the Elasticsearch-formatted
errors (both JSON and CBOR) in that the "error" parameter will be parsed
if of a map type (generally an ES/SQL error) or passed on as is, if of a
string type. The previous behavior was to abandon parsing if "error"
wasn't a map and present the entire error answer to the user; this
wouldn't work well with a CBOR object now.

* fix a copy&paste define error

- the define is not yet used, though.

* add the legal files for tinycbor as 3rd party lib.

- add to the repo the files necessary to generate the legal notices and
reports.

* addressing PR review comments

- reducing code duplication on srv. version checking;
- fixing a couple of comment typos;
- assigning const strings to named vars.

(cherry picked from commit 992f0e0)
@bpintea bpintea mentioned this pull request Sep 10, 2019
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>feature Applicable to PRs adding new functionality v7.4.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants