private async Task ProcessGenres(IDgraphBatchingClient client) { // Each line in genre file looks like // // genre-name|genreID // // We'll use a client-side node named "genre<genreID>" to identify each genre node. // That name isn't persisted in the store in this example, it's just for client-side reference. using (FileStream fs = new FileStream(genreFile, FileMode.Open)) { using (StreamReader genres = new StreamReader(fs)) { string line; while ((line = genres.ReadLine()) != null) { Interlocked.Increment(ref numProcessed); var split = line.Split(new char[] { '|' }); if (split.Length == 2) { var node = await client.GetOrCreateNode("genre" + split[1]); if (node.IsSuccess) { var edge = Clients.BuildProperty(node.Value, "name", GraphValue.BuildStringValue(split[0])); if (edge.IsSuccess) { await client.BatchAddProperty(edge.Value); } } } } } } }
public async Task UpsertHandlesTransactionConflict() { var txn = Substitute.For <ITransaction>(); transactionFactory.NewTransaction(client).Returns(txn); txn.Query(Arg.Any <string>()).Returns(Results.Ok <string>("{\"q\":[]}"), Results.Ok <string>("{\"q\":[{\"uid\":\"0x1bf\"}]}")); txn.Mutate(Arg.Any <string>()).Returns(Results.Ok <IDictionary <string, string> >(new Dictionary <string, string> { { "blank-0", "0xfff" } })); txn.Commit().Returns(Results.Fail("This transaction had a conflict")); var result = await client.Upsert("aPredicate", GraphValue.BuildStringValue("aString"), "a mutation"); Assert.IsTrue(result.IsSuccess); var(node, existed) = result.Value; Assert.IsTrue(existed); switch (node) { case UIDNode uidnode: Assert.AreEqual((ulong)447, uidnode.UID); break; default: Assert.Fail("Wrong node type"); break; } }
public void UpsertAllocatesNewNode() { var txn = Substitute.For <ITransaction>(); transactionFactory.NewTransaction(client).Returns(txn); txn.Query(Arg.Any <string>()).Returns(Results.Ok <string>("{\"q\":[]}")); txn.Mutate(Arg.Any <string>()).Returns(Results.Ok <IDictionary <string, string> >(new Dictionary <string, string> { { "upsertNode", "0x1bf" } })); txn.Commit().Returns(Results.Ok()); var result = client.Upsert("aPredicate", GraphValue.BuildStringValue("aString")); Assert.IsTrue(result.IsSuccess); var(node, existed) = result.Value; Assert.IsFalse(existed); switch (node) { case UIDNode uidnode: Assert.AreEqual((ulong)447, uidnode.UID); break; default: Assert.Fail("Wrong node type"); break; } }
public async Task UpsertReturnsExistingNode() { var txn = Substitute.For <ITransaction>(); transactionFactory.NewTransaction(client).Returns(txn); txn.Query(Arg.Any <string>()).Returns(Results.Ok <string>("{\"q\":[{\"uid\":\"0x1bf\"}]}")); var result = await client.Upsert( "aPredicate", GraphValue.BuildStringValue("aString"), "a mutation", "aBlankName"); Assert.IsTrue(result.IsSuccess); var(node, existed) = result.Value; Assert.IsTrue(existed); switch (node) { case UIDNode uidnode: Assert.AreEqual((ulong)447, uidnode.UID); break; default: Assert.Fail("Wrong node type"); break; } }
public void ValuesAreRight() { Assert.AreEqual( true, GraphValue.BuildBoolValue(true).BoolValue); Assert.AreEqual( new byte[] { 0x20, 0x20, 0x20 }, GraphValue.BuildBytesValue(new byte[] { 0x20, 0x20, 0x20 }).Bytesvalue); var now = DateTime.Now; Assert.AreEqual( Encoding.UTF8.GetBytes( now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo)), GraphValue.BuildDateValue( Encoding.UTF8.GetBytes( now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo))).DateValue); var valNow = GraphValue.BuildDateValue(now); Assert.AreEqual( now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo), Encoding.UTF8.GetString(valNow.DateValue, 0, valNow.DateValue.Length)); Assert.AreEqual( "blaa", GraphValue.BuildDefaultValue("blaa").DefaultValue); Assert.AreEqual( 123, GraphValue.BuildDoubleValue(123).DoubleValue); Assert.AreEqual( Encoding.UTF8.GetBytes("{'type':'Point','coordinates':[-122.4220186,37.772318]}"), GraphValue.BuildGeoValue( Encoding.UTF8.GetBytes("{'type':'Point','coordinates':[-122.4220186,37.772318]}")).GeoValue); var geojson = "{'type':'Point','coordinates':[-122.4220186,37.772318]}"; var geojsonVal = GraphValue.BuildGeoValue(geojson); Assert.AreEqual( geojson, Encoding.UTF8.GetString(geojsonVal.GeoValue, 0, geojsonVal.GeoValue.Length)); Assert.AreEqual( 123, GraphValue.BuildIntValue(123).IntValue); Assert.AreEqual( "secret", GraphValue.BuildPasswordValue("secret").PasswordValue); Assert.AreEqual( "something", GraphValue.BuildStringValue("something").StringValue); }
static void Main(string[] args) { using (IDgraphMutationsClient client = DgraphDotNet.Clients.NewDgraphMutationsClient("127.0.0.1:5080")) { client.Connect("127.0.0.1:9080"); client.AlterSchema( "Username: string @index(hash) .\n" + "Password: password ."); while (true) { Console.WriteLine("Hi, please enter your new username"); var username = Console.ReadLine(); // use Upsert to test for a node and value, and create if // not already in the graph as an atomic operation. var result = client.Upsert("Username", GraphValue.BuildStringValue(username)); if (result.IsFailed) { Console.WriteLine("Something went wrong : " + result); continue; } var(node, existed) = result.Value; if (existed) { Console.WriteLine("This user already existed. Try another username."); continue; } Console.WriteLine("Hi, please enter a password for the new user"); var password = Console.ReadLine(); using (var txn = client.NewTransactionWithMutations()) { var mutation = txn.NewMutation(); var property = Clients.BuildProperty(node, "Password", GraphValue.BuildPasswordValue(password)); if (property.IsFailed) { // ... something went wrong } else { mutation.AddProperty(property.Value); var err = mutation.Submit(); if (err.IsFailed) { // ... something went wrong } } txn.Commit(); } } } }
public async Task UpsertReturnsError() { var txn = Substitute.For <ITransaction>(); transactionFactory.NewTransaction(client).Returns(txn); txn.Query(Arg.Any <string>()).Returns(Results.Fail <string>("This didn't work")); var node = await client.Upsert("aPredicate", GraphValue.BuildStringValue("aString"), "a mutation"); Assert.IsTrue(node.IsFailed); }
private async Task UpsertExistingNodeReturnsTrue(IDgraphClient client) { var upsertResult = await client.Upsert( nameof(User.Username), GraphValue.BuildStringValue(User1.Username), JsonConvert.SerializeObject(User1)); AssertResultIsSuccess(upsertResult); var(node, existing) = upsertResult.Value; existing.Should().BeTrue(); string.Format("0x{0:x}", node.UID).Should().Be(User1.uid); }
private async Task UpsertNewNodeReturnsFalse(IDgraphClient client) { User1.uid = "_:myBlank"; var upsertResult = await client.Upsert( nameof(User.Username), GraphValue.BuildStringValue(User1.Username), JsonConvert.SerializeObject(User1), "myBlank"); AssertResultIsSuccess(upsertResult); var(node, existing) = upsertResult.Value; existing.Should().BeFalse(); User1.uid = string.Format("0x{0:x}", node.UID); }
private async Task ConcurrentUpsertsWorkCorrectly(IDgraphClient client) { var user2 = MintAUser("User2"); user2.uid = "_:myBlank"; var predicate = nameof(User.Username); var username = GraphValue.BuildStringValue(user2.Username); var json = JsonConvert.SerializeObject(user2); var tasks = Enumerable.Range(0, 10). Select(i => client.Upsert( predicate, username, json, "myBlank")); var results = await Task.WhenAll(tasks.ToList()); foreach (var result in results) { AssertResultIsSuccess(result); } // Only one upsert should have acually written a value results.Select(r => r.Value.existing).Where(exists => !exists).Count().Should().Be(1); // Only one uid is ever seen var theUids = results.Select(r => r.Value.node.UID).Distinct(); theUids.Count().Should().Be(1); // There's only one copy of user2 in the DB var queryResult = await client.QueryWithVars( UsersByName, new Dictionary <string, string> { { "$name", user2.Username } }); AssertResultIsSuccess(queryResult); var users = (JArray)JObject.Parse(queryResult.Value) ["q"]; users.Count.Should().Be(1); var user = users[0].ToObject <User>(); string.Format("0x{0:x}", theUids.First()).Should().Be(user.uid); user.Username.Should().Be(user2.Username); }
public void ValuesAreRightType() { Assert.IsTrue(GraphValue.BuildBoolValue(true).IsBoolValue); Assert.IsTrue(GraphValue.BuildBytesValue(new byte[] { 0x20, 0x20, 0x20 }).IsBytesValue); Assert.IsTrue(GraphValue.BuildDateValue( Encoding.UTF8.GetBytes( DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo))) .IsDateValue); Assert.IsTrue(GraphValue.BuildDateValue(DateTime.Now).IsDateValue); Assert.IsTrue(GraphValue.BuildDefaultValue("blaa").IsDefaultValue); Assert.IsTrue(GraphValue.BuildDoubleValue(123).IsDoubleValue); Assert.IsTrue(GraphValue.BuildGeoValue( Encoding.UTF8.GetBytes("{'type':'Point','coordinates':[-122.4220186,37.772318]}")) .IsGeoValue); Assert.IsTrue(GraphValue.BuildGeoValue("{'type':'Point','coordinates':[-122.4220186,37.772318]}").IsGeoValue); Assert.IsTrue(GraphValue.BuildIntValue(123).IsIntValue); Assert.IsTrue(GraphValue.BuildPasswordValue("secret").IsPasswordValue); Assert.IsTrue(GraphValue.BuildStringValue("something").IsStringValue); }
static async Task Main(string[] args) { using (IDgraphMutationsClient client = DgraphDotNet.Clients.NewDgraphMutationsClient("127.0.0.1:5080")) { client.Connect("127.0.0.1:9080"); await client.AlterSchema( "Username: string @index(hash) .\n" + "Password: password ."); var schemaResult = await client.SchemaQuery(); if (schemaResult.IsFailed) { Console.WriteLine($"Something went wrong getting schema."); return; } Console.WriteLine("Queried schema and got :"); foreach (var predicate in schemaResult.Value.Schema) { Console.WriteLine(predicate.ToString()); } while (true) { Console.WriteLine("Hi, please enter your new username"); var username = Console.ReadLine(); // use Upsert to test for a node and value, and create if // not already in the graph as an atomic operation. var result = await client.Upsert( "Username", GraphValue.BuildStringValue(username), $"{{\"uid\": \"_:myBlank\", \"Username\": \"{username}\"}}", "myBlank"); if (result.IsFailed) { Console.WriteLine("Something went wrong : " + result); continue; } var(node, existed) = result.Value; if (existed) { Console.WriteLine("This user already existed. Try another username."); continue; } Console.WriteLine("Hi, please enter a password for the new user"); var password = Console.ReadLine(); using (var txn = client.NewTransactionWithMutations()) { var mutation = txn.NewMutation(); var property = Clients.BuildProperty(node, "Password", GraphValue.BuildPasswordValue(password)); if (property.IsFailed) { // ... something went wrong } else { mutation.AddProperty(property.Value); var err = await mutation.Submit(); if (err.IsFailed) { // ... something went wrong } } await txn.Commit(); } } } }
private Task ProcessMovies(IDgraphBatchingClient client) { return(Task.Run(() => { // The lines of the movie file look like // // movieID|movie-name|date||imdb-address|genre0?|genre1?|...|genre18? // // We'll use "movie<movieID>" as the node name using (FileStream fs = new FileStream(movieFile, FileMode.Open)) { using (StreamReader movies = new StreamReader(fs)) { string line; while ((line = movies.ReadLine()) != null) { Interlocked.Increment(ref numProcessed); var split = line.Split(new char[] { '|' }); if (split.Length == 24) { var movieNode = client.GetOrCreateNode("movie" + split[0]); if (movieNode.IsSuccess) { var name = Clients.BuildProperty(movieNode.Value, "name", GraphValue.BuildStringValue(split[1])); if (name.IsSuccess) { client.BatchAddProperty(name.Value); // 1 in the column means the movie has the corresponding genre for (int i = 5; i < 24; i++) { if (split[i] == "1") { var genreNode = client.GetOrCreateNode("genre" + (i - 5)); if (genreNode.IsSuccess) { var genre = Clients.BuildEdge(movieNode.Value, "genre", genreNode.Value); if (genre.IsSuccess) { client.BatchAddEdge(genre.Value); } } } } } } } } } } })); }
private Task ProcessUsers(IDgraphBatchingClient client) { return(Task.Run(() => { // Each line in the user file looks like // // userID|age|genre|occupation|ZIPcode // // We'll use a node named "user<userID>" to identify each user node using (FileStream fs = new FileStream(userFile, FileMode.Open)) { using (StreamReader users = new StreamReader(fs)) { string line; while ((line = users.ReadLine()) != null) { Interlocked.Increment(ref numProcessed); var split = line.Split(new char[] { '|' }); if (split.Length == 5 && long.TryParse(split[1], out long age)) { var node = client.GetOrCreateNode("user" + split[0]); if (node.IsSuccess) { var ageEdge = Clients.BuildProperty(node.Value, "age", GraphValue.BuildIntValue(age)); var gender = Clients.BuildProperty(node.Value, "gender", GraphValue.BuildStringValue(split[2])); var occupation = Clients.BuildProperty(node.Value, "occupation", GraphValue.BuildStringValue(split[3])); var zipcode = Clients.BuildProperty(node.Value, "zipcode", GraphValue.BuildStringValue(split[4])); if (ageEdge.IsSuccess && gender.IsSuccess && occupation.IsSuccess && zipcode.IsSuccess) { client.BatchAddProperty(ageEdge.Value); client.BatchAddProperty(gender.Value); client.BatchAddProperty(occupation.Value); client.BatchAddProperty(zipcode.Value); } } } } } } })); }