Пример #1
0
    public async Task <List <Album> > ReadStaleDataAsync(string projectId, string instanceId, string databaseId)
    {
        string connectionString = $"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";

        using var connection = new SpannerConnection(connectionString);
        await connection.OpenAsync();

        var staleness = TimestampBound.OfExactStaleness(TimeSpan.FromSeconds(15));

        using var transaction = await connection.BeginReadOnlyTransactionAsync(staleness);

        using var cmd   = connection.CreateSelectCommand("SELECT SingerId, AlbumId, MarketingBudget FROM Albums");
        cmd.Transaction = transaction;

        var albums = new List <Album>();

        using var reader = await cmd.ExecuteReaderAsync();

        while (await reader.ReadAsync())
        {
            albums.Add(new Album
            {
                SingerId        = reader.GetFieldValue <int>("SingerId"),
                AlbumId         = reader.GetFieldValue <int>("AlbumId"),
                MarketingBudget = reader.IsDBNull(reader.GetOrdinal("MarketingBudget")) ? 0 : reader.GetFieldValue <long>("MarketingBudget")
            });
        }
        return(albums);
    }
        public async Task ReadStaleExact()
        {
            await WriteSampleRowsAsync();

            using (var connection = await _testFixture.GetTestDatabaseConnectionAsync())
            {
                await connection.OpenAsync();

                using (var tx =
                           await connection.BeginReadOnlyTransactionAsync(
                               TimestampBound.OfExactStaleness(TimeSpan.FromSeconds(50))))
                {
                    Assert.Equal(TransactionMode.ReadOnly, tx.Mode);
                    Assert.Equal(TimeSpan.FromSeconds(50), tx.TimestampBound.Staleness);

                    var cmd = connection.CreateSelectCommand(
                        "SELECT * FROM TX WHERE K=@k",
                        new SpannerParameterCollection {
                        { "k", _key, SpannerDbType.String }
                    });
                    cmd.Transaction = tx;
                    using (var reader = await cmd.ExecuteReaderAsync())
                    {
                        Assert.False(await reader.ReadAsync(), "We should have read no rows at this time!");
                    }
                }
            }
        }
Пример #3
0
        public void Equality()
        {
            DateTime timestamp1 = new DateTime(2000, 6, 1, 23, 1, 5, DateTimeKind.Utc);
            TimeSpan staleness1 = TimeSpan.FromSeconds(10);

            DateTime timestamp2 = new DateTime(2005, 1, 1, 1, 1, 1, DateTimeKind.Utc);
            TimeSpan staleness2 = TimeSpan.FromSeconds(30);

            var strong         = TimestampBound.Strong;
            var minRead        = TimestampBound.OfMinReadTimestamp(timestamp1);
            var read           = TimestampBound.OfReadTimestamp(timestamp1);
            var exactStaleness = TimestampBound.OfExactStaleness(staleness1);
            var maxStaleness   = TimestampBound.OfMaxStaleness(staleness1);

            EqualityTester.AssertEqual(strong,
                                       new[] { strong },
                                       new[] { minRead, read, exactStaleness, maxStaleness });

            EqualityTester.AssertEqual(minRead,
                                       new[] { TimestampBound.OfMinReadTimestamp(timestamp1) },
                                       new[] { strong, read, exactStaleness, maxStaleness, TimestampBound.OfMinReadTimestamp(timestamp2) });

            EqualityTester.AssertEqual(read,
                                       new[] { TimestampBound.OfReadTimestamp(timestamp1) },
                                       new[] { strong, minRead, exactStaleness, maxStaleness, TimestampBound.OfReadTimestamp(timestamp2) });

            EqualityTester.AssertEqual(exactStaleness,
                                       new[] { TimestampBound.OfExactStaleness(staleness1) },
                                       new[] { strong, minRead, read, maxStaleness, TimestampBound.OfExactStaleness(staleness2) });

            EqualityTester.AssertEqual(maxStaleness,
                                       new[] { TimestampBound.OfMaxStaleness(staleness1) },
                                       new[] { strong, minRead, read, exactStaleness, TimestampBound.OfMaxStaleness(staleness2) });
        }
Пример #4
0
        public void TimeSpan_Valid(long ticks)
        {
            var timeSpan = new TimeSpan(ticks);

            TimestampBound.OfExactStaleness(timeSpan);
            TimestampBound.OfMaxStaleness(timeSpan);
        }
Пример #5
0
        public void TimeSpan_NegativeIsInvalid()
        {
            var timeSpan = new TimeSpan(-1);

            Assert.Throws <ArgumentOutOfRangeException>(() => TimestampBound.OfExactStaleness(timeSpan));
            Assert.Throws <ArgumentOutOfRangeException>(() => TimestampBound.OfMaxStaleness(timeSpan));
        }
Пример #6
0
        public void ToTransactionOptions_OfExactStaleness()
        {
            var bound = TimestampBound.OfExactStaleness(TimeSpan.FromSeconds(5));

            AssertProtoConversion(new ReadOnly {
                ExactStaleness = new Duration {
                    Seconds = 5
                }
            }, bound);
        }
Пример #7
0
        public async Task ReadStaleExactSingle()
        {
            using (var connection = _fixture.GetConnection())
            {
                await connection.OpenAsync();

                var cmd   = CreateSelectAllCommandForKey(connection);
                var bound = TimestampBound.OfExactStaleness(_fixture.Staleness);
                using (var reader = await cmd.ExecuteReaderAsync(bound))
                {
                    Assert.False(await reader.ReadAsync(), "We should have read no rows at this time!");
                }
            }
        }
Пример #8
0
        public async Task ReadStaleExact()
        {
            using (var connection = _fixture.GetConnection())
            {
                await connection.OpenAsync();

                var bound = TimestampBound.OfExactStaleness(_fixture.Staleness);
                using (var tx = await connection.BeginReadOnlyTransactionAsync(bound))
                {
                    Assert.Equal(TransactionMode.ReadOnly, tx.Mode);
                    Assert.Equal(_fixture.Staleness, tx.TimestampBound.Staleness);

                    var cmd = CreateSelectAllCommandForKey(connection);
                    cmd.Transaction = tx;
                    using (var reader = await cmd.ExecuteReaderAsync())
                    {
                        Assert.False(await reader.ReadAsync(), "We should have read no rows at this time!");
                    }
                }
            }
        }