Add checking of data
This commit is contained in:
112
server/src/checker.cpp
Normal file
112
server/src/checker.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
|
||||
#include "sql.hpp"
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include "values/DnaManager.hpp"
|
||||
|
||||
void checker()
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
sqlite3 *db;
|
||||
int rc = sql::open(DB_NAME, &db);
|
||||
if (rc)
|
||||
{
|
||||
fprintf(stderr, "Can't open database: %s\n", sql::errmsg(db));
|
||||
}
|
||||
|
||||
sqlite3_stmt *get_unchecked_stmt;
|
||||
sql::prepare_v2(db, get_unchecked, -1, &get_unchecked_stmt, NULL);
|
||||
|
||||
sqlite3_stmt *get_gen_stmt;
|
||||
sql::prepare_v2(db, get_gen, -1, &get_gen_stmt, NULL);
|
||||
|
||||
sqlite3_stmt *remove_gen_stmt;
|
||||
sql::prepare_v2(db, remove_gen, -1, &remove_gen_stmt, NULL);
|
||||
|
||||
sqlite3_stmt *set_checked_stmt;
|
||||
sql::prepare_v2(db, set_checked, -1, &set_checked_stmt, NULL);
|
||||
|
||||
while (true)
|
||||
{
|
||||
int64_t user_id = 0;
|
||||
if (sql::step(get_unchecked_stmt) != SQL_DONE)
|
||||
{
|
||||
int type = sql::column_type(get_unchecked_stmt, 0);
|
||||
if (type != SQL_NULL)
|
||||
{
|
||||
user_id = sql::column_int64(get_unchecked_stmt, 0);
|
||||
}
|
||||
}
|
||||
sql::reset(get_unchecked_stmt);
|
||||
if (user_id == 0)
|
||||
{
|
||||
std::this_thread::sleep_for(60s);
|
||||
continue;
|
||||
}
|
||||
|
||||
DnaManagerData data;
|
||||
data.id = user_id;
|
||||
data.randSeed = mrand::getState(data.id);
|
||||
data.queued = 0;
|
||||
data.showed = 0;
|
||||
data.generation = 0;
|
||||
data.vector.resize(NUM_PER_GEN);
|
||||
|
||||
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
||||
{
|
||||
DNA::newDna(&data.vector[i], &data.randSeed);
|
||||
}
|
||||
bool found_err = false;
|
||||
while (found_err != true)
|
||||
{
|
||||
sql::bind_int64(get_gen_stmt, 1, data.id);
|
||||
sql::bind_int64(get_gen_stmt, 2, data.generation);
|
||||
|
||||
bool new_gen = false;
|
||||
while (sql::step(get_gen_stmt) != SQL_DONE)
|
||||
{
|
||||
int64_t hash = sql::column_int64(get_gen_stmt, 0);
|
||||
int64_t pos = sql::column_int64(get_gen_stmt, 1);
|
||||
int64_t liked = sql::column_int64(get_gen_stmt, 2);
|
||||
UiUnit unit = DnaManager::next(&data);
|
||||
int64_t cal_hash = mrand::computeCRC32(unit.dna, sizeof(Dna));
|
||||
if ((unit.index != pos) || (hash != cal_hash))
|
||||
{
|
||||
found_err = true;
|
||||
sql::bind_int64(remove_gen_stmt, 1, data.id);
|
||||
sql::bind_int64(remove_gen_stmt, 2, data.generation);
|
||||
sql::step(remove_gen_stmt);
|
||||
sql::reset(remove_gen_stmt);
|
||||
break;
|
||||
}
|
||||
unit.liked = (Liked)liked;
|
||||
new_gen = DnaManager::like(unit, &data);
|
||||
}
|
||||
|
||||
if (!found_err)
|
||||
{
|
||||
sql::bind_int64(set_checked_stmt, 1, data.id);
|
||||
sql::bind_int64(set_checked_stmt, 2, data.generation);
|
||||
sql::step(set_checked_stmt);
|
||||
sql::reset(set_checked_stmt);
|
||||
}
|
||||
if (!new_gen)
|
||||
{
|
||||
found_err = true;
|
||||
sql::bind_int64(remove_gen_stmt, 1, data.id);
|
||||
sql::bind_int64(remove_gen_stmt, 2, data.generation);
|
||||
sql::step(remove_gen_stmt);
|
||||
sql::reset(remove_gen_stmt);
|
||||
}
|
||||
else
|
||||
{
|
||||
DnaManager::newGen(&data);
|
||||
}
|
||||
|
||||
sql::reset(get_gen_stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,11 @@
|
||||
#include "NetConst.hpp"
|
||||
#include "sql.hpp"
|
||||
#include "values/DnaManager.hpp"
|
||||
#include "checker.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <thread>
|
||||
|
||||
// use pthread rw_lock to lock db so you can safy clone .db file
|
||||
|
||||
@@ -105,6 +107,7 @@ void call(int sock, sockaddr_in newSocketInfo)
|
||||
int main()
|
||||
{
|
||||
sql::init();
|
||||
std::thread t(checker);
|
||||
// Bind the server to a port.
|
||||
int err = TcpSocket::listent("0.0.0.0", 8888, call);
|
||||
if (err < 0)
|
||||
|
||||
Reference in New Issue
Block a user