Skip to content

Commit 5019a9f

Browse files
authored
feat: PointData Immutability (#96)
1 parent b6b8e97 commit 5019a9f

File tree

7 files changed

+374
-76
lines changed

7 files changed

+374
-76
lines changed

Client.Core/Client.Core.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,14 @@
3838
<PackageReference Include="RestSharp" Version="106.6.10" />
3939
<PackageReference Include="System.Net.Http" Version="4.3.4" />
4040
</ItemGroup>
41+
42+
<ItemGroup>
43+
<Folder Include="InfluxDB.Client.Api.Domain" />
44+
</ItemGroup>
45+
46+
<ItemGroup>
47+
<Compile Remove="InfluxDB.Client.Api.Domain\WritePrecision.cs" />
48+
<Compile Remove="Writes\Events.cs" />
49+
</ItemGroup>
4150

4251
</Project>

Client.Test/MeasurementMapperTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void ColumnWithoutName()
5353

5454
var lineProtocol = _mapper.ToPoint(poco, WritePrecision.S).ToLineProtocol();
5555

56-
Assert.AreEqual("poco,tag=tag\\ val ValueWithEmptyName=25,ValueWithoutDefaultName=20i,value=15.444 864000", lineProtocol);
56+
Assert.AreEqual("poco,tag=tag\\ val value=15.444,ValueWithEmptyName=25,ValueWithoutDefaultName=20i 864000", lineProtocol);
5757
}
5858

5959
[Test]

Client.Test/PointDataTest.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,42 @@ namespace InfluxDB.Client.Test
99
[TestFixture]
1010
public class PointDataTest
1111
{
12+
[Test]
13+
public void TagEmptyTagValue()
14+
{
15+
var point = PointData.Measurement("h2o")
16+
.Tag("location", "europe")
17+
.Tag("log", "to_delete")
18+
.Tag("log", "")
19+
.Field("level", 2);
20+
21+
Assert.AreEqual("h2o,location=europe level=2i", point.ToLineProtocol());
22+
}
23+
24+
[Test]
25+
public void Immutability()
26+
{
27+
var point = PointData.Measurement("h2 o")
28+
.Tag("location", "europe");
29+
30+
var point1 = point
31+
.Tag("TAG", "VALX")
32+
.Field("level", 2);
33+
34+
var point2 = point
35+
.Tag("TAG", "VALX")
36+
.Field("level", 2);
37+
38+
var point3 = point
39+
.Tag("TAG", "VALY")
40+
.Field("level", 2);
41+
42+
Assert.AreEqual(point1, point2);
43+
Assert.AreNotEqual(point, point1);
44+
Assert.False(ReferenceEquals(point1, point2));
45+
Assert.AreNotEqual(point3, point1);
46+
}
47+
1248
[Test]
1349
public void MeasurementEscape()
1450
{
@@ -273,7 +309,8 @@ public void DefaultTagsOverride()
273309

274310
var defaults = new PointSettings().AddDefaultTag("expensive", "true");
275311

276-
Assert.AreEqual("h2o,expensive=true,location=europe level=2i", point.ToLineProtocol(defaults));
312+
string actual = point.ToLineProtocol(defaults);
313+
Assert.AreEqual("h2o,expensive=true,location=europe level=2i", actual);
277314
}
278315

279316
[Test]
@@ -299,7 +336,8 @@ public void DefaultTagsNotOverride()
299336

300337
var defaults = new PointSettings().AddDefaultTag("expensive", "true");
301338

302-
Assert.AreEqual("h2o,expensive=false,location=europe level=2i", point.ToLineProtocol(defaults));
339+
string lineProtocol = point.ToLineProtocol(defaults);
340+
Assert.AreEqual("h2o,expensive=false,location=europe level=2i", lineProtocol);
303341
}
304342

305343
[Test]

Client/Client.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<None Include="..\Assets\influxdata.jpg" Pack="true" PackagePath=""/>
31-
<ProjectReference Include="..\Client.Core\Client.Core.csproj"/>
30+
<None Include="..\Assets\influxdata.jpg" Pack="true" PackagePath="" />
31+
<ProjectReference Include="..\Client.Core\Client.Core.csproj" />
3232
</ItemGroup>
3333

3434
<ItemGroup>
35-
<PackageReference Include="JsonSubTypes" Version="1.5.2"/>
36-
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.1.1"/>
37-
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0"/>
38-
<PackageReference Include="System.Reactive" Version="4.1.2"/>
35+
<PackageReference Include="JsonSubTypes" Version="1.5.2" />
36+
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.1.1" />
37+
<PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
38+
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
39+
<PackageReference Include="System.Reactive" Version="4.1.2" />
3940
</ItemGroup>
4041

4142
</Project>

Client/Internal/MeasurementMapper.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,29 @@ internal PointData ToPoint<TM>(TM measurement, WritePrecision precision)
5656
var name = !string.IsNullOrEmpty(propertyInfo.Column.Name) ? propertyInfo.Column.Name : propertyInfo.Property.Name;
5757
if (propertyInfo.Column.IsTag)
5858
{
59-
point.Tag(name, value.ToString());
59+
point = point.Tag(name, value.ToString());
6060
}
6161
else if (propertyInfo.Column.IsTimestamp)
6262
{
6363
if (value is long l)
6464
{
65-
point.Timestamp(l, precision);
65+
point = point.Timestamp(l, precision);
6666
}
6767
else if (value is TimeSpan span)
6868
{
69-
point.Timestamp(span, precision);
69+
point = point.Timestamp(span, precision);
7070
}
7171
else if (value is DateTime date)
7272
{
73-
point.Timestamp(date, precision);
73+
point = point.Timestamp(date, precision);
7474
}
7575
else if (value is DateTimeOffset offset)
7676
{
77-
point.Timestamp(offset, precision);
77+
point = point.Timestamp(offset, precision);
7878
}
7979
else if (value is Instant instant)
8080
{
81-
point.Timestamp(instant, precision);
81+
point = point.Timestamp(instant, precision);
8282
}
8383
else
8484
{
@@ -89,55 +89,55 @@ internal PointData ToPoint<TM>(TM measurement, WritePrecision precision)
8989
{
9090
if (value is bool b)
9191
{
92-
point.Field(name, b);
92+
point = point.Field(name, b);
9393
}
9494
else if (value is double d)
9595
{
96-
point.Field(name, d);
96+
point = point.Field(name, d);
9797
}
9898
else if (value is float f)
9999
{
100-
point.Field(name, f);
100+
point = point.Field(name, f);
101101
}
102102
else if (value is decimal dec)
103103
{
104-
point.Field(name, dec);
104+
point = point.Field(name, dec);
105105
}
106106
else if (value is long lng)
107107
{
108-
point.Field(name, lng);
108+
point = point.Field(name, lng);
109109
}
110110
else if (value is ulong ulng)
111111
{
112-
point.Field(name, ulng);
112+
point = point.Field(name, ulng);
113113
}
114114
else if (value is int i)
115115
{
116-
point.Field(name, i);
116+
point = point.Field(name, i);
117117
}
118118
else if (value is byte bt)
119119
{
120-
point.Field(name, bt);
120+
point = point.Field(name, bt);
121121
}
122122
else if (value is sbyte sb)
123123
{
124-
point.Field(name, sb);
124+
point = point.Field(name, sb);
125125
}
126126
else if (value is short sh)
127127
{
128-
point.Field(name, sh);
128+
point = point.Field(name, sh);
129129
}
130130
else if (value is uint ui)
131131
{
132-
point.Field(name, ui);
132+
point = point.Field(name, ui);
133133
}
134134
else if (value is ushort us)
135135
{
136-
point.Field(name, us);
136+
point = point.Field(name, us);
137137
}
138138
else
139139
{
140-
point.Field(name, value.ToString());
140+
point = point.Field(name, value.ToString());
141141
}
142142
}
143143
}

Client/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,57 @@ namespace Examples
412412
}
413413
```
414414

415+
DataPoint Builder Immutability:
416+
The builder is immutable therefore won't have side effect when using for building
417+
multiple point with single builder.
418+
419+
```c#
420+
using System;
421+
using InfluxDB.Client;
422+
using InfluxDB.Client.Api.Domain;
423+
using InfluxDB.Client.Writes;
424+
425+
namespace Examples
426+
{
427+
public static class WriteDataPoint
428+
{
429+
private static readonly char[] Token = "".ToCharArray();
430+
431+
public static void Main(string[] args)
432+
{
433+
var influxDBClient = InfluxDBClientFactory.Create("http://localhost:9999", Token);
434+
435+
//
436+
// Write Data
437+
//
438+
using (var writeApi = influxDBClient.GetWriteApi())
439+
{
440+
//
441+
// Write by Data Point
442+
443+
var builder = PointData.Measurement("temperature")
444+
.Tag("location", "west");
445+
446+
var pointA = builder
447+
.Field("value", 55D)
448+
.Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);
449+
450+
writeApi.WritePoint("bucket_name", "org_id", pointA);
451+
452+
var pointB = builder
453+
.Field("age", 32)
454+
.Timestamp(DateTime.UtcNow, WritePrecision.Ns);
455+
456+
writeApi.WritePoint("bucket_name", "org_id", pointB);
457+
}
458+
459+
influxDBClient.Dispose();
460+
}
461+
}
462+
}
463+
```
464+
465+
415466
#### By LineProtocol
416467

417468
Write Line Protocol record into specified bucket:

0 commit comments

Comments
 (0)