-
Notifications
You must be signed in to change notification settings - Fork 3
/
Generator.cs
280 lines (229 loc) · 11.3 KB
/
Generator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System.Net.Http;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Dapper;
using Microsoft.Data.Sqlite;
using MySqlConnector;
using SharpCompress.Compressors;
using SharpCompress.Compressors.BZip2;
namespace osu.Server.OnlineDbGenerator
{
public class Generator
{
/// <summary>
/// Path to the output online.db cache file.
/// </summary>
private static string sqliteFilePath => Environment.GetEnvironmentVariable("SQLITE_PATH") ?? "sqlite/online.db";
/// <summary>
/// Path to the bz2-compressed online.db cache file.
/// </summary>
private static string sqliteBz2FilePath => $"{sqliteFilePath}.bz2";
/// <summary>
/// Start generating the online.db file.
/// </summary>
public void Run()
{
using (var sqlite = getSqliteConnection())
using (var mysql = getMySqlConnection())
{
Console.WriteLine("Starting generator...");
createSchema(sqlite);
Console.WriteLine("Created schema.");
copyBeatmapSets(mysql, sqlite);
copyBeatmaps(mysql, sqlite);
Console.WriteLine("Compressing...");
using (var inStream = File.OpenRead(sqliteFilePath))
using (var outStream = File.OpenWrite(sqliteBz2FilePath))
using (var bz2 = new BZip2Stream(outStream, CompressionMode.Compress, false))
inStream.CopyTo(bz2);
if (Environment.GetEnvironmentVariable("S3_KEY") != null)
{
Console.WriteLine("Uploading to S3...");
using (var stream = File.OpenRead(sqliteBz2FilePath))
Upload("assets.ppy.sh", "client-resources/online.db.bz2", stream, stream.Length, "application/x-bzip2");
if (Environment.GetEnvironmentVariable("S3_PROXY_CACHE_PURGE_KEY") != null)
{
Console.WriteLine("Purging s3-nginx-proxy cache...");
PurgeCache("https://assets.ppy.sh/client-resources/online.db.bz2", Environment.GetEnvironmentVariable("S3_PROXY_CACHE_PURGE_KEY"));
}
}
}
Console.WriteLine("All done!");
}
/// <summary>
/// Create the schema inside the online.db SQLite database.
/// </summary>
/// <param name="sqlite"></param>
private void createSchema(SqliteConnection sqlite)
{
sqlite.Execute("CREATE TABLE `schema_version` (`number` smallint unsigned NOT NULL)");
sqlite.Execute("INSERT INTO `schema_version` (`number`) VALUES (2)");
sqlite.Execute(@"CREATE TABLE `osu_beatmapsets` (
`beatmapset_id` mediumint unsigned NOT NULL,
`submit_date` timestamp NOT NULL DEFAULT NULL,
`approved_date` timestamp NULL DEFAULT NULL,
`approved` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`beatmapset_id`))");
sqlite.Execute(@"CREATE TABLE `osu_beatmaps` (
`beatmap_id` mediumint unsigned NOT NULL,
`beatmapset_id` mediumint unsigned DEFAULT NULL,
`user_id` int unsigned NOT NULL DEFAULT '0',
`filename` varchar(150) DEFAULT NULL,
`checksum` varchar(32) DEFAULT NULL,
`approved` tinyint NOT NULL DEFAULT '0',
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`beatmap_id`))");
sqlite.Execute("CREATE INDEX `beatmapset_id` ON osu_beatmaps (`beatmapset_id`)");
sqlite.Execute("CREATE INDEX `filename` ON osu_beatmaps (`filename`)");
sqlite.Execute("CREATE INDEX `checksum` ON osu_beatmaps (`checksum`)");
sqlite.Execute("CREATE INDEX `user_id` ON osu_beatmaps (`user_id`)");
}
/// <summary>
/// Copy all beatmaps from online MySQL database to cache SQLite database.
/// </summary>
private void copyBeatmapSets(IDbConnection source, IDbConnection destination)
{
int total = getBeatmapSetCount(source);
Console.WriteLine($"Copying {total} beatmap sets...");
var start = DateTime.Now;
var beatmapSetsReader = source.Query<BeatmapSetRow>("SELECT beatmapset_id, approved, approved_date, submit_date FROM osu_beatmapsets WHERE approved > 0");
insertBeatmapSets(destination, beatmapSetsReader);
var timespan = (DateTime.Now - start).TotalMilliseconds;
int totalSqlite = getBeatmapSetCount(destination);
Console.WriteLine($"Copied beatmap sets in {timespan}ms! (mysql:{total} sqlite:{totalSqlite})");
if (totalSqlite != total)
{
throw new Exception($"Expected {total} beatmap sets, but found {totalSqlite} in sqlite! Aborting");
}
}
/// <summary>
/// Copy all beatmaps from online MySQL database to cache SQLite database.
/// </summary>
private void copyBeatmaps(IDbConnection source, IDbConnection destination)
{
int total = getBeatmapCount(source);
Console.WriteLine($"Copying {total} beatmaps...");
var start = DateTime.Now;
var beatmapsReader = source.Query<BeatmapRow>("SELECT beatmap_id, beatmapset_id, user_id, filename, checksum, approved, last_update FROM osu_beatmaps WHERE approved > 0");
insertBeatmaps(destination, beatmapsReader);
var timespan = (DateTime.Now - start).TotalMilliseconds;
int totalSqlite = getBeatmapCount(destination);
Console.WriteLine($"Copied beatmaps in {timespan}ms! (mysql:{total} sqlite:{totalSqlite})");
if (totalSqlite != total)
{
throw new Exception($"Expected {total} beatmaps, but found {totalSqlite} in sqlite! Aborting");
}
}
/// <summary>
/// Insert beatmap sets into the SQLite database.
/// </summary>
/// <param name="conn">Connection to insert beatmaps into.</param>
/// <param name="beatmaps">DbDataReader object (obtained from SelectBeatmaps) to insert beatmaps from.</param>
private void insertBeatmapSets(IDbConnection conn, IEnumerable<BeatmapSetRow> beatmapsets)
{
const string sql = "INSERT INTO osu_beatmapsets VALUES(@beatmapset_id, @submit_date, @approved_date, @approved)";
int processedItems = 0;
foreach (var beatmapset in beatmapsets)
{
conn.Execute(sql, beatmapset);
if (++processedItems % 50 == 0)
Console.WriteLine($"Copied {processedItems} beatmap sets...");
}
}
/// <summary>
/// Insert beatmaps into the SQLite database.
/// </summary>
/// <param name="conn">Connection to insert beatmaps into.</param>
/// <param name="beatmaps">DbDataReader object (obtained from SelectBeatmaps) to insert beatmaps from.</param>
private void insertBeatmaps(IDbConnection conn, IEnumerable<BeatmapRow> beatmaps)
{
const string sql = "INSERT INTO osu_beatmaps VALUES(@beatmap_id, @beatmapset_id, @user_id, @filename, @checksum, @approved, @last_update)";
int processedItems = 0;
foreach (var beatmap in beatmaps)
{
conn.Execute(sql, beatmap);
if (++processedItems % 50 == 0)
Console.WriteLine($"Copied {processedItems} beatmaps...");
}
}
/// <summary>
/// Count beatmap sets from MySQL or SQLite database.
/// </summary>
/// <param name="conn">Connection to fetch beatmaps from.</param>
private int getBeatmapSetCount(IDbConnection conn) => conn.QuerySingle<int>("SELECT COUNT(beatmapset_id) FROM osu_beatmapsets WHERE approved > 0");
/// <summary>
/// Count beatmaps from MySQL or SQLite database.
/// </summary>
/// <param name="conn">Connection to fetch beatmaps from.</param>
private int getBeatmapCount(IDbConnection conn) => conn.QuerySingle<int>("SELECT COUNT(beatmap_id) FROM osu_beatmaps WHERE approved > 0");
/// <summary>
/// Get a connection to the offline SQLite cache database.
/// </summary>
/// <param name="erase">Whether to start fresh.</param>
private static SqliteConnection getSqliteConnection(bool erase = true)
{
if (erase && File.Exists(sqliteFilePath))
File.Delete(sqliteFilePath);
Directory.CreateDirectory(Path.GetDirectoryName(sqliteFilePath));
var connection = new SqliteConnection($"Data Source={sqliteFilePath}");
connection.Open();
return connection;
}
/// <summary>
/// Get a connection to the online MySQL database.
/// </summary>
private static MySqlConnection getMySqlConnection()
{
string host = (Environment.GetEnvironmentVariable("DB_HOST") ?? "localhost");
string user = (Environment.GetEnvironmentVariable("DB_USER") ?? "root");
var connection = new MySqlConnection($"Server={host};Database=osu;User ID={user};ConnectionTimeout=5;ConnectionReset=false;Pooling=true;");
connection.Open();
return connection;
}
private static AmazonS3Client getClient(RegionEndpoint endpoint = null)
{
string key = Environment.GetEnvironmentVariable("S3_KEY");
string secret = Environment.GetEnvironmentVariable("S3_SECRET");
return new AmazonS3Client(new BasicAWSCredentials(key, secret), new AmazonS3Config
{
CacheHttpClient = true,
HttpClientCacheSize = 32,
RegionEndpoint = endpoint ?? RegionEndpoint.USWest1,
UseHttp = true,
ForcePathStyle = true
});
}
public static void Upload(string bucket, string key, Stream stream, long contentLength, string contentType = null)
{
using (var client = getClient())
{
client.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = key,
CannedACL = S3CannedACL.PublicRead,
Headers =
{
ContentLength = contentLength,
ContentType = contentType,
},
InputStream = stream
}).Wait();
}
}
public static void PurgeCache(string url, string key)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", key);
var request = client.DeleteAsync(url);
request.Wait();
request.Result.EnsureSuccessStatusCode();
}
}
}