-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathModels.Large.cs
More file actions
203 lines (201 loc) · 6.16 KB
/
Models.Large.cs
File metadata and controls
203 lines (201 loc) · 6.16 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace JsonBenchmark.Models.Large
{
[DataContract]
public class Book : IEquatable<Book>
{
private static DateTime NOW = DateTime.UtcNow;
private static List<byte[]> ILLUSTRATIONS = new List<byte[]>();
static Book()
{
var rnd = new Random(1);
for (int i = 0; i < 10; i++)
{
var buf = new byte[256 * i * i * i];
rnd.NextBytes(buf);
ILLUSTRATIONS.Add(buf);
}
}
public Book()
{
pages = new LinkedList<Page>();
changes = new HashSet<DateTime>();
metadata = new Dictionary<string, string>();
genres = new Genre[0];
cover = new byte[0];//otherwise buggy ProtoBuf and some other libs
}
[DataMember(Order = 2)]
public int ID { get; set; }
[DataMember(Order = 3)]
public string title { get; set; }
[DataMember(Order = 4)]
public int authorId { get; set; }
[DataMember(Order = 5)]
public LinkedList<Page> pages { get; set; }
[DataMember(Order = 6)]
public DateTime? published { get; set; }
[DataMember(Order = 7)]
public byte[] cover { get; set; }
[DataMember(Order = 8)]
public HashSet<DateTime> changes { get; set; }
[DataMember(Order = 9)]
public Dictionary<string, string> metadata { get; set; }
[DataMember(Order = 10)]
public Genre[] genres { get; set; }
public override int GetHashCode() { return ID; }
public override bool Equals(object obj) { return Equals(obj as Book); }
public bool Equals(Book other)
{
var otherChanges = other != null ? other.changes.ToList() : null;
var thisChanges = changes.ToList();
if (otherChanges != null) otherChanges.Sort();
thisChanges.Sort();
var otherKeys = other != null ? other.metadata.Keys.ToList() : null;
var thisKeys = this.metadata.Keys.ToList();
if (otherKeys != null) otherKeys.Sort();
thisKeys.Sort();
return other != null && other.ID == this.ID && other.title == this.title
&& other.authorId == this.authorId
&& other.pages != null && Enumerable.SequenceEqual(other.pages, this.pages)
&& other.published == this.published
&& other.cover != null && Enumerable.SequenceEqual(other.cover, this.cover)
&& otherChanges != null && Enumerable.SequenceEqual(otherChanges, thisChanges)
&& otherKeys != null && Enumerable.SequenceEqual(otherKeys, thisKeys)
&& otherKeys.All(it => other.metadata[it] == this.metadata[it])
&& other.genres != null && Enumerable.SequenceEqual(other.genres, this.genres);
}
public static TB Factory<TB, TG, TP, TH, TF>(int i, Func<int, TG> cast)
where TB : new()
where TG : struct
where TP : new()
where TH : new()
where TF : new()
{
dynamic book = new TB();
book.ID = -i;
book.authorId = i / 100;
book.published = i % 3 == 0 ? null : (DateTime?)NOW.AddMinutes(i).Date;
book.title = "book title " + i;
var genres = new TG[i % 2];
for (int j = 0; j < i % 2; j++)
genres[j] = cast((i + j) % 4);
book.genres = genres;
for (int j = 0; j < i % 20; j++)
book.changes.Add(NOW.AddMinutes(i).Date);
for (int j = 0; j < i % 50; j++)
book.metadata["key " + i + j] = "value " + i + j;
if (i % 3 == 0 || i % 7 == 0) book.cover = ILLUSTRATIONS[i % ILLUSTRATIONS.Count];
var sb = new StringBuilder();
for (int j = 0; j < i % 1000; j++)
{
sb.Append("some text on page " + j);
sb.Append("more text for " + i);
dynamic page = new TP();
page.text = sb.ToString();
for (int z = 0; z < i % 100; z++)
{
dynamic note;
if (z % 3 == 0)
{
note = new TH();
note.modifiedAt = NOW.AddSeconds(i);
note.note = "headnote " + j + " at " + z;
}
else
{
note = new TF();
note.createadAt = NOW.AddSeconds(i);
note.note = "footnote " + j + " at " + z;
note.index = i;
}
if (z % 3 == 0)
note.writtenBy = "author " + j + " " + z;
page.notes.Add(note);
}
book.pages.AddLast(page);
}
return book;
}
}
[DataContract]
public enum Genre
{
[DataMember]
Action,
[DataMember]
Romance,
[DataMember]
Comedy,
[DataMember]
SciFi
}
[DataContract]
public class Page : IEquatable<Page>
{
public Page()
{
notes = new List<Note>();
identity = Guid.NewGuid();
}
[DataMember]
public string text { get; set; }
[DataMember]
public List<Note> notes { get; set; }
[DataMember]
public Guid identity { get; set; }
public override int GetHashCode() { return identity.GetHashCode(); }
public override bool Equals(object obj) { return Equals(obj as Page); }
public bool Equals(Page other)
{
return other != null
&& other.text == this.text
&& Enumerable.SequenceEqual(other.notes, this.notes)
&& other.identity == this.identity;
}
}
[DataContract]
public class Footnote : Note, IEquatable<Footnote>
{
[DataMember]
public string note { get; set; }
[DataMember]
public string writtenBy { get; set; }
[DataMember]
public DateTime createadAt { get; set; }
[DataMember]
public long index { get; set; }
public override int GetHashCode() { return (int)index; }
public override bool Equals(object obj) { return Equals(obj as Footnote); }
public bool Equals(Footnote other)
{
return other != null && other.note == this.note && other.writtenBy == this.writtenBy
&& other.createadAt == this.createadAt && other.index == this.index;
}
}
[DataContract]
public class Headnote : Note, IEquatable<Headnote>
{
[DataMember]
public string note { get; set; }
[DataMember]
public string writtenBy { get; set; }
[DataMember]
public DateTime? modifiedAt { get; set; }
public override int GetHashCode() { return (modifiedAt ?? DateTime.MinValue).GetHashCode(); }
public override bool Equals(object obj) { return Equals(obj as Headnote); }
public bool Equals(Headnote other)
{
return other != null && other.note == this.note && other.writtenBy == this.writtenBy
&& other.modifiedAt == this.modifiedAt;
}
}
public interface Note
{
string note { get; set; }
string writtenBy { get; set; }
}
}