public void NegativeOneIsUtcNow() { var negativeOne = new BigtableVersion(-1); var utcNow = new BigtableVersion(DateTime.UtcNow); // Verify they are not more than 1 second apart Assert.True(utcNow.Value - negativeOne.Value < 1_000); }
public BigtableClientFixture() { BigtableClient bigtableClient = BigtableClient.Create(); _bigtableTableAdminClient = BigtableTableAdminClient.Create(); Table table = new Table { Granularity = Table.Types.TimestampGranularity.Millis }; table.ColumnFamilies.Add("stats_summary", new ColumnFamily()); table.ColumnFamilies.Add("cell_plan", new ColumnFamily()); CreateTableRequest createTableRequest = new CreateTableRequest { ParentAsInstanceName = new InstanceName(projectId, instanceId), Table = table, TableId = tableId, }; _bigtableTableAdminClient.CreateTable(createTableRequest); TableName tableName = new TableName(projectId, instanceId, tableId); BigtableVersion timestamp = new BigtableVersion(new DateTime(2020, 1, 10, 14, 0, 0, DateTimeKind.Utc)); BigtableVersion timestamp_minus_hr = new BigtableVersion(new DateTime(2020, 1, 10, 13, 0, 0, DateTimeKind.Utc)); MutateRowsRequest.Types.Entry[] entries = { Mutations.CreateEntry(new BigtableByteString("phone#4c410523#20190501"), Mutations.SetCell("cell_plan", "data_plan_01gb", "false", timestamp), Mutations.SetCell("cell_plan", "data_plan_01gb", "true", timestamp_minus_hr), Mutations.SetCell("cell_plan", "data_plan_05gb", "true", timestamp), Mutations.SetCell("stats_summary", "connected_cell", "1", timestamp), Mutations.SetCell("stats_summary", "connected_wifi", "1", timestamp), Mutations.SetCell("stats_summary", "os_build", "PQ2A.190405.003", timestamp)), Mutations.CreateEntry(new BigtableByteString("phone#4c410523#20190502"), Mutations.SetCell("cell_plan", "data_plan_05gb", "true", timestamp), Mutations.SetCell("stats_summary", "connected_cell", "1", timestamp), Mutations.SetCell("stats_summary", "connected_wifi", "1", timestamp), Mutations.SetCell("stats_summary", "os_build", "PQ2A.190405.004", timestamp)), Mutations.CreateEntry(new BigtableByteString("phone#4c410523#20190505"), Mutations.SetCell("cell_plan", "data_plan_05gb", "true", timestamp), Mutations.SetCell("stats_summary", "connected_cell", "0", timestamp), Mutations.SetCell("stats_summary", "connected_wifi", "1", timestamp), Mutations.SetCell("stats_summary", "os_build", "PQ2A.190406.000", timestamp)), Mutations.CreateEntry(new BigtableByteString("phone#5c10102#20190501"), Mutations.SetCell("cell_plan", "data_plan_10gb", "true", timestamp), Mutations.SetCell("stats_summary", "connected_cell", "1", timestamp), Mutations.SetCell("stats_summary", "connected_wifi", "1", timestamp), Mutations.SetCell("stats_summary", "os_build", "PQ2A.190401.002", timestamp)), Mutations.CreateEntry(new BigtableByteString("phone#5c10102#20190502"), Mutations.SetCell("cell_plan", "data_plan_10gb", "true", timestamp), Mutations.SetCell("stats_summary", "connected_cell", "1", timestamp), Mutations.SetCell("stats_summary", "connected_wifi", "0", timestamp), Mutations.SetCell("stats_summary", "os_build", "PQ2A.190406.000", timestamp)) }; bigtableClient.MutateRows(tableName, entries); }
public void ScaleToDateTime() { var utcNow = DateTime.UtcNow; var v1 = new BigtableVersion(12345); var v2 = new BigtableVersion(12345 + 1_000); var timespanDiff = v2.ToDateTime() - v1.ToDateTime(); Assert.Equal(TimeSpan.FromSeconds(1), timespanDiff); }
public void ScaleFromDateTime() { var utcNow = DateTime.UtcNow; var v1 = new BigtableVersion(utcNow); var v2 = new BigtableVersion(utcNow + TimeSpan.FromSeconds(1)); Assert.Equal(1_000, v2.Value - v1.Value); Assert.Equal(1_000_000, v2.Micros - v1.Micros); }
// [END bigtable_filters_limit_value_regex] // [START bigtable_filters_limit_timestamp_range] /// <summary> /// /// Read using a timestamp range filter from an existing table. ///</summary> /// <param name="projectId">Your Google Cloud Project ID.</param> /// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param> /// <param name="tableId">Your Google Cloud Bigtable table ID.</param> public string filterLimitTimestampRange( String projectId, String instanceId, String tableId) { BigtableVersion timestamp_minus_hr = new BigtableVersion(new DateTime(2020, 1, 10, 13, 0, 0, DateTimeKind.Utc)); // A filter that matches cells whose timestamp is from an hour ago or earlier RowFilter filter = RowFilters.TimestampRange(new DateTime(0), timestamp_minus_hr.ToDateTime()); return(readFilter(projectId, instanceId, tableId, filter)); }
public void RoundTripDateTime() { var dateTime = DateTime.UtcNow; var version = new BigtableVersion(dateTime); // BigtableVersion stores micros, but aligns to millis, which is 10000s of ticks, so truncate the ticks. var expected = new DateTime(dateTime.Ticks - (dateTime.Ticks % 10000), DateTimeKind.Utc); Assert.Equal(expected, version.ToDateTime()); }
public void FormattingDateTime(string expectedText, string dateTimeText) { var dateTime = DateTime.ParseExact( dateTimeText, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); var version = new BigtableVersion(dateTime); Assert.Equal(expectedText, version.ToString()); }
protected override async Task SaveAsync(SnapshotMetadata metadata, object snapshot) { var snapshotBytes = SnapshotToBytes(metadata, snapshot, Context.System); byte[] snapshotMetadataBytes = SnapshotMetadataToBytes(metadata); var request = new MutateRowRequest(); var version = new BigtableVersion(metadata.Timestamp.ToUniversalTime()); request.TableNameAsTableName = _tableName; request.Mutations.Add(Mutations.SetCell(_family, SnapshotColumnQualifier, ByteString.CopyFrom(snapshotBytes), version)); request.Mutations.Add(Mutations.SetCell(_family, SnapshotMetaDataColumnQualifier, ByteString.CopyFrom(snapshotMetadataBytes), version)); request.RowKey = GetRowKey(metadata.PersistenceId, metadata.SequenceNr); await _bigtableClient.MutateRowAsync(request).ConfigureAwait(false); }
/// <summary> /// Mutate multiple rows in an existing table and column family. Updates multiple cells within each row. ///</summary> /// <param name="projectId">Your Google Cloud Project ID.</param> /// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param> /// <param name="tableId">Your Google Cloud Bigtable table ID.</param> public string writeBatch( string projectId = "YOUR-PROJECT-ID", string instanceId = "YOUR-INSTANCE-ID", string tableId = "YOUR-TABLE-ID") { BigtableClient bigtableClient = BigtableClient.Create(); TableName tableName = new TableName(projectId, instanceId, tableId); BigtableVersion timestamp = new BigtableVersion(DateTime.UtcNow); String COLUMN_FAMILY = "stats_summary"; MutateRowsRequest.Types.Entry mutations1 = Mutations.CreateEntry(new BigtableByteString("tablet#a0b81f74#20190501"), Mutations.SetCell(COLUMN_FAMILY, "connected_cell", 1, timestamp), Mutations.SetCell(COLUMN_FAMILY, "os_build", "12155.0.0-rc1", timestamp) ); MutateRowsRequest.Types.Entry mutations2 = Mutations.CreateEntry(new BigtableByteString("tablet#a0b81f74#20190502"), Mutations.SetCell(COLUMN_FAMILY, "connected_cell", 1, timestamp), Mutations.SetCell(COLUMN_FAMILY, "os_build", "12145.0.0-rc6", timestamp) ); MutateRowsRequest.Types.Entry[] entries = { mutations1, mutations2 }; MutateRowsResponse mutateRowResponse = bigtableClient.MutateRows(tableName, entries); foreach (MutateRowsResponse.Types.Entry entry in mutateRowResponse.Entries) { if (entry.Status.Code == 0) { Console.WriteLine($"Row {entry.Index} written successfully"); } else { Console.WriteLine($"\tFailed to write row {entry.Index}"); Console.WriteLine(entry.Status.Message); return(entry.Status.Message); } } return("Successfully wrote 2 rows"); }
public void Comparisons() { var control = new BigtableVersion(5); var less = new[] { new BigtableVersion(0), new BigtableVersion(4) }; var equal = new[] { new BigtableVersion(5) }; var greater = new[] { new BigtableVersion(6), new BigtableVersion(-1), new BigtableVersion(long.MaxValue / 1000) }; ComparisonTester.AssertCompare(control, less, equal, greater); ComparisonTester.AssertCompareOperators(control, less, equal, greater); }
/// <summary> /// Check if a row has a certain value then mutate the row if it does. ///</summary> /// <param name="projectId">Your Google Cloud Project ID.</param> /// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param> /// <param name="tableId">Your Google Cloud Bigtable table ID.</param> public string writeConditional( string projectId = "YOUR-PROJECT-ID", string instanceId = "YOUR-INSTANCE-ID", string tableId = "YOUR-TABLE-ID") { BigtableClient bigtableClient = BigtableClient.Create(); TableName tableName = new TableName(projectId, instanceId, tableId); BigtableByteString rowkey = new BigtableByteString("phone#4c410523#20190501"); BigtableVersion timestamp = new BigtableVersion(DateTime.UtcNow); String COLUMN_FAMILY = "stats_summary"; CheckAndMutateRowResponse checkAndMutateRowResponse = bigtableClient.CheckAndMutateRow( tableName, rowkey, RowFilters.Chain( RowFilters.FamilyNameExact(COLUMN_FAMILY), RowFilters.ColumnQualifierExact("os_build"), RowFilters.ValueRegex("PQ2A\\..*")), Mutations.SetCell(COLUMN_FAMILY, "os_name", "android", timestamp)); return($"Successfully updated row's os_name: {checkAndMutateRowResponse.PredicateMatched}"); }
/// <summary> /// Mutate one row in an existing table and column family. Updates multiple cells within that row using one API call. ///</summary> /// <param name="projectId">Your Google Cloud Project ID.</param> /// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param> /// <param name="tableId">Your Google Cloud Bigtable table ID.</param> public string writeSimple( string projectId = "YOUR-PROJECT-ID", string instanceId = "YOUR-INSTANCE-ID", string tableId = "YOUR-TABLE-ID") { BigtableClient bigtableClient = BigtableClient.Create(); TableName tableName = new TableName(projectId, instanceId, tableId); BigtableByteString rowkey = new BigtableByteString("phone#4c410523#20190501"); BigtableVersion timestamp = new BigtableVersion(DateTime.UtcNow); String COLUMN_FAMILY = "stats_summary"; Mutation[] mutations = { Mutations.SetCell(COLUMN_FAMILY, "connected_cell", 1, timestamp), Mutations.SetCell(COLUMN_FAMILY, "connected_wifi", 1, timestamp), Mutations.SetCell(COLUMN_FAMILY, "os_build", "PQ2A.190405.003", timestamp) }; MutateRowResponse mutateRowResponse = bigtableClient.MutateRow(tableName, rowkey, mutations); Console.WriteLine(mutateRowResponse); return($"Successfully wrote row {rowkey}"); }
public void FormattingLong(string expectedText, long versionValue) { var version = new BigtableVersion(versionValue); Assert.Equal(expectedText, version.ToString()); }
public void RoundTripLong(long value) { var version = new BigtableVersion(value); Assert.Equal(value, version.Value); }