Skip to content

Commit

Permalink
Minor cleanup to the core/db/ sources -- they were largely untouched …
Browse files Browse the repository at this point in the history
…from

the initial set of cleanups.
  • Loading branch information
clangen committed Nov 26, 2016
1 parent 64a187d commit 0b059c8
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 365 deletions.
1 change: 0 additions & 1 deletion src/core/core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
<ClInclude Include="sdk\IPlugin.h" />
<ClInclude Include="sdk\IMetadataWriter.h" />
<ClInclude Include="db\Connection.h" />
<ClInclude Include="db\dbconfig.h" />
<ClInclude Include="db\ScopedTransaction.h" />
<ClInclude Include="db\Statement.h" />
<ClInclude Include="audio\Buffer.h" />
Expand Down
3 changes: 0 additions & 3 deletions src/core/core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@
<ClInclude Include="db\Connection.h">
<Filter>src\db</Filter>
</ClInclude>
<ClInclude Include="db\dbconfig.h">
<Filter>src\db</Filter>
</ClInclude>
<ClInclude Include="db\ScopedTransaction.h">
<Filter>src\db</Filter>
</ClInclude>
Expand Down
167 changes: 42 additions & 125 deletions src/core/db/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,218 +49,135 @@ Connection::Connection()
this->UpdateReferenceCount(true);
}

Connection::~Connection(){
Connection::~Connection() {
this->Close();
this->UpdateReferenceCount(false);
}

//////////////////////////////////////////
///\brief
///Open a connection to the database
///
///\param database
///Connection string. In SQLite this is the filename
///
///\param options
///Bit options. Unused at the moment
///
///\param cache
///Cachesize in KB
///
///\returns
///Error code returned by SQLite
//////////////////////////////////////////
int Connection::Open(const char *database, unsigned int options, unsigned int cache) {
int error;

#ifdef UTF_WIDECHAR
error = sqlite3_open16(database,&this->connection);
error = sqlite3_open16(database, &this->connection);
#else
error = sqlite3_open(database,&this->connection);
error = sqlite3_open(database, &this->connection);
#endif

if (error==SQLITE_OK) {
if (error == SQLITE_OK) {
this->Initialize(cache);
}

return error;
}

//////////////////////////////////////////
///\brief
///Open a connection to the database
///
///\param database
///Connection string. In SQLite this is the filename
///
///\param options
///Bit options. Unused at the moment
///
///\param cache
///Cachesize in KB
///
///\returns
///Error code returned by SQLite
//////////////////////////////////////////
int Connection::Open(const std::string &database,unsigned int options,unsigned int cache){
int Connection::Open(const std::string &database, unsigned int options, unsigned int cache) {
int error;

#ifdef WIN32
std::wstring wdatabase = u8to16(database);
error = sqlite3_open16(wdatabase.c_str(),&this->connection);
error = sqlite3_open16(wdatabase.c_str(), &this->connection);
#else
error = sqlite3_open(database.c_str(),&this->connection);
error = sqlite3_open(database.c_str(), &this->connection);
#endif

if (error==SQLITE_OK) {
if (error == SQLITE_OK) {
this->Initialize(cache);
}

return error;
}

//////////////////////////////////////////
///\brief
///Close connection to the database
///
///\returns
///Errorcode ( musik::core::db::ReturnCode )
//////////////////////////////////////////
int Connection::Close() {
if (sqlite3_close(this->connection) == SQLITE_OK) {
this->connection = 0;
return musik::core::db::Okay;
return Okay;
}

return musik::core::db::Error;
return Error;
}

//////////////////////////////////////////
///\brief
///Execute a SQL string
///
///\param sql
///SQL to execute
///
///\returns
///Errorcode musik::core::db::ReturnCode
///
///\see
///musik::core::db::ReturnCode
//////////////////////////////////////////
int Connection::Execute(const char* sql) {
sqlite3_stmt *stmt = NULL;
sqlite3_stmt *stmt = nullptr;

// Prepaire seems to give errors when interrupted
/* prepare seems to give errors when interrupted */
{
boost::mutex::scoped_lock lock(this->mutex);
if(sqlite3_prepare_v2(this->connection,sql,-1,&stmt,NULL)!=SQLITE_OK){

if (sqlite3_prepare_v2(this->connection, sql, -1, &stmt, nullptr) != SQLITE_OK) {
sqlite3_finalize(stmt);
return db::Error;
return Error;
}
}

// Execute the statement
int error = this->StepStatement(stmt);
if(error!=SQLITE_OK && error!=SQLITE_DONE){
int error = this->StepStatement(stmt);
if (error != SQLITE_OK && error != SQLITE_DONE){
sqlite3_finalize(stmt);
return db::Error;
return Error;
}

sqlite3_reset(stmt);
sqlite3_finalize(stmt);

return musik::core::db::Okay;
return Okay;
}


//////////////////////////////////////////
///\brief
///Execute a SQL string
///
///\param sql
///SQL to execute
///
///\returns
///Errorcode musik::core::db::ReturnCode
///
///\see
///musik::core::db::ReturnCode
//////////////////////////////////////////
int Connection::Execute(const wchar_t* sql) {
sqlite3_stmt *stmt = NULL;
sqlite3_stmt *stmt = nullptr;

{
boost::mutex::scoped_lock lock(this->mutex);
int err = sqlite3_prepare16_v2(this->connection,sql,-1,&stmt,NULL);
if(err!=SQLITE_OK){

int err = sqlite3_prepare16_v2(this->connection, sql, -1, &stmt, nullptr);

if (err != SQLITE_OK) {
sqlite3_finalize(stmt);
return db::Error;
return Error;
}
}

// Execute the statement
int error = this->StepStatement(stmt);
if(error!=SQLITE_OK && error!=SQLITE_DONE){

if (error != SQLITE_OK && error != SQLITE_DONE) {
sqlite3_finalize(stmt);
return db::Error;
return Error;
}

sqlite3_reset(stmt);
sqlite3_finalize(stmt);
return musik::core::db::Okay;
return Okay;
}

void Connection::Checkpoint() {
sqlite3_wal_checkpoint(this->connection, nullptr);
}

//////////////////////////////////////////
///\brief
///Get the last inserted row ID
///
///\returns
///Last inserted row ID
///
///\see
///http://www.sqlite.org/c3ref/last_insert_rowid.html
//////////////////////////////////////////
int Connection::LastInsertedId(){
return (int) sqlite3_last_insert_rowid(this->connection);
}

//////////////////////////////////////////
///\brief
///Initializes the database.
///
///\param cache
///Size of the cache to use in kilobytes
///
///This will set all the initial PRAGMAS
//////////////////////////////////////////
void Connection::Initialize(unsigned int cache) {
// sqlite3_enable_shared_cache(1);
sqlite3_busy_timeout(this->connection, 10000);

sqlite3_exec(this->connection, "PRAGMA synchronous=OFF", NULL, NULL, NULL); // Not a critical DB. Sync set to OFF
sqlite3_exec(this->connection, "PRAGMA page_size=4096", NULL, NULL, NULL); // According to windows standard page size
sqlite3_exec(this->connection, "PRAGMA auto_vacuum=0", NULL, NULL, NULL); // No autovaccum.
sqlite3_exec(this->connection, "PRAGMA auto_vacuum=0", NULL, NULL, NULL); // No autovaccum.
sqlite3_exec(this->connection, "PRAGMA journal_mode=WAL", NULL, NULL, NULL); // Allow reading while writing (write-ahead-logging)
sqlite3_exec(this->connection, "PRAGMA synchronous=OFF", nullptr, nullptr, nullptr); // Not a critical DB. Sync set to OFF
sqlite3_exec(this->connection, "PRAGMA page_size=4096", nullptr, nullptr, nullptr); // According to windows standard page size
sqlite3_exec(this->connection, "PRAGMA auto_vacuum=0", nullptr, nullptr, nullptr); // No autovaccum.
sqlite3_exec(this->connection, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Allow reading while writing (write-ahead-logging)

if (cache != 0) {
// Divide by 4 to since the page_size is 4096
// Total cache is the same as page_size*cache_size
cache = cache / 4;
std::string cacheSize("PRAGMA cache_size=" + boost::lexical_cast<std::string>(cache));
sqlite3_exec(this->connection,cacheSize.c_str(), NULL, NULL, NULL); // size * 1.5kb = 6Mb cache
sqlite3_exec(this->connection,cacheSize.c_str(), nullptr, nullptr, nullptr); // size * 1.5kb = 6Mb cache
}

sqlite3_exec(this->connection, "PRAGMA case_sensitive_like=0", NULL, NULL, NULL); // More speed if case insensitive
sqlite3_exec(this->connection, "PRAGMA count_changes=0", NULL, NULL, NULL); // If set it counts changes on SQL UPDATE. More speed when not.
sqlite3_exec(this->connection, "PRAGMA legacy_file_format=OFF", NULL, NULL, NULL); // No reason to be backwards compatible :)
sqlite3_exec(this->connection, "PRAGMA temp_store=MEMORY", NULL, NULL, NULL); // MEMORY, not file. More speed.
sqlite3_exec(this->connection, "PRAGMA case_sensitive_like=0", nullptr, nullptr, nullptr); // More speed if case insensitive
sqlite3_exec(this->connection, "PRAGMA count_changes=0", nullptr, nullptr, nullptr); // If set it counts changes on SQL UPDATE. More speed when not.
sqlite3_exec(this->connection, "PRAGMA legacy_file_format=OFF", nullptr, nullptr, nullptr); // No reason to be backwards compatible :)
sqlite3_exec(this->connection, "PRAGMA temp_store=MEMORY", nullptr, nullptr, nullptr); // MEMORY, not file. More speed.
}

void Connection::Interrupt(){
void Connection::Interrupt() {
boost::mutex::scoped_lock lock(this->mutex);
sqlite3_interrupt(this->connection);
}
Expand All @@ -279,7 +196,7 @@ void Connection::UpdateReferenceCount(bool init) {
}
else {
--count;
if (count <= 0){
if (count <= 0) {
sqlite3_shutdown();
count = 0;
}
Expand Down
8 changes: 7 additions & 1 deletion src/core/db/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#pragma once

#include <core/config.h>
#include <core/db/dbconfig.h>
#include <core/db/Statement.h>
#include <core/db/ScopedTransaction.h>

Expand All @@ -48,6 +47,13 @@ struct sqlite3_stmt;

namespace musik { namespace core { namespace db {

typedef enum {
Okay = 0,
Row = 100,
Done = 101,
Error = 1
} ReturnCode;

class Connection : boost::noncopyable {
public:
Connection();
Expand Down
4 changes: 3 additions & 1 deletion src/core/db/ScopedTransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void ScopedTransaction::CommitAndRestart() {
this->Begin();
}

void ScopedTransaction::Begin(){
void ScopedTransaction::Begin() {
/* we use an IMMEDIATE transaction because we have write-ahead-logging
enabled on this instance, this generally results in faster queries
and also allows reads while writing */
Expand All @@ -81,4 +81,6 @@ void ScopedTransaction::End() {
this->connection->Checkpoint();
}
}

this->canceled = false;
}
Loading

0 comments on commit 0b059c8

Please sign in to comment.