1 /*
2  * Copyright 2016 MongoDB, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <mongoc.h>
19 #include <stdio.h>
20 
21 
22 const char *COLLECTION_NAME = "things";
23 
24 #include "../doc-common-insert.c"
25 #include "explain.c"
26 #include "copydb.c"
27 #include "clone-collection.c"
28 
29 
30 int
main(int argc,char * argv[])31 main (int argc, char *argv[])
32 {
33    mongoc_database_t *database = NULL;
34    mongoc_client_t *client = NULL;
35    mongoc_collection_t *collection = NULL;
36    char *host_and_port;
37    int res = 0;
38    char *other_host_and_port = NULL;
39 
40    if (argc < 2 || argc > 3) {
41       fprintf (stderr,
42                "usage: %s MONGOD-1-CONNECTION-STRING "
43                "[MONGOD-2-HOST-NAME:MONGOD-2-PORT]\n",
44                argv[0]);
45       fprintf (stderr,
46                "MONGOD-1-CONNECTION-STRING can be "
47                "of the following forms:\n");
48       fprintf (stderr, "localhost\t\t\t\tlocal machine\n");
49       fprintf (stderr, "localhost:27018\t\t\t\tlocal machine on port 27018\n");
50       fprintf (stderr,
51                "mongodb://user:pass@localhost:27017\t"
52                "local machine on port 27017, and authenticate with username "
53                "user and password pass\n");
54       return 1;
55    }
56 
57    mongoc_init ();
58 
59    if (strncmp (argv[1], "mongodb://", 10) == 0) {
60       host_and_port = bson_strdup (argv[1]);
61    } else {
62       host_and_port = bson_strdup_printf ("mongodb://%s", argv[1]);
63    }
64    other_host_and_port = argc > 2 ? argv[2] : NULL;
65 
66    client = mongoc_client_new (host_and_port);
67 
68    if (!client) {
69       fprintf (stderr, "Invalid hostname or port: %s\n", host_and_port);
70       res = 2;
71       goto cleanup;
72    }
73 
74    mongoc_client_set_error_api (client, 2);
75    database = mongoc_client_get_database (client, "test");
76    collection = mongoc_database_get_collection (database, COLLECTION_NAME);
77 
78    printf ("Inserting data\n");
79    if (!insert_data (collection)) {
80       res = 3;
81       goto cleanup;
82    }
83 
84    printf ("explain\n");
85    if (!explain (collection)) {
86       res = 4;
87       goto cleanup;
88    }
89 
90    if (other_host_and_port) {
91       printf ("copydb\n");
92       if (!copydb (client, other_host_and_port)) {
93          res = 5;
94          goto cleanup;
95       }
96 
97       printf ("clone collection\n");
98       if (!clone_collection (database, other_host_and_port)) {
99          res = 6;
100          goto cleanup;
101       }
102    }
103 
104 cleanup:
105    if (collection) {
106       mongoc_collection_destroy (collection);
107    }
108 
109    if (database) {
110       mongoc_database_destroy (database);
111    }
112 
113    if (client) {
114       mongoc_client_destroy (client);
115    }
116 
117    bson_free (host_and_port);
118    mongoc_cleanup ();
119    return res;
120 }
121