mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-07-16 16:11:02 +03:00
Compare commits
3 Commits
3e891e19c3
...
6ce0ef7d55
Author | SHA1 | Date | |
---|---|---|---|
|
6ce0ef7d55 | ||
|
fd09ac496e | ||
|
9ca56bd910 |
@ -53,9 +53,12 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
await progressDialog.ShowDialog(window);
|
await progressDialog.ShowDialog(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
private const int BatchSize = 100;
|
private const int BatchSize = 500;
|
||||||
|
|
||||||
private async Task GenerateRandomData(int channelCount, int userCount, int messageCount, IProgressCallback callback) {
|
private async Task GenerateRandomData(int channelCount, int userCount, int messageCount, IProgressCallback callback) {
|
||||||
|
int batchCount = (messageCount + BatchSize - 1) / BatchSize;
|
||||||
|
await callback.Update("Adding messages in batches of " + BatchSize, 0, batchCount);
|
||||||
|
|
||||||
var rand = new Random();
|
var rand = new Random();
|
||||||
var server = new DHT.Server.Data.Server {
|
var server = new DHT.Server.Data.Server {
|
||||||
Id = RandomId(rand),
|
Id = RandomId(rand),
|
||||||
@ -87,9 +90,6 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
db.AddChannel(channel);
|
db.AddChannel(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
int batchCount = (messageCount + BatchSize - 1) / BatchSize;
|
|
||||||
await callback.Update("Adding messages in batches of 100", 0, batchCount);
|
|
||||||
|
|
||||||
var now = DateTimeOffset.Now;
|
var now = DateTimeOffset.Now;
|
||||||
int batchIndex = 0;
|
int batchIndex = 0;
|
||||||
|
|
||||||
@ -119,8 +119,8 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
|
|
||||||
db.AddMessages(messages);
|
db.AddMessages(messages);
|
||||||
|
|
||||||
messageCount -= 100;
|
messageCount -= BatchSize;
|
||||||
await callback.Update("Adding messages in batches of 100", ++batchIndex, batchCount);
|
await callback.Update("Adding messages in batches of " + BatchSize, ++batchIndex, batchCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,10 +36,13 @@ namespace DHT.Server.Database.Sqlite {
|
|||||||
|
|
||||||
private readonly Log log;
|
private readonly Log log;
|
||||||
private readonly SqliteConnectionPool pool;
|
private readonly SqliteConnectionPool pool;
|
||||||
|
private readonly SqliteMessageStatisticsThread messageStatisticsThread;
|
||||||
|
|
||||||
private SqliteDatabaseFile(string path, SqliteConnectionPool pool) {
|
private SqliteDatabaseFile(string path, SqliteConnectionPool pool) {
|
||||||
this.log = Log.ForType(typeof(SqliteDatabaseFile), System.IO.Path.GetFileName(path));
|
this.log = Log.ForType(typeof(SqliteDatabaseFile), System.IO.Path.GetFileName(path));
|
||||||
this.pool = pool;
|
this.pool = pool;
|
||||||
|
this.messageStatisticsThread = new SqliteMessageStatisticsThread(pool, UpdateMessageStatistics);
|
||||||
|
|
||||||
this.Path = path;
|
this.Path = path;
|
||||||
this.Statistics = new DatabaseStatistics();
|
this.Statistics = new DatabaseStatistics();
|
||||||
|
|
||||||
@ -51,6 +54,7 @@ namespace DHT.Server.Database.Sqlite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
|
messageStatisticsThread.Dispose();
|
||||||
pool.Dispose();
|
pool.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,7 +303,7 @@ namespace DHT.Server.Database.Sqlite {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tx.Commit();
|
tx.Commit();
|
||||||
UpdateMessageStatistics(conn);
|
messageStatisticsThread.RequestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int CountMessages(MessageFilter? filter = null) {
|
public int CountMessages(MessageFilter? filter = null) {
|
||||||
|
54
app/Server/Database/Sqlite/SqliteMessageStatisticsThread.cs
Normal file
54
app/Server/Database/Sqlite/SqliteMessageStatisticsThread.cs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using DHT.Server.Database.Sqlite.Utils;
|
||||||
|
|
||||||
|
namespace DHT.Server.Database.Sqlite {
|
||||||
|
sealed class SqliteMessageStatisticsThread : IDisposable {
|
||||||
|
private readonly SqliteConnectionPool pool;
|
||||||
|
private readonly Action<ISqliteConnection> action;
|
||||||
|
|
||||||
|
private readonly CancellationTokenSource cancellationTokenSource = new();
|
||||||
|
private readonly CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
private readonly AutoResetEvent requestEvent = new (false);
|
||||||
|
|
||||||
|
public SqliteMessageStatisticsThread(SqliteConnectionPool pool, Action<ISqliteConnection> action) {
|
||||||
|
this.pool = pool;
|
||||||
|
this.action = action;
|
||||||
|
|
||||||
|
this.cancellationToken = cancellationTokenSource.Token;
|
||||||
|
|
||||||
|
var thread = new Thread(RunThread) {
|
||||||
|
Name = "DHT message statistics thread",
|
||||||
|
IsBackground = true
|
||||||
|
};
|
||||||
|
thread.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() {
|
||||||
|
try {
|
||||||
|
cancellationTokenSource.Cancel();
|
||||||
|
} catch (ObjectDisposedException) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RequestUpdate() {
|
||||||
|
try {
|
||||||
|
requestEvent.Set();
|
||||||
|
} catch (ObjectDisposedException) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RunThread() {
|
||||||
|
try {
|
||||||
|
while (!cancellationToken.IsCancellationRequested) {
|
||||||
|
if (requestEvent.WaitOne(TimeSpan.FromMilliseconds(100))) {
|
||||||
|
using var conn = pool.Take();
|
||||||
|
action(conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
cancellationTokenSource.Dispose();
|
||||||
|
requestEvent.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,7 +24,14 @@ namespace DHT.Server.Database.Sqlite.Utils {
|
|||||||
for (int i = 0; i < poolSize; i++) {
|
for (int i = 0; i < poolSize; i++) {
|
||||||
var conn = new SqliteConnection(connectionString);
|
var conn = new SqliteConnection(connectionString);
|
||||||
conn.Open();
|
conn.Open();
|
||||||
free.Add(new PooledConnection(this, conn));
|
|
||||||
|
var pooledConn = new PooledConnection(this, conn);
|
||||||
|
|
||||||
|
using (var cmd = pooledConn.Command("PRAGMA journal_mode=WAL")) {
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
free.Add(pooledConn);
|
||||||
}
|
}
|
||||||
|
|
||||||
used = new List<PooledConnection>(poolSize);
|
used = new List<PooledConnection>(poolSize);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user