SocketAPI: Cache database conections and statements

This commit is contained in:
Klaas Freitag 2014-10-13 17:31:40 +02:00
parent dd28a645a1
commit 26ffa078ef
2 changed files with 50 additions and 18 deletions

View file

@ -224,6 +224,11 @@ void SocketApi::slotUnregisterPath( const QString& alias )
Folder *f = FolderMan::instance()->folder(alias);
if (f) {
broadcastMessage(QLatin1String("UNREGISTER_PATH"), f->path(), QString::null, true );
if( _dbConnections.contains(f)) {
sqlite3_close_v2(_dbConnections[f]._db);
}
_dbConnections.remove(f);
}
}
@ -407,6 +412,36 @@ SyncFileStatus SocketApi::recursiveFolderStatus(Folder *folder, const QString& f
return result;
}
SqliteHandle SocketApi::getSqliteHandle( Folder *folder )
{
if( _dbConnections.contains(folder) ) {
return _dbConnections[folder];
}
SqliteHandle h;
h._db = NULL;
h._stmt = NULL;
if( !folder ) {
return h;
}
int rc;
const char* query = "SELECT inode, mode, modtime, type, md5, fileid, remotePerm FROM "
"metadata WHERE phash=:ph";
QString dbFileName = folder->journalDb()->databaseFilePath();
if( sqlite3_open_v2(dbFileName.toUtf8().constData(), &(h._db),
SQLITE_OPEN_READONLY+SQLITE_OPEN_NOMUTEX, NULL) == SQLITE_OK ) {
rc = sqlite3_prepare_v2(h._db, query, strlen(query), &(h._stmt), NULL);
if( rc != SQLITE_OK ) {
qDebug() << "Unable to prepare the query statement.";
return h; // do not insert into hash
}
_dbConnections.insert( folder, h);
}
return h;
}
SyncJournalFileRecord SocketApi::dbFileRecord_capi( Folder *folder, QString fileName )
{
if( !(folder && folder->journalDb()) ) {
@ -417,29 +452,18 @@ SyncJournalFileRecord SocketApi::dbFileRecord_capi( Folder *folder, QString file
fileName.remove(0, folder->path().length());
}
QString dbFileName = folder->journalDb()->databaseFilePath();
SqliteHandle h = getSqliteHandle(folder);
sqlite3 *db = NULL;
sqlite3_stmt *stmt = NULL;
sqlite3_stmt *stmt = h._stmt;
SyncJournalFileRecord rec;
int rc;
const char* query = "SELECT inode, mode, modtime, type, md5, fileid, remotePerm FROM "
"metadata WHERE phash=:ph";
if( sqlite3_open_v2(dbFileName.toUtf8().constData(), &db,
SQLITE_OPEN_READONLY+SQLITE_OPEN_NOMUTEX, NULL) == SQLITE_OK ) {
rc = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
if( rc != SQLITE_OK ) {
qDebug() << "Unable to prepare the query statement.";
return rec;
}
if( h._db && h._stmt ) {
qlonglong phash = SyncJournalDb::getPHash( fileName );
sqlite3_bind_int64(stmt, 1, (long long signed int)phash);
// int column_count = sqlite3_column_count(stmt);
rc = sqlite3_step(stmt);
int rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW ) {
rec._path = fileName;
@ -451,8 +475,7 @@ SyncJournalFileRecord SocketApi::dbFileRecord_capi( Folder *folder, QString file
rec._fileId = QByteArray((char*)sqlite3_column_text(stmt, 5));
rec._remotePerm = QByteArray((char*)sqlite3_column_text(stmt, 6));
}
sqlite3_finalize(stmt);
sqlite3_close(db);
sqlite3_reset(stmt);
}
return rec;
}

View file

@ -20,6 +20,8 @@ extern "C" {
#include <std/c_string.h>
}
#include <sqlite3.h>
#include <QWeakPointer>
#include <QTcpSocket>
#include <QTcpServer>
@ -38,6 +40,12 @@ typedef QLocalSocket SocketType;
class SyncFileStatus;
class Folder;
struct SqliteHandle
{
sqlite3 *_db;
sqlite3_stmt *_stmt;
};
class SocketApi : public QObject
{
Q_OBJECT
@ -64,6 +72,7 @@ private:
SyncJournalFileRecord dbFileRecord( Folder *folder, QString fileName );
SyncJournalFileRecord dbFileRecord_capi( Folder *folder, QString fileName );
SyncFileStatus recursiveFolderStatus(Folder *folder, const QString& fileName, c_strlist_t *excludes );
SqliteHandle getSqliteHandle( Folder *folder );
void sendMessage(SocketType* socket, const QString& message, bool doWait = false);
void broadcastMessage(const QString& verb, const QString &path, const QString &status = QString::null, bool doWait = false);
@ -73,7 +82,6 @@ private:
Q_INVOKABLE void command_VERSION(const QString& argument, SocketType* socket);
private:
#ifdef SOCKETAPI_TCP
QTcpServer _localServer;
#else
@ -81,6 +89,7 @@ private:
#endif
QList<SocketType*> _listeners;
c_strlist_t *_excludes;
QHash<Folder*, SqliteHandle> _dbConnections;
};
}