Skip to content

Commit f812194

Browse files
committed
More tests
1 parent 13ac701 commit f812194

File tree

10 files changed

+345
-118
lines changed

10 files changed

+345
-118
lines changed

.mtslconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"ignore_dirs":["node_modules",".git","tests"]}
1+
{"ignore_dirs":[".git","node_modules","tests"]}

cpp/ConnectionState.cpp

+41-20
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,72 @@
11
#include "ConnectionState.h"
22
#include "fileUtils.h"
33
#include "sqlite3.h"
4+
#include "logs.h"
45

56
const std::string EMPTY_LOCK_ID = "";
67

78
SQLiteOPResult genericSqliteOpenDb(string const dbName, string const docPath,
89
sqlite3 **db, int sqlOpenFlags);
910

1011
ConnectionState::ConnectionState(const std::string dbName,
11-
const std::string docPath, int SQLFlags) {
12+
const std::string docPath, int SQLFlags)
13+
{
1214
auto result = genericSqliteOpenDb(dbName, docPath, &connection, SQLFlags);
1315

1416
this->clearLock();
1517
threadDone = false;
1618
thread = new std::thread(&ConnectionState::doWork, this);
1719
}
1820

19-
ConnectionState::~ConnectionState() {
21+
ConnectionState::~ConnectionState()
22+
{
2023
// So threads know it's time to shut down
2124
threadDone = true;
2225

2326
// Wake up all the threads, so they can finish and be joined
2427
workQueueConditionVariable.notify_all();
25-
if (thread->joinable()) {
28+
if (thread->joinable())
29+
{
2630
thread->join();
2731
}
2832

2933
delete thread;
3034
}
3135

32-
void ConnectionState::clearLock() {
33-
if (!workQueue.empty()) {
36+
void ConnectionState::clearLock()
37+
{
38+
if (!workQueue.empty())
39+
{
3440
waitFinished();
3541
}
3642
_currentLockId = EMPTY_LOCK_ID;
3743
}
3844

39-
void ConnectionState::activateLock(const ConnectionLockId &lockId) {
45+
void ConnectionState::activateLock(const ConnectionLockId &lockId)
46+
{
4047
_currentLockId = lockId;
4148
}
4249

43-
bool ConnectionState::matchesLock(const ConnectionLockId &lockId) {
50+
bool ConnectionState::matchesLock(const ConnectionLockId &lockId)
51+
{
4452
return _currentLockId == lockId;
4553
}
4654

4755
bool ConnectionState::isEmptyLock() { return _currentLockId == EMPTY_LOCK_ID; }
4856

49-
void ConnectionState::close() {
50-
if (!workQueue.empty()) {
57+
void ConnectionState::close()
58+
{
59+
if (!workQueue.empty())
60+
{
5161
waitFinished();
5262
}
5363
// So that the thread can stop (if not already)
5464
threadDone = true;
5565
sqlite3_close_v2(connection);
5666
}
5767

58-
void ConnectionState::queueWork(std::function<void(sqlite3 *)> task) {
68+
void ConnectionState::queueWork(std::function<void(sqlite3 *)> task)
69+
{
5970
// Grab the mutex
6071
std::lock_guard<std::mutex> g(workQueueMutex);
6172

@@ -66,22 +77,27 @@ void ConnectionState::queueWork(std::function<void(sqlite3 *)> task) {
6677
workQueueConditionVariable.notify_all();
6778
}
6879

69-
void ConnectionState::doWork() {
80+
void ConnectionState::doWork()
81+
{
7082
// Loop while the queue is not destructing
71-
while (!threadDone) {
83+
LOGSIMPLE("[ConnectionState::doWork] %s", threadDone);
84+
while (!threadDone)
85+
{
86+
LOGSIMPLE("[ConnectionState::doWork] while start %lld", timestamp);
7287
std::function<void(sqlite3 *)> task;
7388

7489
// Create a scope, so we don't lock the queue for longer than necessary
7590
{
7691
std::unique_lock<std::mutex> g(workQueueMutex);
77-
workQueueConditionVariable.wait(g, [&] {
92+
workQueueConditionVariable.wait(g, [&]
93+
{
7894
// Only wake up if there are elements in the queue or the program is
7995
// shutting down
80-
return !workQueue.empty() || threadDone;
81-
});
96+
return !workQueue.empty() || threadDone; });
8297

8398
// If we are shutting down exit without trying to process more work
84-
if (threadDone) {
99+
if (threadDone)
100+
{
85101
break;
86102
}
87103

@@ -95,23 +111,28 @@ void ConnectionState::doWork() {
95111
// Need to notify in order for waitFinished to be updated when
96112
// the queue is empty and not busy
97113
workQueueConditionVariable.notify_all();
114+
LOGSIMPLE("[ConnectionState::doWork] while end %lld", timestamp);
98115
}
99116
}
100117

101-
void ConnectionState::waitFinished() {
118+
void ConnectionState::waitFinished()
119+
{
102120
std::unique_lock<std::mutex> g(workQueueMutex);
103121
workQueueConditionVariable.wait(
104-
g, [&] { return workQueue.empty() && (threadBusy == 0); });
122+
g, [&]
123+
{ return workQueue.empty() && (threadBusy == 0); });
105124
}
106125

107126
SQLiteOPResult genericSqliteOpenDb(string const dbName, string const docPath,
108-
sqlite3 **db, int sqlOpenFlags) {
127+
sqlite3 **db, int sqlOpenFlags)
128+
{
109129
string dbPath = get_db_path(dbName, docPath);
110130

111131
int exit = 0;
112132
exit = sqlite3_open_v2(dbPath.c_str(), db, sqlOpenFlags, nullptr);
113133

114-
if (exit != SQLITE_OK) {
134+
if (exit != SQLITE_OK)
135+
{
115136
return SQLiteOPResult{.type = SQLiteError,
116137
.errorMessage = sqlite3_errmsg(*db)};
117138
}

cpp/logs.h

+6
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@
3636
printf(" "); \
3737
printf(__VA_ARGS__);
3838
#endif // ANDROID
39+
40+
#include <chrono>
41+
using namespace std::chrono;
42+
43+
milliseconds timestamp = duration_cast<milliseconds>(
44+
system_clock::now().time_since_epoch());

cpp/sqliteBridge.cpp

+48-23
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ using namespace facebook;
2525
std::map<std::string, ConnectionPool *> dbMap =
2626
std::map<std::string, ConnectionPool *>();
2727

28-
SQLiteOPResult generateNotOpenResult(std::string const &dbName) {
28+
SQLiteOPResult generateNotOpenResult(std::string const &dbName)
29+
{
2930
return SQLiteOPResult{
3031
.type = SQLiteError,
3132
.errorMessage = dbName + " is not open",
@@ -42,8 +43,10 @@ sqliteOpenDb(string const dbName, string const docPath,
4243
const char *, sqlite3_int64),
4344
void (*onTransactionFinalizedCallback)(
4445
const TransactionCallbackPayload *event),
45-
uint32_t numReadConnections) {
46-
if (dbMap.count(dbName) == 1) {
46+
uint32_t numReadConnections)
47+
{
48+
if (dbMap.count(dbName) == 1)
49+
{
4750
return SQLiteOPResult{
4851
.type = SQLiteError,
4952
.errorMessage = dbName + " is already open",
@@ -60,8 +63,10 @@ sqliteOpenDb(string const dbName, string const docPath,
6063
};
6164
}
6265

63-
SQLiteOPResult sqliteCloseDb(string const dbName) {
64-
if (dbMap.count(dbName) == 0) {
66+
SQLiteOPResult sqliteCloseDb(string const dbName)
67+
{
68+
if (dbMap.count(dbName) == 0)
69+
{
6570
return generateNotOpenResult(dbName);
6671
}
6772

@@ -76,8 +81,10 @@ SQLiteOPResult sqliteCloseDb(string const dbName) {
7681
};
7782
}
7883

79-
void sqliteCloseAll() {
80-
for (auto const &x : dbMap) {
84+
void sqliteCloseAll()
85+
{
86+
for (auto const &x : dbMap)
87+
{
8188
x.second->closeAll();
8289
delete x.second;
8390
}
@@ -86,8 +93,10 @@ void sqliteCloseAll() {
8693

8794
SQLiteOPResult sqliteQueueInContext(std::string dbName,
8895
ConnectionLockId const contextId,
89-
std::function<void(sqlite3 *)> task) {
90-
if (dbMap.count(dbName) == 0) {
96+
std::function<void(sqlite3 *)> task)
97+
{
98+
if (dbMap.count(dbName) == 0)
99+
{
91100
return generateNotOpenResult(dbName);
92101
}
93102

@@ -96,33 +105,41 @@ SQLiteOPResult sqliteQueueInContext(std::string dbName,
96105
}
97106

98107
void sqliteReleaseLock(std::string const dbName,
99-
ConnectionLockId const contextId) {
100-
if (dbMap.count(dbName) == 0) {
108+
ConnectionLockId const contextId)
109+
{
110+
LOGSIMPLE("[sqliteReleaseLock] start %lld", timestamp);
111+
if (dbMap.count(dbName) == 0)
112+
{
101113
// Do nothing if the lock does not actually exist
102114
return;
103115
}
104116

105117
ConnectionPool *connection = dbMap[dbName];
106118
connection->closeContext(contextId);
119+
LOGSIMPLE("[sqliteReleaseLock] end %lld", timestamp);
107120
}
108121

109122
SQLiteOPResult sqliteRequestLock(std::string const dbName,
110123
ConnectionLockId const contextId,
111-
ConcurrentLockType lockType) {
112-
if (dbMap.count(dbName) == 0) {
124+
ConcurrentLockType lockType)
125+
{
126+
if (dbMap.count(dbName) == 0)
127+
{
113128
return generateNotOpenResult(dbName);
114129
}
115130

116131
ConnectionPool *connection = dbMap[dbName];
117132

118-
if (connection == nullptr) {
133+
if (connection == nullptr)
134+
{
119135
return SQLiteOPResult{
120136
.type = SQLiteOk,
121137

122138
};
123139
}
124140

125-
switch (lockType) {
141+
switch (lockType)
142+
{
126143
case ConcurrentLockType::ReadLock:
127144
connection->readLock(contextId);
128145
break;
@@ -141,35 +158,43 @@ SQLiteOPResult sqliteRequestLock(std::string const dbName,
141158

142159
SQLiteOPResult sqliteAttachDb(string const mainDBName, string const docPath,
143160
string const databaseToAttach,
144-
string const alias) {
145-
if (dbMap.count(mainDBName) == 0) {
161+
string const alias)
162+
{
163+
if (dbMap.count(mainDBName) == 0)
164+
{
146165
return generateNotOpenResult(mainDBName);
147166
}
148167

149168
ConnectionPool *connection = dbMap[mainDBName];
150169
return connection->attachDatabase(databaseToAttach, docPath, alias);
151170
}
152171

153-
SQLiteOPResult sqliteDetachDb(string const mainDBName, string const alias) {
154-
if (dbMap.count(mainDBName) == 0) {
172+
SQLiteOPResult sqliteDetachDb(string const mainDBName, string const alias)
173+
{
174+
if (dbMap.count(mainDBName) == 0)
175+
{
155176
return generateNotOpenResult(mainDBName);
156177
}
157178

158179
ConnectionPool *connection = dbMap[mainDBName];
159180
return connection->detachDatabase(alias);
160181
}
161182

162-
SQLiteOPResult sqliteRemoveDb(string const dbName, string const docPath) {
163-
if (dbMap.count(dbName) == 1) {
183+
SQLiteOPResult sqliteRemoveDb(string const dbName, string const docPath)
184+
{
185+
if (dbMap.count(dbName) == 1)
186+
{
164187
SQLiteOPResult closeResult = sqliteCloseDb(dbName);
165-
if (closeResult.type == SQLiteError) {
188+
if (closeResult.type == SQLiteError)
189+
{
166190
return closeResult;
167191
}
168192
}
169193

170194
string dbPath = get_db_path(dbName, docPath);
171195

172-
if (!file_exists(dbPath)) {
196+
if (!file_exists(dbPath))
197+
{
173198
return SQLiteOPResult{
174199
.type = SQLiteOk,
175200
.errorMessage =

0 commit comments

Comments
 (0)