Skip to content

Commit 2d4ddcf

Browse files
authored
Merge pull request #39 from torrust/revert-38-revert-34-development
Revert "Revert "Development""
2 parents 4cf4e95 + cef2016 commit 2d4ddcf

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Torrust Tracker is a lightweight but incredibly powerful and feature-rich BitTor
1414
* [X] Peer authentication using time-bound keys
1515
* [X] newTrackon check supported for both HTTP, UDP, where IPv4 and IPv6 is properly handled
1616
* [X] SQLite3 Persistent loading and saving of the torrent hashes and completed count
17+
* [X] MySQL support added as engine option
18+
* [X] Periodically saving added, interval can be configured
1719

1820
### Implemented BEPs
1921
* [BEP 3](https://www.bittorrent.org/beps/bep_0003.html): The BitTorrent Protocol

src/mysql_database.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,26 @@ impl Database for MysqlDatabase {
8080

8181
let mut db_transaction = conn.start_transaction(TxOpts::default()).map_err(|_| database::Error::DatabaseError)?;
8282

83+
let mut insert_vector= vec![];
84+
8385
for (info_hash, torrent_entry) in torrents {
8486
let (_seeders, completed, _leechers) = torrent_entry.get_stats();
85-
if db_transaction.exec_drop("INSERT INTO torrents (info_hash, completed) VALUES (UNHEX(?), ?) ON DUPLICATE KEY UPDATE completed = completed", (info_hash.to_string(), completed.to_string())).is_err() {
87+
insert_vector.push(format!("(UNHEX('{}'), {})", info_hash.to_string(), completed.to_string()));
88+
if insert_vector.len() == 1000 {
89+
let query = format!("INSERT INTO torrents (info_hash, completed) VALUES {} ON DUPLICATE KEY UPDATE completed = VALUES(completed)", insert_vector.join(","));
90+
if db_transaction.query_drop(query).is_err() {
91+
return Err(Error::InvalidQuery);
92+
}
93+
insert_vector.clear();
94+
}
95+
}
96+
97+
if insert_vector.len() != 0 {
98+
let query = format!("INSERT INTO torrents (info_hash, completed) VALUES {} ON DUPLICATE KEY UPDATE completed = VALUES(completed)", insert_vector.join(","));
99+
if db_transaction.query_drop(query).is_err() {
86100
return Err(Error::InvalidQuery);
87101
}
88-
debug!("INSERT INTO torrents (info_hash, completed) VALUES (UNHEX('{}'), {}) ON DUPLICATE KEY UPDATE completed = completed", info_hash.to_string(), completed.to_string());
102+
insert_vector.clear();
89103
}
90104

91105
if db_transaction.commit().is_err() {

src/sqlite_database.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ impl Database for SqliteDatabase {
8282

8383
for (info_hash, torrent_entry) in torrents {
8484
let (_seeders, completed, _leechers) = torrent_entry.get_stats();
85-
let _ = db_transaction.execute("INSERT OR REPLACE INTO torrents (info_hash, completed) VALUES (?, ?)", &[info_hash.to_string(), completed.to_string()]);
85+
let _ = db_transaction.execute("INSERT OR IGNORE INTO torrents (info_hash, completed) VALUES (?, ?)", &[info_hash.to_string(), completed.to_string()]);
86+
let _ = db_transaction.execute("UPDATE torrents SET completed = ? WHERE info_hash = ?", &[completed.to_string(), info_hash.to_string()]);
8687
}
8788

8889
let _ = db_transaction.commit();

src/tracker.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::BTreeMap;
33
use std::net::SocketAddr;
44
use std::sync::Arc;
55

6-
use log::info;
6+
use log::{debug, info};
77
use serde::{Deserialize, Serialize};
88
use serde;
99
use tokio::sync::{RwLock, RwLockReadGuard};
@@ -128,6 +128,7 @@ impl TorrentTracker {
128128
let torrents = self.database.load_persistent_torrent_data().await?;
129129

130130
for torrent in torrents {
131+
debug!("{:#?}", torrent);
131132
let _ = self.add_torrent(torrent.0, 0, torrent.1, 0).await;
132133
}
133134

@@ -307,41 +308,48 @@ impl TorrentTracker {
307308
let mut updates = self.updates.write().await;
308309
let mut updates_cloned: std::collections::HashMap<InfoHash, u32> = std::collections::HashMap::new();
309310
// let mut torrent_hashes: Vec<InfoHash> = Vec::new();
311+
info!("Copying updates to updates_cloned...");
310312
for (k, completed) in updates.iter() {
311-
updates_cloned.insert(*k, *completed);
313+
updates_cloned.insert(k.clone(), completed.clone());
312314
}
313315
updates.clear();
314316
drop(updates);
315317

316-
let mut shadows = self.shadow.write().await;
318+
info!("Copying updates_cloned into the shadow to overwrite...");
317319
for (k, completed) in updates_cloned.iter() {
320+
let mut shadows = self.shadow.write().await;
318321
if shadows.contains_key(k) {
319322
shadows.remove(k);
320323
}
321-
shadows.insert(*k, *completed);
324+
shadows.insert(k.clone(), completed.clone());
325+
drop(shadows);
322326
}
323327
drop(updates_cloned);
324328

325329
// We updated the shadow data from the updates data, let's handle shadow data as expected.
330+
info!("Handle shadow_copy to be updated into SQL...");
326331
let mut shadow_copy: BTreeMap<InfoHash, TorrentEntry> = BTreeMap::new();
332+
let shadows = self.shadow.read().await;
327333
for (infohash, completed) in shadows.iter() {
328-
shadow_copy.insert(*infohash, TorrentEntry {
334+
shadow_copy.insert(infohash.clone(), TorrentEntry {
329335
peers: Default::default(),
330-
completed: *completed,
336+
completed: completed.clone(),
331337
seeders: 0,
332338
});
333339
}
334-
335-
// Drop the lock
336340
drop(shadows);
337341

338342
// We will now save the data from the shadow into the database.
339343
// This should not put any strain on the server itself, other then the harddisk/ssd.
344+
info!("Start saving shadow data into SQL...");
340345
let result = self.database.save_persistent_torrent_data(&shadow_copy).await;
341346
if result.is_ok() {
347+
info!("Done saving data to SQL and succeeded, emptying shadow...");
342348
let mut shadow = self.shadow.write().await;
343349
shadow.clear();
344350
drop(shadow);
351+
} else {
352+
info!("Done saving data to SQL and failed, not emptying shadow...");
345353
}
346354
}
347355
}

0 commit comments

Comments
 (0)