void write_db_users() { m_write_db_users_time = srv_time(); if (!m_use_sql) return; if (!m_torrents_users_updates_buffer.empty()) { m_torrents_users_updates_buffer.pop_back(); async_query("insert into " + db_name("files_users") + " (active, announced, completed, downloaded, `left`, uploaded, mtime, fid, uid) values " + m_torrents_users_updates_buffer + " on duplicate key update" + " active = values(active)," + " announced = announced + values(announced)," + " completed = completed + values(completed)," + " downloaded = downloaded + values(downloaded)," + " `left` = values(`left`)," + " uploaded = uploaded + values(uploaded)," + " mtime = values(mtime)"); m_torrents_users_updates_buffer.erase(); } async_query("update " + db_name("files_users") + " set active = 0 where mtime < unix_timestamp() - 60 * 60"); if (!m_users_updates_buffer.empty()) { m_users_updates_buffer.pop_back(); async_query("insert into " + db_name("users") + " (downloaded, uploaded, " + db_name("uid") + ") values " + m_users_updates_buffer + " on duplicate key update" + " downloaded = downloaded + values(downloaded)," + " uploaded = uploaded + values(uploaded)"); m_users_updates_buffer.erase(); } }
void write_db_torrents() { m_write_db_torrents_time = srv_time(); if (!m_use_sql) return; try { std::string buffer; while (1) { for (auto& i : m_torrents) { t_torrent& file = i.second; if (!file.dirty) continue; if (!file.fid) { Csql_query(m_database, "insert into @files (info_hash, mtime, ctime) values (?, unix_timestamp(), unix_timestamp())")(i.first).execute(); file.fid = m_database.insert_id(); } buffer += Csql_query(m_database, "(?,?,?,?),")(file.leechers)(file.seeders)(file.completed)(file.fid).read(); file.dirty = false; if (buffer.size() > 255 << 10) break; } if (buffer.empty()) break; buffer.pop_back(); async_query("insert into " + db_name("files") + " (" + db_name("leechers") + ", " + db_name("seeders") + ", " + db_name("completed") + ", " + db_name("fid") + ") values " + buffer + " on duplicate key update" + " " + db_name("leechers") + " = values(" + db_name("leechers") + ")," + " " + db_name("seeders") + " = values(" + db_name("seeders") + ")," + " " + db_name("completed") + " = values(" + db_name("completed") + ")," + " mtime = unix_timestamp()"); buffer.clear(); } } catch (bad_query&) { } if (!m_announce_log_buffer.empty()) { m_announce_log_buffer.pop_back(); async_query("insert delayed into " + db_name("announce_log") + " (ipa, port, event, info_hash, peer_id, downloaded, left0, uploaded, uid, mtime) values " + m_announce_log_buffer); m_announce_log_buffer.erase(); } if (!m_scrape_log_buffer.empty()) { m_scrape_log_buffer.pop_back(); async_query("insert delayed into " + db_name("scrape_log") + " (ipa, uid, mtime) values " + m_scrape_log_buffer); m_scrape_log_buffer.erase(); } }
int main () { RpcAsyncCall async_call; RPC_STATUS status; DWORD wait_result; unsigned char *message; int counter = 0; int cancelled; rpc_client_bind (&async_rpc_interface_handle, DEFAULT_ENDPOINT, RPC_SYSTEM_WIDE); printf ("Client has binding to %s\n", DEFAULT_ENDPOINT); message = NULL; sync_query ("Joe", &message); printf ("%x -> %s (%i)\n", (int)message, message, strlen(message)); MIDL_user_free (message); message = NULL; rpc_async_call_init (&async_call); async_query (&async_call, "Frank", &message); rpc_async_call_complete (&async_call, &counter); printf ("%x -> %s (%i) - %i\n", (int)message, message, strlen(message), counter); MIDL_user_free (message); rpc_async_call_init (&async_call); black_hole (&async_call); printf ("client: Called black_hole ()\n"); cancelled = rpc_async_call_cancel (&async_call); printf ("client: Cancel completed: %i\n", cancelled); rpc_client_unbind (&async_rpc_interface_handle); printf ("client: success\n"); }
// 异步删除 void Client::async_delete(const char *dbname, const std::string &statement, CallBack &&cb) { async_query(dbname, ActionType::kDelete, statement, std::forward<CallBack>(cb)); }