From fcabcf3bbd6d389900baaf20b33d75eca36d3808 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C4=83t=C4=83lin=20Badea?= <catalin.badea392@gmail.com>
Date: Sun, 19 Aug 2012 23:11:14 +0300
Subject: Use async database access.


diff --git a/Swiften/History/SQLiteHistoryStorage.cpp b/Swiften/History/SQLiteHistoryStorage.cpp
index 5e3148b..ed0d6a3 100644
--- a/Swiften/History/SQLiteHistoryStorage.cpp
+++ b/Swiften/History/SQLiteHistoryStorage.cpp
@@ -8,6 +8,7 @@
 #include <boost/lexical_cast.hpp>
 
 #include <sqlite3.h>
+#include <3rdParty/SQLite/sqlite3async.h>
 #include <Swiften/History/SQLiteHistoryStorage.h>
 #include <boost/date_time/gregorian/gregorian.hpp>
 
@@ -25,7 +26,14 @@ inline std::string getEscapedString(const std::string& s) {
 namespace Swift {
 
 SQLiteHistoryStorage::SQLiteHistoryStorage(const std::string& file) : db_(0) {
-	sqlite3_open(file.c_str(), &db_);
+	sqlite3_vfs vfs;
+
+	sqlite3async_initialize(NULL, true);
+	sqlite3_vfs_register(&vfs, false);
+
+	thread_ = new boost::thread(boost::bind(&SQLiteHistoryStorage::run, this));
+
+	sqlite3_open_v2(file.c_str(), &db_, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, "sqlite3async");
 	if (!db_) {
 		std::cerr << "Error opening database " << file << std::endl;
 	}
@@ -45,7 +53,9 @@ SQLiteHistoryStorage::SQLiteHistoryStorage(const std::string& file) : db_(0) {
 }
 
 SQLiteHistoryStorage::~SQLiteHistoryStorage() {
+	sqlite3async_shutdown();
 	sqlite3_close(db_);
+	delete thread_;
 }
 
 void SQLiteHistoryStorage::addMessage(const HistoryMessage& message) {
@@ -371,4 +381,8 @@ boost::posix_time::ptime SQLiteHistoryStorage::getLastTimeStampFromMUC(const JID
 	return boost::posix_time::ptime(boost::posix_time::not_a_date_time);
 }
 
+void SQLiteHistoryStorage::run() {
+	sqlite3async_run();
+}
+
 }
diff --git a/Swiften/History/SQLiteHistoryStorage.h b/Swiften/History/SQLiteHistoryStorage.h
index aae6db8..782334a 100644
--- a/Swiften/History/SQLiteHistoryStorage.h
+++ b/Swiften/History/SQLiteHistoryStorage.h
@@ -9,6 +9,7 @@
 #include <boost/optional.hpp>
 
 #include <Swiften/History/HistoryStorage.h>
+#include <boost/thread.hpp>
 
 struct sqlite3;
 
@@ -26,6 +27,7 @@ namespace Swift {
 			boost::posix_time::ptime getLastTimeStampFromMUC(const JID& selfJID, const JID& mucJID) const;
 
 		private:
+			void run();
 			boost::gregorian::date getNextDateWithLogs(const JID& selfJID, const JID& contactJID, HistoryMessage::Type type, const boost::gregorian::date& date, bool reverseOrder) const;
 			int getIDForJID(const JID&);
 			int addJID(const JID&);
@@ -34,5 +36,6 @@ namespace Swift {
 			boost::optional<int> getIDFromJID(const JID& jid) const;
 
 			sqlite3* db_;
+			boost::thread* thread_;
 	};
 }
-- 
cgit v0.10.2-6-g49f6