Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
Samples/DocumentDB/Sources/Main.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Frameworks/StroikaPreComp.h"
5
6#include <cstdlib>
7#include <iostream>
8
9#include "Stroika/Foundation/Database/Document/MongoDBClient.h"
13#include "Stroika/Foundation/Execution/CommandLine.h"
14#include "Stroika/Foundation/Execution/Module.h"
17
18#include "ComputerNetwork.h"
19#include "EmployeesDB.h"
20
21using namespace std;
22
23using namespace Stroika::Foundation;
25using namespace Stroika::Foundation::Database;
26using namespace Stroika::Foundation::Execution;
27
28int main ([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[])
29{
30 using namespace Database::Document;
31
32 using namespace Stroika::Samples::Document;
33
34#if qStroika_HasComponent_mongocxxdriver
35 optional<String> mongoConnectionString;
36#endif
37
38 // handle command-line arguments
39 {
40 using namespace StandardCommandLineOptions;
41 const CommandLine::Option kMongoConnectionStringOption_{
42 .fLongName = "mongoConnectionString"sv,
43 .fSupportsArgument = true,
44 .fHelpOptionText = "Connect to this mongo database for testing, eg: mongodb://admin:pass@localhost:27017; OR ENV VAR MONGO_CONNECTION_STRING"sv};
45
46 const initializer_list<CommandLine::Option> kAllOptions_{kHelp,
47#if qStroika_HasComponent_mongocxxdriver
48 kMongoConnectionStringOption_
49#endif
50 };
51
52 CommandLine cmdLine{argc, argv};
53 if (cmdLine.Has (kHelp)) {
54 cerr << CommandLine::GenerateUsage ("documentDB", kAllOptions_);
55 return EXIT_SUCCESS;
56 }
57 if (auto error = cmdLine.ValidateQuietly (kAllOptions_)) {
58 cerr << "{}"_f(*error) << endl;
59 cerr << CommandLine::GenerateUsage ("myApp", kAllOptions_) << endl;
60 }
61#if qStroika_HasComponent_mongocxxdriver
62 mongoConnectionString = Memory::Or_Else (cmdLine.GetArgument (kMongoConnectionStringOption_),
63 [] () { return kEnvironment->Lookup ("MONGO_CONNECTION_STRING"sv); });
64#endif
65 }
66
67#if qStroika_HasComponent_mongocxxdriver
68 Database::Document::MongoDBClient::Activator activator{}; // must exist while using mongocxxclient library
69#endif
70
71#if qStroika_HasComponent_mongocxxdriver
72 if (not mongoConnectionString) {
73 cerr << "Warning: skipping mongodb test because no connection string specified on command-line nor environment variable" << endl;
74 }
75 if (mongoConnectionString) {
76 using namespace Stroika::Foundation::Database::Document::MongoDBClient;
77 const String kTestDBName_ = "DocumentDB-Sample-Networks"sv;
78
79 try {
80 static const Activity kMongoCXXActivity_{"performing MongoDBConnection test on {}"_f(kTestDBName_)};
81 DeclareActivity da{&kMongoCXXActivity_};
82 auto adminDB = AdminConnection::New (AdminConnection::Options{.fConnectionTarget = *mongoConnectionString});
83 IgnoreExceptionsForCall (adminDB.DropDatabase (kTestDBName_));
84 adminDB.CreateDatabase (kTestDBName_);
85 Database::Document::Connection::Ptr p = MongoDBClient::Connection::New (
86 MongoDBClient::Connection::Options{.fConnectionTarget = *mongoConnectionString, .fDatabase = kTestDBName_});
87 cerr << "Starting mongodb networks sample:" << endl;
88 ComputerNetworksModel ([=] () {
89 cerr << "\tConnecting to {} database {}"_f(*mongoConnectionString, kTestDBName_) << endl;
90 return MongoDBClient::Connection::New (
91 MongoDBClient::Connection::Options{.fConnectionTarget = *mongoConnectionString, .fDatabase = kTestDBName_});
92 });
93 cerr << "done." << endl;
94 }
95 catch (...) {
96 cerr << "\t{}"_f(current_exception ()) << endl;
97 }
98 }
99 if (mongoConnectionString) {
100 using namespace Stroika::Foundation::Database::Document::MongoDBClient;
101 const String kTestDBName_ = "DocumentDB-Sample-Employees"sv;
102
103 try {
104 static const Activity kMongoCXXActivity_{"performing MongoDBConnection test on {}"_f(kTestDBName_)};
105 DeclareActivity da{&kMongoCXXActivity_};
106 auto adminDB = AdminConnection::New (AdminConnection::Options{.fConnectionTarget = *mongoConnectionString});
107 IgnoreExceptionsForCall (adminDB.DropDatabase (kTestDBName_));
108 adminDB.CreateDatabase (kTestDBName_);
109 Database::Document::Connection::Ptr p = MongoDBClient::Connection::New (
110 MongoDBClient::Connection::Options{.fConnectionTarget = *mongoConnectionString, .fDatabase = kTestDBName_});
111 cerr << "Starting mongodb employees sample:" << endl;
112 EmployeesDB ([=] () {
113 cerr << "\tConnecting to {} database {}"_f(*mongoConnectionString, kTestDBName_) << endl;
114 return MongoDBClient::Connection::New (
115 MongoDBClient::Connection::Options{.fConnectionTarget = *mongoConnectionString, .fDatabase = kTestDBName_});
116 });
117 cerr << "done." << endl;
118 }
119 catch (...) {
120 cerr << "\t{}"_f(current_exception ()) << endl;
121 }
122 }
123#endif
124
125#if qStroika_HasComponent_sqlite
126 // quick tests with in-memory DB
127 {
128 const String kTestDBName_ = "DocumentDB-Sample-Networks"sv;
129 cerr << "Starting sqlite document db networks sample:" << endl;
130 ComputerNetworksModel ([=] () {
131 cerr << "\tConnecting to sqlite memory db: {}"_f(kTestDBName_) << endl;
132 return SQLite::Connection::New (SQLite::Connection::Options{.fInMemoryDB = kTestDBName_});
133 });
134 cerr << "done." << endl;
135 }
136 {
137 const String kTestDBName_ = "DocumentDB-Sample-Employees"sv;
138 cerr << "Starting sqlite document db employees sample:" << endl;
139 EmployeesDB ([=] () {
140 cerr << "\tConnecting to sqlite memory db: {}"_f(kTestDBName_) << endl;
141 // works poorly with 100ms busyTimeout, but better than any other value - what am I missing!
142 return SQLite::Connection::New (SQLite::Connection::Options{.fInMemoryDB = kTestDBName_, .fBusyTimeout = 100ms});
143 });
144 cerr << "done." << endl;
145 }
146 // or run same test on filesystem
147 {
148 auto dbPath = IO::FileSystem::WellKnownLocations::GetTemporary () / "networks-test.db";
149 remove (dbPath); // test assumes empty db
150 cerr << "Starting sqlite document db networks sample:" << endl;
151 ComputerNetworksModel ([=] () {
152 cerr << "\tConnecting to sqlite db: {}"_f(dbPath) << endl;
153 return SQLite::Connection::New (SQLite::Connection::Options{.fDBPath = dbPath});
154 });
155 cerr << "done." << endl;
156 }
157 {
158 auto dbPath = IO::FileSystem::WellKnownLocations::GetTemporary () / "employees-test.db";
159 remove (dbPath); // test assumes empty db
160 cerr << "Starting sqlite document db employees sample:" << endl;
161 EmployeesDB ([=] () {
162 cerr << "\tConnecting to sqlite db: {}"_f(dbPath) << endl;
163 // works poorly with 100ms busyTimeout, but better than any other value - what am I missing!
164 auto c = SQLite::Connection::New (SQLite::Connection::Options{.fDBPath = dbPath, .fBusyTimeout = 100ms});
165 return c;
166 });
167 cerr << "done." << endl;
168 }
169#endif
170 {
171 cerr << "Starting trivial document db networks sample:" << endl;
172 auto internallySynchronizedDBConnection =
173 TrivialDocumentDB::New (TrivialDocumentDB::Options{.fStorage = TrivialDocumentDB::Options::MemoryStorage{}});
174 ComputerNetworksModel ([=] () {
175 cerr << "\tConnecting to trivial document db: {}" << endl;
176 return internallySynchronizedDBConnection;
177 });
178 cerr << "done." << endl;
179 }
180 {
181 cerr << "Starting trivial document db employees sample:" << endl;
182 auto internallySynchronizedDBConnection =
183 TrivialDocumentDB::New (TrivialDocumentDB::Options{.fStorage = TrivialDocumentDB::Options::MemoryStorage{}});
184 EmployeesDB ([=] () {
185 cerr << "\tConnecting to trivial document db: {}" << endl;
186 return internallySynchronizedDBConnection;
187 });
188 cerr << "done." << endl;
189 }
190 return EXIT_SUCCESS;
191}
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
STL namespace.