-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDagLink.cs
157 lines (141 loc) · 4.81 KB
/
DagLink.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
using System.IO;
using Google.Protobuf;
namespace Ipfs
{
/// <summary>
/// A link to another node in the IPFS Merkle DAG.
/// </summary>
public class DagLink : IMerkleLink
{
/// <summary>
/// Create a new instance of <see cref="DagLink"/> class.
/// </summary>
/// <param name="name">The name associated with the linked node.</param>
/// <param name="id">The <see cref="Cid"/> of the linked node.</param>
/// <param name="size">The serialised size (in bytes) of the linked node.</param>
public DagLink(string? name, Cid id, ulong size)
{
Name = name;
Id = id;
Size = size;
}
/// <summary>
/// Creates a new instance of the <see cref="DagLink"/> class from the
/// specified <see cref="IMerkleLink"/>.
/// </summary>
/// <param name="link">
/// Some type of a Merkle link.
/// </param>
public DagLink(IMerkleLink link)
{
Name = link.Name;
Id = link.Id;
Size = link.Size;
}
/// <summary>
/// Creates a new instance of the <see cref="DagLink"/> class from the
/// specified <see cref="Stream"/>.
/// </summary>
/// <param name="stream">
/// A <see cref="Stream"/> containing the binary representation of the
/// <b>DagLink</b>.
/// </param>
public DagLink(Stream stream)
{
(Name, Id, Size) = Read(stream);
}
/// <summary>
/// Creates a new instance of the <see cref="DagLink"/> class from the
/// specified <see cref="CodedInputStream"/>.
/// </summary>
/// <param name="stream">(
/// A <see cref="CodedInputStream"/> containing the binary representation of the
/// <b>DagLink</b>.
/// </param>
public DagLink(CodedInputStream stream)
{
(Name, Id, Size) = Read(stream);
}
/// <inheritdoc />
public string? Name { get; private set; }
/// <inheritdoc />
public Cid Id { get; private set; }
/// <inheritdoc />
public ulong Size { get; private set; }
/// <summary>
/// Writes the binary representation of the link to the specified <see cref="Stream"/>.
/// </summary>
/// <param name="stream">
/// The <see cref="Stream"/> to write to.
/// </param>
public void Write(Stream stream)
{
using var cos = new CodedOutputStream(stream, true);
Write(cos);
}
/// <summary>
/// Writes the binary representation of the link to the specified <see cref="CodedOutputStream"/>.
/// </summary>
/// <param name="stream">
/// The <see cref="CodedOutputStream"/> to write to.
/// </param>
public void Write(CodedOutputStream stream)
{
stream.WriteTag(1, WireFormat.WireType.LengthDelimited);
Id.Write(stream);
if (Name is not null)
{
stream.WriteTag(2, WireFormat.WireType.LengthDelimited);
stream.WriteString(Name);
}
stream.WriteTag(3, WireFormat.WireType.Varint);
stream.WriteInt64((long)Size);
}
private (string?, Cid, ulong) Read(Stream stream)
{
using var cis = new CodedInputStream(stream, true);
return Read(cis);
}
private (string?, Cid, ulong) Read(CodedInputStream stream)
{
string? name = null;
Cid? id = null;
ulong size = 0;
while (!stream.IsAtEnd)
{
var tag = stream.ReadTag();
switch (WireFormat.GetTagFieldNumber(tag))
{
case 1:
id = Cid.Read(stream);
break;
case 2:
name = stream.ReadString();
break;
case 3:
size = (ulong)stream.ReadInt64();
break;
default:
throw new InvalidDataException("Unknown field number");
}
}
if (id is null)
{
throw new InvalidDataException($"Missing CID id from record");
}
return (name, id, size);
}
/// <summary>
/// Returns the IPFS binary representation as a byte array.
/// </summary>
/// <returns>
/// A byte array.
/// </returns>
public byte[] ToArray()
{
using var ms = new MemoryStream();
Write(ms);
return ms.ToArray();
}
}
}