diff options
author | Antonin Kral <a.kral@bobek.cz> | 2011-09-14 17:08:06 +0200 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2011-09-14 17:08:06 +0200 |
commit | 5d342a758c6095b4d30aba0750b54f13b8916f51 (patch) | |
tree | 762e9aa84781f5e3b96db2c02d356c29cf0217c0 /client/examples | |
parent | cbe2d992e9cd1ea66af9fa91df006106775d3073 (diff) | |
download | mongodb-5d342a758c6095b4d30aba0750b54f13b8916f51.tar.gz |
Imported Upstream version 2.0.0
Diffstat (limited to 'client/examples')
-rw-r--r-- | client/examples/clientTest.cpp | 29 | ||||
-rw-r--r-- | client/examples/httpClientTest.cpp | 33 | ||||
-rw-r--r-- | client/examples/insert_demo.cpp | 47 | ||||
-rw-r--r-- | client/examples/rs.cpp | 80 | ||||
-rwxr-xr-x | client/examples/simple_client_demo.vcxproj | 92 | ||||
-rwxr-xr-x | client/examples/simple_client_demo.vcxproj.filters | 21 | ||||
-rw-r--r-- | client/examples/whereExample.cpp | 3 |
7 files changed, 285 insertions, 20 deletions
diff --git a/client/examples/clientTest.cpp b/client/examples/clientTest.cpp index 96c014e..aaea6bd 100644 --- a/client/examples/clientTest.cpp +++ b/client/examples/clientTest.cpp @@ -246,5 +246,34 @@ int main( int argc, const char **argv ) { //MONGO_PRINT(out); } + { + // test timeouts + + DBClientConnection conn( true , 0 , 2 ); + if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) { + cout << "couldn't connect : " << errmsg << endl; + throw -11; + } + conn.insert( "test.totest" , BSON( "x" << 1 ) ); + BSONObj res; + + bool gotError = false; + assert( conn.eval( "test" , "return db.totest.findOne().x" , res ) ); + try { + conn.eval( "test" , "sleep(5000); return db.totest.findOne().x" , res ); + } + catch ( std::exception& e ) { + gotError = true; + log() << e.what() << endl; + } + assert( gotError ); + // sleep so the server isn't locked anymore + sleepsecs( 4 ); + + assert( conn.eval( "test" , "return db.totest.findOne().x" , res ) ); + + + } + cout << "client test finished!" << endl; } diff --git a/client/examples/httpClientTest.cpp b/client/examples/httpClientTest.cpp index 4fa5fd8..4055d44 100644 --- a/client/examples/httpClientTest.cpp +++ b/client/examples/httpClientTest.cpp @@ -18,10 +18,27 @@ #include <iostream> #include "client/dbclient.h" -#include "util/httpclient.h" +#include "util/net/httpclient.h" using namespace mongo; +void play( string url ) { + cout << "[" << url << "]" << endl; + + HttpClient c; + HttpClient::Result r; + MONGO_assert( c.get( url , &r ) == 200 ); + + HttpClient::Headers h = r.getHeaders(); + MONGO_assert( h["Content-Type"].find( "text/html" ) == 0 ); + + cout << "\tHeaders" << endl; + for ( HttpClient::Headers::iterator i = h.begin() ; i != h.end(); ++i ) { + cout << "\t\t" << i->first << "\t" << i->second << endl; + } + +} + int main( int argc, const char **argv ) { int port = 27017; @@ -32,12 +49,10 @@ int main( int argc, const char **argv ) { } port += 1000; - stringstream ss; - ss << "http://localhost:" << port << "/"; - string url = ss.str(); - - cout << "[" << url << "]" << endl; - - HttpClient c; - MONGO_assert( c.get( url ) == 200 ); + play( str::stream() << "http://localhost:" << port << "/" ); + +#ifdef MONGO_SSL + play( "https://www.10gen.com/" ); +#endif + } diff --git a/client/examples/insert_demo.cpp b/client/examples/insert_demo.cpp new file mode 100644 index 0000000..14ac79e --- /dev/null +++ b/client/examples/insert_demo.cpp @@ -0,0 +1,47 @@ +/* + C++ client program which inserts documents in a MongoDB database. + + How to build and run: + + Using mongo_client_lib.cpp: + g++ -I .. -I ../.. insert_demo.cpp ../mongo_client_lib.cpp -lboost_thread-mt -lboost_filesystem + ./a.out +*/ + +#include <iostream> +#include "dbclient.h" // the mongo c++ driver + +using namespace std; +using namespace mongo; +using namespace bson; + +int main() { + try { + cout << "connecting to localhost..." << endl; + DBClientConnection c; + c.connect("localhost"); + cout << "connected ok" << endl; + + bo o = BSON( "hello" << "world" ); + + cout << "inserting..." << endl; + + time_t start = time(0); + for( unsigned i = 0; i < 1000000; i++ ) { + c.insert("test.foo", o); + } + + // wait until all operations applied + cout << "getlasterror returns: \"" << c.getLastError() << '"' << endl; + + time_t done = time(0); + time_t dt = done-start; + cout << dt << " seconds " << 1000000/dt << " per second" << endl; + } + catch(DBException& e) { + cout << "caught DBException " << e.toString() << endl; + return 1; + } + + return 0; +} diff --git a/client/examples/rs.cpp b/client/examples/rs.cpp index 7813ec6..3307d87 100644 --- a/client/examples/rs.cpp +++ b/client/examples/rs.cpp @@ -21,11 +21,62 @@ #include "client/dbclient.h" #include <iostream> +#include <vector> using namespace mongo; using namespace std; +void workerThread( string collName , bool print , DBClientReplicaSet * conn ) { + + while ( true ) { + try { + conn->update( collName , BSONObj() , BSON( "$inc" << BSON( "x" << 1 ) ) , true ); + + BSONObj x = conn->findOne( collName , BSONObj() ); + + if ( print ) { + cout << x << endl; + } + + BSONObj a = conn->slaveConn().findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ); + BSONObj b = conn->findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ); + + if ( print ) { + cout << "\t A " << a << endl; + cout << "\t B " << b << endl; + } + } + catch ( std::exception& e ) { + cout << "ERROR: " << e.what() << endl; + } + sleepmillis( 10 ); + } +} + int main( int argc , const char ** argv ) { + + unsigned nThreads = 1; + bool print = false; + bool testTimeout = false; + + for ( int i=1; i<argc; i++ ) { + if ( mongoutils::str::equals( "--threads" , argv[i] ) ) { + nThreads = atoi( argv[++i] ); + } + else if ( mongoutils::str::equals( "--print" , argv[i] ) ) { + print = true; + } + // Run a special mode to demonstrate the DBClientReplicaSet so_timeout option. + else if ( mongoutils::str::equals( "--testTimeout" , argv[i] ) ) { + testTimeout = true; + } + else { + cerr << "unknown option: " << argv[i] << endl; + return 1; + } + + } + string errmsg; ConnectionString cs = ConnectionString::parse( "foo/127.0.0.1" , errmsg ); if ( ! cs.isValid() ) { @@ -33,7 +84,7 @@ int main( int argc , const char ** argv ) { return 1; } - DBClientReplicaSet * conn = (DBClientReplicaSet*)cs.connect( errmsg ); + DBClientReplicaSet * conn = dynamic_cast<DBClientReplicaSet*>(cs.connect( errmsg, testTimeout ? 10 : 0 )); if ( ! conn ) { cout << "error connecting: " << errmsg << endl; return 2; @@ -42,17 +93,26 @@ int main( int argc , const char ** argv ) { string collName = "test.rs1"; conn->dropCollection( collName ); - while ( true ) { + + if ( testTimeout ) { + conn->insert( collName, BSONObj() ); try { - conn->update( collName , BSONObj() , BSON( "$inc" << BSON( "x" << 1 ) ) , true ); - cout << conn->findOne( collName , BSONObj() ) << endl; - cout << "\t A" << conn->slaveConn().findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ) << endl; - cout << "\t B " << conn->findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ) << endl; - } - catch ( std::exception& e ) { - cout << "ERROR: " << e.what() << endl; + conn->count( collName, BSON( "$where" << "sleep(40000)" ) ); + } catch( DBException& ) { + return 0; } - sleepsecs( 1 ); + cout << "expected socket exception" << endl; + return 1; + } + + vector<boost::shared_ptr<boost::thread> > threads; + for ( unsigned i=0; i<nThreads; i++ ) { + string errmsg; + threads.push_back( boost::shared_ptr<boost::thread>( new boost::thread( boost::bind( workerThread , collName , print , (DBClientReplicaSet*)cs.connect(errmsg) ) ) ) ); + } + + for ( unsigned i=0; i<threads.size(); i++ ) { + threads[i]->join(); } } diff --git a/client/examples/simple_client_demo.vcxproj b/client/examples/simple_client_demo.vcxproj new file mode 100755 index 0000000..4658a42 --- /dev/null +++ b/client/examples/simple_client_demo.vcxproj @@ -0,0 +1,92 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>{89C30BC3-2874-4F2C-B4DA-EB04E9782236}</ProjectGuid>
+ <Keyword>Win32Proj</Keyword>
+ <RootNamespace>simple_client_demo</RootNamespace>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>Application</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <LinkIncremental>true</LinkIncremental>
+ <IncludePath>..\..;..\..\pcre-7.4;$(IncludePath)</IncludePath>
+ <LibraryPath>\boost\lib\vs2010_32;$(LibraryPath)</LibraryPath>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <LinkIncremental>false</LinkIncremental>
+ <IncludePath>..\..;..\..\pcre-7.4;$(IncludePath)</IncludePath>
+ <LibraryPath>\boost\lib\vs2010_32;$(LibraryPath)</LibraryPath>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <PrecompiledHeader>
+ </PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <PreprocessorDefinitions> _CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <AdditionalIncludeDirectories>c:\boost;\boost</AdditionalIncludeDirectories>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <PrecompiledHeader>
+ </PrecompiledHeader>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <PreprocessorDefinitions> _CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+ <AdditionalIncludeDirectories>c:\boost;\boost</AdditionalIncludeDirectories>
+ </ClCompile>
+ <Link>
+ <SubSystem>Console</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ <AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemGroup>
+ <ClCompile Include="..\mongo_client_lib.cpp" />
+ <ClCompile Include="..\simple_client_demo.cpp" />
+ </ItemGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project>
\ No newline at end of file diff --git a/client/examples/simple_client_demo.vcxproj.filters b/client/examples/simple_client_demo.vcxproj.filters new file mode 100755 index 0000000..d6580c3 --- /dev/null +++ b/client/examples/simple_client_demo.vcxproj.filters @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <Filter Include="Source Files">
+ <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+ <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+ </Filter>
+ <Filter Include="Header Files">
+ <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+ <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
+ </Filter>
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="..\simple_client_demo.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ <ClCompile Include="..\mongo_client_lib.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ </ItemGroup>
+</Project>
\ No newline at end of file diff --git a/client/examples/whereExample.cpp b/client/examples/whereExample.cpp index ce4174b..12b68d7 100644 --- a/client/examples/whereExample.cpp +++ b/client/examples/whereExample.cpp @@ -1,4 +1,5 @@ -// whereExample.cpp +// @file whereExample.cpp +// @see http://www.mongodb.org/display/DOCS/Server-side+Code+Execution /* Copyright 2009 10gen Inc. * |