From 27284444dd43c4b908f0edadc8de4bea024c0f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 19 Nov 2019 00:43:27 +1100 Subject: [PATCH] Optimization on SQLite3 internal library --- include/sqlite3db.h | 197 ++++--------------------------------- lib/sqlite3db.cpp | 232 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 229 insertions(+), 200 deletions(-) diff --git a/include/sqlite3db.h b/include/sqlite3db.h index 79816d5e7..d6d569913 100644 --- a/include/sqlite3db.h +++ b/include/sqlite3db.h @@ -11,109 +11,19 @@ class SQLite3_row { int *sizes; char **fields; char *data; - SQLite3_row(int c) { - sizes=(int *)malloc(sizeof(int)*c); - fields=(char **)malloc(sizeof(char *)*c); - memset(fields,0,sizeof(char *)*c); - cnt=c; - data=NULL; - ds=0; - }; - unsigned long long get_size() { - unsigned long long s = sizeof(SQLite3_row); - s += cnt * sizeof(int); - s += cnt * sizeof(char *); - s += ds; - return s; - }; - ~SQLite3_row() { - free(fields); - free(sizes); - if (data) { - free(data); - } - }; - void add_fields(sqlite3_stmt *stmt) { - int i; - int t; - int data_size=0; - int data_ptr=0; - // compute the length - for (i=0;i column_definition; std::vector rows; - SQLite3_result() { - columns=0; - }; - unsigned long long get_size() { - unsigned long long s = sizeof(SQLite3_result); - s += column_definition.size() * sizeof(SQLite3_column *); - s += rows.size() * sizeof(SQLite3_row *); - for (std::vector::iterator it = column_definition.begin() ; it != column_definition.end(); ++it) { - SQLite3_column *r=*it; - s+= sizeof(SQLite3_column) + strlen(r->name); - } - for (std::vector::iterator it = rows.begin() ; it != rows.end(); ++it) { - SQLite3_row *r=*it; - s += r->get_size(); - } - return s; - }; - void add_column_definition(int a, const char *b) { - SQLite3_column *cf=new SQLite3_column(a,b); - column_definition.push_back(cf); - //columns++; - }; - int add_row(sqlite3_stmt *stmt) { - int rc=sqlite3_step(stmt); - if (rc!=SQLITE_ROW) return rc; - SQLite3_row *row=new SQLite3_row(columns); - row->add_fields(stmt); - rows.push_back(row); - rows_count++; - return SQLITE_ROW; - }; - int add_row(char **_fields) { - SQLite3_row *row=new SQLite3_row(columns); - row->add_fields(_fields); - if (enabled_mutex) { - pthread_mutex_lock(&m); - } - rows.push_back(row); - rows_count++; - if (enabled_mutex) { - pthread_mutex_unlock(&m); - } - return SQLITE_ROW; - }; - int add_row(SQLite3_row *old_row) { - SQLite3_row *row=new SQLite3_row(columns); - row->add_fields(old_row->fields); - rows.push_back(row); - rows_count++; - return SQLITE_ROW; - }; - SQLite3_result(sqlite3_stmt *stmt) { - rows_count=0; - columns=sqlite3_column_count(stmt); - for (int i=0; i::iterator it = column_definition.begin() ; it != column_definition.end(); ++it) { - SQLite3_column *c=*it; - delete c; - } - for (std::vector::iterator it = rows.begin() ; it != rows.end(); ++it) { - SQLite3_row *r=*it; - delete r; - } - }; + SQLite3_result(); + SQLite3_result(SQLite3_result *); + unsigned long long get_size(); + void add_column_definition(int a, const char *b); + int add_row(sqlite3_stmt *stmt); + int add_row(char **_fields); + int add_row(SQLite3_row *old_row); + SQLite3_result(sqlite3_stmt *stmt); + SQLite3_result(int num_columns, bool en_mutex=false); + ~SQLite3_result(); void dump_to_stderr(); }; @@ -213,11 +54,7 @@ class SQLite3DB { private: char *url; sqlite3 *db; -#ifdef PROXYSQL_SQLITE3DB_PTHREAD_MUTEX pthread_rwlock_t rwlock; -#else - rwlock_t rwlock; -#endif public: char *get_url() const { return url; } sqlite3 *get_db() const { return db; } diff --git a/lib/sqlite3db.cpp b/lib/sqlite3db.cpp index 546c58a1a..b97b1005e 100644 --- a/lib/sqlite3db.cpp +++ b/lib/sqlite3db.cpp @@ -4,15 +4,117 @@ #define USLEEP_SQLITE_LOCKED 100 +SQLite3_column::SQLite3_column(int a, const char *b) { + datatype=a; + if (b) { + name=strdup(b); + } else { + name=strdup((char *)""); + } +} + +SQLite3_column::~SQLite3_column() { + free(name); +} + + +SQLite3_row::SQLite3_row(int c) { + sizes=(int *)malloc(sizeof(int)*c); + fields=(char **)malloc(sizeof(char *)*c); + memset(fields,0,sizeof(char *)*c); + cnt=c; + data=NULL; + ds=0; +} + +unsigned long long SQLite3_row::get_size() { + unsigned long long s = sizeof(SQLite3_row); + s += cnt * sizeof(int); + s += cnt * sizeof(char *); + s += ds; + return s; +} + +SQLite3_row::~SQLite3_row() { + free(fields); + free(sizes); + if (data) { + free(data); + } +} + +void SQLite3_row::add_fields(sqlite3_stmt *stmt) { + int i; + int t; + int data_size=0; + int data_ptr=0; + // compute the length + for (i=0;icolumns; + if (src->enabled_mutex) { + pthread_mutex_init(&m,NULL); + enabled_mutex = true; + } else { + enabled_mutex = false; + } + for (std::vector::iterator it = src->column_definition.begin() ; it != src->column_definition.end(); ++it) { + SQLite3_column *r=*it; + add_column_definition(SQLITE_TEXT,r->name); + } + for (std::vector::iterator it = src->rows.begin() ; it != src->rows.end(); ++it) { + SQLite3_row *r=*it; + add_row(r); + } +} + +unsigned long long SQLite3_result::get_size() { + unsigned long long s = sizeof(SQLite3_result); + s += column_definition.size() * sizeof(SQLite3_column *); + s += rows.size() * sizeof(SQLite3_row *); + for (std::vector::iterator it = column_definition.begin() ; it != column_definition.end(); ++it) { + SQLite3_column *r=*it; + s+= sizeof(SQLite3_column) + strlen(r->name); + } + for (std::vector::iterator it = rows.begin() ; it != rows.end(); ++it) { + SQLite3_row *r=*it; + s += r->get_size(); + } + return s; +} + +void SQLite3_result::add_column_definition(int a, const char *b) { + SQLite3_column *cf=new SQLite3_column(a,b); + column_definition.push_back(cf); +} + +int SQLite3_result::add_row(sqlite3_stmt *stmt) { + int rc=sqlite3_step(stmt); + if (rc!=SQLITE_ROW) return rc; + SQLite3_row *row=new SQLite3_row(columns); + row->add_fields(stmt); + rows.push_back(row); + rows_count++; + return SQLITE_ROW; +} + +int SQLite3_result::add_row(char **_fields) { + SQLite3_row *row=new SQLite3_row(columns); + row->add_fields(_fields); + if (enabled_mutex) { + pthread_mutex_lock(&m); + } + rows.push_back(row); + rows_count++; + if (enabled_mutex) { + pthread_mutex_unlock(&m); + } + return SQLITE_ROW; +} + +int SQLite3_result::add_row(SQLite3_row *old_row) { + SQLite3_row *row=new SQLite3_row(columns); + row->add_fields(old_row->fields); + rows.push_back(row); + rows_count++; + return SQLITE_ROW; +} + +SQLite3_result::SQLite3_result(sqlite3_stmt *stmt) { + rows_count=0; + columns=sqlite3_column_count(stmt); + for (int i=0; i::iterator it = column_definition.begin() ; it != column_definition.end(); ++it) { + SQLite3_column *c=*it; + delete c; + } + for (std::vector::iterator it = rows.begin() ; it != rows.end(); ++it) { + SQLite3_row *r=*it; + delete r; + } +} + +SQLite3_result::SQLite3_result() { + columns=0; +}