-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCommentID.cs
108 lines (93 loc) · 2.65 KB
/
CommentID.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFX;
using NFX.DataAccess.Distributed;
using NFX.Serialization.JSON;
namespace Agni.Social.Graph
{
/// <summary>
/// Represents a read-only tuple of { gVolume: GDID, gComment: GDID}.
/// The gRating is a globally-unique ID however graph system prepends it with
/// gVolume which allows for instant location of a concrete data store which holds gRating.
/// </summary>
[Serializable]
public struct CommentID : IEquatable<CommentID>, IJSONWritable
{
public CommentID(GDID gVolume, GDID gComment) { G_Volume = gVolume; G_Comment = gComment; }
/// <summary>
/// Sharding GDID used to instantly find the data store shard where data is kept
/// </summary>
public readonly GDID G_Volume;
/// <summary>
///The global unique id of a comment
/// </summary>
public readonly GDID G_Comment;
/// <summary>
/// True if struct is unassigned
/// </summary>
public bool Unassigned { get{return G_Volume.IsZero;} }
/// <summary>
/// True if G_Volume | G_Comment isZero
/// </summary>
public bool IsZero
{
get { return Unassigned || G_Comment.IsZero; }
}
public bool Equals(CommentID other)
{
return this.G_Volume == other.G_Volume &&
this.G_Comment == other.G_Comment;
}
public override bool Equals(object obj)
{
if (!(obj is CommentID)) return false;
return this.Equals((CommentID)obj);
}
public override int GetHashCode()
{
return G_Volume.GetHashCode() ^ G_Comment.GetHashCode();
}
public string Stringify()
{
var eLink = new ELink(G_Volume, G_Comment.Bytes);
return eLink.Link;
}
public override string ToString()
{
return "Comment [{0}@{1}]".Args(G_Volume, G_Comment);
}
public void WriteAsJSON(TextWriter wri, int nestingLevel, JSONWritingOptions options = null)
{
wri.Write('"');
wri.Write(Stringify());
wri.Write('"');
}
public static CommentID Parse(string str)
{
CommentID result;
if (!TryParse(str, out result))
throw new SocialException("CommentID.Parse({0})".Args(str));
return result;
}
public static bool TryParse(string str, out CommentID commentId)
{
try
{
var eLink = new ELink(str);
var gVolume = eLink.GDID;
var gComment = new GDID(eLink.Metadata);
commentId = new CommentID(gVolume, gComment);
return true;
}
catch
{
commentId = default(CommentID);
return false;
}
}
}
}