|
65 | 65 | #include <mongocxx/config/private/prelude.hh>
|
66 | 66 |
|
67 | 67 | using bsoncxx::builder::basic::kvp;
|
| 68 | +using bsoncxx::builder::basic::make_array; |
68 | 69 | using bsoncxx::builder::basic::make_document;
|
69 | 70 | using bsoncxx::builder::basic::sub_array;
|
70 | 71 | using bsoncxx::builder::basic::sub_document;
|
@@ -752,136 +753,43 @@ std::int64_t collection::count(view_or_value filter, const options::count& optio
|
752 | 753 | return result;
|
753 | 754 | }
|
754 | 755 |
|
755 |
| -bsoncxx::document::value collection::create_index(view_or_value keys, |
756 |
| - const options::index& options) { |
757 |
| - scoped_bson_t bson_keys{keys}; |
758 |
| - bson_error_t error; |
759 |
| - ::mongoc_index_opt_geo_t geo_opt{}; |
760 |
| - ::mongoc_index_opt_t opt{}; |
761 |
| - ::mongoc_index_opt_wt_t wt_opt{}; |
762 |
| - libmongoc::index_opt_init(&opt); |
763 |
| - |
764 |
| - // keep our safe copies alive |
765 |
| - bsoncxx::string::view_or_value name_copy{}; |
766 |
| - bsoncxx::string::view_or_value wt_config_copy{}; |
767 |
| - bsoncxx::string::view_or_value default_language_copy{}; |
768 |
| - bsoncxx::string::view_or_value language_override_copy{}; |
769 |
| - |
770 |
| - if (options.background()) { |
771 |
| - opt.background = *options.background(); |
772 |
| - } |
773 |
| - |
774 |
| - if (options.unique()) { |
775 |
| - opt.unique = *options.unique(); |
776 |
| - } |
777 |
| - |
778 |
| - if (options.name()) { |
779 |
| - name_copy = options.name()->terminated(); |
780 |
| - opt.name = name_copy.data(); |
781 |
| - } |
782 |
| - |
783 |
| - if (options.sparse()) { |
784 |
| - opt.sparse = *options.sparse(); |
785 |
| - } |
786 |
| - |
787 |
| - if (options.storage_options()) { |
788 |
| - const options::index::wiredtiger_storage_options* wt_options; |
789 |
| - libmongoc::index_opt_wt_init(&wt_opt); |
| 756 | +bsoncxx::document::value collection::create_index(bsoncxx::document::view_or_value keys, |
| 757 | + bsoncxx::document::view_or_value opts) { |
| 758 | + using namespace bsoncxx; |
790 | 759 |
|
791 |
| - if (options.storage_options()->type() == |
792 |
| - ::mongoc_index_storage_opt_type_t::MONGOC_INDEX_STORAGE_OPT_WIREDTIGER) { |
793 |
| - wt_options = static_cast<const options::index::wiredtiger_storage_options*>( |
794 |
| - options.storage_options().get()); |
| 760 | + builder::basic::document index; |
| 761 | + document::view opts_view = opts.view(); |
795 | 762 |
|
796 |
| - if (wt_options->config_string()) { |
797 |
| - wt_config_copy = wt_options->config_string()->terminated(); |
798 |
| - wt_opt.config_str = wt_config_copy.data(); |
799 |
| - } |
800 |
| - opt.storage_options = reinterpret_cast<mongoc_index_opt_storage_t*>(&wt_opt); |
801 |
| - } |
802 |
| - } |
| 763 | + if (!opts_view["name"]) { |
| 764 | + scoped_bson_t keys_bson{keys}; |
803 | 765 |
|
804 |
| - if (options.expire_after()) { |
805 |
| - const auto count = options.expire_after()->count(); |
806 |
| - if ((count < 0) || (count > std::numeric_limits<int32_t>::max())) { |
807 |
| - throw logic_error{error_code::k_invalid_parameter}; |
808 |
| - } |
809 |
| - opt.expire_after_seconds = static_cast<std::int32_t>(count); |
810 |
| - } |
| 766 | + const auto name_from_keys = libmongoc::collection_keys_to_index_string(keys_bson.bson()); |
| 767 | + const auto cleanup_name_from_keys = make_guard([&] { bson_free(name_from_keys); }); |
811 | 768 |
|
812 |
| - if (options.version()) { |
813 |
| - opt.v = *options.version(); |
| 769 | + index.append(kvp("name", name_from_keys)); |
814 | 770 | }
|
815 | 771 |
|
816 |
| - if (options.weights()) { |
817 |
| - scoped_bson_t weights{*options.weights()}; |
818 |
| - opt.weights = weights.bson(); |
819 |
| - } |
| 772 | + index.append(builder::basic::concatenate(make_document(kvp("key", keys))), |
| 773 | + builder::basic::concatenate(opts)); |
820 | 774 |
|
821 |
| - if (options.default_language()) { |
822 |
| - default_language_copy = options.default_language()->terminated(); |
823 |
| - opt.default_language = default_language_copy.data(); |
824 |
| - } |
| 775 | + array::view_or_value index_array = make_array(index.view()); |
| 776 | + document::view_or_value command = |
| 777 | + make_document(kvp("createIndexes", name()), kvp("indexes", index_array.view())); |
825 | 778 |
|
826 |
| - if (options.language_override()) { |
827 |
| - language_override_copy = options.language_override()->terminated(); |
828 |
| - opt.language_override = language_override_copy.data(); |
829 |
| - } |
830 |
| - |
831 |
| - if (options.collation()) { |
832 |
| - scoped_bson_t collation{*options.collation()}; |
833 |
| - opt.collation = collation.bson(); |
834 |
| - } |
835 |
| - |
836 |
| - if (options.partial_filter_expression()) { |
837 |
| - scoped_bson_t partial_filter_expression{*options.partial_filter_expression()}; |
838 |
| - opt.partial_filter_expression = partial_filter_expression.bson(); |
839 |
| - } |
840 |
| - |
841 |
| - if (options.twod_sphere_version() || options.twod_bits_precision() || |
842 |
| - options.twod_location_min() || options.twod_location_max() || |
843 |
| - options.haystack_bucket_size()) { |
844 |
| - libmongoc::index_opt_geo_init(&geo_opt); |
845 |
| - |
846 |
| - if (options.twod_sphere_version()) { |
847 |
| - geo_opt.twod_sphere_version = *options.twod_sphere_version(); |
848 |
| - } |
849 |
| - |
850 |
| - if (options.twod_bits_precision()) { |
851 |
| - geo_opt.twod_bits_precision = *options.twod_bits_precision(); |
852 |
| - } |
853 |
| - |
854 |
| - if (options.twod_location_min()) { |
855 |
| - geo_opt.twod_location_min = *options.twod_location_min(); |
856 |
| - } |
857 |
| - |
858 |
| - if (options.twod_location_max()) { |
859 |
| - geo_opt.twod_location_max = *options.twod_location_max(); |
860 |
| - } |
861 |
| - |
862 |
| - if (options.haystack_bucket_size()) { |
863 |
| - geo_opt.haystack_bucket_size = *options.haystack_bucket_size(); |
864 |
| - } |
| 779 | + scoped_bson_t reply; |
| 780 | + bson_error_t error; |
865 | 781 |
|
866 |
| - opt.geo_options = &geo_opt; |
867 |
| - } |
| 782 | + scoped_bson_t command_bson{command}; |
| 783 | + scoped_bson_t opts_bson{make_document()}; |
868 | 784 |
|
869 |
| - auto result = libmongoc::collection_create_index( |
870 |
| - _get_impl().collection_t, bson_keys.bson(), &opt, &error); |
| 785 | + auto result = libmongoc::collection_command_simple( |
| 786 | + _get_impl().collection_t, command_bson.bson(), NULL, reply.bson_for_init(), &error); |
871 | 787 |
|
872 | 788 | if (!result) {
|
873 | 789 | throw_exception<operation_exception>(error);
|
874 | 790 | }
|
875 | 791 |
|
876 |
| - if (options.name()) { |
877 |
| - return make_document(kvp("name", *options.name())); |
878 |
| - } else { |
879 |
| - const auto keys = libmongoc::collection_keys_to_index_string(bson_keys.bson()); |
880 |
| - |
881 |
| - const auto clean_keys = make_guard([&] { bson_free(keys); }); |
882 |
| - |
883 |
| - return make_document(kvp("name", keys)); |
884 |
| - } |
| 792 | + return make_document(kvp("name", index.view()["name"].get_utf8().value)); |
885 | 793 | }
|
886 | 794 |
|
887 | 795 | cursor collection::distinct(bsoncxx::string::view_or_value field_name,
|
|
0 commit comments