Skip to content

Commit f4cf860

Browse files
authored
Merge pull request #16 from zhouyuguangsc/v3.0.0
update version to 3.0.0
2 parents bdb89da + 4580013 commit f4cf860

File tree

304 files changed

+2397
-121858
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

304 files changed

+2397
-121858
lines changed

GraphClientExample/Program.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NebulaNet;
22
using System;
3+
using System.Net;
34
using System.Text;
45
using System.Threading.Tasks;
56

@@ -9,32 +10,50 @@ internal class Program
910
{
1011
private static async Task Main(string[] args)
1112
{
12-
GraphClient graphClient = new GraphClient("127.0.0.1", 9669);
13-
graphClient.Authenticate("root", "nebula");
14-
Console.WriteLine(graphClient.sessionId);
13+
NebulaConnection graphClient = new NebulaConnection();
14+
await graphClient.OpenAsync("127.0.0.1", 9669);
15+
var authResponse = await graphClient.AuthenticateAsync("root", "123456");
16+
Console.WriteLine(authResponse.Session_id);
1517

1618
StringBuilder sb = new StringBuilder();
1719
sb.Append("CREATE SPACE IF NOT EXISTS test(vid_type=FIXED_STRING(30));");
1820
sb.Append("USE test;");
1921
sb.Append("CREATE TAG IF NOT EXISTS person(name string, age int);");
2022
sb.Append("CREATE EDGE like (likeness double);");
2123

22-
var executionResponse = await graphClient.Execute(sb.ToString());
24+
var executionResponse = await graphClient.ExecuteAsync(authResponse.Session_id, sb.ToString());
2325

2426
await Task.Delay(10000);
2527

26-
executionResponse = await graphClient.Execute("INSERT VERTEX person(name, age) VALUES \"Bob\":(\"Bob\", 10), \"Lily\":(\"Lily\", 9);");
28+
executionResponse = await graphClient.ExecuteAsync(authResponse.Session_id, "INSERT VERTEX person(name, age) VALUES \"Bob\":(\"Bob\", 10), \"Lily\":(\"Lily\", 9);");
2729
await Task.Delay(5000);
28-
executionResponse = await graphClient.Execute("INSERT EDGE like(likeness) VALUES \"Bob\"->\"Lily\":(80.0);");
30+
executionResponse = await graphClient.ExecuteAsync(authResponse.Session_id, "INSERT EDGE like(likeness) VALUES \"Bob\"->\"Lily\":(80.0);");
2931
await Task.Delay(5000);
30-
executionResponse = await graphClient.Execute("FETCH PROP ON person \"Bob\" YIELD vertex as node;");
32+
executionResponse = await graphClient.ExecuteAsync(authResponse.Session_id, "FETCH PROP ON person \"Bob\" YIELD vertex as node;");
3133
await Task.Delay(5000);
32-
executionResponse = await graphClient.Execute("FETCH PROP ON like \"Bob\"->\"Lily\" YIELD edge as e;");
34+
executionResponse = await graphClient.ExecuteAsync(authResponse.Session_id, "FETCH PROP ON like \"Bob\"->\"Lily\" YIELD edge as e;");
3335
await Task.Delay(5000);
34-
executionResponse = await graphClient.Execute("DROP SPACE test;");
36+
37+
//
38+
var testDtos = await graphClient.ExecuteAsync(authResponse.Session_id, "FETCH PROP ON person \"Bob\",\"Lily\" YIELD properties(vertex).name AS name,properties(vertex).age AS age;")
39+
.ToListAsync<TestDto>();
40+
foreach (var item in testDtos)
41+
{
42+
Console.WriteLine($"1.name:{item.Name},age:{item.Age}");
43+
}
44+
45+
executionResponse = await graphClient.ExecuteAsync(authResponse.Session_id, "DROP SPACE test;");
3546
await Task.Delay(5000);
3647

37-
await graphClient.SignOff();
48+
await graphClient.SignOutAsync(authResponse.Session_id);
49+
50+
51+
Console.ReadKey();
3852
}
3953
}
54+
public class TestDto
55+
{
56+
public string Name { get; set; }
57+
public long Age { get; set; }
58+
}
4059
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Nebula.Graph;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace NebulaNet
10+
{
11+
public static class EnumerableExtensions
12+
{
13+
public static async Task<T[]> ToArrayAsync<T>(this Task<ExecutionResponse> executionTask)
14+
{
15+
var executionResponse = await executionTask;
16+
if (executionResponse.Data == null)
17+
return default;
18+
if (executionResponse.Data.Rows.Count != 1)
19+
return default;//不能解析多行
20+
21+
return (T[])executionResponse.Data.Rows[0].Values[0].Mapping(typeof(T[]));
22+
}
23+
public static async Task<IList<T>> ToListAsync<T>(this Task<ExecutionResponse> executionTask)
24+
{
25+
var executionResponse = await executionTask;
26+
if (executionResponse.Data == null)
27+
return default;
28+
29+
//获取列名
30+
var columnNames = executionResponse.Data.Column_names
31+
.Select(x => Encoding.UTF8.GetString(x).ToLower()).ToArray();
32+
33+
//查找可用属性和数据索引
34+
var indexAndProps = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)//BindingFlags.IgnoreCase
35+
.Select(x => new { Index = Array.IndexOf(columnNames, x.Name.ToLower()), Prop = x })
36+
.Where(x => x.Index >= 0);
37+
38+
//映射对象
39+
var result = new List<T>();
40+
foreach (var row in executionResponse.Data.Rows)
41+
{
42+
var o = Activator.CreateInstance<T>();
43+
foreach (var item in indexAndProps)
44+
{
45+
item.Prop.SetValue(o, row.Values[item.Index].Mapping(item.Prop.PropertyType));
46+
}
47+
result.Add(o);
48+
}
49+
50+
return result;
51+
}
52+
}
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.Extensions.ObjectPool;
2+
using NebulaNet;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace Microsoft.Extensions.DependencyInjection
8+
{
9+
public static class NebulaServiceExtension
10+
{
11+
public static IServiceCollection AddNebulaGraph(this IServiceCollection services,Action<NebulaConfig> configProvider)
12+
{
13+
var config = new NebulaConfig(configProvider);
14+
15+
services.AddSingleton<ObjectPool<NebulaConnection>>(serviceProvider =>
16+
{
17+
var objectPoolProvider = new DefaultObjectPoolProvider();
18+
objectPoolProvider.MaximumRetained = 30;
19+
return objectPoolProvider.Create(new NebulaConnPoolPolicy(config));
20+
});
21+
services.AddSingleton<ObjectPool<SessionId>>(serviceProvider =>
22+
{
23+
var objectPoolProvider=new DefaultObjectPoolProvider();
24+
objectPoolProvider.MaximumRetained = 30;
25+
return objectPoolProvider.Create(new NebulaSessionIdPoolPolicy());
26+
});
27+
services.AddSingleton(serviceProvider =>
28+
{
29+
var connPool = serviceProvider.GetRequiredService<ObjectPool<NebulaConnection>>();
30+
var sessionIdPool = serviceProvider.GetRequiredService<ObjectPool<SessionId>>();
31+
return new NebulaPool(connPool, sessionIdPool, config);
32+
});
33+
34+
return services;
35+
}
36+
}
37+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using Nebula.Common;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Text;
8+
9+
namespace NebulaNet
10+
{
11+
internal static class ValueMappingExtensions
12+
{
13+
internal static object Mapping(this @Value value, Type targetType)
14+
{
15+
Type mapType = null;
16+
switch (targetType.Name)
17+
{
18+
case "Bool":
19+
return value.BVal;
20+
case "Int64":
21+
return value.IVal;
22+
case "Double":
23+
return value.FVal;
24+
case "String":
25+
return Encoding.UTF8.GetString(value.SVal);
26+
case "Nebula.Common.Date":
27+
return value.DVal;
28+
case "Nebula.Common.Time":
29+
return value.TVal;
30+
case "DateTime":
31+
return value.DtVal;
32+
case "Vertex":
33+
return new NotImplementedException(); //value.VVal;
34+
case "Edge":
35+
return new NotImplementedException(); // value.EVal;
36+
case "Path":
37+
return new NotImplementedException(); // value.PVal;
38+
case "Dictionary":
39+
return new NotImplementedException(); //value.MVal;
40+
case "HashSet":
41+
return new NotImplementedException(); //value.UVal;
42+
case "DataSet":
43+
return new NotImplementedException(); //value.GVal;
44+
case "Geography":
45+
return new NotImplementedException(); //value.GgVal;
46+
case "Duration":
47+
return new NotImplementedException(); //value.DuVal;
48+
case "List`1"://List<T>
49+
mapType = targetType.GetGenericArguments()[0];
50+
if (mapType.Name== "Int64")
51+
{
52+
return value.LVal.Values.Select(x => x.Mapping(mapType)).Cast<long>().ToList();
53+
}
54+
if (mapType.Name == "String")
55+
{
56+
return value.LVal.Values.Select(x => x.Mapping(mapType)).Cast<string>().ToList();
57+
}
58+
return new TypeLoadException();
59+
default:
60+
if (targetType.IsArray)//数组
61+
{
62+
mapType = targetType.GetElementType();
63+
if (mapType.Name == "Int64")
64+
{
65+
return value.LVal.Values.Select(x => x.Mapping(mapType)).Cast<long>().ToArray();
66+
}
67+
if (mapType.Name == "String")
68+
{
69+
return value.LVal.Values.Select(x => x.Mapping(mapType)).Cast<string>().ToArray();
70+
}
71+
return new TypeLoadException();
72+
}
73+
return new TypeLoadException();
74+
}
75+
}
76+
77+
/// <summary>
78+
/// 将一个对象转换为指定类型
79+
/// </summary>
80+
/// <param name="obj">待转换的对象</param>
81+
/// <param name="type">目标类型</param>
82+
/// <returns>转换后的对象</returns>
83+
private static object ConvertToObject(object obj, Type type)
84+
{
85+
if (type == null) return obj;
86+
if (obj == null) return type.IsValueType ? Activator.CreateInstance(type) : null;
87+
88+
Type underlyingType = Nullable.GetUnderlyingType(type);
89+
// 如果待转换对象的类型与目标类型兼容,则无需转换
90+
if (type.IsInstanceOfType(obj))
91+
{
92+
return obj;
93+
}
94+
// 如果待转换的对象的基类型为枚举
95+
else if ((underlyingType ?? type).IsEnum)
96+
{
97+
// 如果目标类型为可空枚举,并且待转换对象为null 则直接返回null值
98+
if (underlyingType != null && string.IsNullOrEmpty(obj.ToString()))
99+
{
100+
return null;
101+
}
102+
else
103+
{
104+
return Enum.Parse(underlyingType ?? type, obj.ToString());
105+
}
106+
}
107+
// 如果目标类型的基类型实现了IConvertible,则直接转换
108+
else if (typeof(IConvertible).IsAssignableFrom(underlyingType ?? type))
109+
{
110+
try
111+
{
112+
return Convert.ChangeType(obj, underlyingType ?? type, null);
113+
}
114+
catch
115+
{
116+
return underlyingType == null ? Activator.CreateInstance(type) : null;
117+
}
118+
}
119+
else
120+
{
121+
TypeConverter converter = TypeDescriptor.GetConverter(type);
122+
if (converter.CanConvertFrom(obj.GetType()))
123+
{
124+
return converter.ConvertFrom(obj);
125+
}
126+
ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
127+
if (constructor != null)
128+
{
129+
object o = constructor.Invoke(null);
130+
PropertyInfo[] propertys = type.GetProperties();
131+
Type oldType = obj.GetType();
132+
foreach (PropertyInfo property in propertys)
133+
{
134+
PropertyInfo p = oldType.GetProperty(property.Name);
135+
if (property.CanWrite && p != null && p.CanRead)
136+
{
137+
property.SetValue(o, ConvertToObject(p.GetValue(obj, null), property.PropertyType), null);
138+
}
139+
}
140+
return o;
141+
}
142+
}
143+
return obj;
144+
}
145+
146+
}
147+
}

0 commit comments

Comments
 (0)