Skip to content

Commit

Permalink
update to MongoDB C Driver v0.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
aoym committed Oct 4, 2013
1 parent d19bfdd commit 1ee260b
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 22 deletions.
18 changes: 18 additions & 0 deletions 3rdparty/mongo-c-driver/HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# MongoDB C Driver History

## 0.8.1
2013-9-26

This point relese includes the following.

** API CHANGE **

1. mongo_create_index now has a ttl parameter

int mongo_create_index( mongo *conn, const char *ns, const bson *key, const char *name, int options, int ttl, bson *out );

- bson_has_data that was missing from release 0.8
- mongo_create_index returns a bson object in case of errors
- mongo_cursor_next returns MONGO_CURSOR_EXHAUSTED if there is no result rather than MONGO_CURSOR_INVALID
- _get_host_port allocates memory for thread safety
- dylib name fix for Mac OSX
- various compiler warning fixes

## 0.8
2013-8-12

Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/mongo-c-driver/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

MAJOR_VERSION = "0"
MINOR_VERSION = "8"
PATCH_VERSION = "0"
PATCH_VERSION = "1"
VERSION = MAJOR_VERSION + "." + MINOR_VERSION + "." + PATCH_VERSION

# --- options ----
Expand Down
58 changes: 58 additions & 0 deletions 3rdparty/mongo-c-driver/docs/examples/aggregate_bcon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* aggregate_bcon.cpp
*
* Created on: Sep 11, 2013
* Author: Charlie
*/

#include <mongo.h>
#include <bcon.h>
#include <stdio.h>

int main() {
/*
* We assume objects in the form of {_id:<any_id>, list:[{a:<int>,b:<int>}, ...]}
*/
char table[] = "agg";
mongo conn[1];
mongo_init(conn);
if(MONGO_OK != mongo_client(conn, "127.0.0.1", 27017))
return 1;
bson b[1], b_result[1];

/*create the aggregation command in bson*/
bcon cmd_aggregate[] = { "aggregate", BRS(table),
"pipeline",
"[",
"{",
"$unwind", "$list",
"}",
"{",
"$group",
"{",
"_id", "$list",
"distinct_count",
"{",
"$sum", BI(1),
"}",
"}",
"}",
"]",
BEND
};
bson_from_bcon(b, cmd_aggregate);

/*So you can see your command*/
bson_print(b);

/*run the command*/
mongo_run_command(conn, "test", b, b_result);

/*command results*/
bson_print(b_result);

bson_destroy(b_result);
bson_destroy(b);
mongo_destroy(conn);
return 0;
}
51 changes: 51 additions & 0 deletions 3rdparty/mongo-c-driver/docs/examples/aggregate_bson.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* aggregate_bson.cpp
*
* Created on: Sep 11, 2013
* Author: Charlie
*/

#include <mongo.h>
#include <stdio.h>

int main() {
/*
* We assume objects in the form of {_id:<any_id>, list:[{a:<int>,b:<int>}, ...]}
*/
mongo conn[1];
mongo_init(conn);
if(MONGO_OK != mongo_client(conn, "127.0.0.1", 27017))
return 1;
bson b[1], b_result[1];
/*create the aggregation command in bson*/
bson_init(b);
bson_append_string(b, "aggregate", "agg");
bson_append_start_array(b, "pipeline");
bson_append_start_object(b,"0");
bson_append_string(b, "$unwind", "$list");
bson_append_finish_object(b);
bson_append_start_object(b,"1");
bson_append_start_object(b,"$group");
bson_append_string(b,"_id", "$list");
bson_append_start_object(b, "distinct_count");
bson_append_int(b, "$sum", 1);
bson_append_finish_object(b);
bson_append_finish_object(b);
bson_append_finish_object(b);
bson_append_finish_array(b);
bson_finish(b);

/*So you can see your command*/
bson_print(b);

/*run the command*/
mongo_run_command(conn, "test", b, b_result);

/*command results*/
bson_print(b_result);

bson_destroy(b_result);
bson_destroy(b);
mongo_destroy(conn);
return 0;
}
2 changes: 1 addition & 1 deletion 3rdparty/mongo-c-driver/docs/source/sphinx/source/bson.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ named "address", we use ``bson_find()`` like so:

.. code-block:: c
bson_iterator i[1], sub[i];
bson_iterator i[1], sub[1];
bson_type type;
type = bson_find( i, b, "address" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ Building the MongoDB C Driver
=============================

First checkout the version you want to build. *Always build from a particular tag, since HEAD may be
a work in progress.* For example, to build version 0.8, run:
a work in progress.* For example, to build version 0.8.1, run:

.. code-block:: bash
git checkout v0.8
git checkout v0.8.1
Then follow the build steps below.

Expand Down
4 changes: 2 additions & 2 deletions 3rdparty/mongo-c-driver/docs/source/sphinx/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
# built documents.
#
# The short X.Y version.
version = '0.8'
version = '0.8.1'
# The full version, including alpha/beta/rc tags.
release = '0.8'
release = '0.8.1'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/mongo-c-driver/doxygenConfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#---------------------------------------------------------------------------
DOXYFILE_ENCODING = UTF-8
PROJECT_NAME = MongoDB C Driver
PROJECT_NUMBER = 0.8
PROJECT_NUMBER = 0.8.1
OUTPUT_DIRECTORY = docs/source/doxygen
CREATE_SUBDIRS = NO
OUTPUT_LANGUAGE = English
Expand Down
4 changes: 2 additions & 2 deletions 3rdparty/mongo-c-driver/src/bcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#define ARRAY_INDEX_BUFFER_SIZE 9

char *bcon_errstr[] = {
const char *bcon_errstr[] = {
"OK",
"ERROR",
"bcon document or nesting incomplete",
Expand Down Expand Up @@ -292,7 +292,7 @@ bcon_error_t bson_from_bcon(bson *b, const bcon *bc) {

void bcon_print(const bcon *bc) { /* prints internal representation, not JSON */
char *typespec = 0;
char *delim = "";
const char *delim = "";
int end_of_data;
bcon *bcp;
putchar('{');
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/mongo-c-driver/src/bcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ typedef enum bcon_error_t {
BCON_BSON_ERROR /**< bson finish error */
} bcon_error_t;

extern char *bcon_errstr[]; /**< bcon_error_t text messages */
extern const char *bcon_errstr[]; /**< bcon_error_t text messages */

/**
* Append a BCON object to a BSON object.
Expand Down
6 changes: 5 additions & 1 deletion 3rdparty/mongo-c-driver/src/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ MONGO_EXPORT const char *bson_data( const bson *b ) {
return (const char *)b->data;
}

MONGO_EXPORT int bson_has_data( const bson *b ) {
return b->data != NULL;
}

static char hexbyte( char hex ) {
if (hex >= '0' && hex <= '9')
return (hex - '0');
Expand Down Expand Up @@ -561,7 +565,7 @@ MONGO_EXPORT bson_date_t bson_iterator_date( const bson_iterator *i ) {
}

MONGO_EXPORT time_t bson_iterator_time_t( const bson_iterator *i ) {
return bson_iterator_date( i ) / 1000;
return (time_t) bson_iterator_date( i ) / 1000;
}

MONGO_EXPORT int bson_iterator_bin_len( const bson_iterator *i ) {
Expand Down
4 changes: 2 additions & 2 deletions 3rdparty/mongo-c-driver/src/gridfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ MONGO_EXPORT int gridfs_init(mongo *client, const char *dbname, const char *pref
bson_init(&b);
bson_append_int(&b, "filename", 1);
bson_finish(&b);
if( mongo_create_index(gfs->client, gfs->files_ns, &b, NULL, 0, NULL) != MONGO_OK) {
if( mongo_create_index(gfs->client, gfs->files_ns, &b, NULL, 0, -1, NULL) != MONGO_OK) {
bson_destroy( &b );
gridfs_destroy( gfs );
return MONGO_ERROR;
Expand All @@ -175,7 +175,7 @@ MONGO_EXPORT int gridfs_init(mongo *client, const char *dbname, const char *pref
bson_append_int(&b, "files_id", 1);
bson_append_int(&b, "n", 1);
bson_finish(&b);
if( mongo_create_index(gfs->client, gfs->chunks_ns, &b, NULL, MONGO_INDEX_UNIQUE, NULL) != MONGO_OK ) {
if( mongo_create_index(gfs->client, gfs->chunks_ns, &b, NULL, MONGO_INDEX_UNIQUE, -1, NULL) != MONGO_OK ) {
bson_destroy(&b);
gridfs_destroy( gfs );
return MONGO_ERROR;
Expand Down
26 changes: 20 additions & 6 deletions 3rdparty/mongo-c-driver/src/mongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ MONGO_EXPORT int mongo_get_op_timeout(mongo* conn) {
}


static const char* _get_host_port(mongo_host_port* hp) {
static char _hp[sizeof(hp->host)+12];
static const char* _get_host_port(mongo_host_port* hp) {
char *_hp = (char*) bson_malloc(sizeof(hp->host)+12);
bson_sprintf(_hp, "%s:%d", hp->host, hp->port);
return _hp;
}


/* Memory returned by this function MUST be freed with bson_free */
MONGO_EXPORT const char* mongo_get_primary(mongo* conn) {
mongo* conn_ = (mongo*)conn;
if( !(conn_->connected) || (conn_->primary->host[0] == '\0') )
Expand All @@ -86,6 +87,7 @@ MONGO_EXPORT int mongo_get_host_count(mongo* conn) {
}


/* Memory returned by this function MUST be freed with bson_free */
MONGO_EXPORT const char* mongo_get_host(mongo* conn, int i) {
mongo_replica_set* r = conn->replica_set;
mongo_host_port* hp;
Expand Down Expand Up @@ -1361,10 +1363,11 @@ MONGO_EXPORT const bson *mongo_cursor_bson( mongo_cursor *cursor ) {
MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ) {
char *next_object;
char *message_end;
int already_sent = ( cursor->flags & MONGO_CURSOR_QUERY_SENT ) == MONGO_CURSOR_QUERY_SENT;

if( cursor == NULL ) return MONGO_ERROR;

if( ! ( cursor->flags & MONGO_CURSOR_QUERY_SENT ) )
if( ! already_sent )
if( mongo_cursor_op_query( cursor ) != MONGO_OK )
return MONGO_ERROR;

Expand All @@ -1383,7 +1386,10 @@ MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ) {
}

else {
cursor->err = MONGO_CURSOR_INVALID;
if( already_sent ) {
cursor->err = MONGO_CURSOR_INVALID;
}
/* else preserve the MONGO_CURSOR_EXHAUSTED error flag set by mongo_cursor_get_more */
return MONGO_ERROR;
}
}
Expand Down Expand Up @@ -1459,7 +1465,7 @@ MONGO_EXPORT int mongo_cursor_destroy( mongo_cursor *cursor ) {
#define INDEX_NAME_BUFFER_SIZE 255
#define INDEX_NAME_MAX_LENGTH (INDEX_NAME_BUFFER_SIZE - 1)

MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key, const char *name, int options, bson *out ) {
MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key, const char *name, int options, int ttl, bson *out ) {
bson b;
bson_iterator it;
char default_name[INDEX_NAME_BUFFER_SIZE] = {'\0'};
Expand Down Expand Up @@ -1492,17 +1498,25 @@ MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *ke
bson_append_bool( &b, "background", 1 );
if ( options & MONGO_INDEX_SPARSE )
bson_append_bool( &b, "sparse", 1 );

if( ttl > 0 )
bson_append_int( &b, "expireAfterSeconds", ttl );

bson_finish( &b );

strncpy( idxns, ns, 1024-16 );
p = strchr( idxns, '.' );
if ( !p ) {
bson_destroy( &b );
if( out )
bson_init_zero(out);
return MONGO_ERROR;
}
strcpy( p, ".system.indexes" );
if ( mongo_insert( conn, idxns, &b, NULL ) != MONGO_OK) {
bson_destroy( &b );
if( out )
bson_init_zero(out);
return MONGO_ERROR;
}
bson_destroy( &b );
Expand All @@ -1519,7 +1533,7 @@ MONGO_EXPORT bson_bool_t mongo_create_simple_index( mongo *conn, const char *ns,
bson_append_int( b, field, 1 );
bson_finish( b );

success = mongo_create_index( conn, ns, b, NULL, options, out );
success = mongo_create_index( conn, ns, b, NULL, options, -1, out );
bson_destroy( b );
return success;
}
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/mongo-c-driver/src/mongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ MONGO_EXPORT double mongo_count( mongo *conn, const char *db, const char *coll,
* Use bson_has_data() on the returned 'out' for determining this.
*/
MONGO_EXPORT int mongo_create_index( mongo *conn, const char *ns, const bson *key,
const char *name, int options, bson *out );
const char *name, int options, int ttl, bson *out );

/**
* Create an index with a single key.
Expand Down
5 changes: 5 additions & 0 deletions 3rdparty/mongo-c-driver/test/errors_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ int test_get_last_error_commands( void ) {
bson_destroy( &obj );

ASSERT( mongo_cmd_get_last_error( conn, db, &obj ) == MONGO_OK );
ASSERT( bson_has_data( &obj) );
bson_destroy( &obj );

/*********************/
Expand Down Expand Up @@ -251,6 +252,10 @@ int test_get_last_error_commands( void ) {
mongo_cmd_drop_db( conn, db );
mongo_destroy( conn );

/* for bson_has_data */
ASSERT( MONGO_ERROR == mongo_create_index( conn, "testbar", bson_shared_empty(), NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, -1, &obj ));
ASSERT( !bson_has_data( &obj) );

return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions 3rdparty/mongo-c-driver/test/helpers_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void test_index_helper( mongo *conn ) {
bson_append_int( &b, "foo", -1 );
bson_finish( &b );

mongo_create_index( conn, "test.bar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, &out );
mongo_create_index( conn, "test.bar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, -1, &out );

bson_destroy( &b );
bson_destroy( &out );
Expand Down Expand Up @@ -50,7 +50,7 @@ void test_index_helper_invalid( mongo *conn ) {
bson_append_int( &b, "foo", -1 );
bson_finish( &b );

ASSERT( MONGO_ERROR == mongo_create_index( conn, "testbar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, &out ));
ASSERT( MONGO_ERROR == mongo_create_index( conn, "testbar", &b, NULL, MONGO_INDEX_SPARSE | MONGO_INDEX_UNIQUE, -1, &out ));

bson_destroy( &b );
bson_destroy( &out );
Expand Down

0 comments on commit 1ee260b

Please sign in to comment.