Switch to storing in sqlite
This commit is contained in:
parent
477af8b063
commit
cb743d15d9
@ -1,5 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
const char create_table[] = "CREATE TABLE IF NOT EXISTS prim_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, GEN INTEGER, HASH INTEGER, POS INTEGER, LIKED INTEGER);";
|
||||||
|
|
||||||
|
const char create_index[] = "CREATE INDEX IF NOT EXISTS idx_usrer_id ON prim_table (USER_ID);";
|
||||||
|
|
||||||
|
const char insert_data[] = "INSERT INTO prim_table (USER_ID, GEN, HASH, POS, LIKED) VALUES(?, ?, ?, ?, ?);";
|
||||||
|
|
||||||
|
const char max_gen[] = "SELECT MAX(GEN) FROM prim_table WHERE USER_ID = ?;";
|
||||||
|
|
||||||
|
constexpr char DB_NAME[] = "data.db";
|
||||||
|
|
||||||
namespace sql
|
namespace sql
|
||||||
{
|
{
|
||||||
void init();
|
void init();
|
||||||
|
@ -53,13 +53,13 @@ int main()
|
|||||||
TcpSocket::sendt(sock, &message, sizeof(Message));
|
TcpSocket::sendt(sock, &message, sizeof(Message));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
idf.open(filename, std::ios_base::binary | std::ios_base::in);
|
std::ifstream sfile(filename, std::ios_base::binary | std::ios_base::in);
|
||||||
|
|
||||||
std::vector<NetUnit> net;
|
std::vector<NetUnit> net;
|
||||||
net.resize(NUM_PER_GEN);
|
net.resize(NUM_PER_GEN);
|
||||||
|
|
||||||
idf.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit));
|
sfile.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit));
|
||||||
|
|
||||||
message.mess = Mess::RES_OK;
|
message.mess = Mess::RES_OK;
|
||||||
message.data = NUM_PER_GEN * sizeof(NetUnit);
|
message.data = NUM_PER_GEN * sizeof(NetUnit);
|
||||||
|
@ -2,21 +2,18 @@
|
|||||||
#include "NetConst.hpp"
|
#include "NetConst.hpp"
|
||||||
#include "sql.hpp"
|
#include "sql.hpp"
|
||||||
#include "values/DnaManager.hpp"
|
#include "values/DnaManager.hpp"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
|
#include <sqlite3.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
// use pthread rw_lock to lock db so you can safy clone .db file
|
// use pthread rw_lock to lock db so you can safy clone .db file
|
||||||
|
|
||||||
typedef std::vector<NetUnit> NetList;
|
|
||||||
typedef std::vector<NetList> UserList;
|
|
||||||
|
|
||||||
std::map<uint64_t, UserList> data;
|
|
||||||
|
|
||||||
// When a new client connected:
|
// When a new client connected:
|
||||||
void call(int sock, sockaddr_in newSocketInfo)
|
void call(int sock, sockaddr_in newSocketInfo)
|
||||||
{
|
{
|
||||||
std::cout << "new User" << std::endl;
|
printf("new User");
|
||||||
|
|
||||||
int64_t conf, id;
|
int64_t conf, id;
|
||||||
Message message;
|
Message message;
|
||||||
@ -36,21 +33,41 @@ void call(int sock, sockaddr_in newSocketInfo)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto found = data.find(id);
|
bool ok = true;
|
||||||
if (found == data.end())
|
|
||||||
|
sqlite3 *db;
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
|
int rc = sqlite3_open(DB_NAME, &db);
|
||||||
|
if (rc)
|
||||||
{
|
{
|
||||||
UserList list;
|
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||||
data.insert(std::pair<uint64_t, UserList>(id, list));
|
ok = false;
|
||||||
found = data.find(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true)
|
sqlite3_prepare_v2(db, max_gen, -1, &stmt, NULL);
|
||||||
|
sqlite3_bind_int64(stmt, 1, id);
|
||||||
|
int64_t gen = 0;
|
||||||
|
while (sqlite3_step(stmt) != SQLITE_DONE)
|
||||||
{
|
{
|
||||||
size_t gen = found->second.size();
|
int num_cols = sqlite3_column_count(stmt);
|
||||||
|
|
||||||
|
int type = sqlite3_column_type(stmt, 0);
|
||||||
|
|
||||||
|
if(type == SQLITE_NULL) break;
|
||||||
|
gen = sqlite3_column_int64(stmt, 0);
|
||||||
|
gen++;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
|
||||||
|
sqlite3_prepare_v2(db, insert_data, -1, &stmt, NULL);
|
||||||
|
|
||||||
|
while (ok)
|
||||||
|
{
|
||||||
message.mess = Mess::REQ_SEND_GEN;
|
message.mess = Mess::REQ_SEND_GEN;
|
||||||
message.data = gen;
|
message.data = gen;
|
||||||
|
|
||||||
TcpSocket::sendt(sock, &message, sizeof(Message));
|
TcpSocket::sendt(sock, &message, sizeof(Message));
|
||||||
TcpSocket::recvt(sock, &message, sizeof(Message));
|
TcpSocket::recvt(sock, &message, sizeof(Message));
|
||||||
|
|
||||||
@ -59,16 +76,28 @@ void call(int sock, sockaddr_in newSocketInfo)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetList list;
|
std::vector<NetUnit> list;
|
||||||
list.resize(NUM_PER_GEN);
|
list.resize(NUM_PER_GEN);
|
||||||
|
|
||||||
TcpSocket::recvt(sock, list.data(), NUM_PER_GEN * sizeof(NetUnit));
|
TcpSocket::recvt(sock, list.data(), NUM_PER_GEN * sizeof(NetUnit));
|
||||||
found->second.push_back(list);
|
|
||||||
|
for(size_t i = 0; i < list.size(); i++){
|
||||||
|
sqlite3_bind_int64(stmt, 1, id);
|
||||||
|
sqlite3_bind_int64(stmt, 2, gen);
|
||||||
|
sqlite3_bind_int64(stmt, 3, list[i].hash);
|
||||||
|
sqlite3_bind_int64(stmt, 4, list[i].index);
|
||||||
|
sqlite3_bind_int64(stmt, 5, list[i].liked);
|
||||||
|
sqlite3_step(stmt);
|
||||||
|
sqlite3_reset(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
gen++;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("id: %ld, size: %ld\n", id, found->second.size());
|
sqlite3_finalize(stmt);
|
||||||
|
|
||||||
std::cout << "del USER" << std::endl;
|
sqlite3_close(db);
|
||||||
|
printf("del USER");
|
||||||
TcpSocket::closet(sock);
|
TcpSocket::closet(sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,23 +5,6 @@
|
|||||||
|
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
|
|
||||||
const char create_table[] = "CREATE TABLE IF NOT EXISTS prim_table ( ID INTEGER PRIMARY KEY, GEN INTEGER, HASH INTEGER, POS INTEGER, LIKED INTEGER);";
|
|
||||||
|
|
||||||
const char create_index[] = "CREATE INDEX IF NOT EXISTS idx_id ON prim_table (ID);";
|
|
||||||
|
|
||||||
constexpr char DB_NAME[] = "data.db";
|
|
||||||
|
|
||||||
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < argc; i++)
|
|
||||||
{
|
|
||||||
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void sql::init()
|
void sql::init()
|
||||||
{
|
{
|
||||||
sqlite3_initialize();
|
sqlite3_initialize();
|
||||||
@ -43,7 +26,7 @@ void sql::init()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Execute SQL statement */
|
/* Execute SQL statement */
|
||||||
rc = sqlite3_exec(db, create_table, callback, 0, &zErrMsg);
|
rc = sqlite3_exec(db, create_table, nullptr, 0, &zErrMsg);
|
||||||
|
|
||||||
if (rc != SQLITE_OK)
|
if (rc != SQLITE_OK)
|
||||||
{
|
{
|
||||||
@ -56,7 +39,7 @@ void sql::init()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Execute SQL statement */
|
/* Execute SQL statement */
|
||||||
rc = sqlite3_exec(db, create_index, callback, 0, &zErrMsg);
|
rc = sqlite3_exec(db, create_index, nullptr, 0, &zErrMsg);
|
||||||
|
|
||||||
if (rc != SQLITE_OK)
|
if (rc != SQLITE_OK)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user