-
Notifications
You must be signed in to change notification settings - Fork 546
CXX-3077 BSON Binary Vector accessor, sub_binary builder #1356
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
Merged
Merged
Changes from 84 commits
Commits
Show all changes
85 commits
Select commit
Hold shift + click to select a range
826cdd8
Add binary vector subtype
5101593
Add an example that demonstrates most vector view API features
5bddf8c
Bump required libmongoc version to pre-release 2.0.0
fa061e8
Additional binary builder tests, including sub_binary
b2dd2f8
vector API tests
2a478bb
New error codes for use in bsoncxx::vector
8ed6fb9
document the bsoncxx::vector namespaces
b316f50
Add forwarding headers for vector and sub_binary
5360c23
initial implementation for bsoncxx::vector
f573b11
sub_binary builder and integration with core
2b5c35a
remove obsolete workaround for CDRIVER-5732
16b8e1f
avoid int32 overflow prior to bson_append_utf8 invocation
5c6d77e
Temporary MONGOC_VERSION_MINIMUM bump for pre-2.0.0
37fe267
Fixes for conversion warnings
d577c15
Add missing includes
d93b1d7
Fix endian detection for win32
d29c352
fixup! Fix endian detection for win32
5019c3d
Replace problematic operator overloads and tests
8bddeb6
lambda missing capture mode
479a5ac
Add missing include
37ddd51
cstdint consistency fixes
d2b2130
More header and name qualification fixes
983da6b
Revert "avoid int32 overflow prior to bson_append_utf8 invocation"
d1f59dd
Revert "remove obsolete workaround for CDRIVER-5732"
36e3f6c
Merge branch 'master' into CXX-3077
03af662
clang-format
0c26bc8
Update src/bsoncxx/include/bsoncxx/docs/v_noabi.hpp
6bb3376
Revert change to required libbson/libmongoc versions
411a643
Remove ALL_VECTOR_FORMATS
1a3bf3c
Update src/bsoncxx/include/bsoncxx/docs/v_noabi.hpp
d45eca7
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/vector/formats.hpp
9be7d04
Replace most REQUIREs with CHECKs
e184ea5
Replace most REQUIRE with CHECK in bson_builder too
a8c285c
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/builder/basic/sub_…
9d8e4c7
k_vector_out_of_range
973f7b6
doxygen suggestions
7194f60
Rename bsoncxx::v_noabi::vector::impl to detail
d38f8aa
Take the impl out of impl_data
fa51a16
Refactor forward declarations
d69d6e2
Rename vector view to vector accessor
61bfb59
Refactor exported symbols out of 'detail' namespace
c52a7f1
Fix doc comments for vector elements/iterators namespaces
9737bf0
Can't rely on constexpr std::array operator[]
53dc708
Move internal parts of format exports to detail namespace
6b9cd01
Update vector fwd headers
8b61e63
Remove extra includes
799c897
Add core::open_binary for symmetry
a581e0a
Fix duplicate forward declaration
da06589
Add include for completeness
3dfc9f1
Fix typo in test name
9a670cf
Comment about accessor_data constructor assumptions
4ca9431
Document types in vector::accessor
007192b
Add cbegin/cend
352bb73
Requested change to header order
165a5b7
Replace open_binary with suggestion
dccb2fd
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/builder/basic/sub_…
868179a
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/vector/accessor-fw…
b8df720
Update src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp
3422b2e
Update src/bsoncxx/test/vector.cpp
2ef0bb2
More header order changes
7ffdb17
Update src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp
66b2a02
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/vector/accessor.hpp
e809f9f
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/vector/accessor.hpp
b567118
Revert "Update src/bsoncxx/test/vector.cpp"
d170e9e
Shorten doc comments
9218f97
Disable gcc -Wfloat-equal warnings for comparisons in bsoncxx::vector…
42250ea
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/vector/formats-fwd…
3f61ef4
Hide detail from accessor type declarations
bd78a37
vector example: shorter output, more comments
ebf3ec1
Update src/bsoncxx/test/vector.cpp
c147b88
Prefer CHECK_THROWS_WITH_CODE
5067724
Clarify comment
5b7c991
Unclutter the vector example some more
87849b3
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/types.hpp
bb61396
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/vector/elements.hpp
3747aae
Update src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp
107b192
Update src/bsoncxx/lib/bsoncxx/v_noabi/bsoncxx/vector.cpp
f8ad45d
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/vector/detail.hpp
73737dd
Update src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/vector/elements.hpp
14019ab
Add accessor::as_const
cfee0fb
Prefer {} initializers
e66887d
Add test for swap()
9872b74
Portability: replace arithmetic right shift with signed division
4b87eb0
Clarify test name
77205e5
Merge branch 'master' into CXX-3077
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
// Copyright 2009-present MongoDB, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <algorithm> | ||
#include <cmath> | ||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
#include <bsoncxx/builder/basic/document.hpp> | ||
#include <bsoncxx/builder/basic/kvp.hpp> | ||
#include <bsoncxx/builder/basic/sub_binary.hpp> | ||
#include <bsoncxx/json.hpp> | ||
#include <bsoncxx/types.hpp> | ||
#include <bsoncxx/vector/accessor.hpp> | ||
#include <bsoncxx/vector/formats.hpp> | ||
|
||
#include <examples/macros.hh> | ||
|
||
int EXAMPLES_CDECL main() { | ||
using bsoncxx::binary_sub_type; | ||
using bsoncxx::builder::basic::kvp; | ||
using bsoncxx::builder::basic::make_document; | ||
using bsoncxx::builder::basic::sub_binary; | ||
using bsoncxx::vector::accessor; | ||
using bsoncxx::vector::formats::f_float32; | ||
using bsoncxx::vector::formats::f_int8; | ||
using bsoncxx::vector::formats::f_packed_bit; | ||
|
||
bsoncxx::document::value doc = make_document( | ||
|
||
// | ||
// Added along with BSON Binary Vector support, the new sub_binary builder | ||
// allows allocating any type of BSON Binary item in-place. A callback taking | ||
// a sub_binary argument can calculate the required space before calling allocate() | ||
// on the sub_binary to get a pointer to the new in-place allocation. | ||
// | ||
// Every byte of the allocated binary region must be written or the resulting BSON | ||
// will have undefined contents. If allocate() isn't called exactly once, | ||
// an exception will be thrown. | ||
// | ||
kvp("binary", | ||
[&](sub_binary sbin) { | ||
uint32_t len = 10; | ||
uint8_t* data = sbin.allocate(binary_sub_type::k_binary, len); | ||
memset(data, 0x55, len); | ||
}), | ||
|
||
// | ||
// The sub_binary also provides an allocate() method for BSON Binary Vector. | ||
// Instead of a sub_type and byte length, this takes a vector format | ||
// and an element count. | ||
// | ||
// This example uses the f_int8 vector format, which has int8_t elements. | ||
// The allocate() call here returns a bsoncxx::vector::accessor instance | ||
// that works like a random access container but does not own memory directly. | ||
// | ||
kvp("vector_int8", | ||
[&](sub_binary sbin) { | ||
auto vec = sbin.allocate(f_int8{}, 10); | ||
int8_t i = -5; | ||
std::generate(vec.begin(), vec.end(), [&] { return ++i; }); | ||
}), | ||
|
||
// | ||
// BSON Binary Vector supports formats that do not map directly to C++ | ||
// built-in types. The f_float32 format is an unaligned little endian | ||
// serialization of IEEE 754 32-bit binary floating point. On some platforms, | ||
// this sort of data could be accessed using a raw float*, but for consistent | ||
// portability we have a bsoncxx::v_noabi::vector::elements::float32 type which | ||
// has the unaligned little-endian representation in memory but supports automatic | ||
// conversion to and from 'float'. | ||
// | ||
// The vector accessor works like a container of floats. Elements can be assigned | ||
// from float expressions or used as float expressions. Assignment operators | ||
// operate by automatically convering to float and then back to elements::float32. | ||
// | ||
kvp("vector_float32", | ||
[&](sub_binary sbin) { | ||
auto vec = sbin.allocate(f_float32{}, 10); | ||
// Calculate a fibonacci sequence starting near the smallest representable value | ||
vec[0] = 0.f; | ||
vec[1] = 1e-38f; | ||
for (size_t i = 2; i < vec.size(); i++) { | ||
vec[i] = vec[i - 1] + vec[i - 2]; | ||
} | ||
// Demonstrate assignment operators | ||
vec[0] += 1.f; | ||
vec[1] *= 1e38f; | ||
vec[1] /= 2.f; | ||
vec[1] -= 1.f + vec[0]; | ||
}), | ||
|
||
// | ||
// packed_bit vectors support any number of single-bit elements, | ||
// using an accessor that works like a random-access container of | ||
// bool values. This works using a reference-proxy type | ||
// bsoncxx::v_noabi::vector::elements::packed_bit_element and an iterator | ||
// bsoncxx::v_noabi::vector::iterators::packed_bit_element. | ||
// | ||
// Every bsoncxx::vector::accessor can be accessed either in per-element | ||
// or per-byte mode. Byte mode is particularly useful for applications that | ||
// may want to use packed_bit vectors in the serialized format without | ||
// accessing individual elements. | ||
// | ||
kvp("vector_packed_bit", [&](sub_binary sbin) { | ||
auto vec = sbin.allocate(f_packed_bit{}, 61); | ||
// Start by setting all bits to 1 | ||
std::fill(vec.begin(), vec.end(), true); | ||
// Flip a bit using a boolean expression | ||
vec[5] = !vec[5]; | ||
// Assignment of a packed_bit_element reference copies the referenced bit value | ||
vec[6] = vec[1]; | ||
vec[7] = vec[5]; | ||
// Bits can be assigned from boolean expressions, and from zero. | ||
vec[8] = 0; | ||
vec[60] = false; | ||
// Demonstrate addressing bits backward from the end of the vector | ||
std::fill(vec.end() - 20, vec.end() - 4, false); | ||
std::fill(vec.end() - 8, vec.end() - 5, true); | ||
// Flip all bits, operating an entire byte at a time. | ||
// The last byte will have bits that do not correspond to any elements, and writes to these are ignored. | ||
for (auto i = vec.byte_begin(); i != vec.byte_end(); i++) { | ||
*i ^= 0xFF; | ||
} | ||
// Demonstrate copying bit ranges and byte ranges using std::copy | ||
std::copy(vec.byte_begin(), vec.byte_begin() + 2, vec.byte_begin() + 2); | ||
std::copy(vec.begin() + 5, vec.begin() + 9, vec.begin() + 56); | ||
})); | ||
|
||
// Demonstrate extended JSON serialization of the entire document | ||
std::cout << bsoncxx::to_json(doc) << std::endl; | ||
|
||
// Iterate over elements in the int8 vector | ||
{ | ||
accessor<f_int8 const> vec{doc["vector_int8"].get_binary()}; | ||
std::cout << "int8: " << vec.size() << std::endl; | ||
for (auto&& i : vec) { | ||
std::cout << int{i} << " "; | ||
} | ||
std::cout << std::endl; | ||
} | ||
|
||
// Iterate over bytes in the int8 vector | ||
{ | ||
accessor<f_int8 const> vec{doc["vector_int8"].get_binary()}; | ||
std::cout << "int8 bytes: " << vec.byte_size() << std::hex << std::endl; | ||
for (auto i = vec.byte_begin(); i != vec.byte_end(); i++) { | ||
std::cout << int{*i} << " "; | ||
} | ||
std::cout << std::dec << std::endl; | ||
} | ||
|
||
// Iterate over elements in the float32 vector | ||
{ | ||
accessor<f_float32 const> vec{doc["vector_float32"].get_binary()}; | ||
std::cout << "float32: " << vec.size() << std::endl; | ||
for (auto&& i : vec) { | ||
std::cout << i << " "; | ||
} | ||
std::cout << std::endl; | ||
} | ||
|
||
// Iterate over bytes in the float32 vector | ||
{ | ||
accessor<f_float32 const> vec{doc["vector_float32"].get_binary()}; | ||
std::cout << "float32 bytes: " << vec.byte_size() << std::hex << std::endl; | ||
for (auto i = vec.byte_begin(); i != vec.byte_end(); i++) { | ||
std::cout << int{*i} << " "; | ||
} | ||
std::cout << std::dec << std::endl; | ||
} | ||
|
||
// Iterate over elements in the packed_bit vector | ||
{ | ||
accessor<f_packed_bit const> vec{doc["vector_packed_bit"].get_binary()}; | ||
std::cout << "packed_bit: " << vec.size() << std::endl; | ||
for (auto&& i : vec) { | ||
std::cout << i << " "; | ||
} | ||
std::cout << std::endl; | ||
} | ||
|
||
// Iterate over bytes in the packed_bit vector | ||
{ | ||
accessor<f_packed_bit const> vec{doc["vector_packed_bit"].get_binary()}; | ||
std::cout << "packed_bit bytes: " << vec.byte_size() << std::hex << std::endl; | ||
for (auto i = vec.byte_begin(); i != vec.byte_end(); i++) { | ||
std::cout << int{*i} << " "; | ||
} | ||
std::cout << std::dec << std::endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/bsoncxx/include/bsoncxx/v_noabi/bsoncxx/builder/basic/sub_binary-fwd.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2009-present MongoDB, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
namespace bsoncxx { | ||
namespace v_noabi { | ||
namespace builder { | ||
namespace basic { | ||
|
||
class sub_binary; | ||
|
||
} // namespace basic | ||
} // namespace builder | ||
} // namespace v_noabi | ||
} // namespace bsoncxx | ||
|
||
namespace bsoncxx { | ||
namespace builder { | ||
namespace basic { | ||
|
||
using ::bsoncxx::v_noabi::builder::basic::sub_binary; | ||
|
||
} // namespace basic | ||
} // namespace builder | ||
} // namespace bsoncxx | ||
|
||
/// | ||
/// @file | ||
/// Declares @ref bsoncxx::v_noabi::builder::basic::sub_binary | ||
/// |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.