Esempio n. 1
0
view::const_iterator view::find(stdx::string_view key) const {
    bson_t b;
    bson_iter_t iter;

    if (!bson_init_static(&b, _data, _length)) {
        return cend();
    }

    if (!bson_iter_init(&iter, &b)) {
        return cend();
    }

    if (key.empty()) {
        return cend();
    }

    while (bson_iter_next(&iter)) {
        const char* ikey = bson_iter_key(&iter);
        if (0 == strncmp(key.data(), ikey, key.size())) {
            return const_iterator(element(iter.raw, iter.len, iter.off));
        }
    }

    return cend();
}
Esempio n. 2
0
view::const_iterator view::find(stdx::string_view key) const {
    bson_t b;
    if (!bson_init_static(&b, _data, _length)) {
        return cend();
    }

    bson_iter_t iter;

    // Logically, a default constructed string_view represents the
    // empty string just as does string_view(""), but they have,
    // potentially, different represntations, the former having .data
    // returning nullptr though the latter probably does not. But the
    // C functions like strncmp below can't be called with nullptr. If
    // we were called with a string_data such that its .data() member
    // returns nullptr, then, barring undefined behavior, its length
    // is known to be zero, and it is equivalent to the empty string,
    // an instance of which we reset it to.
    if (key.data() == nullptr) {
        key = "";
    }

    if (!bson_iter_init_find_w_len(&iter, &b, key.data(), static_cast<int>(key.size()))) {
        return cend();
    }

    return const_iterator(element(
        _data, static_cast<uint32_t>(_length), bson_iter_offset(&iter), bson_iter_key_len(&iter)));
}
Esempio n. 3
0
oid::oid(stdx::string_view str) : _is_valid(bson_oid_is_valid(str.data(), str.length())) {
    if (_is_valid) {
        bson_oid_t oid;
        bson_oid_init_from_string(&oid, str.data());
        memcpy(_bytes, oid.bytes, sizeof(_bytes));
    }
}
Esempio n. 4
0
document::value from_json(stdx::string_view json) {
    bson_error_t error;
    bson_t* result =
        bson_new_from_json(reinterpret_cast<const uint8_t*>(json.data()), json.size(), &error);

    if (!result) throw exception(error_code::k_json_parse_failure, error.message);

    std::uint32_t length;
    std::uint8_t* buf = bson_destroy_with_steal(result, true, &length);

    return document::value{buf, length, bson_free_deleter};
}
Esempio n. 5
0
stdx::optional<document::value> from_json(stdx::string_view json) {
    bson_error_t error;
    bson_t* result =
        bson_new_from_json(reinterpret_cast<const uint8_t*>(json.data()), json.size(), &error);

    if (!result) return stdx::nullopt;

    std::uint32_t length;
    std::uint8_t* buf = bson_destroy_with_steal(result, true, &length);

    return document::value{buf, length, bson_free_deleter};
}
Esempio n. 6
0
BSONCXX_INLINE_NAMESPACE_BEGIN

decimal128::decimal128(stdx::string_view str) {
    bson_decimal128_t d128;
    if (!bson_decimal128_from_string(str.to_string().c_str(), &d128)) {
        throw bsoncxx::exception{error_code::k_invalid_decimal128};
    }
    _high = d128.high;
    _low = d128.low;
}
Esempio n. 7
0
void collection::rename(stdx::string_view new_name, bool drop_target_before_rename) {
    bson_error_t error;

    auto result = libmongoc::collection_rename(_get_impl().collection_t, _get_impl().database_name.c_str(),
                                               new_name.data(), drop_target_before_rename, &error);

    if (!result) {
        throw_exception<operation_exception>(error);
    }
}
Esempio n. 8
0
view::iterator view::find(stdx::string_view key) const {
    bson_t b;
    bson_iter_t iter;

    if (!bson_init_static(&b, _data, _length)) {
        return end();
    }

    if (bson_iter_init_find(&iter, &b, key.data())) {
        return iterator(element(iter.raw, iter.len, iter.off));
    } else {
        return end();
    }
}
Esempio n. 9
0
class collection database::create_collection(stdx::string_view name,
                                             const options::create_collection& options) {
    bson_error_t error;

    libbson::scoped_bson_t opts_bson{options.to_document()};
    auto result = libmongoc::database_create_collection(_get_impl().database_t, name.data(),
                                                        opts_bson.bson(), &error);

    if (!result) {
        throw_exception<operation_exception>(error);
    }

    return mongocxx::collection(*this, static_cast<void*>(result));
}
Esempio n. 10
0
void write_concern::tag(stdx::string_view confirm_from) {
    libmongoc::write_concern_set_wtag(_impl->write_concern_t, confirm_from.to_string().data());
}
Esempio n. 11
0
collection::collection(const database& database, stdx::string_view collection_name,
                       void* collection)
    : _impl(stdx::make_unique<impl>(static_cast<mongoc_collection_t*>(collection), database.name(),
                                    database._impl->client_impl, collection_name.data())) {
}
Esempio n. 12
0
collection::collection(const database& database, stdx::string_view collection_name)
    : _impl(stdx::make_unique<impl>(
          libmongoc::database_get_collection(database._impl->database_t, collection_name.data()),
          database.name(), database._impl->client_impl, collection_name.data())) {
}
Esempio n. 13
0
oid::oid(stdx::string_view str) : _is_valid(bson_oid_is_valid(str.data(), str.length())) {
    if (_is_valid) {
        bson_oid_t oid;
        bson_oid_init_from_string(&oid, str.data());
    }
}
Esempio n. 14
0
void read_concern::acknowledge_string(stdx::string_view rc_string) {
    // libmongoc uses a NULL level to mean "use the server's default read_concern."
    libmongoc::read_concern_set_level(
        _impl->read_concern_t,
        rc_string.empty() ? NULL : bsoncxx::string::to_string(rc_string).data());
}
Esempio n. 15
0
bool database::has_collection(stdx::string_view name) const {
    bson_error_t error;
    return libmongoc::database_has_collection(_get_impl().database_t, name.data(), &error);
}