-
Notifications
You must be signed in to change notification settings - Fork 6
/
Models.Standard.cs
197 lines (192 loc) · 6.73 KB
/
Models.Standard.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace JsonBenchmark.Models.Standard
{
[DataContract]
public enum PostState
{
[DataMember]
Draft,
[DataMember]
Published,
[DataMember]
Hidden,
}
[DataContract]
public class DeletePost : IEquatable<DeletePost>
{
private static DateTime NOW = DateTime.UtcNow;
private static readonly Guid[] GUIDS = Enumerable.Range(0, 100).Select(it => Guid.NewGuid()).ToArray();
[DataMember(Order = 1)]
public int postID { get; set; }
[DataMember(Order = 2)]
public Guid? referenceId { get; set; }
[DataMember(Order = 3)]
public DateTime lastModified { get; set; }
[DataMember(Order = 4)]
public long deletedBy { get; set; }
[DataMember(Order = 5)]
public string reason { get; set; }
[DataMember(Order = 6)]
public long[] versions { get; set; }
[DataMember(Order = 7)]
public PostState? state { get; set; }
[DataMember(Order = 8)]
public List<bool?> votes { get; set; }
public override int GetHashCode() { return postID; }
public override bool Equals(object obj) { return Equals(obj as DeletePost); }
public bool Equals(DeletePost other)
{
return other != null && other.postID == this.postID && other.referenceId == this.referenceId
&& other.lastModified == this.lastModified && other.deletedBy == this.deletedBy
&& other.reason == this.reason
&& (other.versions == this.versions || other.versions != null && Enumerable.SequenceEqual(other.versions, this.versions))
&& other.state == this.state
&& (other.votes == this.votes || other.votes != null && Enumerable.SequenceEqual(other.votes, this.votes));
}
public static TD Factory<TD, TS>(int i, Func<int, TS> cast)
where TD : new()
where TS : struct
{
dynamic delete = new TD();
delete.postID = i;
delete.deletedBy = i / 100;
delete.lastModified = NOW.AddSeconds(i);
delete.reason = "no reason";
if (i % 3 == 0) delete.referenceId = GUIDS[i % 100];
if (i % 5 == 0) delete.state = cast(i % 3);
if (i % 7 == 0)
{
delete.versions = new long[i % 100 + 1];//ProtoBuf hack - always add object since Protobuf can't differentiate
for (int x = 0; x <= i % 100; x++)
delete.versions[x] = i * x + x;
}
if (i % 2 == 0 && i % 10 != 0)
{
delete.votes = new List<bool?>();
for (int j = 0; j < i % 10; j++)
delete.votes.Add((i + j) % 3 == 0 ? true : j % 2 == 0 ? (bool?)false : null);
}
return delete;
}
}
[DataContract]
public class Post : IEquatable<Post>
{
private static DateTime NOW = DateTime.UtcNow;
private static DateTime TODAY = DateTime.UtcNow.Date;
private static string[][] TAGS = new[] { new string[0], new[] { "JSON" }, new[] { ".NET", "Java", "benchmark" } };
public Post()
{
comments = new List<Comment>();
}
[DataMember(Order = 2)]
public int ID { get; set; }
[DataMember(Order = 3)]
public string title { get; set; }
[DataMember(Order = 4)]
public string text { get; set; }
[DataMember(Order = 5)]
public DateTime created { get; set; }
[DataMember(Order = 6)]
public HashSet<string> tags { get; set; }
[DataMember(Order = 7)]
public DateTime? approved { get; set; }
[DataMember(Order = 8)]
public List<Comment> comments { get; set; }
[DataMember(Order = 9)]
public Vote votes { get; set; }
[DataMember(Order = 10)]
public List<string> notes { get; set; }
[DataMember(Order = 11)]
public PostState state { get; set; }
public override int GetHashCode() { return ID; }
public override bool Equals(object obj) { return Equals(obj as Post); }
public bool Equals(Post other)
{
var otherTags = other == null || other.tags == null ? null : other.tags.ToList();
var thisTags = this.tags != null ? this.tags.ToList() : null;
if (thisTags != null) thisTags.Sort();
if (otherTags != null) otherTags.Sort();
return other != null && other.ID == this.ID && other.title == this.title
&& other.text == this.text && other.created == this.created
&& (otherTags == thisTags || otherTags != null && thisTags != null && Enumerable.SequenceEqual(otherTags, thisTags))
&& other.approved == this.approved
&& Enumerable.SequenceEqual(other.comments, this.comments)
&& other.votes.Equals(this.votes)
&& (other.notes == this.notes || other.notes != null && Enumerable.SequenceEqual(other.notes, this.notes))
&& other.state == this.state;
}
public static TP Factory<TP, TV, TS, TC>(int i, Func<int, TS> cast)
where TP : new()
where TV : new()
where TC : new()
where TS : struct
{
dynamic post = new TP();
post.ID = -i;
post.approved = i % 2 == 0 ? null : (DateTime?)NOW.AddMilliseconds(i);
dynamic votes = new TV();
votes.downvote = i / 3;
votes.upvote = i / 2;
post.votes = votes;
post.text = "some text describing post " + i;
post.title = "post title " + i;
post.state = cast(i % 3);
post.tags = new HashSet<string>(TAGS[i % 3]);
post.created = TODAY.AddDays(i);
for (int j = 0; j < i % 100; j++)
{
dynamic comment = new TC();
comment.created = TODAY.AddDays(i + j);
comment.message = "comment number " + i + " for " + j;
dynamic v = new TV();
v.upvote = j;
v.downvote = j * 2;
comment.votes = v;
comment.approved = j % 3 != 0 ? null : (DateTime?)NOW.AddMilliseconds(i);
comment.user = "some random user " + i;
post.comments.Add(comment);
}
return post;
}
}
[DataContract]
public class Comment : IEquatable<Comment>
{
[DataMember(Order = 4)]
public DateTime created { get; set; }
[DataMember(Order = 5)]
public DateTime? approved { get; set; }
[DataMember(Order = 6)]
public string user { get; set; }
[DataMember(Order = 7)]
public string message { get; set; }
[DataMember(Order = 8)]
public Vote votes { get; set; }
public override int GetHashCode() { return created.GetHashCode(); }
public override bool Equals(object obj) { return Equals(obj as Comment); }
public bool Equals(Comment other)
{
return other != null
&& other.created == this.created && other.approved == this.approved && other.user == this.user
&& other.message == this.message && other.votes.Equals(this.votes);
}
}
[DataContract]
public class Vote : IEquatable<Vote>
{
[DataMember(Order = 1)]
public int upvote { get; set; }
[DataMember(Order = 2)]
public int downvote { get; set; }
public override int GetHashCode() { return upvote ^ downvote; }
public override bool Equals(object obj) { return Equals(obj as Vote); }
public bool Equals(Vote other)
{
return other != null && other.upvote == this.upvote && other.downvote == this.downvote;
}
}
}