forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBConnection.cpp
672 lines (566 loc) · 18.2 KB
/
DBConnection.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
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*!********************************************************************
Sneedacity: A Digital Audio Editor
@file DBConnection.cpp
@brief Implements DBConnection
Paul Licameli -- split from ProjectFileIO.cpp
**********************************************************************/
#include "DBConnection.h"
#include "sqlite3.h"
#include <wx/progdlg.h>
#include <wx/string.h>
#include "SneedacityLogger.h"
#include "FileNames.h"
#include "Internat.h"
#include "Project.h"
#include "FileException.h"
#include "wxFileNameWrapper.h"
// Configuration to provide "safe" connections
static const char *SafeConfig =
"PRAGMA <schema>.busy_timeout = 5000;"
"PRAGMA <schema>.locking_mode = SHARED;"
"PRAGMA <schema>.synchronous = NORMAL;"
"PRAGMA <schema>.journal_mode = WAL;"
"PRAGMA <schema>.wal_autocheckpoint = 0;";
// Configuration to provide "Fast" connections
static const char *FastConfig =
"PRAGMA <schema>.busy_timeout = 5000;"
"PRAGMA <schema>.locking_mode = SHARED;"
"PRAGMA <schema>.synchronous = OFF;"
"PRAGMA <schema>.journal_mode = OFF;";
DBConnection::DBConnection(
const std::weak_ptr<SneedacityProject> &pProject,
const std::shared_ptr<DBConnectionErrors> &pErrors,
CheckpointFailureCallback callback)
: mpProject{ pProject }
, mpErrors{ pErrors }
, mCallback{ std::move(callback) }
{
mDB = nullptr;
mCheckpointDB = nullptr;
mBypass = false;
}
DBConnection::~DBConnection()
{
wxASSERT(mDB == nullptr);
if (mDB)
{
wxLogMessage("Database left open at connection destruction %s\n",
sqlite3_db_filename(mDB, nullptr));
}
}
void DBConnection::SetBypass( bool bypass )
{
mBypass = bypass;
}
bool DBConnection::ShouldBypass()
{
return mBypass;
}
void DBConnection::SetError(
const TranslatableString &msg, const TranslatableString &libraryError, int errorCode)
{
mpErrors->mErrorCode = errorCode;
mpErrors->mLastError = msg;
mpErrors->mLibraryError = errorCode && libraryError.empty()
? XO("(%d): %s").Format(errorCode, sqlite3_errstr(errorCode))
: libraryError;
wxLogMessage("DBConnection SetError\n"
"\tErrorCode: %d\n"
"\tLastError: %s\n"
"\tLibraryError: %s",
mpErrors->mErrorCode,
mpErrors->mLastError.Debug(),
mpErrors->mLibraryError.Debug());
auto logger = SneedacityLogger::Get();
if (logger)
{
mpErrors->mLog = logger->GetLog(10);
}
}
void DBConnection::SetDBError(
const TranslatableString &msg, const TranslatableString &libraryError, int errorCode)
{
auto db = DB();
mpErrors->mErrorCode = errorCode < 0 && db
? sqlite3_errcode(db)
: errorCode;
mpErrors->mLastError = msg.empty()
? XO("(%d): %s").Format(mpErrors->mErrorCode, sqlite3_errstr(mpErrors->mErrorCode))
: msg;
mpErrors->mLibraryError = libraryError.empty() && db
? Verbatim(sqlite3_errmsg(db))
: libraryError;
wxLogMessage("DBConnection SetDBError\n"
"\tErrorCode: %d\n"
"\tLastError: %s\n"
"\tLibraryError: %s",
mpErrors->mErrorCode,
mpErrors->mLastError.Debug(),
mpErrors->mLibraryError.Debug());
auto logger = SneedacityLogger::Get();
if (logger)
{
mpErrors->mLog = logger->GetLog(10);
}
}
int DBConnection::Open(const FilePath fileName)
{
wxASSERT(mDB == nullptr);
int rc;
// Initialize checkpoint controls
mCheckpointStop = false;
mCheckpointPending = false;
mCheckpointActive = false;
rc = OpenStepByStep( fileName );
if ( rc != SQLITE_OK)
{
if (mCheckpointDB)
{
sqlite3_close(mCheckpointDB);
mCheckpointDB = nullptr;
}
if (mDB)
{
sqlite3_close(mDB);
mDB = nullptr;
}
}
return rc;
}
int DBConnection::OpenStepByStep(const FilePath fileName)
{
const char *name = fileName.ToUTF8();
bool success = false;
int rc = sqlite3_open(name, &mDB);
if (rc != SQLITE_OK)
{
wxLogMessage("Failed to open primary connection to %s: %d, %s\n",
fileName,
rc,
sqlite3_errstr(rc));
return rc;
}
// Set default mode
// (See comments in ProjectFileIO::SaveProject() about threading
rc = SafeMode();
if (rc != SQLITE_OK)
{
SetDBError(XO("Failed to set safe mode on primary connection to %s").Format(fileName));
return rc;
}
rc = sqlite3_open(name, &mCheckpointDB);
if (rc != SQLITE_OK)
{
wxLogMessage("Failed to open checkpoint connection to %s: %d, %s\n",
fileName,
rc,
sqlite3_errstr(rc));
return rc;
}
rc = ModeConfig(mCheckpointDB, "main", SafeConfig);
if (rc != SQLITE_OK) {
SetDBError(XO("Failed to set safe mode on checkpoint connection to %s").Format(fileName));
return rc;
}
auto db = mCheckpointDB;
mCheckpointThread = std::thread(
[this, db, fileName]{ CheckpointThread(db, fileName); });
// Install our checkpoint hook
sqlite3_wal_hook(mDB, CheckpointHook, this);
return rc;
}
bool DBConnection::Close()
{
wxASSERT(mDB != nullptr);
int rc;
// Protect...
if (mDB == nullptr)
{
return true;
}
// Uninstall our checkpoint hook so that no additional checkpoints
// are sent our way. (Though this shouldn't really happen.)
sqlite3_wal_hook(mDB, nullptr, nullptr);
// Display a progress dialog if there's active or pending checkpoints
if (mCheckpointPending || mCheckpointActive)
{
TranslatableString title = XO("Checkpointing project");
// Get access to the active project
auto project = mpProject.lock();
if (project)
{
title = XO("Checkpointing %s").Format(project->GetProjectName());
}
// Provides a progress dialog with indeterminate mode
wxGenericProgressDialog pd(title.Translation(),
XO("This may take several seconds").Translation(),
300000, // range
nullptr, // parent
wxPD_APP_MODAL | wxPD_ELAPSED_TIME | wxPD_SMOOTH);
// Wait for the checkpoints to end
while (mCheckpointPending || mCheckpointActive)
{
wxMilliSleep(50);
pd.Pulse();
}
}
// Tell the checkpoint thread to shutdown
{
std::lock_guard<std::mutex> guard(mCheckpointMutex);
mCheckpointStop = true;
mCheckpointCondition.notify_one();
}
// And wait for it to do so
if (mCheckpointThread.joinable())
{
mCheckpointThread.join();
}
// We're done with the prepared statements
{
std::lock_guard<std::mutex> guard(mStatementMutex);
for (auto stmt : mStatements)
{
// No need to process return code, but log it for diagnosis
rc = sqlite3_finalize(stmt.second);
if (rc != SQLITE_OK)
{
wxLogMessage("Failed to finalize statement on %s\n"
"\tErrMsg: %s\n"
"\tSQL: %s",
sqlite3_db_filename(mDB, nullptr),
sqlite3_errmsg(mDB),
stmt.second);
}
}
mStatements.clear();
}
// Not much we can do if the closes fail, so just report the error
// Close the checkpoint connection
rc = sqlite3_close(mCheckpointDB);
if (rc != SQLITE_OK)
{
wxLogMessage("Failed to close checkpoint connection for %s\n"
"\tError: %s\n",
sqlite3_db_filename(mCheckpointDB, nullptr),
sqlite3_errmsg(mCheckpointDB));
}
mCheckpointDB = nullptr;
// Close the primary connection
rc = sqlite3_close(mDB);
if (rc != SQLITE_OK)
{
wxLogMessage("Failed to close %s\n"
"\tError: %s\n",
sqlite3_db_filename(mDB, nullptr),
sqlite3_errmsg(mDB));
}
mDB = nullptr;
return true;
}
[[noreturn]] void DBConnection::ThrowException( bool write ) const
{
// Sqlite3 documentation says returned character string
// does NOT require freeing by us.
wxString dbName{ sqlite3_db_filename(mDB, "main") };
// Now we have an absolute path. Throw a message box exception that
// formats a helpful message just as used to be done before sqlite3
// was used for projects.
throw FileException{
write ? FileException::Cause::Write : FileException::Cause::Read,
dbName
};
}
int DBConnection::SafeMode(const char *schema /* = "main" */)
{
return ModeConfig(mDB, schema, SafeConfig);
}
int DBConnection::FastMode(const char *schema /* = "main" */)
{
return ModeConfig(mDB, schema, FastConfig);
}
int DBConnection::ModeConfig(sqlite3 *db, const char *schema, const char *config)
{
// Ensure attached DB connection gets configured
int rc;
// Replace all schema "keywords" with the schema name
wxString sql = config;
sql.Replace(wxT("<schema>"), schema);
// Set the configuration
rc = sqlite3_exec(db, sql, nullptr, nullptr, nullptr);
if (rc != SQLITE_OK)
{
// Don't store in connection, just report it
wxLogMessage("Failed to set mode on %s\n"
"\tError: %s\n"
"\tSQL: %s",
sqlite3_db_filename(mDB, nullptr),
sqlite3_errmsg(mDB),
sql);
}
return rc;
}
sqlite3 *DBConnection::DB()
{
wxASSERT(mDB != nullptr);
return mDB;
}
int DBConnection::GetLastRC() const
{
return sqlite3_errcode(mDB);
}
const wxString DBConnection::GetLastMessage() const
{
return sqlite3_errmsg(mDB);
}
sqlite3_stmt *DBConnection::Prepare(enum StatementID id, const char *sql)
{
std::lock_guard<std::mutex> guard(mStatementMutex);
int rc;
// See bug 2673
// We must not use the same prepared statement from two different threads.
// Therefore, in the cache, use the thread id too.
StatementIndex ndx(id, std::this_thread::get_id());
// Return an existing statement if it's already been prepared
auto iter = mStatements.find(ndx);
if (iter != mStatements.end())
{
return iter->second;
}
// Prepare the statement
sqlite3_stmt *stmt = nullptr;
rc = sqlite3_prepare_v3(mDB, sql, -1, SQLITE_PREPARE_PERSISTENT, &stmt, 0);
if (rc != SQLITE_OK)
{
wxLogMessage("Failed to prepare statement for %s\n"
"\tError: %s\n"
"\tSQL: %s",
sqlite3_db_filename(mDB, nullptr),
sqlite3_errmsg(mDB),
sql);
// TODO: Look into why this causes an access violation
THROW_INCONSISTENCY_EXCEPTION;
}
// There are a small number (10 or so) of different id's corresponding
// to different SQL statements, see enum StatementID
// We have relatively few threads running at any one time,
// e.g. main gui thread, a playback thread, a thread for compacting.
// However the cache might keep growing, as we start/stop audio,
// perhaps, if we chose to use a new thread each time.
// For 3.0.0 I think that's OK. If it's a data leak it's a slow
// enough one. wxLogDebugs seem to show that the audio play thread
// is being reused, not recreated with a new ID, i.e. no leak at all.
// ANSWER-ME Just how serious is the data leak? How best to fix?
// Remember the cached statement.
mStatements.insert({ndx, stmt});
return stmt;
}
void DBConnection::CheckpointThread(sqlite3 *db, const FilePath &fileName)
{
int rc = SQLITE_OK;
bool giveUp = false;
while (true)
{
{
// Wait for work or the stop signal
std::unique_lock<std::mutex> lock(mCheckpointMutex);
mCheckpointCondition.wait(lock,
[&]
{
return mCheckpointPending || mCheckpointStop;
});
// Requested to stop, so bail
if (mCheckpointStop)
{
break;
}
// Capture the number of pages that need checkpointing and reset
mCheckpointActive = true;
mCheckpointPending = false;
}
// And kick off the checkpoint. This may not checkpoint ALL frames
// in the WAL. They'll be gotten the next time around.
using namespace std::chrono;
do {
rc = giveUp ? SQLITE_OK :
sqlite3_wal_checkpoint_v2(
db, nullptr, SQLITE_CHECKPOINT_PASSIVE, nullptr, nullptr);
}
// Contentions for an exclusive lock on the database are possible,
// even while the main thread is merely drawing the tracks, which
// may perform reads
while (rc == SQLITE_BUSY && (std::this_thread::sleep_for(1ms), true));
// Reset
mCheckpointActive = false;
if (rc != SQLITE_OK)
{
wxLogMessage("Failed to perform checkpoint on %s\n"
"\tErrCode: %d\n"
"\tErrMsg: %s",
fileName,
sqlite3_errcode(db),
sqlite3_errmsg(db));
// Can't checkpoint -- maybe the device has too little space
wxFileNameWrapper fName{ fileName };
auto path = FileNames::AbbreviatePath(fName);
auto name = fName.GetFullName();
auto longname = name + "-wal";
// TODO: Should we return the actual error message if it's not a
// disk full condition?
auto message1 = rc == SQLITE_FULL
? XO("Could not write to %s.\n").Format(path)
: TranslatableString{};
auto message = XO(
"Disk is full.\n"
"%s\n"
"For tips on freeing up space, click the help button."
).Format(message1);
// Stop trying to checkpoint
giveUp = true;
// Stop the audio.
GuardedCall(
[&message, rc] {
throw SimpleMessageBoxException{ rc != SQLITE_FULL ? ExceptionType::Internal : ExceptionType::BadEnvironment,
message, XO("Warning"), "Error:_Disk_full_or_not_writable" }; },
SimpleGuard<void>{},
[this](SneedacityException * e) {
// This executes in the main thread.
if (mCallback)
mCallback();
if (e)
e->DelayedHandlerAction();
}
);
}
}
return;
}
int DBConnection::CheckpointHook(void *data, sqlite3 *db, const char *schema, int pages)
{
// Get access to our object
DBConnection *that = static_cast<DBConnection *>(data);
// Queue the database pointer for our checkpoint thread to process
std::lock_guard<std::mutex> guard(that->mCheckpointMutex);
that->mCheckpointPending = true;
that->mCheckpointCondition.notify_one();
return SQLITE_OK;
}
bool TransactionScope::TransactionStart(const wxString &name)
{
char *errmsg = nullptr;
int rc = sqlite3_exec(mConnection.DB(),
wxT("SAVEPOINT ") + name + wxT(";"),
nullptr,
nullptr,
&errmsg);
if (errmsg)
{
mConnection.SetDBError(
XO("Failed to create savepoint:\n\n%s").Format(name)
);
sqlite3_free(errmsg);
}
return rc == SQLITE_OK;
}
bool TransactionScope::TransactionCommit(const wxString &name)
{
char *errmsg = nullptr;
int rc = sqlite3_exec(mConnection.DB(),
wxT("RELEASE ") + name + wxT(";"),
nullptr,
nullptr,
&errmsg);
if (errmsg)
{
mConnection.SetDBError(
XO("Failed to release savepoint:\n\n%s").Format(name)
);
sqlite3_free(errmsg);
}
return rc == SQLITE_OK;
}
bool TransactionScope::TransactionRollback(const wxString &name)
{
char *errmsg = nullptr;
int rc = sqlite3_exec(mConnection.DB(),
wxT("ROLLBACK TO ") + name + wxT(";"),
nullptr,
nullptr,
&errmsg);
if (errmsg)
{
mConnection.SetDBError(
XO("Failed to release savepoint:\n\n%s").Format(name)
);
sqlite3_free(errmsg);
}
return rc == SQLITE_OK;
}
TransactionScope::TransactionScope(
DBConnection &connection, const char *name)
: mConnection(connection),
mName(name)
{
mInTrans = TransactionStart(mName);
if ( !mInTrans )
// To do, improve the message
throw SimpleMessageBoxException( ExceptionType::Internal,
XO("Database error. Sorry, but we don't have more details."),
XO("Warning"),
"Error:_Disk_full_or_not_writable"
);
}
TransactionScope::~TransactionScope()
{
if (mInTrans)
{
// Rollback AND REMOVE the transaction
// -- must do both; rolling back a savepoint only rewinds it
// without removing it, unlike the ROLLBACK command
if (!(TransactionRollback(mName) &&
TransactionCommit(mName) ) )
{
// Do not throw from a destructor!
// This has to be a no-fail cleanup that does the best that it can.
wxLogMessage("Transaction active at scope destruction");
}
}
}
bool TransactionScope::Commit()
{
if ( !mInTrans )
{
wxLogMessage("No active transaction to commit");
// Misuse of this class
THROW_INCONSISTENCY_EXCEPTION;
}
mInTrans = !TransactionCommit(mName);
return mInTrans;
}
ConnectionPtr::~ConnectionPtr()
{
wxASSERT_MSG(!mpConnection, wxT("Project file was not closed at shutdown"));
if (mpConnection)
{
wxLogMessage("Project file was not closed at connection destruction");
}
}
static const SneedacityProject::AttachedObjects::RegisteredFactory
sConnectionPtrKey{
[]( SneedacityProject & ){
// Ignore the argument; this is just a holder of a
// unique_ptr to DBConnection, which must be filled in later
// (when we can get a weak_ptr to the project)
auto result = std::make_shared< ConnectionPtr >();
return result;
}
};
ConnectionPtr &ConnectionPtr::Get( SneedacityProject &project )
{
auto &result =
project.AttachedObjects::Get< ConnectionPtr >( sConnectionPtrKey );
return result;
}
const ConnectionPtr &ConnectionPtr::Get( const SneedacityProject &project )
{
return Get( const_cast< SneedacityProject & >( project ) );
}