Permalink
Browse files

Driver Sync; also mongo.distinct() added

  • Loading branch information...
gerald-lindsly committed Mar 5, 2012
1 parent 506d6d0 commit f8a6e3ee8428bb517a4efd3e695509c97ccffb12
Showing with 147 additions and 90 deletions.
  1. +4 −2 DllMain.c
  2. +27 −7 Mongo.m
  3. +7 −7 MongoMatlabDriver.h
  4. +0 −6 MongoMatlabDriver.sln
  5. +6 −5 MongoMatlabDriver.vcxproj
  6. +5 −6 MongoMatlabDriver.vcxproj.filters
  7. +3 −0 MongoTest.m
  8. +11 −5 api_bson.c
  9. +4 −4 api_gridfs.c
  10. +11 −48 api_mongo.c
  11. BIN mongo-c-driver-src.zip
  12. +69 −0 substr.m
View
@@ -19,12 +19,14 @@
#include "bson.h"
#include <mex.h>
-extern int sock_init();
+extern int mongo_sock_init();
+bson empty_bson;
int main(void)
{
- sock_init();
+ mongo_sock_init();
+ bson_empty(&empty_bson);
bson_printf = mexPrintf;
bson_errprintf = mexPrintf;
set_bson_err_handler(mexErrMsgTxt);
View
34 Mongo.m
@@ -41,7 +41,7 @@
end
m.h = libpointer('mongo_Ptr');
- calllib('MongoMatlabDriver', 'mongo_create', m.h)
+ calllib('MongoMatlabDriver', 'mmongo_create', m.h)
if strcmp('replset', host)
calllib('MongoMatlabDriver', 'mmongo_replset_init', m.h, replset_name);
else
@@ -87,7 +87,7 @@ function disconnect(m)
if isNull(m.h)
b = false
else
- b = (calllib('MongoMatlabDriver', 'mongo_is_connected', m.h) ~= 0);
+ b = (calllib('MongoMatlabDriver', 'mmongo_is_connected', m.h) ~= 0);
end
end
@@ -118,12 +118,12 @@ function setTimeout(m, timeout)
function host = getPrimary(m)
% host = mongo.getPrimary() Get the host to which we are connected
% as a host:port string.
- host = calllib('MongoMatlabDriver', 'mongo_get_primary', m.h);
+ host = calllib('MongoMatlabDriver', 'mmongo_get_primary', m.h);
end
function socket = getSocket(m)
% socket = getSocket(m) Get the TCP/IP socket handle.
- socket = calllib('MongoMatlabDriver', 'mongo_get_socket', m.h);
+ socket = calllib('MongoMatlabDriver', 'mmongo_get_socket', m.h);
end
function hosts = getHosts(m)
@@ -136,7 +136,7 @@ function setTimeout(m, timeout)
% err = mongo.getErr() Get the error code of the connection
% object as reported by the C driver. This may be checked if an
% operation failed.
- err = calllib('MongoMatlabDriver', 'mongo_get_err', m.h);
+ err = calllib('MongoMatlabDriver', 'mmongo_get_err', m.h);
end
function databases = getDatabases(m)
@@ -394,6 +394,26 @@ function list(m)
end
end
+ function keys = distinct(m, ns, key)
+ % keys = mongo.distinct(ns, key) Get the distinct keys of a collection.
+ % The collection namespace (ns) is in the form 'database.collection'.
+ % key is a string naming the field for which to return distinct values.
+ pos = strfind(ns, '.');
+ if isempty(pos)
+ error('Mongo:distinct', 'Expected a ''.'' in the namespace');
+ end
+ db = substr(ns, 1, pos(1)-1);
+ collection = substr(ns, pos(1)+1, length(ns)-pos(1));
+ buf = BsonBuffer;
+ buf.append('distinct', collection);
+ buf.append('key', key);
+ cmd = buf.finish;
+ keys = m.command(db, cmd);
+ if ~isempty(keys)
+ keys = keys.value('values');
+ end
+ end
+
function result = simpleCommand(m, db, cmdstr, arg)
% result = mongo.simpleCommand(db, cmdstr, arg) Issue a simple command
% to the server which canbe specified by just a command string and an
@@ -433,12 +453,12 @@ function resetErr(m, db)
function errNo = getServerErr(m)
% errNo = mongo.getServerErr() Get the server error code
- errNo = calllib('MongoMatlabDriver', 'mongo_get_server_err', m.h);
+ errNo = calllib('MongoMatlabDriver', 'mmongo_get_server_err', m.h);
end
function errStr = getServerErrString(m)
% errStr = mongo.getServerErrString() Get a string decribing the error
- errStr = calllib('MongoMatlabDriver', 'mongo_get_server_err_string', m.h);
+ errStr = calllib('MongoMatlabDriver', 'mmongo_get_server_err_string', m.h);
end
function ok = dropDatabase(m, db)
View
@@ -120,9 +120,9 @@ EXPORT void mongo_bson_iterator_code_scope(struct bson_iterator_* i, struct bson
EXPORT int mongo_bson_iterator_timestamp(struct bson_iterator_* i, int* increment);
EXPORT mxArray* mongo_bson_array_value(struct bson_iterator_* i);
-EXPORT void mongo_create(struct mongo_** conn);
+EXPORT void mmongo_create(struct mongo_** conn);
EXPORT void mmongo_connect(struct mongo_* conn, char* host);
-EXPORT int mongo_is_connected(struct mongo_* conn);
+EXPORT int mmongo_is_connected(struct mongo_* conn);
EXPORT int mongo_is_master(struct mongo_* conn);
EXPORT void mmongo_destroy(struct mongo_* conn);
EXPORT void mmongo_replset_init(struct mongo_* conn, char* name);
@@ -133,10 +133,10 @@ EXPORT int mmongo_reconnect(struct mongo_* conn);
EXPORT int mmongo_check_connection(struct mongo_* conn);
EXPORT void mongo_set_timeout(struct mongo_* conn, int timeout);
EXPORT int mongo_get_timeout(struct mongo_* conn);
-EXPORT const char* mongo_get_primary(struct mongo_* conn);
-EXPORT int mongo_get_socket(struct mongo_* conn);
+EXPORT const char* mmongo_get_primary(struct mongo_* conn);
+EXPORT int mmongo_get_socket(struct mongo_* conn);
EXPORT mxArray* mongo_get_hosts(struct mongo_* conn);
-EXPORT int mongo_get_err(struct mongo_* conn);
+EXPORT int mmongo_get_err(struct mongo_* conn);
EXPORT mxArray* mongo_get_databases(struct mongo_* conn);
EXPORT mxArray* mongo_get_database_collections(struct mongo_* conn, char* db);
EXPORT int mongo_rename(struct mongo_* conn, char* from_ns, char* to_ns);
@@ -155,8 +155,8 @@ EXPORT int mongo_authenticate(struct mongo_* conn, char* db, char* user, char*
EXPORT int mongo_command(struct mongo_* conn, char* db, struct bson_* cmd, struct bson_** result);
EXPORT int mongo_get_last_err(struct mongo_* conn, char* db, struct bson_** err);
EXPORT int mongo_get_prev_err(struct mongo_* conn, char* db, struct bson_** err);
-EXPORT int mongo_get_server_err(struct mongo_* conn);
-EXPORT char* mongo_get_server_err_string(struct mongo_* conn);
+EXPORT int mmongo_get_server_err(struct mongo_* conn);
+EXPORT char* mmongo_get_server_err_string(struct mongo_* conn);
EXPORT int mongo_drop_database(struct mongo_* conn, char* db);
EXPORT int mongo_drop(struct mongo_* conn, char* ns);
View
@@ -9,8 +9,6 @@ Global
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
- Release64|Win32 = Release64|Win32
- Release64|x64 = Release64|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0F402EBC-FD55-4FB7-92B1-D1804FE4496E}.Debug|Win32.ActiveCfg = Debug|Win32
@@ -21,10 +19,6 @@ Global
{0F402EBC-FD55-4FB7-92B1-D1804FE4496E}.Release|Win32.Build.0 = Release|Win32
{0F402EBC-FD55-4FB7-92B1-D1804FE4496E}.Release|x64.ActiveCfg = Release|x64
{0F402EBC-FD55-4FB7-92B1-D1804FE4496E}.Release|x64.Build.0 = Release|x64
- {0F402EBC-FD55-4FB7-92B1-D1804FE4496E}.Release64|Win32.ActiveCfg = Release64|x64
- {0F402EBC-FD55-4FB7-92B1-D1804FE4496E}.Release64|Win32.Build.0 = Release64|x64
- {0F402EBC-FD55-4FB7-92B1-D1804FE4496E}.Release64|x64.ActiveCfg = Release64|x64
- {0F402EBC-FD55-4FB7-92B1-D1804FE4496E}.Release64|x64.Build.0 = Release64|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
@@ -107,7 +107,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir)mongo-c-driver-src;C:\Program Files (x86)\MATLAB\R2011b\extern\include;$(IncludePath)</IncludePath>
- <LibraryPath>C:\Program Files (x86)\MATLAB\R2011b\extern\lib\win32\microsoft;$(LibraryPath)</LibraryPath>
+ <LibraryPath>C:\Program Files (x86)\MATLAB\R2011b\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release64|Win32'">
<LinkIncremental>false</LinkIncremental>
@@ -124,7 +124,7 @@
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
- <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;MONGO_USE__INT64;WIN32;_DEBUG;_WINDOWS;_USRDLL;MONGOMATLABDRIVER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;MONGO_USE__INT64;WIN32;_DEBUG;_WINDOWS;_USRDLL;MONGO_STATIC_BUILD;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@@ -154,7 +154,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
- <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;MONGO_USE__INT64;WIN32;NDEBUG;_WINDOWS;_USRDLL;MONGOMATLABDRIVER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;MONGO_USE__INT64;WIN32;NDEBUG;MONGO_STATIC_BUILD;_WINDOWS;_USRDLL;MONGO_STATIC_BUILD;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@@ -172,7 +172,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
- <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_WINDOWS;_USRDLL;MONGOMATLABDRIVER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;MONGO_USE__INT64;WIN32;NDEBUG;_WINDOWS;_USRDLL;MONGO_STATIC_BUILD;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@@ -190,7 +190,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
- <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;MONGOMATLABDRIVER_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;MONGO_STATIC_BUILD;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@@ -262,6 +262,7 @@
<None Include="MongoStop.m" />
<None Include="MongoTest.m" />
<None Include="README.md" />
+ <None Include="substr.m" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
@@ -123,16 +123,15 @@
<None Include="Gridfile.m">
<Filter>Source Files</Filter>
</None>
- <None Include="README.md">
- <Filter>Source Files</Filter>
- </None>
- <None Include=".gitignore">
- <Filter>Source Files</Filter>
- </None>
<None Include="COPYRIGHT.txt" />
<None Include="LICENSE.txt" />
<None Include="MongoSample.m">
<Filter>Source Files</Filter>
</None>
+ <None Include=".gitignore" />
+ <None Include="README.md" />
+ <None Include="substr.m">
+ <Filter>Source Files</Filter>
+ </None>
</ItemGroup>
</Project>
View
@@ -257,6 +257,9 @@
clear cursor
end
+ % get the distinct people names
+ distinct = mongo.distinct(people, 'name')
+
% count the number of documents in 'test.people'
num = mongo.count(people)
View
@@ -597,10 +597,15 @@ EXPORT mxArray* mongo_bson_array_value(struct bson_iterator_* i) {
case BSON_INT: ;
case BSON_LONG: ;
case BSON_DOUBLE: ;
- /* case BSON_STRING: ; */
case BSON_BOOL: ;
case BSON_DATE:
break;
+ case BSON_STRING:
+ if (ndims > 1) {
+ mexPrintf("Unable to convert array - Only 1 dimenisonal arrays of strings supported");
+ return 0;
+ }
+ break;
case BSON_OBJECT:
if (_iterator_getComplex(&sub[ndims], &z))
break;
@@ -653,7 +658,7 @@ EXPORT mxArray* mongo_bson_array_value(struct bson_iterator_* i) {
case BSON_INT: ;
case BSON_LONG: ;
case BSON_DOUBLE: ;
-/* case BSON_STRING: ; */
+ case BSON_STRING: ;
case BSON_BOOL: ;
case BSON_DATE: ;
GotEl: {
@@ -711,7 +716,7 @@ GotEl: {
case BSON_LONG: ret = mxCreateNumericArray(ndims, mdim, mxINT64_CLASS, mxREAL); break;
case BSON_DATE:
case BSON_DOUBLE: ret = mxCreateNumericArray(ndims, mdim, mxDOUBLE_CLASS, mxREAL); break;
-/* case BSON_STRING: */
+ case BSON_STRING: ret = mxCreateCellMatrix(len, 1); break;
case BSON_BOOL: ret = mxCreateLogicalArray(ndims, mdim); break;
case BSON_OBJECT: ret = mxCreateNumericArray(ndims, mdim, mxDOUBLE_CLASS, mxCOMPLEX); break;
default:
@@ -752,8 +757,6 @@ GotEl: {
case BSON_LONG:
((int64_t*)mxGetData(ret))[ofs] = bson_iterator_long(&sub[depth]);
break;
- case BSON_STRING:
- break;
case BSON_BOOL: ;
((mxLogical*)mxGetData(ret))[ofs] = bson_iterator_bool(&sub[depth]);
break;
@@ -762,6 +765,9 @@ GotEl: {
mxGetPr(ret)[ofs] = z.r;
mxGetPi(ret)[ofs] = z.i;
break;
+ case BSON_STRING:
+ mxSetCell(ret, count[depth]-1, mxCreateString(bson_iterator_string(&sub[depth])));
+ break;
default: ;
/* never reaches here */
}
View
@@ -181,8 +181,8 @@ EXPORT void mongo_gridfile_get_descriptor(struct gridfile_* gf, struct bson_** o
int mongo_gridfile_get_metadata(struct gridfile_* gf, struct bson_** out) {
- bson meta = gridfile_get_metadata((gridfile*)gf);
- bson* b;
+ bson meta, *b;
+ gridfile_get_metadata((gridfile*)gf, &meta);
if (bson_size(&meta) <= 5)
return 0;
b = (bson*)malloc(sizeof(bson));
@@ -193,8 +193,8 @@ int mongo_gridfile_get_metadata(struct gridfile_* gf, struct bson_** out) {
int mongo_gridfile_get_chunk(struct gridfile_* gf, int i, struct bson_** out) {
- bson chunk = gridfile_get_chunk((gridfile*)gf, i);
- bson* b;
+ bson chunk, *b;
+ gridfile_get_chunk((gridfile*)gf, i, &chunk);
if (bson_size(&chunk) <= 5)
return 0;
b = (bson*)malloc(sizeof(bson));
Oops, something went wrong.

0 comments on commit f8a6e3e

Please sign in to comment.