コード例 #1
0
ファイル: UpsertTest.cs プロジェクト: wangwe/Dgraph-dotnet
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        static async Task Main(string[] args)
        {
            using (IDgraphClient client = DgraphDotNet.Clients.NewDgraphClient()) {
                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.NewTransaction()) {
                        var mutation = txn.NewMutation();
                        var property = Clients.BuildProperty(node, "Password", GraphValue.BuildPasswordValue(password));
                        if (property.IsFailed)
                        {
                            // ... something went wrong
                            Console.Write("uhh");
                        }
                        else
                        {
                            mutation.AddProperty(property.Value);
                            var err = await mutation.Submit();

                            if (err.IsFailed)
                            {
                                // ... something went wrong
                                Console.Write(err);
                            }
                        }
                        await txn.Commit();
                    }
                }
            }
        }