-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.cs
120 lines (104 loc) · 4.08 KB
/
Data.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileHelpers;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Builders;
using System.Configuration;
using System.Diagnostics;
namespace MongoBench {
/// <summary>
///
/// </summary>
public class Data {
/// <summary>
/// Post documents
/// </summary>
private static BsonDocument[] POSTS;
/// <summary>
/// Individual comments
/// </summary>
private static BsonDocument[] COMMENTS;
/// <summary>
/// Constructor
/// </summary>
public static bool Initialize() {
var commentEngine = new FileHelperEngine(typeof(Comment));
commentEngine.Options.IgnoreFirstLines = 1;
commentEngine.Options.IgnoreEmptyLines = true;
commentEngine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue;
Comment[] commentData = null;
try {
commentData = commentEngine.ReadFile("comments.csv") as Comment[];
}
catch (Exception ex) {
Debug.Write(ex.ToString());
return false;
}
COMMENTS = Array.ConvertAll<Comment, BsonDocument>(commentData,
c => new BsonDocument() {
{ "author", c.author },
{ "text", c.text },
{ "date", c.date }
});
var postEngine = new FileHelperEngine(typeof(Post));
postEngine.Options.IgnoreFirstLines = 1;
postEngine.Options.IgnoreEmptyLines = true;
postEngine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue;
Post[] postData = null;
try {
postData = postEngine.ReadFile("posts.csv") as Post[];
}
catch (Exception ex) {
Debug.Write(ex.ToString());
return false;
}
var rnd = new Random(DateTime.Now.Second);
var minCommentCount = int.Parse(ConfigurationManager.AppSettings["MIN_COMMENT_COUNT"]);
var maxCommentCount = int.Parse(ConfigurationManager.AppSettings["MAX_COMMENT_COUNT"]);
POSTS = Array.ConvertAll<Post, BsonDocument>(postData,
p => new BsonDocument() {
{ "author", p.author },
{ "title", p.title },
{ "text", p.text },
{ "tags", new BsonArray(p.tags.Split(' ')) },
{ "comments", new BsonArray(COMMENTS.Take(rnd.Next(minCommentCount, maxCommentCount))) },
{ "date", p.date }
});
return true;
}
/// <summary>
/// Gets the document.
/// </summary>
/// <param name="index">The index.</param>
public static BsonDocument GetDocument(int index) {
while(index >= POSTS.Length)
index -= POSTS.Length;
return POSTS[index].DeepClone() as BsonDocument;
}
}
[DelimitedRecord(",")]
public class Post {
[FieldQuoted('"', QuoteMode.OptionalForBoth)]
public string author;
[FieldQuoted('"', QuoteMode.OptionalForBoth)]
public string title;
[FieldQuoted('"', QuoteMode.OptionalForBoth)]
public string text;
[FieldQuoted('"', QuoteMode.OptionalForBoth)]
public string tags;
public int date;
}
[DelimitedRecord(",")]
public class Comment {
[FieldQuoted('"', QuoteMode.OptionalForBoth)]
public string author;
[FieldQuoted('"', QuoteMode.OptionalForBoth)]
public string text;
public int date;
}
}