Skip to content

Commit 05c4d2b

Browse files
committed
add new type uuid, ipaddr
1 parent 94d2fe9 commit 05c4d2b

20 files changed

+829
-14
lines changed
Binary file not shown.

dolphindb_csharpapi/data/AbstractVector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public abstract class AbstractVector : AbstractEntity, IVector
2020

2121
private DATA_FORM df_;
2222

23+
protected static int BUF_SIZE = 4096;
24+
25+
protected byte[] buf = new byte[BUF_SIZE];
26+
2327
public virtual DataTable toDataTable()
2428
{
2529
DataTable dt = buildTable();

dolphindb_csharpapi/data/BasicEntityFactory.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public BasicEntityFactory()
3535
factories[(int)DATA_TYPE.DT_CODE] = new MetaCodeFactory();
3636
factories[(int)DATA_TYPE.DT_DATASOURCE] = new DataSourceFactory();
3737
factories[(int)DATA_TYPE.DT_RESOURCE] = new ResourceFactory();
38+
factories[(int)DATA_TYPE.DT_UUID] = new UuidFactory();
39+
factories[(int)DATA_TYPE.DT_INT128] = new Int128Factory();
40+
factories[(int)DATA_TYPE.DT_IPADDR] = new IPAddrFactory();
3841
}
3942

4043
public IEntity createEntity(DATA_FORM form, DATA_TYPE type, ExtendedDataInput @in)
@@ -229,6 +232,45 @@ private class DoubleFactory : TypeFactory
229232
public IMatrix createMatrixWithDefaultValue(int rows, int columns) { return new BasicDoubleMatrix(rows, columns); }
230233
}
231234

235+
private class UuidFactory : TypeFactory
236+
{
237+
238+
public IScalar createScalar(ExtendedDataInput @in) { return new BasicUuid(@in); }
239+
public IVector createVector(ExtendedDataInput @in) { return new BasicUuidVector(DATA_FORM.DF_VECTOR, @in); }
240+
public IVector createPair(ExtendedDataInput @in) { return new BasicUuidVector(DATA_FORM.DF_PAIR, @in); }
241+
public IMatrix createMatrix(ExtendedDataInput @in) { throw new Exception("Matrix for UUID not supported yet.");}
242+
public IScalar createScalarWithDefaultValue() { return new BasicUuid(0, 0); }
243+
public IVector createVectorWithDefaultValue(int size) { return new BasicUuidVector(size); }
244+
public IVector createPairWithDefaultValue() { return new BasicUuidVector(DATA_FORM.DF_PAIR, 2); }
245+
public IMatrix createMatrixWithDefaultValue(int rows, int columns) { throw new Exception("Matrix for UUID not supported yet."); }
246+
}
247+
248+
private class IPAddrFactory : TypeFactory
249+
{
250+
251+
public IScalar createScalar(ExtendedDataInput @in) { return new BasicIPAddr(@in); }
252+
public IVector createVector(ExtendedDataInput @in){ return new BasicIPAddrVector(DATA_FORM.DF_VECTOR, @in); }
253+
public IVector createPair(ExtendedDataInput @in){ return new BasicIPAddrVector(DATA_FORM.DF_PAIR, @in);}
254+
public IMatrix createMatrix(ExtendedDataInput @in){ throw new Exception("Matrix for IPADDR not supported yet.");}
255+
public IScalar createScalarWithDefaultValue() { return new BasicIPAddr(0, 0); }
256+
public IVector createVectorWithDefaultValue(int size) { return new BasicIPAddrVector(size); }
257+
public IVector createPairWithDefaultValue() { return new BasicIPAddrVector(DATA_FORM.DF_PAIR, 2); }
258+
public IMatrix createMatrixWithDefaultValue(int rows, int columns) { throw new Exception("Matrix for IPADDR not supported yet."); }
259+
}
260+
261+
private class Int128Factory : TypeFactory
262+
{
263+
264+
public IScalar createScalar(ExtendedDataInput @in){ return new BasicInt128(@in); }
265+
public IVector createVector(ExtendedDataInput @in){ return new BasicInt128Vector(DATA_FORM.DF_VECTOR, @in);}
266+
public IVector createPair(ExtendedDataInput @in){ return new BasicInt128Vector(DATA_FORM.DF_PAIR, @in);}
267+
public IMatrix createMatrix(ExtendedDataInput @in){ throw new Exception("Matrix for INT128 not supported yet.");}
268+
public IScalar createScalarWithDefaultValue() { return new BasicInt128(0, 0); }
269+
public IVector createVectorWithDefaultValue(int size) { return new BasicInt128Vector(size); }
270+
public IVector createPairWithDefaultValue() { return new BasicInt128Vector(DATA_FORM.DF_PAIR, 2); }
271+
public IMatrix createMatrixWithDefaultValue(int rows, int columns) { throw new Exception("Matrix for INT128 not supported yet."); }
272+
}
273+
232274
private class StringFactory : TypeFactory
233275
{
234276

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
using dolphindb.io;
2+
using System;
3+
using System.Text;
4+
5+
namespace dolphindb.data
6+
{
7+
public class BasicIPAddr : BasicInt128
8+
{
9+
10+
11+
public BasicIPAddr(long high, long low) : base(high, low)
12+
{
13+
14+
}
15+
16+
17+
public BasicIPAddr(ExtendedDataInput @in) : base(@in)
18+
{
19+
}
20+
21+
22+
public override DATA_TYPE getDataType()
23+
{
24+
return DATA_TYPE.DT_IPADDR;
25+
}
26+
27+
28+
public override String getString()
29+
{
30+
return getString(value);
31+
}
32+
33+
public static String getString(Long2 value)
34+
{
35+
if (value.high == 0 && (value.low >> 32) == 0)
36+
{
37+
//ip4
38+
long ip4 = value.low;
39+
return String.Format("{0}.{1}.{2}.{3}", ip4 >> 24, (ip4 >> 16) & 0xff, (ip4 >> 8) & 0xff, ip4 & 0xff);
40+
}
41+
else
42+
{
43+
//ip6
44+
StringBuilder sb = new StringBuilder();
45+
bool consecutiveZeros = false;
46+
int[] data = new int[8];
47+
data[0] = (int)(value.high >> 48);
48+
data[1] = (int)((value.high >> 32) & 0xffff);
49+
data[2] = (int)((value.high >> 16) & 0xffff);
50+
data[3] = (int)(value.high & 0xffff);
51+
data[4] = (int)(value.low >> 48);
52+
data[5] = (int)((value.low >> 32) & 0xffff);
53+
data[6] = (int)((value.low >> 16) & 0xffff);
54+
data[7] = (int)(value.low & 0xffff);
55+
for (int i = 0; i < 8; ++i)
56+
{
57+
if (i > 0 && i < 6 && !consecutiveZeros && data[i] == 0 && data[i + 1] == 0)
58+
{
59+
//consecutive zeros
60+
consecutiveZeros = true;
61+
++i;
62+
while (i < 6 && data[i + 1] == 0) ++i;
63+
}
64+
else
65+
{
66+
//swap two bytes
67+
sb.Append(String.Format("{0}x", (short)data[i]));
68+
}
69+
sb.Append(':');
70+
}
71+
return sb.ToString().Substring(0, sb.Length - 1);
72+
}
73+
}
74+
75+
public static BasicIPAddr fromString(String name)
76+
{
77+
if (name.Length < 7)
78+
return null;
79+
for (int i = 0; i < 4; ++i)
80+
{
81+
if (name.Substring(i, 1) == ".")
82+
return parseIP4(name);
83+
}
84+
return parseIP6(name);
85+
}
86+
87+
public static BasicIPAddr parseIP4(String str)
88+
{
89+
int byteIndex = 0;
90+
long curByte = 0;
91+
int len = str.Length;
92+
long low = 0;
93+
for (int i = 0; i <= len; ++i)
94+
{
95+
if (i == len || str.Substring(i, 1) == ".")
96+
{
97+
if (curByte < 0 || curByte > 255 || byteIndex > 3)
98+
return null;
99+
low += curByte << ((3 - byteIndex) * 8);
100+
byteIndex++;
101+
curByte = 0;
102+
continue;
103+
}
104+
char ch = Convert.ToChar(str.Substring(i, 1));
105+
if (ch < '0' || ch > '9')
106+
return null;
107+
curByte = curByte * 10 + ch - 48;
108+
}
109+
if (byteIndex != 4)
110+
return null;
111+
return new BasicIPAddr(0, low);
112+
}
113+
114+
public static BasicIPAddr parseIP6(String str)
115+
{
116+
int byteIndex = 0;
117+
int curByte = 0;
118+
int len = str.Length;
119+
int lastColonPos = -1;
120+
byte[] buf = new byte[16];
121+
122+
for (int i = 0; i <= len; ++i)
123+
{
124+
if (i == len || str.Substring(i, 1) == ":")
125+
{
126+
//check consecutive colon
127+
if (lastColonPos == (int)i - 1)
128+
{
129+
//check how many colons in the remaining string
130+
int colons = byteIndex / 2;
131+
for (int k = i + 1; k < len; ++k)
132+
if (str.Substring(k, 1) == ":")
133+
++colons;
134+
int consecutiveZeros = (7 - colons) * 2;
135+
for (int k = 0; k < consecutiveZeros; ++k)
136+
buf[byteIndex++] = 0;
137+
}
138+
else
139+
{
140+
if (curByte < 0 || curByte > 65535 || byteIndex > 15)
141+
return null;
142+
buf[byteIndex++] = (byte)(curByte >> 8);
143+
buf[byteIndex++] = (byte)(curByte & 255);
144+
curByte = 0;
145+
}
146+
lastColonPos = i;
147+
continue;
148+
}
149+
char ch = Convert.ToChar(str.Substring(i, 1));
150+
char value = (char)(ch >= 97 ? ch - 87 : (ch >= 65 ? ch - 55 : ch - 48));
151+
if (value < 0 || value > 15)
152+
return null;
153+
curByte = (curByte << 4) + value;
154+
}
155+
if (byteIndex == 16)
156+
{
157+
long low = (buf[8] & 0xFFL) << 56 | (buf[9] & 0xFFL) << 48 | (buf[10] & 0xFFL) << 40 | (buf[11] & 0xFFL) << 32
158+
| (buf[12] & 0xFFL) << 24 | (buf[13] & 0xFFL) << 16 | (buf[14] & 0xFFL) << 8 | (buf[15] & 0xFFL);
159+
160+
long high = (buf[0] & 0xFFL) << 56 | (buf[1] & 0xFFL) << 48 | (buf[2] & 0xFFL) << 40 | (buf[3] & 0xFFL) << 32
161+
| (buf[4] & 0xFFL) << 24 | (buf[5] & 0xFFL) << 16 | (buf[6] & 0xFFL) << 8 | (buf[7] & 0xFFL);
162+
return new BasicIPAddr(high, low);
163+
}
164+
else
165+
return null;
166+
}
167+
}
168+
169+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using dolphindb.io;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace dolphindb.data
8+
{
9+
public class BasicIPAddrVector : BasicInt128Vector
10+
{
11+
12+
13+
public BasicIPAddrVector(int size):base(size)
14+
{
15+
16+
}
17+
18+
public BasicIPAddrVector(List<Long2> list):base(list)
19+
{
20+
}
21+
22+
public BasicIPAddrVector(Long2[] array):base(array)
23+
{
24+
}
25+
26+
internal BasicIPAddrVector(DATA_FORM df, int size):base(df,size)
27+
{
28+
}
29+
30+
internal BasicIPAddrVector(DATA_FORM df, ExtendedDataInput @in):base(df,@in)
31+
{
32+
}
33+
34+
public override IScalar get(int index)
35+
{
36+
return new BasicIPAddr(values[index].high, values[index].low);
37+
}
38+
39+
public override DATA_TYPE getDataType()
40+
{
41+
return DATA_TYPE.DT_IPADDR;
42+
}
43+
44+
public override Type getElementClass()
45+
{
46+
return typeof(BasicIPAddr);
47+
}
48+
}
49+
50+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using dolphindb.io;
3+
4+
namespace dolphindb.data
5+
{
6+
public class BasicInt128 : AbstractScalar
7+
{
8+
9+
protected Long2 value;
10+
11+
public BasicInt128(long high, long low)
12+
{
13+
value = new Long2(high, low);
14+
}
15+
16+
public BasicInt128(ExtendedDataInput @in)
17+
{
18+
value = @in.readLong2();
19+
}
20+
21+
public Long2 getLong2()
22+
{
23+
return value;
24+
}
25+
26+
27+
public override bool isNull()
28+
{
29+
return value.isNull();
30+
}
31+
32+
33+
public override void setNull()
34+
{
35+
value.setNull();
36+
}
37+
38+
public long getMostSignicantBits()
39+
{
40+
return value.high;
41+
}
42+
43+
public long getLeastSignicantBits()
44+
{
45+
return value.low;
46+
}
47+
48+
public override Number getNumber()
49+
{
50+
throw new Exception("Imcompatible data type");
51+
}
52+
53+
public override object getObject()
54+
{
55+
throw new NotImplementedException();
56+
}
57+
58+
public override void setObject(object value)
59+
{
60+
throw new NotImplementedException();
61+
}
62+
public override Object getTemporal()
63+
{
64+
throw new Exception("Imcompatible data type");
65+
}
66+
67+
public override DATA_CATEGORY getDataCategory()
68+
{
69+
return DATA_CATEGORY.BINARY;
70+
}
71+
public override DATA_TYPE getDataType()
72+
{
73+
return DATA_TYPE.DT_INT128;
74+
}
75+
76+
public override String getString()
77+
{
78+
return String.Format("%016x%016x", value.high, value.low);
79+
}
80+
81+
public bool equals(Object o)
82+
{
83+
if (!(o is BasicInt128) || o == null)
84+
return false;
85+
else
86+
return value.equals(((BasicInt128)o).value);
87+
}
88+
89+
90+
public int hashCode()
91+
{
92+
return value.hashCode();
93+
}
94+
95+
96+
protected override void writeScalarToOutputStream(ExtendedDataOutput @out)
97+
{
98+
@out.writeLong2(value);
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)