-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.cpp
247 lines (203 loc) · 7.95 KB
/
pool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// Created by Rakesh on 16/07/2021.
//
#include "status.hpp"
#include "tasks.hpp"
#include "../../src/api/contextholder.hpp"
#include "../../src/api/pool/pool.hpp"
#include "../../src/common/util/bson.hpp"
#include <boost/asio/co_spawn.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#if defined(_WIN32) || defined(WIN32)
#include "client.hpp"
#endif
using spt::mongoservice::pool::Pool;
namespace spt::client::pool
{
boost::asio::awaitable<bsoncxx::oid> create( Pool<Client>& pool, int i, std::string_view value )
{
namespace basic = bsoncxx::builder::basic;
using basic::kvp;
bsoncxx::oid id;
bsoncxx::document::value document = basic::make_document(
kvp( "action", "create" ),
kvp( "database", "itest" ),
kvp( "collection", "test" ),
kvp( "document", basic::make_document(
kvp( "key", value ), kvp( "_id", id ) ) ) );
auto copt = pool.acquire();
assert( copt );
auto opt = co_await (*copt)->execute( document.view() );
assert( opt );
auto view = opt->view();
LOG_INFO << "[pool-client]" << i << " create - " << bsoncxx::to_json( view );
assert( view.find( "error" ) == view.end() );
assert( view.find( "database" ) != view.end() );
assert( view.find( "collection" ) != view.end() );
assert( view.find( "entity" ) != view.end() );
assert( view.find( "_id" ) != view.end() );
assert( view["entity"].get_oid().value == id );
co_return id;
}
boost::asio::awaitable<void> count( Pool<Client>& pool, int i )
{
namespace basic = bsoncxx::builder::basic;
using basic::kvp;
bsoncxx::document::value document = basic::make_document(
kvp( "action", "count" ),
kvp( "database", "itest" ),
kvp( "collection", "test" ),
kvp( "document", basic::make_document()));
auto copt = pool.acquire();
assert( copt );
auto opt = co_await (*copt)->execute( document.view() );
assert( opt );
auto view = opt->view();
LOG_INFO << "[pool-client]" << i << " count - " << bsoncxx::to_json( view );
assert( view.find( "error" ) == view.end());
const auto count = spt::util::bsonValueIfExists<int64_t>( "count", view );
assert( count );
}
boost::asio::awaitable<void> byId( Pool<Client>& pool, bsoncxx::oid id, int i, std::string_view value )
{
namespace basic = bsoncxx::builder::basic;
using basic::kvp;
bsoncxx::document::value document = basic::make_document(
kvp( "action", "retrieve" ),
kvp( "database", "itest" ),
kvp( "collection", "test" ),
kvp( "document", basic::make_document( kvp( "_id", id ))));
auto copt = pool.acquire();
assert( copt );
auto opt = co_await (*copt)->execute( document.view() );
assert( opt );
auto view = opt->view();
LOG_INFO << "[pool-client]" << i << " byId - " << bsoncxx::to_json( view );
assert( view.find( "error" ) == view.end());
assert( view.find( "result" ) != view.end());
assert( view.find( "results" ) == view.end());
const auto dv = spt::util::bsonValue<bsoncxx::document::view>( "result", view );
assert( dv["_id"].get_oid().value == id );
const auto key = spt::util::bsonValueIfExists<std::string>( "key", dv );
assert( key );
assert( *key == value );
}
boost::asio::awaitable<void> byProperty( Pool<Client>& pool,
std::string_view name, std::string_view value, bsoncxx::oid id, int i )
{
namespace basic = bsoncxx::builder::basic;
using basic::kvp;
bsoncxx::document::value document = basic::make_document(
kvp( "action", "retrieve" ),
kvp( "database", "itest" ),
kvp( "collection", "test" ),
kvp( "document", basic::make_document( kvp( name, value ))));
auto copt = pool.acquire();
assert( copt );
auto opt = co_await (*copt)->execute( document.view() );
assert( opt );
auto view = opt->view();
LOG_INFO << "[pool-client]" << i << " byProperty - " << bsoncxx::to_json( view );
assert( view.find( "error" ) == view.end());
assert( view.find( "result" ) == view.end());
assert( view.find( "results" ) != view.end());
const auto arr = spt::util::bsonValue<bsoncxx::array::view>( "results", view );
assert( !arr.empty() );
bool found = false;
for ( auto e : arr )
{
const auto dv = e.get_document().view();
if ( dv["_id"].get_oid().value == id ) found = true;
const auto key = spt::util::bsonValueIfExists<std::string>( "key", dv );
assert( key );
assert( key == value );
}
assert( found );
}
boost::asio::awaitable<void> update( Pool<Client>& pool, bsoncxx::oid id, int i, std::string_view value )
{
namespace basic = bsoncxx::builder::basic;
using basic::kvp;
bsoncxx::document::value document = basic::make_document(
kvp( "action", "update" ),
kvp( "database", "itest" ),
kvp( "collection", "test" ),
kvp( "document", basic::make_document(
kvp( "key1", "value1" ),
kvp( "date", bsoncxx::types::b_date{
std::chrono::system_clock::now() } ),
kvp( "_id", id ))));
auto copt = pool.acquire();
assert( copt );
auto opt = co_await (*copt)->execute( document.view() );
assert( opt );
auto view = opt->view();
LOG_INFO << "[pool-client]" << i << " update - " << bsoncxx::to_json( view );
assert( view.find( "error" ) == view.end() );
const auto doc = spt::util::bsonValueIfExists<bsoncxx::document::view>( "document", view );
assert( doc );
assert( doc->find( "_id" ) != doc->end() );
assert(( *doc )["_id"].get_oid().value == id );
auto key = spt::util::bsonValueIfExists<std::string>( "key", *doc );
assert( key );
assert( *key == value );
key = spt::util::bsonValueIfExists<std::string>( "key1", *doc );
assert( key );
assert( *key == "value1" );
}
boost::asio::awaitable<void> remove( Pool<Client>& pool, bsoncxx::oid id, int i )
{
namespace basic = bsoncxx::builder::basic;
using basic::kvp;
bsoncxx::document::value document = basic::make_document(
kvp( "action", "delete" ),
kvp( "database", "itest" ),
kvp( "collection", "test" ),
kvp( "document", basic::make_document( kvp( "_id", id ))));
auto copt = pool.acquire();
assert( copt );
auto opt = co_await (*copt)->execute( document.view() );
assert( opt );
auto view = opt->view();
LOG_INFO << "[pool-client]" << i << " remove - " << bsoncxx::to_json( view );
assert( view.find( "error" ) == view.end() );
assert( view.find( "success" ) != view.end() );
assert( view.find( "history" ) != view.end() );
}
boost::asio::awaitable<bool> crud( Pool<Client>& pool, int i )
{
using namespace std::string_view_literals;
std::string value{};
value.reserve( 12 );
value.append( "value " ).append( std::to_string( i ) );
auto id = co_await pool::create( pool, i, value );
LOG_INFO << "[pool-client]" << i << " id - " << id.to_string();
co_await count( pool, i );
co_await byId( pool, id, i, value );
co_await byProperty( pool, "key"sv, value, id, i );
co_await update( pool, id, i, value );
co_await remove( pool, id, i );
co_return true;
}
}
boost::asio::awaitable<void> spt::client::crud()
{
auto config = mongoservice::pool::Configuration{};
config.initialSize = 1;
config.maxPoolSize = 10;
config.maxConnections = 1000;
config.maxIdleTime = std::chrono::seconds{ 3 };
Pool<Client> pool{ createClient, std::move( config ) };
auto& ctx = mongoservice::api::ContextHolder::instance();
for ( int i = 0; i < 100; ++i )
{
LOG_INFO << "Spawning crud operations " << i;
boost::asio::co_spawn( ctx.ioc, pool::crud( pool, i ),
[](std::exception_ptr e, bool) { ++Status::instance().counter; } );
}
while ( Status::instance().counter.load() < 101 ) std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
LOG_INFO << "Finished multi-threaded crud operations";
ctx.ioc.stop();
co_return;
}