Index: TODO =================================================================== diff -u -r5c04bee69bb1658d1ef900891a34cda0cad5e34d -ra6a6d9c14bfefba367b3147e05fbab4137f044ce --- TODO (.../TODO) (revision 5c04bee69bb1658d1ef900891a34cda0cad5e34d) +++ TODO (.../TODO) (revision a6a6d9c14bfefba367b3147e05fbab4137f044ce) @@ -4929,6 +4929,8 @@ when using e.g. "bson_iter_t i[1]" - some c-code cleanup - tested with mongodb-c-driver 0.92.3 +- added mongo::collection::stats +- extended regression test ======================================================================== TODO: Index: library/mongodb/mongoAPI.decls =================================================================== diff -u -r7543d1df847248f723f02fa1abc6645713b9d10f -ra6a6d9c14bfefba367b3147e05fbab4137f044ce --- library/mongodb/mongoAPI.decls (.../mongoAPI.decls) (revision 7543d1df847248f723f02fa1abc6645713b9d10f) +++ library/mongodb/mongoAPI.decls (.../mongoAPI.decls) (revision a6a6d9c14bfefba367b3147e05fbab4137f044ce) @@ -70,7 +70,10 @@ {-argName "-limit" -required 0 -type int32} {-argName "-skip" -required 0 -type int32} } - +cmd "collection::stats" NsfMongoCollectionStats { + {-argName "collection" -required 1 -type mongoc_collection_t} + {-argName "-options" -required 0 -type tclobj} +} cmd "collection::update" NsfMongoCollectionUpdate { {-argName "collection" -required 1 -type mongoc_collection_t} {-argName "cond" -required 1 -type tclobj} Index: library/mongodb/mongoAPI.h =================================================================== diff -u -r7543d1df847248f723f02fa1abc6645713b9d10f -ra6a6d9c14bfefba367b3147e05fbab4137f044ce --- library/mongodb/mongoAPI.h (.../mongoAPI.h) (revision 7543d1df847248f723f02fa1abc6645713b9d10f) +++ library/mongodb/mongoAPI.h (.../mongoAPI.h) (revision a6a6d9c14bfefba367b3147e05fbab4137f044ce) @@ -85,7 +85,7 @@ /* just to define the symbol */ -static Nsf_methodDefinition method_definitions[26]; +static Nsf_methodDefinition method_definitions[27]; static CONST char *method_command_namespace_names[] = { "::mongo" @@ -98,6 +98,7 @@ static int NsfMongoCollectionIndexStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv []); static int NsfMongoCollectionInsertStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv []); static int NsfMongoCollectionQueryStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv []); +static int NsfMongoCollectionStatsStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv []); static int NsfMongoCollectionUpdateStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv []); static int NsfMongoConnectStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv []); static int NsfMongoCursorCloseStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv []); @@ -124,6 +125,7 @@ static int NsfMongoCollectionIndex(Tcl_Interp *interp, mongoc_collection_t *collectionPtr, Tcl_Obj *attributes, CONST char *withName, int withBackground, int withDropdups, int withSparse, int withTtl, int withUnique); static int NsfMongoCollectionInsert(Tcl_Interp *interp, mongoc_collection_t *collectionPtr, Tcl_Obj *values); static int NsfMongoCollectionQuery(Tcl_Interp *interp, mongoc_collection_t *collectionPtr, Tcl_Obj *query, Tcl_Obj *withAtts, int withLimit, int withSkip); +static int NsfMongoCollectionStats(Tcl_Interp *interp, mongoc_collection_t *collectionPtr, Tcl_Obj *withOptions); static int NsfMongoCollectionUpdate(Tcl_Interp *interp, mongoc_collection_t *collectionPtr, Tcl_Obj *cond, Tcl_Obj *values, int withUpsert, int withAll); static int NsfMongoConnect(Tcl_Interp *interp, CONST char *withUri); static int NsfMongoCursorClose(Tcl_Interp *interp, mongoc_cursor_t *cursorPtr, Tcl_Obj *cursorObj); @@ -151,6 +153,7 @@ NsfMongoCollectionIndexIdx, NsfMongoCollectionInsertIdx, NsfMongoCollectionQueryIdx, + NsfMongoCollectionStatsIdx, NsfMongoCollectionUpdateIdx, NsfMongoConnectIdx, NsfMongoCursorCloseIdx, @@ -340,6 +343,26 @@ } static int +NsfMongoCollectionStatsStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { + ParseContext pc; + (void)clientData; + + if (likely(ArgumentParse(interp, objc, objv, NULL, objv[0], + method_definitions[NsfMongoCollectionStatsIdx].paramDefs, + method_definitions[NsfMongoCollectionStatsIdx].nrParameters, 0, NSF_ARGPARSE_BUILTIN, + &pc) == TCL_OK)) { + mongoc_collection_t *collectionPtr = (mongoc_collection_t *)pc.clientData[0]; + Tcl_Obj *withOptions = (Tcl_Obj *)pc.clientData[1]; + + assert(pc.status == 0); + return NsfMongoCollectionStats(interp, collectionPtr, withOptions); + + } else { + return TCL_ERROR; + } +} + +static int NsfMongoCollectionUpdateStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { ParseContext pc; (void)clientData; @@ -686,7 +709,7 @@ } } -static Nsf_methodDefinition method_definitions[26] = { +static Nsf_methodDefinition method_definitions[27] = { {"::mongo::collection::close", NsfCollectionCloseStub, 1, { {"collection", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Pointer, NULL,NULL,"mongoc_collection_t",NULL,NULL,NULL,NULL,NULL}} }, @@ -727,6 +750,10 @@ {"-limit", 0, 1, Nsf_ConvertTo_Int32, NULL,NULL,"int32",NULL,NULL,NULL,NULL,NULL}, {"-skip", 0, 1, Nsf_ConvertTo_Int32, NULL,NULL,"int32",NULL,NULL,NULL,NULL,NULL}} }, +{"::mongo::collection::stats", NsfMongoCollectionStatsStub, 2, { + {"collection", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Pointer, NULL,NULL,"mongoc_collection_t",NULL,NULL,NULL,NULL,NULL}, + {"-options", 0, 1, Nsf_ConvertTo_Tclobj, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}} +}, {"::mongo::collection::update", NsfMongoCollectionUpdateStub, 5, { {"collection", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Pointer, NULL,NULL,"mongoc_collection_t",NULL,NULL,NULL,NULL,NULL}, {"cond", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Tclobj, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}, Index: library/mongodb/nsfmongo.c =================================================================== diff -u -r5c04bee69bb1658d1ef900891a34cda0cad5e34d -ra6a6d9c14bfefba367b3147e05fbab4137f044ce --- library/mongodb/nsfmongo.c (.../nsfmongo.c) (revision 5c04bee69bb1658d1ef900891a34cda0cad5e34d) +++ library/mongodb/nsfmongo.c (.../nsfmongo.c) (revision a6a6d9c14bfefba367b3147e05fbab4137f044ce) @@ -917,7 +917,46 @@ return TCL_OK; } +/* +cmd "collection::stats" NsfMongoCollectionStats { + {-argName "collection" -required 1 -type mongoc_collection_t} + {-argName "-options" -required 0 -type tclobj} +} +*/ +static int +NsfMongoCollectionStats(Tcl_Interp *interp, + mongoc_collection_t *collectionPtr, + Tcl_Obj *optionsObj) { + int objc = 0, result, success; + Tcl_Obj **objv = NULL; + bson_t options, *optionsPtr = NULL; + bson_t stats, *statsPtr = &stats; + bson_error_t bsonError; + if (optionsObj) { + result = Tcl_ListObjGetElements(interp, optionsObj, &objc, &objv); + if (result != TCL_OK || (objc % 3 != 0)) { + return NsfPrintError(interp, "%s: must contain a multiple of 3 elements", ObjStr(optionsObj)); + } + optionsPtr = &options; + BsonAppendObjv(interp, optionsPtr, objc, objv); + } + + success = mongoc_collection_stats(collectionPtr, optionsPtr, statsPtr, &bsonError); + + if (optionsPtr) { + bson_destroy (optionsPtr); + } + + if (success) { + Tcl_SetObjResult(interp, BsonToList(interp, statsPtr, 0)); + bson_destroy (statsPtr); + return TCL_OK; + } else { + return NsfPrintError(interp, "mongo::collection::stats: error: %s", bsonError.message); + } +} + /* cmd "collection::update" NsfMongoCollectionUpdate { {-argName "collection" -required 1 -type mongoc_collection_t} Index: library/mongodb/tests/nsf-mongo.test =================================================================== diff -u -rcef3de5c4f65e767d0c66389bacc77bc3c2e5a68 -ra6a6d9c14bfefba367b3147e05fbab4137f044ce --- library/mongodb/tests/nsf-mongo.test (.../nsf-mongo.test) (revision cef3de5c4f65e767d0c66389bacc77bc3c2e5a68) +++ library/mongodb/tests/nsf-mongo.test (.../nsf-mongo.test) (revision a6a6d9c14bfefba367b3147e05fbab4137f044ce) @@ -162,9 +162,19 @@ ? { llength [::mongo::collection::query $mongoColl \ [list \$query document {projects document {$in array {0 string nsf 1 string gtat}}}]]} 3 -puts stderr "\nClose collecton $mongoColl" -::mongo::collection::close $mongoColl +puts stderr "\nStatistics of $mongoColl" +puts stderr [::mongo::collection::stats $mongoColl] +set stats [::mongo::collection::stats $mongoColl] +? {expr [llength $stats] % 3 == 0} 1 +? {expr [llength $stats] > 0} 1 + +if {[llength $stats] % 3 == 0} { + package req nx::mongo + nx::mongo::Class create C + puts [C bson pp $stats] +} + puts stderr "\nClose connection $mongoConn" ::mongo::close $mongoConn