forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLedgerTxnImpl.h
655 lines (549 loc) · 24.4 KB
/
LedgerTxnImpl.h
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright 2018 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "database/Database.h"
#include "ledger/LedgerTxn.h"
#include "util/RandomEvictionCache.h"
#include <list>
#ifdef USE_POSTGRES
#include <iomanip>
#include <libpq-fe.h>
#include <limits>
#include <sstream>
#endif
namespace stellar
{
// Precondition: The keys associated with entries are unique and constitute a
// subset of keys
std::unordered_map<LedgerKey, std::shared_ptr<LedgerEntry const>>
populateLoadedEntries(std::unordered_set<LedgerKey> const& keys,
std::vector<LedgerEntry> const& entries);
// A defensive heuristic to ensure prefetching stops if entry cache is filling
// up.
static const double ENTRY_CACHE_FILL_RATIO = 0.5;
class EntryIterator::AbstractImpl
{
public:
virtual ~AbstractImpl()
{
}
virtual void advance() = 0;
virtual bool atEnd() const = 0;
virtual LedgerEntry const& entry() const = 0;
virtual bool entryExists() const = 0;
virtual LedgerKey const& key() const = 0;
virtual std::unique_ptr<AbstractImpl> clone() const = 0;
};
// Helper struct to accumulate common cases that we can sift out of the
// commit stream and perform in bulk (as single SQL statements per-type)
// rather than making each insert/update/delete individually. This uses the
// postgres and sqlite-supported "ON CONFLICT"-style upserts, and uses
// soci's bulk operations where it can (i.e. for sqlite, or potentially
// others), and manually-crafted postgres unnest([array]) calls where it
// can't. This is not great, but it appears to be less work than
// reorganizing the relevant parts of soci.
class BulkLedgerEntryChangeAccumulator
{
std::vector<EntryIterator> mAccountsToUpsert;
std::vector<EntryIterator> mAccountsToDelete;
std::vector<EntryIterator> mAccountDataToUpsert;
std::vector<EntryIterator> mAccountDataToDelete;
std::vector<EntryIterator> mOffersToUpsert;
std::vector<EntryIterator> mOffersToDelete;
std::vector<EntryIterator> mTrustLinesToUpsert;
std::vector<EntryIterator> mTrustLinesToDelete;
public:
std::vector<EntryIterator>&
getAccountsToUpsert()
{
return mAccountsToUpsert;
}
std::vector<EntryIterator>&
getAccountsToDelete()
{
return mAccountsToDelete;
}
std::vector<EntryIterator>&
getTrustLinesToUpsert()
{
return mTrustLinesToUpsert;
}
std::vector<EntryIterator>&
getTrustLinesToDelete()
{
return mTrustLinesToDelete;
}
std::vector<EntryIterator>&
getOffersToUpsert()
{
return mOffersToUpsert;
}
std::vector<EntryIterator>&
getOffersToDelete()
{
return mOffersToDelete;
}
std::vector<EntryIterator>&
getAccountDataToUpsert()
{
return mAccountDataToUpsert;
}
std::vector<EntryIterator>&
getAccountDataToDelete()
{
return mAccountDataToDelete;
}
void accumulate(EntryIterator const& iter);
};
// Many functions in LedgerTxn::Impl provide a basic exception safety
// guarantee that states that certain caches may be modified or cleared if an
// exception is thrown. It is always safe to continue using the LedgerTxn
// object in such a case and the results of any successful query are correct.
// However, it should be noted that a query which would have succeeded had there
// not been an earlier exception may fail in the case where there had been an
// earlier exception. This could occur, for example, if in the first case the
// query would have hit the cache but in the second case the query hits the
// database because the cache has been cleared but the database connection has
// been lost.
class LedgerTxn::Impl
{
class EntryIteratorImpl;
typedef std::unordered_map<LedgerKey, std::shared_ptr<LedgerEntry>>
EntryMap;
AbstractLedgerTxnParent& mParent;
AbstractLedgerTxn* mChild;
std::unique_ptr<LedgerHeader> mHeader;
std::shared_ptr<LedgerTxnHeader::Impl> mActiveHeader;
EntryMap mEntry;
std::unordered_map<LedgerKey, std::shared_ptr<EntryImplBase>> mActive;
bool const mShouldUpdateLastModified;
bool mIsSealed;
LedgerTxnConsistency mConsistency;
void throwIfChild() const;
void throwIfSealed() const;
void throwIfNotExactConsistency() const;
// getDeltaVotes has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
std::map<AccountID, int64_t> getDeltaVotes() const;
// getTotalVotes has the strong exception safety guarantee
std::map<AccountID, int64_t>
getTotalVotes(std::vector<InflationWinner> const& parentWinners,
std::map<AccountID, int64_t> const& deltaVotes,
int64_t minVotes) const;
// enumerateInflationWinners has the strong exception safety guarantee
std::vector<InflationWinner>
enumerateInflationWinners(std::map<AccountID, int64_t> const& totalVotes,
size_t maxWinners, int64_t minVotes) const;
// getEntryIterator has the strong exception safety guarantee
EntryIterator getEntryIterator(EntryMap const& entries) const;
// maybeUpdateLastModified has the strong exception safety guarantee
EntryMap maybeUpdateLastModified() const;
// maybeUpdateLastModifiedThenInvokeThenSeal has the same exception safety
// guarantee as f
void maybeUpdateLastModifiedThenInvokeThenSeal(
std::function<void(EntryMap const&)> f);
public:
// Constructor has the strong exception safety guarantee
Impl(LedgerTxn& self, AbstractLedgerTxnParent& parent,
bool shouldUpdateLastModified);
// addChild has the strong exception safety guarantee
void addChild(AbstractLedgerTxn& child);
// commit has the strong exception safety guarantee.
void commit();
// commitChild has the strong exception safety guarantee.
void commitChild(EntryIterator iter, LedgerTxnConsistency cons);
// create has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
LedgerTxnEntry create(LedgerTxn& self, LedgerEntry const& entry);
// deactivate has the strong exception safety guarantee
void deactivate(LedgerKey const& key);
// deactivateHeader has the strong exception safety guarantee
void deactivateHeader();
// erase has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
void erase(LedgerKey const& key);
// getAllOffers has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified.
std::unordered_map<LedgerKey, LedgerEntry> getAllOffers();
// getBestOffer has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, modified or even
// cleared
// - the best offers cache may be, but is not guaranteed to be, modified or
// even cleared
std::shared_ptr<LedgerEntry const>
getBestOffer(Asset const& buying, Asset const& selling,
std::unordered_set<LedgerKey>& exclude);
// getChanges has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
LedgerEntryChanges getChanges();
// getDelta has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
LedgerTxnDelta getDelta();
// getOffersByAccountAndAsset has the basic exception safety guarantee. If
// it throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
std::unordered_map<LedgerKey, LedgerEntry>
getOffersByAccountAndAsset(AccountID const& account, Asset const& asset);
// getHeader does not throw
LedgerHeader const& getHeader() const;
// getInflationWinners has the basic exception safety guarantee. If it
// throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
std::vector<InflationWinner> getInflationWinners(size_t maxWinners,
int64_t minBalance);
// queryInflationWinners has the basic exception safety guarantee. If it
// throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
std::vector<InflationWinner> queryInflationWinners(size_t maxWinners,
int64_t minBalance);
// getAllEntries has the strong exception safety guarantee
void getAllEntries(std::vector<LedgerEntry>& initEntries,
std::vector<LedgerEntry>& liveEntries,
std::vector<LedgerKey>& deadEntries);
// getNewestVersion has the basic exception safety guarantee. If it throws
// an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
std::shared_ptr<LedgerEntry const>
getNewestVersion(LedgerKey const& key) const;
// load has the basic exception safety guarantee. If it throws an exception,
// then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
LedgerTxnEntry load(LedgerTxn& self, LedgerKey const& key);
// createOrUpdateWithoutLoading has the strong exception safety guarantee.
// If it throws an exception, then the current LedgerTxn::Impl is unchanged.
void createOrUpdateWithoutLoading(LedgerTxn& self,
LedgerEntry const& entry);
// eraseWithoutLoading has the strong exception safety guarantee. If it
// throws an exception, then the current LedgerTxn::Impl is unchanged.
void eraseWithoutLoading(LedgerKey const& key);
// loadAllOffers has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
std::map<AccountID, std::vector<LedgerTxnEntry>>
loadAllOffers(LedgerTxn& self);
// loadBestOffer has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, modified or even
// cleared
// - the best offers cache may be, but is not guaranteed to be, modified or
// even cleared
LedgerTxnEntry loadBestOffer(LedgerTxn& self, Asset const& buying,
Asset const& selling);
// loadHeader has the strong exception safety guarantee
LedgerTxnHeader loadHeader(LedgerTxn& self);
// loadOffersByAccountAndAsset has the basic exception safety guarantee. If
// it throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
std::vector<LedgerTxnEntry>
loadOffersByAccountAndAsset(LedgerTxn& self, AccountID const& accountID,
Asset const& asset);
// loadWithoutRecord has the basic exception safety guarantee. If it throws
// an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
ConstLedgerTxnEntry loadWithoutRecord(LedgerTxn& self,
LedgerKey const& key);
// rollback does not throw
void rollback();
// rollbackChild does not throw
void rollbackChild();
// unsealHeader has the same exception safety guarantee as f
void unsealHeader(LedgerTxn& self, std::function<void(LedgerHeader&)> f);
};
class LedgerTxn::Impl::EntryIteratorImpl : public EntryIterator::AbstractImpl
{
typedef LedgerTxn::Impl::EntryMap::const_iterator IteratorType;
IteratorType mIter;
IteratorType const mEnd;
public:
EntryIteratorImpl(IteratorType const& begin, IteratorType const& end);
void advance() override;
bool atEnd() const override;
LedgerEntry const& entry() const override;
bool entryExists() const override;
LedgerKey const& key() const override;
std::unique_ptr<EntryIterator::AbstractImpl> clone() const override;
};
// Many functions in LedgerTxnRoot::Impl provide a basic exception safety
// guarantee that states that certain caches may be modified or cleared if an
// exception is thrown. It is always safe to continue using the LedgerTxn
// object in such a case and the results of any successful query are correct.
// However, it should be noted that a query which would have succeeded had there
// not been an earlier exception may fail in the case where there had been an
// earlier exception. This could occur, for example, if in the first case the
// query would have hit the cache but in the second case the query hits the
// database because the cache has been cleared but the database connection has
// been lost.
class LedgerTxnRoot::Impl
{
struct KeyAccesses
{
uint64_t hits{0};
uint64_t misses{0};
};
enum class LoadType
{
IMMEDIATE,
PREFETCH
};
struct CacheEntry
{
std::shared_ptr<LedgerEntry const> entry;
LoadType type;
};
typedef RandomEvictionCache<LedgerKey, CacheEntry> EntryCache;
typedef std::string BestOffersCacheKey;
struct BestOffersCacheEntry
{
std::list<LedgerEntry> bestOffers;
bool allLoaded;
};
typedef RandomEvictionCache<std::string, BestOffersCacheEntry>
BestOffersCache;
Database& mDatabase;
std::unique_ptr<LedgerHeader> mHeader;
mutable EntryCache mEntryCache;
mutable BestOffersCache mBestOffersCache;
mutable std::unordered_map<LedgerKey, KeyAccesses> mPrefetchMetrics;
mutable uint64_t mTotalPrefetchHits{0};
size_t mMaxCacheSize;
size_t mBulkLoadBatchSize;
std::unique_ptr<soci::transaction> mTransaction;
AbstractLedgerTxn* mChild;
void throwIfChild() const;
std::shared_ptr<LedgerEntry const> loadAccount(LedgerKey const& key) const;
std::shared_ptr<LedgerEntry const> loadData(LedgerKey const& key) const;
std::shared_ptr<LedgerEntry const> loadOffer(LedgerKey const& key) const;
std::vector<LedgerEntry> loadAllOffers() const;
std::list<LedgerEntry>::const_iterator
loadOffers(StatementContext& prep, std::list<LedgerEntry>& offers) const;
std::list<LedgerEntry>::const_iterator
loadBestOffers(std::list<LedgerEntry>& offers, Asset const& buying,
Asset const& selling, size_t numOffers, size_t offset) const;
std::vector<LedgerEntry>
loadOffersByAccountAndAsset(AccountID const& accountID,
Asset const& asset) const;
std::vector<LedgerEntry> loadOffers(StatementContext& prep) const;
std::vector<InflationWinner> loadInflationWinners(size_t maxWinners,
int64_t minBalance) const;
std::shared_ptr<LedgerEntry const>
loadTrustLine(LedgerKey const& key) const;
void bulkApply(BulkLedgerEntryChangeAccumulator& bleca,
size_t bufferThreshold, LedgerTxnConsistency cons);
void bulkUpsertAccounts(std::vector<EntryIterator> const& entries);
void bulkDeleteAccounts(std::vector<EntryIterator> const& entries,
LedgerTxnConsistency cons);
void bulkUpsertTrustLines(std::vector<EntryIterator> const& entries);
void bulkDeleteTrustLines(std::vector<EntryIterator> const& entries,
LedgerTxnConsistency cons);
void bulkUpsertOffers(std::vector<EntryIterator> const& entries);
void bulkDeleteOffers(std::vector<EntryIterator> const& entries,
LedgerTxnConsistency cons);
void bulkUpsertAccountData(std::vector<EntryIterator> const& entries);
void bulkDeleteAccountData(std::vector<EntryIterator> const& entries,
LedgerTxnConsistency cons);
static std::string tableFromLedgerEntryType(LedgerEntryType let);
// The entry cache maintains relatively strong invariants:
//
// - It is only ever populated during a database operation, at root.
//
// - Until the (bulk) LedgerTxnRoot::commitChild operation, the only
// database operations are SELECTs, which only populate the cache
// with fresh data from the DB.
//
// - On LedgerTxnRoot::commitChild, the cache is cleared.
//
// - It is therefore always kept in exact correspondence with the
// database for the keyset that it has entries for. It's a precise
// image of a subset of the database.
std::shared_ptr<LedgerEntry const>
getFromEntryCache(LedgerKey const& key) const;
void putInEntryCache(LedgerKey const& key,
std::shared_ptr<LedgerEntry const> const& entry,
LoadType type) const;
BestOffersCacheEntry&
getFromBestOffersCache(Asset const& buying, Asset const& selling,
BestOffersCacheEntry& defaultValue) const;
std::unordered_map<LedgerKey, std::shared_ptr<LedgerEntry const>>
bulkLoadAccounts(std::unordered_set<LedgerKey> const& keys) const;
std::unordered_map<LedgerKey, std::shared_ptr<LedgerEntry const>>
bulkLoadTrustLines(std::unordered_set<LedgerKey> const& keys) const;
std::unordered_map<LedgerKey, std::shared_ptr<LedgerEntry const>>
bulkLoadOffers(std::unordered_set<LedgerKey> const& keys) const;
std::unordered_map<LedgerKey, std::shared_ptr<LedgerEntry const>>
bulkLoadData(std::unordered_set<LedgerKey> const& keys) const;
public:
// Constructor has the strong exception safety guarantee
Impl(Database& db, size_t entryCacheSize, size_t bestOfferCacheSize,
size_t prefetchBatchSize);
~Impl();
// addChild has the strong exception safety guarantee.
void addChild(AbstractLedgerTxn& child);
// commitChild has the strong exception safety guarantee.
void commitChild(EntryIterator iter, LedgerTxnConsistency cons);
// countObjects has the strong exception safety guarantee.
uint64_t countObjects(LedgerEntryType let) const;
uint64_t countObjects(LedgerEntryType let,
LedgerRange const& ledgers) const;
// deleteObjectsModifiedOnOrAfterLedger has no exception safety guarantees.
void deleteObjectsModifiedOnOrAfterLedger(uint32_t ledger) const;
// dropAccounts, dropData, dropOffers, and dropTrustLines have no exception
// safety guarantees.
void dropAccounts();
void dropData();
void dropOffers();
void dropTrustLines();
// getAllOffers has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified.
std::unordered_map<LedgerKey, LedgerEntry> getAllOffers();
// getBestOffer has the basic exception safety guarantee. If it throws an
// exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, modified or even
// cleared
// - the best offers cache may be, but is not guaranteed to be, modified or
// even cleared
std::shared_ptr<LedgerEntry const>
getBestOffer(Asset const& buying, Asset const& selling,
std::unordered_set<LedgerKey>& exclude);
// getOffersByAccountAndAsset has the basic exception safety guarantee. If
// it throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
std::unordered_map<LedgerKey, LedgerEntry>
getOffersByAccountAndAsset(AccountID const& account, Asset const& asset);
// getHeader does not throw
LedgerHeader const& getHeader() const;
// getInflationWinners has the basic exception safety guarantee. If it
// throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
std::vector<InflationWinner> getInflationWinners(size_t maxWinners,
int64_t minBalance);
// getNewestVersion has the basic exception safety guarantee. If it throws
// an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
// - the entry cache may be, but is not guaranteed to be, cleared.
std::shared_ptr<LedgerEntry const>
getNewestVersion(LedgerKey const& key) const;
// rollbackChild has the strong exception safety guarantee.
void rollbackChild();
// writeSignersTableIntoAccountsTable has the basic exception safety
// guarantee. If it throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
void writeSignersTableIntoAccountsTable();
// encodedDataNamesBase64 has the basic exception safety guarantee. If it
// throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
void encodeDataNamesBase64();
// encodedHomeDomainsBase64 has the basic exception safety guarantee. If it
// throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
void encodeHomeDomainsBase64();
// writeOffersIntoSimplifiedOffersTable has the basic exception safety
// guarantee. If it throws an exception, then
// - the prepared statement cache may be, but is not guaranteed to be,
// modified
void writeOffersIntoSimplifiedOffersTable();
// Prefetch some or all of given keys in batches. Note that no prefetching
// could occur if the cache is at its fill ratio. Returns number of keys
// prefetched.
uint32_t prefetch(std::unordered_set<LedgerKey> const& keys);
double getPrefetchHitRate() const;
};
#ifdef USE_POSTGRES
template <typename T>
inline void
marshalToPGArrayItem(PGconn* conn, std::ostringstream& oss, const T& item)
{
// NB: This setprecision is very important to ensuring that a double
// gets marshaled to enough decimal digits to reconstruct exactly the
// same double on the postgres side (that precision-level is exactly
// what max_digits10 is defined as). Do not remove it!
oss << std::setprecision(std::numeric_limits<T>::max_digits10) << item;
}
template <>
inline void
marshalToPGArrayItem<std::string>(PGconn* conn, std::ostringstream& oss,
const std::string& item)
{
std::vector<char> buf(item.size() * 2 + 1, '\0');
int err = 0;
size_t len =
PQescapeStringConn(conn, buf.data(), item.c_str(), item.size(), &err);
if (err != 0)
{
throw std::runtime_error("Could not escape string in SQL");
}
oss << '"';
oss.write(buf.data(), len);
oss << '"';
}
template <typename T>
inline void
marshalToPGArray(PGconn* conn, std::string& out, const std::vector<T>& v,
const std::vector<soci::indicator>* ind = nullptr)
{
std::ostringstream oss;
oss << '{';
for (size_t i = 0; i < v.size(); ++i)
{
if (i > 0)
{
oss << ',';
}
if (ind && (*ind)[i] == soci::i_null)
{
oss << "NULL";
}
else
{
marshalToPGArrayItem(conn, oss, v[i]);
}
}
oss << '}';
out = oss.str();
}
#endif
}