-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportStackExchangeDump.cs
493 lines (457 loc) · 16.2 KB
/
ImportStackExchangeDump.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
using System.Xml;
using System.Xml.Serialization;
using SharpCompress.Archives;
using Raven.Client.Documents;
using System.Diagnostics;
using Raven.Client.Documents.Session;
using var store = new DocumentStore
{
Urls = new[] { "http://127.0.0.1:8080" },
Database = "Hugin",
Conventions =
{
MaxNumberOfRequestsPerSession = int.MaxValue
}
}.Initialize();
var sp = Stopwatch.StartNew();
string file = Environment.GetCommandLineArgs()[1];
string prefix = Environment.GetCommandLineArgs()[2];
using var archive = ArchiveFactory.Open(file);
var answerToQuestion = new Dictionary<int, int>();
var state = new State { Session = store.OpenAsyncSession() };
using (var users = archive.Entries.Single(x => x.Key == "Users.xml").OpenEntryStream())
{
foreach(var user in ImportFrom<xUser>(GetReader(users)))
{
await Import(user, store, answerToQuestion, state);
}
}
using (var posts = archive.Entries.Single(x => x.Key == "Posts.xml").OpenEntryStream())
{
foreach (var post in ImportFrom<xPost>(GetReader(posts)))
{
if (post.PostTypeId == 2)
{
answerToQuestion[post.Id] = post.ParentId;
}
if (post.PostTypeId is 1 or 2)
{
await Import(post, store, answerToQuestion, state);
}
}
}
using (var posts = archive.Entries.Single(x => x.Key == "Comments.xml").OpenEntryStream())
{
foreach (var comment in ImportFrom<xComment>(GetReader(posts)))
{
await Import(comment, store, answerToQuestion, state);
}
}
using (var comments = archive.Entries.Single(x => x.Key == "Posts.xml").OpenEntryStream())
{
foreach (var comment in ImportFrom<xComment>(GetReader(comments)))
{
await Import(comment, store, answerToQuestion, state);
}
}
using (var comments = archive.Entries.Single(x => x.Key == "Badges.xml").OpenEntryStream())
{
foreach (var badge in ImportFrom<xBadge>(GetReader(comments)))
{
await Import(badge, store, answerToQuestion, state);
}
}
await state.Session.SaveChangesAsync();
Console.WriteLine(sp.Elapsed);
async Task Import(object item, IDocumentStore store, Dictionary<int, int> answerToQuestion, State state)
{
if (state.Writes > 1024)
{
await state.Session.SaveChangesAsync();
state.Session.Dispose();
state.Session = store.OpenAsyncSession();
state.Writes = 0;
}
state.Writes++;
switch (item)
{
case xPost post:
{
switch (post.PostTypeId)
{
case 1: // question
{
var q = new Question
{
Id = "questions/" + prefix+"/" + post.Id,
Community = prefix,
AcceptedAnswerId = post.AcceptedAnswerId,
Answers = new(),
Body = post.Body,
Comments = new(),
CreationDate = post.CreationDate,
FavoriteCount = post.FavoriteCount,
LastEditDate = post.LastEditDate == DateTime.MinValue ? null : post.LastEditDate,
LastEditor = post.LastEditorUserId == 0 ? null : "users/" +prefix +"/" + post.LastEditorUserId,
Owner = "users/" + prefix + "/" + post.OwnerUserId,
Score = post.Score,
Tags = post.Tags.Split(new[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries),
Title = post.Title,
ViewCount = post.ViewCount
};
if (state.OutOfOrders.Remove(post.Id, out var ooo))
{
q.Answers = ooo;
}
await state.Session.StoreAsync(q, "questions/" + prefix + "/" + post.Id);
break;
}
case 2:// answer
{
var a = new Answer
{
Body = post.Body,
Comments = new(),
CreationDate = post.CreationDate,
Id = post.Id,
LastEditDate = post.LastEditDate == DateTime.MinValue ? null : post.LastEditDate,
LastEditor = post.LastEditorUserId == 0 ? null : "users/" + prefix + "/" + post.LastEditorUserId,
Owner = "users/" + prefix + "/" + post.OwnerUserId,
Score = post.Score,
ViewCount = post.ViewCount
};
answerToQuestion[post.Id] = post.ParentId;
var q = await state.Session.LoadAsync<Question>("questions/" + prefix + "/" + post.ParentId);
if (q == null)
{
if (state.OutOfOrders.TryGetValue(post.ParentId, out var ooo) == false)
{
state.OutOfOrders[post.ParentId] = ooo = new List<Answer>();
}
ooo.Add(a);
return;
}
q.Answers.Add(a);
break;
}
}
break;
}
case xComment comment:
{
var c = new Comment
{
CreationDate = comment.CreationDate,
Id = comment.Id,
Score = comment.Score,
Text = comment.Text,
User = "users/" + prefix + "/" + comment.UserId,
};
if (answerToQuestion.TryGetValue(comment.PostId, out var qId))
{
var q = await state.Session.LoadAsync<Question>("questions/" + prefix + "/" + qId);
if (q == null)
{
Console.WriteLine("Missing question's answer: " + qId + " -> " + comment.PostId);
return;
}
try
{
q.Answers.Single(a => a.Id == comment.PostId).Comments.Add(c);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine(comment.PostId + " -> " + qId);
}
}
else
{
var q = await state.Session.LoadAsync<Question>("questions/" + prefix + "/" + comment.PostId);
if (q == null)
{
Console.WriteLine("Missing question: " + comment.PostId);
return;
}
q.Comments.Add(c);
}
break;
}
case xUser user:
{
var u = new User
{
Community = prefix,
CreationDate = user.CreationDate,
Id = "users/" + prefix + "/" + user.Id,
AboutMe = user.AboutMe,
DisplayName = user.DisplayName,
DownVotes = user.DownVotes,
LastAccessDate = user.LastAccessDate,
Reputation = user.Reputation,
UpVotes = user.UpVotes,
Views = user.UpVotes,
Badges = new()
};
await state.Session.StoreAsync(u, "users/" + prefix + "/" + user.Id);
break;
}
case xBadge badge:
{
var u = await state.Session.LoadAsync<User>("users/" + prefix + "/" + badge.UserId);
if (u == null) return;
u.Badges.Add(new Badge
{
Date = badge.Date,
Name = badge.Name,
Rank = badge.Class switch
{
1 => "Gold",
2 => "Silver",
3 => "Bronze",
_ => null
},
TagBased = bool.Parse(badge.TagBased)
});
break;
}
}
}
IEnumerable<T> ImportFrom<T>(XmlReader reader)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
while (reader.Read())
{
if (reader.NodeType != XmlNodeType.Element)
continue;
yield return (T)serializer.Deserialize(reader);
}
}
static XmlReader GetReader(Stream stream)
{
var reader = XmlReader.Create(stream);
reader.MoveToContent();
return reader;
}
public class State
{
public IAsyncDocumentSession Session;
public int Writes;
public Dictionary<int, List<Answer>> OutOfOrders = new();
}
[XmlRoot(ElementName = "row")]
public class xUser
{
[Newtonsoft.Json.JsonIgnore]
[XmlAttribute(AttributeName = "Id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "Reputation")]
public int Reputation { get; set; }
[XmlAttribute(AttributeName = "CreationDate")]
public DateTime CreationDate { get; set; }
[XmlAttribute(AttributeName = "DisplayName")]
public string DisplayName { get; set; }
[XmlAttribute(AttributeName = "LastAccessDate")]
public DateTime LastAccessDate { get; set; }
[XmlAttribute(AttributeName = "AboutMe")]
public string AboutMe { get; set; }
[XmlAttribute(AttributeName = "Views")]
public int Views { get; set; }
[XmlAttribute(AttributeName = "UpVotes")]
public int UpVotes { get; set; }
[XmlAttribute(AttributeName = "DownVotes")]
public int DownVotes { get; set; }
}
[XmlRoot(ElementName = "row")]
public class xTag
{
[XmlAttribute(AttributeName = "Id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "TagName")]
public string TagName { get; set; }
[XmlAttribute(AttributeName = "Count")]
public int Count { get; set; }
[XmlAttribute(AttributeName = "ExcerptPostId")]
public int ExcerptPostId { get; set; }
[XmlAttribute(AttributeName = "WikiPostId")]
public int WikiPostId { get; set; }
}
[XmlRoot(ElementName = "row")]
public class xBadge
{
[XmlAttribute(AttributeName = "Id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "UserId")]
public int UserId { get; set; }
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Date")]
public DateTime Date { get; set; }
[XmlAttribute(AttributeName = "Class")]
public int Class { get; set; }
[XmlAttribute(AttributeName = "TagBased")]
public string TagBased { get; set; }
}
[XmlRoot(ElementName = "row")]
public class xComment
{
[XmlAttribute(AttributeName = "Id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "PostId")]
public int PostId { get; set; }
[XmlAttribute(AttributeName = "Score")]
public int Score { get; set; }
[XmlAttribute(AttributeName = "Text")]
public string Text { get; set; }
[XmlAttribute(AttributeName = "CreationDate")]
public DateTime CreationDate { get; set; }
[XmlAttribute(AttributeName = "UserId")]
public int UserId { get; set; }
[XmlAttribute(AttributeName = "ContentLicense")]
public string ContentLicense { get; set; }
}
[XmlRoot(ElementName = "row")]
public class xPostHistory
{
[XmlAttribute(AttributeName = "Id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "PostHistoryTypeId")]
public int PostHistoryTypeId { get; set; }
[XmlAttribute(AttributeName = "PostId")]
public int PostId { get; set; }
[XmlAttribute(AttributeName = "RevisionGUID")]
public string RevisionGUID { get; set; }
[XmlAttribute(AttributeName = "CreationDate")]
public DateTime CreationDate { get; set; }
[XmlAttribute(AttributeName = "UserId")]
public int UserId { get; set; }
[XmlAttribute(AttributeName = "Text")]
public string Text { get; set; }
[XmlAttribute(AttributeName = "ContentLicense")]
public string ContentLicense { get; set; }
}
[XmlRoot(ElementName = "row")]
public class xPostLink
{
[XmlAttribute(AttributeName = "Id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "CreationDate")]
public DateTime CreationDate { get; set; }
[XmlAttribute(AttributeName = "PostId")]
public int PostId { get; set; }
[XmlAttribute(AttributeName = "RelatedPostId")]
public int RelatedPostId { get; set; }
[XmlAttribute(AttributeName = "LinkTypeId")]
public int LinkTypeId { get; set; }
}
[XmlRoot(ElementName = "row")]
public class xPost
{
[XmlAttribute(AttributeName = "Id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "PostTypeId")]
public int PostTypeId { get; set; }
[XmlAttribute(AttributeName = "ParentId")]
public int ParentId { get; set; }
[XmlAttribute(AttributeName = "AcceptedAnswerId")]
public int AcceptedAnswerId { get; set; }
[XmlAttribute(AttributeName = "CreationDate")]
public DateTime CreationDate { get; set; }
[XmlAttribute(AttributeName = "Score")]
public int Score { get; set; }
[XmlAttribute(AttributeName = "ViewCount")]
public int ViewCount { get; set; }
[XmlAttribute(AttributeName = "Body")]
public string Body { get; set; }
[XmlAttribute(AttributeName = "OwnerUserId")]
public int OwnerUserId { get; set; }
[XmlAttribute(AttributeName = "LastEditorUserId")]
public int LastEditorUserId { get; set; }
[XmlAttribute(AttributeName = "LastEditDate")]
public DateTime LastEditDate { get; set; }
[XmlAttribute(AttributeName = "LastActivityDate")]
public DateTime LastActivityDate { get; set; }
[XmlAttribute(AttributeName = "Title")]
public string Title { get; set; }
[XmlAttribute(AttributeName = "Tags")]
public string Tags { get; set; }
[XmlAttribute(AttributeName = "AnswerCount")]
public int AnswerCount { get; set; }
[XmlAttribute(AttributeName = "CommentCount")]
public int CommentCount { get; set; }
[XmlAttribute(AttributeName = "FavoriteCount")]
public int FavoriteCount { get; set; }
[XmlAttribute(AttributeName = "ContentLicense")]
public string ContentLicense { get; set; }
}
[XmlRoot(ElementName = "row")]
public class xVote
{
[XmlAttribute(AttributeName = "Id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "PostId")]
public int PostId { get; set; }
[XmlAttribute(AttributeName = "VoteTypeId")]
public int VoteTypeId { get; set; }
[XmlAttribute(AttributeName = "CreationDate")]
public DateTime CreationDate { get; set; }
}
public class Question
{
public string Id;
public string Community;
public int AcceptedAnswerId;
public DateTime CreationDate;
public int Score;
public int ViewCount;
public string Body;
public string Owner;
public string LastEditor;
public DateTime? LastEditDate;
public string Title;
public string[] Tags;
public int FavoriteCount;
public List<Answer> Answers;
public List<Comment> Comments;
}
public class Answer
{
public int Id;
public DateTime CreationDate;
public int Score;
public int ViewCount;
public string Body;
public string Owner;
public string LastEditor;
public DateTime? LastEditDate;
public List<Comment> Comments;
}
public class Comment
{
public int Id;
public int Score;
public string Text;
public DateTime CreationDate;
public string User;
}
public class User
{
public string Community;
public string Id;
public int Reputation;
public DateTime CreationDate;
public string DisplayName;
public DateTime LastAccessDate;
public string AboutMe;
public int Views;
public int UpVotes;
public int DownVotes;
public List<Badge> Badges;
}
public class Badge
{
public string Name;
public DateTime Date;
public bool TagBased;
public string Rank;
}