public async Task test_client_with_deadline() { var channel = new Channel($"{HOSTNAME}:{PORT}", ChannelCredentials.Insecure); var client = new DgraphNetClient(new DgraphConnection(channel), 1); var op = new Operation { Schema = "name: string @index(exact) ." }; // Alters schema without exceeding the given deadline. await client.AlterAsync(op); // Creates a blocking stub directly, in order to force a deadline to be exceeded. var anyConnectionMethod = typeof(DgraphNetClient) .GetMethod("AnyConnection", BindingFlags.NonPublic | BindingFlags.Instance); var(conn, callOptions) = (ValueTuple <DgraphConnection, CallOptions>)anyConnectionMethod .Invoke(client, Array.Empty <object>()); var stub = conn.Client; Thread.Sleep(1001); Assert.Throws <RpcException>(() => stub.Alter(op, callOptions), "Deadline should have been exceeded"); }
public void test_merge_context() { var dst = new LinRead(); dst.Ids.Add(new Dictionary <uint, ulong> { { 1, 10L }, { 2, 15L }, { 3, 10L } }); var src = new LinRead(); src.Ids.Add(new Dictionary <uint, ulong> { { 2, 10L }, { 3, 15L }, { 4, 10L } }); var result = DgraphNetClient.MergeLinReads(dst, src); Assert.AreEqual(4, result.Ids.Count); Assert.AreEqual(10L, result.Ids[1]); Assert.AreEqual(15L, result.Ids[2]); Assert.AreEqual(15L, result.Ids[3]); Assert.AreEqual(10L, result.Ids[4]); }
public async Task ModifyNode(int projectId, string uid, string author, string data, string note, string source, string typeOfData) { DgraphConnection dgraphConnection = new DgraphConnection(_options.Value.Host, _options.Value.Port, ChannelCredentials.Insecure); DgraphConnectionPool dgraphConnectionPool = new DgraphConnectionPool().Add(dgraphConnection); DgraphNetClient dgraphNetClient = new DgraphNetClient(dgraphConnectionPool); using (var transaction = dgraphNetClient.NewTransaction()) { StringBuilder req = new StringBuilder(); req.AppendLine("{"); req.AppendLine("\"uid\" : \"" + uid + "\","); req.AppendLine("\"author\" : \"" + author + "\","); req.AppendLine("\"data\" : \"" + data + "\","); req.AppendLine("\"note\" : \"" + note + "\","); req.AppendLine("\"projectId\" : \"" + projectId + "\","); req.AppendLine("\"source\" : \"" + source + "\","); req.AppendLine("\"typeOfData\" : \"" + typeOfData + "\","); req.AppendLine("\"lastUpdate\" : \"" + DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo) + "\""); req.AppendLine("}"); Mutation mu = new Mutation { SetJson = ByteString.CopyFromUtf8(req.ToString()) }; transaction.Mutate(mu); transaction.Commit(); } }
public async Task OneTimeSetUp() { var pool = new DgraphConnectionPool() .Add(new Channel($"{HOSTNAME}:{PORT}", ChannelCredentials.Insecure)); _client = new DgraphNetClient(pool); await _client.AlterAsync(new Operation { DropAll = true }); }
public void DataUpload(string pathSortedJson, string dgraphSchema) { //connection à dgraph DgraphConnection dgraphConnection = new DgraphConnection("localhost", 9080, ChannelCredentials.Insecure); DgraphConnectionPool dgraphConnectionPool = new DgraphConnectionPool().Add(dgraphConnection); DgraphNetClient dgraphNetClient = new DgraphNetClient(dgraphConnectionPool); //index le schéma string schema = dgraphSchema; Operation op = new Operation { Schema = schema }; dgraphNetClient.Alter(op); //lecture du JSON cible JObject jsonObj = new JObject(); jsonObj = JObject.Parse(File.ReadAllText(pathSortedJson)); //parsing du JSON string jsonString = JsonConvert.SerializeObject(jsonObj); //on upload le JSON sur la bdd dgraph using (Transaction txn = dgraphNetClient.NewTransaction()) { Mutation mu = new Mutation { SetJson = ByteString.CopyFromUtf8(jsonString) }; txn.Mutate(mu); txn.Commit(); } }
static void Main(string[] args) { var connection = new DgraphConnection("localhost", 9080, ChannelCredentials.Insecure); var pool = new DgraphConnectionPool().Add(connection); var client = new DgraphNetClient(pool); client.Alter(new Operation { DropAll = true }); //Index le schéma string schema = "domain_name_index: string @index(hash) ."; Operation op = new Operation { Schema = schema }; client.Alter(op); using (Transaction txn = client.NewTransaction()) { /***************** JSON MIL TEST*********************/ //Ouvre le json cible JObject o1 = JObject.Parse(File.ReadAllText(@"c:\marche.json")); Dictionary <string, JToken> dic_proprety = new Dictionary <string, JToken>(); foreach (JProperty property in o1.Properties()) { if (property.Value.HasValues) { dic_proprety.Add(property.Name, property.Value); } } foreach (var item in dic_proprety) { if (item.Value.GetType() == typeof(JArray)) { if (!item.Value.HasValues) { dic_proprety.Remove(item.Key); } var lolae = item.Value; JArray qs = (JArray)lolae; for (int i = 0; i < qs.Count; i++) { if (qs[i].GetType() == typeof(JArray)) { qs.RemoveAt(i); } } } } JObject k = new JObject(); int b = 0; foreach (var item in dic_proprety) { if (item.Value.HasValues) { JArray array = new JArray(); b++; JObject obj = new JObject( new JObject( new JProperty("name_value_list" + b, item.Value) ) ); array.Add(obj); JObject objSort = new JObject(); objSort["list" + b] = array; JObject u = new JObject(); u.Add("obj" + b, objSort); k.Merge(u, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Union }); } } k.Add("domain_name_index", "Millenium"); //Parse et seria le json cible var json2 = JsonConvert.SerializeObject(k); string json3 = json2.Replace("true", "0"); string json4 = json3.Replace("false", "1"); Console.WriteLine(k); // Run mutation //JObject o7 = JObject.Parse(File.ReadAllText(@"c:\Millenium_sorted.json")); //var json77 = JsonConvert.SerializeObject(o7); Mutation mu = new Mutation { SetJson = ByteString.CopyFromUtf8(json4) }; txn.Mutate(mu); txn.Commit(); Console.ReadLine(); //================CREATE DATA===================== /* Person p = new Person(); * p.Name = "Marceau"; * */ } // Query /* string query = * "query all($a: string){\n" + * " all(func: eq(name,$a)) {\n" + * " name\n" + * " }\n" + * "}\n"; * * IDictionary<string, string> vars = new Dictionary<string, string> * { * { "$a", "Marceau" } * }; * * Response res = client.NewTransaction().QueryWithVars(query, vars); * * //Deserialize * People ppl = JsonConvert.DeserializeObject<People>(res.Json.ToStringUtf8()); * * //Print results * Console.WriteLine($"people found: {ppl.All.Count}"); * * foreach (var p in ppl.All) * { * Console.WriteLine(p.Name); * * } * Console.ReadLine();*/ }
public async Task test_delete_edges() { Person alice = new Person { Name = "Alice", Age = 26, Married = true, Location = "Riley Street" }; School school = new School { Name = "Crown Public School" }; alice.Schools.Add(school); Person bob = new Person { Name = "Bob", Age = 24 }; alice.Friends.Add(bob); Person charlie = new Person { Name = "Charlie", Age = 29 }; alice.Friends.Add(charlie); var op = new Operation { Schema = "age: int .\nmarried: bool ." }; await _client.AlterAsync(op); var mut = new Mutation { CommitNow = true, SetJson = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(alice)) }; var ag = await _client.NewTransaction().MutateAsync(mut); var uid = ag.Uids["blank-0"]; string q = new StringBuilder() .AppendLine("{") .AppendLine($"me(func: uid({uid}))") .AppendLine("{") .AppendLine("uid") .AppendLine("name") .AppendLine("age") .AppendLine("loc") .AppendLine("married") .AppendLine("friends") .AppendLine("{") .AppendLine("uid") .AppendLine("name") .AppendLine("age") .AppendLine("}") .AppendLine("schools") .AppendLine("{") .AppendLine("uid") .AppendLine("name@en") .AppendLine("}") .AppendLine("}") .AppendLine("}") .ToString(); var resp = await _client.NewTransaction().QueryAsync(q); Console.WriteLine(resp.Json.ToStringUtf8()); mut = DgraphNetClient.CreateDeleteEdgesMutation( new Mutation { CommitNow = true }, uid, "friends", "loc" ); await _client.NewTransaction().MutateAsync(mut); resp = await _client.NewTransaction().QueryAsync(q); Console.WriteLine(resp.Json.ToStringUtf8()); var root = JsonConvert.DeserializeObject <Root>(resp.Json.ToStringUtf8()); Assert.IsTrue(root.Me[0].Friends.Count == 0); }
public async Task AddNodesToNode(int projectId, string uidMasterNode, List <AddNodesToNodeData> nodes) { List <string> jsonMutation = new List <string>(); StringBuilder req = new StringBuilder(); for (int i = 0; i < nodes.Count; i++) { AddNodesToNodeData node = nodes[i]; // Verify if node exists string r = GetNodeBy(projectId, node.Data, node.TypeOfData); dynamic nodeDGraph = JsonConvert.DeserializeObject(r); bool nodeExists = nodeDGraph.FindNode.Count > 0; bool nodeHasLink = node.Link != null; if (nodeExists && nodeHasLink) { // On vérifie si les 2 noeuds ne sont pas linké string sourceUid; for (int y = 0; y < node.Link.Count; y++) { if (node.Link[y].Uid == "master") { sourceUid = uidMasterNode; } else { sourceUid = node.Link[y].Uid; } bool nodeAlreadyLinked = false; if (nodeDGraph.FindNode[0]["link"] != null) { for (int z = 0; z < nodeDGraph.FindNode[0].link.Count; z++) { if (nodeDGraph.FindNode[0].link[z].uid == sourceUid) { nodeAlreadyLinked = true; break; } } } if (nodeDGraph.FindNode[0]["~link"] != null) { for (int z = 0; z < nodeDGraph.FindNode[0]["~link"].Count; z++) { if (nodeDGraph.FindNode[0]["~link"][z].uid == sourceUid) { nodeAlreadyLinked = true; break; } } } if (!nodeAlreadyLinked) { await AddEdge(projectId, sourceUid, (string)nodeDGraph.FindNode[0].uid); } } } else { if (nodeHasLink) { for (int y = 0; y < node.Link.Count; y++) { string nodeSource; if (node.Link[y].Uid == "master") { nodeSource = uidMasterNode; } else { nodeSource = node.Link[y].Uid; } string nodeTarget = node.Data + node.TypeOfData; req = new StringBuilder(); req.AppendLine("{"); req.AppendLine("\"uid\" : \"" + nodeSource + "\","); req.AppendLine("\"link\" : {"); req.AppendLine("\"uid\" : \"_:" + nodeTarget + "\""); req.AppendLine("}"); req.AppendLine("}"); jsonMutation.Add(req.ToString()); } } req = new StringBuilder(); req.AppendLine("{"); req.AppendLine("\"uid\" : \"_:" + node.Data + node.TypeOfData + "\","); req.AppendLine("\"data\" : \"" + node.Data + "\","); req.AppendLine("\"author\" : \"" + node.Author + "\","); req.AppendLine("\"note\" : \"" + node.Note + "\","); req.AppendLine("\"projectId\" : \"" + node.ProjectId + "\","); req.AppendLine("\"source\" : \"" + node.Source + "\","); req.AppendLine("\"typeOfData\" : \"" + node.TypeOfData + "\","); req.AppendLine("\"lastUpdate\" : \"" + node.LastUpdate + "\""); req.AppendLine("}"); jsonMutation.Add(req.ToString()); } } req = new StringBuilder(); req.AppendLine("["); for (int n = 0; n < jsonMutation.Count; n++) { string json = jsonMutation[n]; req.AppendLine(json); if (n != jsonMutation.Count - 1) { req.AppendLine(","); } } req.AppendLine("]"); // Fourberie mais ça veut dire que y'a pas de changement donc ça sert à rien if (req.ToString().Length != 6) { //SAVE DATA DgraphConnection dgraphConnection = new DgraphConnection(_options.Value.Host, _options.Value.Port, ChannelCredentials.Insecure); DgraphConnectionPool dgraphConnectionPool = new DgraphConnectionPool().Add(dgraphConnection); DgraphNetClient dgraphNetClient = new DgraphNetClient(dgraphConnectionPool); using (var transaction = dgraphNetClient.NewTransaction()) { Console.WriteLine(req.ToString()); Mutation mu = new Mutation { SetJson = ByteString.CopyFromUtf8(req.ToString()) }; transaction.Mutate(mu); transaction.Commit(); } } }