Skip to content

Commit 2c85418

Browse files
TylerBrockhanumantmk
authored andcommitted
CDRIVER-360 add additional geo index options
Closes #109
1 parent 9060c73 commit 2c85418

File tree

7 files changed

+210
-18
lines changed

7 files changed

+210
-18
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
TODO: Existing complex index names may contain a zero instead of a type due to
2+
a bug in mongoc_collection_keys_to_index_string. As a result those indexes may
3+
be hard to drop from the driver as they have a name you would not expect.
4+
5+
-- Ask Tyler if you need more info.
6+
17
mongo-c-driver 1.0.2
28
====================
39

build/cmake/libmongoc.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ mongoc_gridfs_find_one_by_filename
129129
mongoc_gridfs_get_chunks
130130
mongoc_gridfs_get_files
131131
mongoc_gridfs_remove_by_filename
132+
mongoc_index_opt_geo_get_default
133+
mongoc_index_opt_geo_init
132134
mongoc_index_opt_get_default
133135
mongoc_index_opt_init
134136
mongoc_init

src/libmongoc.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ mongoc_gridfs_find_one_by_filename
130130
mongoc_gridfs_get_chunks
131131
mongoc_gridfs_get_files
132132
mongoc_gridfs_remove_by_filename
133+
mongoc_index_opt_geo_get_default
134+
mongoc_index_opt_geo_init
133135
mongoc_index_opt_get_default
134136
mongoc_index_opt_init
135137
mongoc_init

src/mongoc/mongoc-collection.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,24 @@ mongoc_collection_keys_to_index_string (const bson_t *keys)
683683
s = bson_string_new (NULL);
684684

685685
while (bson_iter_next (&iter)) {
686-
bson_string_append_printf (s,
687-
(i++ ? "_%s_%d" : "%s_%d"),
688-
bson_iter_key (&iter),
689-
bson_iter_int32 (&iter));
686+
687+
/* Index type can be specified as a string ("2d") or as an integer representing direction */
688+
if (bson_iter_type(&iter) == BSON_TYPE_UTF8)
689+
{
690+
bson_string_append_printf (s,
691+
(i++ ? "_%s_%s" : "%s_%s"),
692+
bson_iter_key (&iter),
693+
bson_iter_utf8 (&iter, NULL));
694+
}
695+
else
696+
{
697+
698+
bson_string_append_printf (s,
699+
(i++ ? "_%s_%d" : "%s_%d"),
700+
bson_iter_key (&iter),
701+
bson_iter_int32 (&iter));
702+
}
703+
690704
}
691705

692706
return bson_string_free (s, false);
@@ -811,12 +825,14 @@ mongoc_collection_create_index (mongoc_collection_t *collection,
811825
bson_error_t *error)
812826
{
813827
const mongoc_index_opt_t *def_opt;
828+
const mongoc_index_opt_geo_t *def_geo;
814829
bson_error_t local_error;
815830
const char *name;
816831
bson_t cmd = BSON_INITIALIZER;
817832
bson_t ar;
818833
bson_t doc;
819834
bson_t reply;
835+
const mongoc_index_opt_geo_t *geo_opt;
820836
char *alloc_name = NULL;
821837
bool ret = false;
822838

@@ -870,6 +886,25 @@ mongoc_collection_create_index (mongoc_collection_t *collection,
870886
if (opt->language_override != def_opt->language_override) {
871887
BSON_APPEND_UTF8 (&doc, "language_override", opt->language_override);
872888
}
889+
if (opt->geo_options) {
890+
geo_opt = opt->geo_options;
891+
def_geo = mongoc_index_opt_geo_get_default ();
892+
if (geo_opt->twod_sphere_version != def_geo->twod_sphere_version) {
893+
BSON_APPEND_INT32 (&doc, "2dsphereIndexVersion", geo_opt->twod_sphere_version);
894+
}
895+
if (geo_opt->twod_bits_precision != def_geo->twod_bits_precision) {
896+
BSON_APPEND_INT32 (&doc, "bits", geo_opt->twod_bits_precision);
897+
}
898+
if (geo_opt->twod_location_min != def_geo->twod_location_min) {
899+
BSON_APPEND_DOUBLE (&doc, "min", geo_opt->twod_location_min);
900+
}
901+
if (geo_opt->twod_location_max != def_geo->twod_location_max) {
902+
BSON_APPEND_DOUBLE (&doc, "max", geo_opt->twod_location_max);
903+
}
904+
if (geo_opt->haystack_bucket_size != def_geo->haystack_bucket_size) {
905+
BSON_APPEND_DOUBLE (&doc, "bucketSize", geo_opt->haystack_bucket_size);
906+
}
907+
}
873908
bson_append_document_end (&ar, &doc);
874909
bson_append_array_end (&cmd, &ar);
875910

src/mongoc/mongoc-index.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,28 @@ static mongoc_index_opt_t gMongocIndexOptDefault = {
3232
0,
3333
-1,
3434
-1,
35+
NULL
3536
};
3637

38+
static mongoc_index_opt_geo_t gMongocIndexOptGeoDefault = {
39+
26,
40+
-90,
41+
90,
42+
-1,
43+
2
44+
};
3745

3846
const mongoc_index_opt_t *
3947
mongoc_index_opt_get_default (void)
4048
{
4149
return &gMongocIndexOptDefault;
4250
}
4351

52+
const mongoc_index_opt_geo_t *
53+
mongoc_index_opt_geo_get_default (void)
54+
{
55+
return &gMongocIndexOptGeoDefault;
56+
}
4457

4558
void
4659
mongoc_index_opt_init (mongoc_index_opt_t *opt)
@@ -49,3 +62,11 @@ mongoc_index_opt_init (mongoc_index_opt_t *opt)
4962

5063
memcpy (opt, &gMongocIndexOptDefault, sizeof *opt);
5164
}
65+
66+
void
67+
mongoc_index_opt_geo_init (mongoc_index_opt_geo_t *opt)
68+
{
69+
BSON_ASSERT (opt);
70+
71+
memcpy (opt, &gMongocIndexOptGeoDefault, sizeof *opt);
72+
}

src/mongoc/mongoc-index.h

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,38 @@
2626

2727
BSON_BEGIN_DECLS
2828

29+
typedef struct
30+
{
31+
uint8_t twod_sphere_version;
32+
uint8_t twod_bits_precision;
33+
double twod_location_min;
34+
double twod_location_max;
35+
double haystack_bucket_size;
36+
uint8_t *padding[32];
37+
} mongoc_index_opt_geo_t;
2938

3039
typedef struct
3140
{
32-
bool is_initialized;
33-
bool background;
34-
bool unique;
35-
const char *name;
36-
bool drop_dups;
37-
bool sparse;
38-
int32_t expire_after_seconds;
39-
int32_t v;
40-
const bson_t *weights;
41-
const char *default_language;
42-
const char *language_override;
43-
void *padding[8];
41+
bool is_initialized;
42+
bool background;
43+
bool unique;
44+
const char *name;
45+
bool drop_dups;
46+
bool sparse;
47+
int32_t expire_after_seconds;
48+
int32_t v;
49+
const bson_t *weights;
50+
const char *default_language;
51+
const char *language_override;
52+
mongoc_index_opt_geo_t *geo_options;
53+
void *padding[7];
4454
} mongoc_index_opt_t;
4555

4656

47-
const mongoc_index_opt_t *mongoc_index_opt_get_default (void) BSON_GNUC_CONST;
48-
void mongoc_index_opt_init (mongoc_index_opt_t *opt);
57+
const mongoc_index_opt_t *mongoc_index_opt_get_default (void) BSON_GNUC_CONST;
58+
const mongoc_index_opt_geo_t *mongoc_index_opt_geo_get_default (void) BSON_GNUC_CONST;
59+
void mongoc_index_opt_init (mongoc_index_opt_t *opt);
60+
void mongoc_index_opt_geo_init (mongoc_index_opt_geo_t *opt);
4961

5062

5163
BSON_END_DECLS

tests/test-mongoc-collection.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,118 @@ test_index (void)
506506
mongoc_client_destroy(client);
507507
}
508508

509+
static void
510+
test_index_compound (void)
511+
{
512+
mongoc_collection_t *collection;
513+
mongoc_database_t *database;
514+
mongoc_client_t *client;
515+
mongoc_index_opt_t opt;
516+
bson_error_t error;
517+
bool r;
518+
bson_t keys;
519+
520+
mongoc_index_opt_init(&opt);
521+
522+
client = mongoc_client_new(gTestUri);
523+
ASSERT (client);
524+
525+
database = get_test_database (client);
526+
ASSERT (database);
527+
528+
collection = get_test_collection (client, "test_index_compound");
529+
ASSERT (collection);
530+
531+
bson_init(&keys);
532+
bson_append_int32(&keys, "hello", -1, 1);
533+
bson_append_int32(&keys, "world", -1, -1);
534+
r = mongoc_collection_create_index(collection, &keys, &opt, &error);
535+
ASSERT (r);
536+
537+
r = mongoc_collection_create_index(collection, &keys, &opt, &error);
538+
ASSERT (r);
539+
540+
r = mongoc_collection_drop_index(collection, "hello_1_world_-1", &error);
541+
ASSERT (r);
542+
543+
bson_destroy(&keys);
544+
545+
r = mongoc_collection_drop (collection, &error);
546+
ASSERT (r);
547+
548+
mongoc_collection_destroy(collection);
549+
mongoc_database_destroy(database);
550+
mongoc_client_destroy(client);
551+
}
552+
553+
static void
554+
test_index_geo (void)
555+
{
556+
mongoc_collection_t *collection;
557+
mongoc_database_t *database;
558+
mongoc_client_t *client;
559+
mongoc_index_opt_t opt;
560+
mongoc_index_opt_geo_t geo_opt;
561+
bson_error_t error;
562+
bool r;
563+
bson_t keys;
564+
565+
mongoc_index_opt_init(&opt);
566+
mongoc_index_opt_geo_init(&geo_opt);
567+
568+
client = mongoc_client_new(gTestUri);
569+
ASSERT (client);
570+
571+
database = get_test_database (client);
572+
ASSERT (database);
573+
574+
collection = get_test_collection (client, "test_geo_index");
575+
ASSERT (collection);
576+
577+
/* Create a basic 2d index */
578+
bson_init(&keys);
579+
BSON_APPEND_UTF8(&keys, "location", "2d");
580+
r = mongoc_collection_create_index(collection, &keys, &opt, &error);
581+
ASSERT (r);
582+
583+
r = mongoc_collection_drop_index(collection, "location_2d", &error);
584+
ASSERT (r);
585+
586+
/* Create a 2d index with bells and whistles */
587+
bson_init(&keys);
588+
BSON_APPEND_UTF8(&keys, "location", "2d");
589+
590+
geo_opt.twod_location_min = -123;
591+
geo_opt.twod_location_max = +123;
592+
geo_opt.twod_bits_precision = 30;
593+
opt.geo_options = &geo_opt;
594+
595+
r = mongoc_collection_create_index(collection, &keys, &opt, &error);
596+
ASSERT (r);
597+
598+
r = mongoc_collection_drop_index(collection, "location_2d", &error);
599+
ASSERT (r);
600+
601+
/* Create a Haystack index */
602+
bson_init(&keys);
603+
BSON_APPEND_UTF8(&keys, "location", "geoHaystack");
604+
BSON_APPEND_INT32(&keys, "category", 1);
605+
606+
mongoc_index_opt_geo_init(&geo_opt);
607+
geo_opt.haystack_bucket_size = 5;
608+
opt.geo_options = &geo_opt;
609+
610+
r = mongoc_collection_create_index(collection, &keys, &opt, &error);
611+
ASSERT (r);
612+
613+
r = mongoc_collection_drop_index(collection, "location_geoHaystack_category_1", &error);
614+
ASSERT (r);
615+
616+
mongoc_collection_destroy(collection);
617+
mongoc_database_destroy(database);
618+
mongoc_client_destroy(client);
619+
}
620+
509621

510622
static void
511623
test_count (void)
@@ -1216,6 +1328,8 @@ test_collection_install (TestSuite *suite)
12161328
TestSuite_Add (suite, "/Collection/insert", test_insert);
12171329
TestSuite_Add (suite, "/Collection/save", test_save);
12181330
TestSuite_Add (suite, "/Collection/index", test_index);
1331+
TestSuite_Add (suite, "/Collection/index_compound", test_index_compound);
1332+
TestSuite_Add (suite, "/Collection/index_geo", test_index_geo);
12191333
TestSuite_Add (suite, "/Collection/regex", test_regex);
12201334
TestSuite_Add (suite, "/Collection/update", test_update);
12211335
TestSuite_Add (suite, "/Collection/remove", test_remove);

0 commit comments

Comments
 (0)