Beispiel #1
0
        /// <inheritdoc />
        public override long GetBytes(int ordinal, long fieldOffset, byte[] buffer, int bufferOffset, int length)
        {
            GaxPreconditions.CheckArgument(GetSpannerFieldType(ordinal).TypeCode == TypeCode.Bytes, nameof(ordinal), "Spanner only supports conversion to byte arrays for columns of type BYTES.");
            GaxPreconditions.CheckArgumentRange(bufferOffset, nameof(bufferOffset), 0, buffer?.Length ?? 0);
            GaxPreconditions.CheckArgumentRange(length, nameof(length), 0, buffer?.Length ?? int.MaxValue);
            if (buffer != null)
            {
                GaxPreconditions.CheckArgumentRange(bufferOffset + length, nameof(length), 0, buffer.Length);
            }

            var bytes = IsDBNull(ordinal) ? null : GetFieldValue <byte[]>(ordinal);

            if (buffer == null)
            {
                // Return the length of the value if `buffer` is null:
                // https://docs.microsoft.com/en-us/dotnet/api/system.data.idatarecord.getbytes?view=netstandard-2.1#remarks
                return(bytes?.Length ?? 0);
            }
            var copyLength = Math.Min(length, (bytes?.Length ?? 0) - (int)fieldOffset);

            if (copyLength < 0)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(fieldOffset), "Field offset exceeds the length of the field data");
            }
            if (bytes != null)
            {
                Array.Copy(bytes, (int)fieldOffset, buffer, bufferOffset, copyLength);
            }
            return(copyLength);
        }
            internal void Validate()
            {
                GaxPreconditions.CheckArgumentRange(StreamAckDeadline, nameof(StreamAckDeadline), MinimumStreamAckDeadline, MaximumStreamAckDeadline);
                var maxAckExtension = TimeSpan.FromTicks((StreamAckDeadline ?? DefaultStreamAckDeadline).Ticks / 2);

                GaxPreconditions.CheckArgumentRange(AckExtensionWindow, nameof(AckExtensionWindow), MinimumAckExtensionWindow, maxAckExtension);
            }
Beispiel #3
0
 internal Timestamp(long seconds, int nanoseconds)
 {
     GaxPreconditions.CheckArgumentRange(seconds, nameof(seconds), MinSeconds, MaxSeconds);
     GaxPreconditions.CheckArgumentRange(nanoseconds, nameof(nanoseconds), 0, 999_999_999);
     _seconds     = seconds;
     _nanoseconds = nanoseconds;
 }
        // Note to readers: we should make it as easy as possible to do the work below, of course.
        // Possibly BigqueryClient should have a GetResultSet(jobReference, pageSize, pageToken) call.

        /// <summary>
        /// Eagerly fetches a set of rows, up to the specified count.
        /// </summary>
        /// <remarks>
        /// If <paramref name="maxRows"/> is smaller than the number of rows originally requested,
        /// this may require another server call to retrieve an appropriate page token. When working
        /// in pages, always specify a maximum per-request number of rows less than or equal to the
        /// page size you want.
        /// </remarks>
        /// <param name="maxRows">The maximum number of rows to retrieve. Must be positive.</param>
        /// <returns>An in-memory result set of at most the given number of rows.</returns>
        public BigqueryResultSet GetResultSet(int maxRows)
        {
            GaxPreconditions.CheckArgumentRange(maxRows, nameof(maxRows), 1, int.MaxValue);
            if (!Completed)
            {
                throw new InvalidOperationException($"Cannot call {nameof(GetResultSet)} on an incomplete job");
            }
            GetQueryResultsOptions clonedOptions = _options?.Clone() ?? new GetQueryResultsOptions();
            List <BigqueryRow>     rows          = new List <BigqueryRow>(maxRows);

            if ((clonedOptions.PageSize == null || clonedOptions.PageSize > maxRows) && _response.Rows?.Count > maxRows)
            {
                // Oops. Do it again from scratch, with a useful page size.
                clonedOptions.PageSize = maxRows;
                return(_client.GetQueryResults(JobReference, clonedOptions).GetResultSet(maxRows));
            }
            // First add the rows from the first response which is part of the state
            // of the object.
            rows.AddRange(ResponseRows);
            string pageToken = _response.PageToken;

            clonedOptions.StartIndex = null;
            // Now keep going until we've filled the result set or know there's no more data.
            while (rows.Count > maxRows && pageToken != null)
            {
                clonedOptions.PageToken = pageToken;
                clonedOptions.PageSize  = maxRows - rows.Count;
                var job = _client.GetQueryResults(JobReference, clonedOptions);
                rows.AddRange(job.ResponseRows);
                pageToken = job._response.PageToken;
            }
            return(new BigqueryResultSet(rows, Schema, JobReference, pageToken));
        }
Beispiel #5
0
        /// <summary>
        /// Specifies the maximum number of results to return.
        /// </summary>
        /// <remarks>
        /// This call replaces any previously-specified limit in the query.
        /// </remarks>
        /// <param name="limit">The maximum number of results to return. Must be greater than or equal to 0.</param>
        /// <returns>A new query based on the current one, but with the specified limit applied.</returns>
        public Query Limit(int limit)
        {
            GaxPreconditions.CheckArgumentRange(limit, nameof(limit), 0, int.MaxValue);
            var query = QueryProto.Clone();

            query.Limit = limit;
            return(WithQuery(query));
        }
Beispiel #6
0
        /// <summary>
        /// Specifies a number of results to skip.
        /// </summary>
        /// <remarks>
        /// This call replaces any previously-specified offset in the query.
        /// </remarks>
        /// <param name="offset">The number of results to skip. Must be greater than or equal to 0.</param>
        /// <returns>A new query based on the current one, but with the specified offset applied.</returns>
        public Query Offset(int offset)
        {
            GaxPreconditions.CheckArgumentRange(offset, nameof(offset), 0, int.MaxValue);
            var query = QueryProto.Clone();

            query.Offset = offset;
            return(WithQuery(query));
        }
        /// <summary>
        /// Eagerly fetches a set of rows, up to the specified count, providing a page of results with a next page token
        /// if more results are available. This is typically used within web applications, where the next page token
        /// is propagated to the client along with the results, so that the next page can be retrieved in another request.
        /// </summary>
        /// <param name="pageSize">The maximum number of rows to retrieve. Must be positive.</param>
        /// <returns>An in-memory result set of at most the given number of rows.</returns>
        public BigQueryPage ReadPage(int pageSize)
        {
            GaxPreconditions.CheckArgumentRange(pageSize, nameof(pageSize), 1, int.MaxValue);
            // Make sure we start off by trying to read the right page size...
            var options = GetOptionsWithPageSize(pageSize);
            var page    = _client.ListRows(TableReference, Schema, options).ReadPage(pageSize);

            return(new BigQueryPage(page, Schema, JobReference, TableReference));
        }
        /// <summary>
        /// Asynchronously but eagerly fetches a set of rows, up to the specified count, providing a page of results with a next page token
        /// if more results are available. This is typically used within web applications, where the next page token
        /// is propagated to the client along with the results, so that the next page can be retrieved in another request.
        /// </summary>
        /// <param name="pageSize">The maximum number of rows to retrieve. Must be positive.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>A task representing the asynchronous operation. When complete, the result is
        /// an in-memory result set of at most the given number of rows.</returns>
        public async Task <BigQueryPage> ReadPageAsync(int pageSize, CancellationToken cancellationToken = default(CancellationToken))
        {
            GaxPreconditions.CheckArgumentRange(pageSize, nameof(pageSize), 1, int.MaxValue);
            // Make sure we start off by trying to read the right page size...
            var options = GetOptionsWithPageSize(pageSize);
            var page    = await _client.ListRowsAsync(TableReference, Schema, options)
                          .ReadPageAsync(pageSize, cancellationToken)
                          .ConfigureAwait(false);

            return(new BigQueryPage(page, Schema, JobReference, TableReference));
        }
 internal void Validate()
 {
     if (BatchingSettings != null)
     {
         GaxPreconditions.CheckArgumentRange(BatchingSettings.ElementCountThreshold,
                                             $"{nameof(BatchingSettings)}.{nameof(BatchingSettings.ElementCountThreshold)}", 1, ApiMaxBatchingSettings.ElementCountThreshold.Value);
         GaxPreconditions.CheckArgumentRange(BatchingSettings.ByteCountThreshold,
                                             $"{nameof(BatchingSettings)}.{nameof(BatchingSettings.ByteCountThreshold)}", 1, ApiMaxBatchingSettings.ByteCountThreshold.Value);
         GaxPreconditions.CheckArgument((BatchingSettings.DelayThreshold ?? TimeSpan.FromSeconds(1)) > TimeSpan.Zero,
                                        $"{nameof(BatchingSettings)}.{nameof(BatchingSettings.DelayThreshold)}", "Must be positive");
     }
 }
 private BigtableVersion(long value, bool valueIsMillis)
 {
     if (valueIsMillis)
     {
         GaxPreconditions.CheckArgumentRange(value, nameof(value), -1, long.MaxValue / MillisPerMicro);
         _micros = value == -1 ? MicrosFromTimestamp(DateTime.UtcNow) : value * MillisPerMicro;
     }
     else
     {
         GaxPreconditions.CheckArgumentRange(value, nameof(value), -1, long.MaxValue);
         _micros = value;
     }
 }
Beispiel #11
0
 internal RetryOptions(RetryType retryType, ExceptionHandling exceptionHandling, BufferOverflow bufferOverflow,
                       int?bufferSizeBytes = null, int?retryAttempts = null, TimeSpan?retryInterval = null)
 {
     RetryType         = GaxPreconditions.CheckEnumValue(retryType, nameof(retryType));
     ExceptionHandling = GaxPreconditions.CheckEnumValue(exceptionHandling, nameof(exceptionHandling));
     BufferOverflow    = GaxPreconditions.CheckEnumValue(bufferOverflow, nameof(bufferOverflow));
     BufferSizeBytes   = GaxPreconditions.CheckArgumentRange(
         bufferSizeBytes ?? 0, nameof(bufferSizeBytes), 0, int.MaxValue);
     RetryAttempts = GaxPreconditions.CheckArgumentRange(
         retryAttempts ?? 0, nameof(retryAttempts), 0, int.MaxValue);
     RetryInterval = retryInterval ?? TimeSpan.Zero;
     GaxPreconditions.CheckArgument(RetryInterval >= TimeSpan.Zero, nameof(retryInterval),
                                    $"{nameof(retryInterval)} must be greater than 0");
 }
        /// <summary>
        /// Creates a new <see cref="BigtableVersion"/> value from the milliseconds of a timestamp since the Unix epoch.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Note: version values are stored on the server as if they are microseconds since the Unix epoch.
        /// However, the server only supports millisecond granularity, so the server only allows microseconds
        /// in multiples of 1,000. <see cref="BigtableVersion"/> attempts to hide this complexity by exposing
        /// its underlying <see cref="Value"/> in terms of milliseconds, so if desired, a custom versioning
        /// scheme of 1, 2, ... can be used rather than 1000, 2000, ... However, access to the underlying
        /// microsecond value is still provided via <see cref="Micros"/>.
        /// </para>
        /// <para>
        /// Note: when using ReadModifyWriteRow, modified columns automatically use a server version, which
        /// is based on the current timestamp since the Unix epoch. For those columns, other reads and writes
        /// should use <see cref="BigtableVersion"/> values constructed from DateTime values, as opposed to
        /// using a custom versioning scheme with 64-bit values.
        /// </para>
        /// </remarks>
        /// <param name="timestamp">
        /// The timestamp whose milliseconds since the Unix epoch should be used as the version value. It must be specified in UTC.
        /// </param>
        public BigtableVersion(DateTime timestamp)
        {
            GaxPreconditions.CheckArgument(
                timestamp.Kind == DateTimeKind.Utc,
                nameof(timestamp),
                $"The {nameof(BigtableVersion)} timestamp must be specified in UTC.");
            GaxPreconditions.CheckArgumentRange(
                timestamp,
                nameof(timestamp),
                UnixEpoch,
                DateTime.MaxValue);

            _micros = MicrosFromTimestamp(timestamp);
        }
 /// <summary>
 /// Streams back the contents of all requested rows in key order, optionally
 /// applying the same Reader filter to each.
 /// </summary>
 /// <remarks>
 /// <para>
 /// This method simply delegates to <see cref="ReadRows(ReadRowsRequest, CallSettings)"/>.
 /// </para>
 /// </remarks>
 /// <param name="tableName">
 /// The unique name of the table from which to read. Must not be null.
 /// </param>
 /// <param name="rows">
 /// The row keys and/or ranges to read. If null, reads from all rows.
 /// </param>
 /// <param name="filter">
 /// The filter to apply to the contents of the specified row(s). If null,
 /// reads the entirety of each row.
 /// </param>
 /// <param name="rowsLimit">
 /// The read will terminate after committing to N rows' worth of results.
 /// If null or 0, returns all results.
 /// </param>
 /// <param name="callSettings">
 /// If not null, applies overrides to this RPC call.
 /// </param>
 /// <returns>
 /// The server stream.
 /// </returns>
 public virtual ReadRowsStream ReadRows(
     TableName tableName,
     RowSet rows               = null,
     RowFilter filter          = null,
     long?rowsLimit            = null,
     CallSettings callSettings = null) =>
 ReadRows(
     new ReadRowsRequest
 {
     TableNameAsTableName = GaxPreconditions.CheckNotNull(tableName, nameof(tableName)),
     Rows      = rows,
     Filter    = filter,
     RowsLimit = GaxPreconditions.CheckArgumentRange(rowsLimit ?? 0, nameof(rowsLimit), 0, long.MaxValue)
 },
     callSettings);
 internal DocumentChange(DocumentSnapshot document, Type type, int?oldIndex, int?newIndex)
 {
     ChangeType = type;
     Document   = document;
     if (oldIndex != null)
     {
         GaxPreconditions.CheckArgumentRange(oldIndex, nameof(oldIndex), 0, int.MaxValue);
     }
     if (newIndex != null)
     {
         GaxPreconditions.CheckArgumentRange(newIndex, nameof(newIndex), 0, int.MaxValue);
     }
     OldIndex = oldIndex;
     NewIndex = newIndex;
 }
Beispiel #15
0
 /// <summary>
 /// Constructor with complete control that does not perform any validation.
 /// </summary>
 internal ResultStream(
     SpannerClient client,
     ReadOrQueryRequest request,
     Session session,
     CallSettings callSettings,
     int maxBufferSize,
     RetrySettings retrySettings)
 {
     _buffer        = new LinkedList <PartialResultSet>();
     _client        = GaxPreconditions.CheckNotNull(client, nameof(client));
     _request       = GaxPreconditions.CheckNotNull(request, nameof(request));
     _session       = GaxPreconditions.CheckNotNull(session, nameof(session));
     _callSettings  = callSettings;
     _maxBufferSize = GaxPreconditions.CheckArgumentRange(maxBufferSize, nameof(maxBufferSize), 1, 10_000);
     _retrySettings = GaxPreconditions.CheckNotNull(retrySettings, nameof(retrySettings));
 }
Beispiel #16
0
 /// <summary>
 /// Creates a new instance with the given settings.
 /// </summary>
 /// <param name="maxAttempts">The maximum number of attempts to make. Must be positive.</param>
 /// <param name="initialBackoff">The backoff after the initial failure. Must be non-negative.</param>
 /// <param name="maxBackoff">The maximum backoff. Must be at least <paramref name="initialBackoff"/>.</param>
 /// <param name="backoffMultiplier">The multiplier to apply to backoff times. Must be at least 1.0.</param>
 /// <param name="retryFilter">The predicate to use to check whether an error should be retried. Must not be null.</param>
 /// <param name="backoffJitter">The jitter to use on each backoff. Must not be null.</param>
 internal RetrySettings(
     int maxAttempts,
     TimeSpan initialBackoff,
     TimeSpan maxBackoff,
     double backoffMultiplier,
     Predicate <Exception> retryFilter,
     IJitter backoffJitter)
 {
     MaxAttempts    = GaxPreconditions.CheckArgumentRange(maxAttempts, nameof(maxAttempts), 1, int.MaxValue);
     InitialBackoff = GaxPreconditions.CheckNonNegativeDelay(initialBackoff, nameof(initialBackoff));
     MaxBackoff     = GaxPreconditions.CheckNonNegativeDelay(maxBackoff, nameof(maxBackoff));
     GaxPreconditions.CheckArgument(maxBackoff >= initialBackoff, nameof(maxBackoff), "Maximum backoff must be at least as large as initial backoff");
     BackoffMultiplier = GaxPreconditions.CheckArgumentRange(backoffMultiplier, nameof(backoffMultiplier), 1.0, double.MaxValue);
     RetryFilter       = GaxPreconditions.CheckNotNull(retryFilter, nameof(retryFilter));
     BackoffJitter     = GaxPreconditions.CheckNotNull(backoffJitter, nameof(backoffJitter));
 }
 /// <summary>
 /// Constructor with complete control, for testing purposes.
 /// </summary>
 internal SqlResultStream(
     SpannerClient client,
     ExecuteSqlRequest request,
     Session session,
     CallSettings callSettings,
     int maxBufferSize,
     BackoffSettings backoffSettings,
     IJitter backoffJitter)
 {
     _buffer          = new LinkedList <PartialResultSet>();
     _client          = GaxPreconditions.CheckNotNull(client, nameof(client));
     _request         = GaxPreconditions.CheckNotNull(request, nameof(request));
     _session         = GaxPreconditions.CheckNotNull(session, nameof(session));
     _callSettings    = callSettings;
     _maxBufferSize   = GaxPreconditions.CheckArgumentRange(maxBufferSize, nameof(maxBufferSize), 1, 10_000);
     _backoffSettings = GaxPreconditions.CheckNotNull(backoffSettings, nameof(backoffSettings));
     _backoffJitter   = GaxPreconditions.CheckNotNull(backoffJitter, nameof(backoffJitter));
 }
        private async Task <IReadOnlyList <T> > AnnotateSingleFeatureTypeAsync <T>(
            Feature.Types.Type featureType,
            Func <AnnotateImageResponse, RepeatedField <T> > annotationExtractor,
            Image image,
            ImageContext context,
            int maxResults,
            CallSettings callSettings)
        {
            GaxPreconditions.CheckNotNull(image, nameof(image));
            GaxPreconditions.CheckArgumentRange(maxResults, nameof(maxResults), 0, int.MaxValue);
            var request = new AnnotateImageRequest
            {
                Image        = image,
                ImageContext = context,
                Features     = { new Feature {
                                     Type = featureType, MaxResults = maxResults
                                 } }
            };
            // This will throw on error.
            var response = await AnnotateAsync(request, callSettings).ConfigureAwait(false);

            return(annotationExtractor(response));
        }
Beispiel #19
0
 /// <summary>
 /// Creates a new endpoint with the given host and port.
 /// </summary>
 /// <param name="host">The host name to connect to. Must not be null or empty.</param>
 /// <param name="port">The port to connect to, in the range 1 to 65535 inclusive.</param>
 public ServiceEndpoint(string host, int port)
 {
     Host = GaxPreconditions.CheckNotNullOrEmpty(host, nameof(host));
     Port = GaxPreconditions.CheckArgumentRange(port, nameof(port), 1, 65535);
 }
Beispiel #20
0
 public void CheckRangeTDouble_Invalid(double value)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => GaxPreconditions.CheckArgumentRange(value, nameof(value), RangeMin, RangeMax));
 }
Beispiel #21
0
 public void CheckRangeNullableInt32_Valid(int?value)
 {
     Assert.Equal(value, GaxPreconditions.CheckArgumentRange(value, nameof(value), RangeMin, RangeMax));
 }
Beispiel #22
0
 public void CheckRangeNullableInt32_Invalid(int?value)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => GaxPreconditions.CheckArgumentRange(value, nameof(value), RangeMin, RangeMax));
 }
Beispiel #23
0
 public void CheckRangeInt64_Valid(long value)
 {
     Assert.Equal(value, GaxPreconditions.CheckArgumentRange(value, nameof(value), RangeMin, RangeMax));
 }
 internal void Validate()
 {
     // Fairly arbitrary upper limit.
     GaxPreconditions.CheckArgumentRange(ClientCount ?? 1, nameof(ClientCount), 1, 256);
 }
 /// <summary>
 /// Creates a new <see cref="RowFilter"/> instance which matches all cells
 /// from a row with probability p, and matches no cells from the row with
 /// probability 1-p.
 /// </summary>
 /// <param name="probability">
 /// The probability with which rows should be matched. Must be between 0 and 1, inclusive.
 /// </param>
 /// <returns>The created row filter.</returns>
 public static RowFilter RowSample(double probability) =>
 new RowFilter
 {
     RowSampleFilter = GaxPreconditions.CheckArgumentRange(probability, nameof(probability), 0.0, 1.0)
 };
Beispiel #26
0
 /// <summary>
 /// Specifies a number of results to skip.
 /// </summary>
 /// <remarks>
 /// This call replaces any previously-specified offset in the query.
 /// </remarks>
 /// <param name="offset">The number of results to skip. Must be greater than or equal to 0.</param>
 /// <returns>A new query based on the current one, but with the specified offset applied.</returns>
 public Query Offset(int offset)
 {
     GaxPreconditions.CheckArgumentRange(offset, nameof(offset), 0, int.MaxValue);
     return(new Query(Collection, offset, _limit, _orderings, _filters, _projections, _startAt, _endAt));
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="PlatformApiEndpoint" /> class.
 ///     Creates a new endpoint with the given host and port.
 /// </summary>
 /// <param name="host">The host name to connect to. Must not be null or empty.</param>
 /// <param name="port">The port to connect to, in the range 1 to 65535 inclusive.</param>
 /// <param name="insecure">
 ///     Whether the connection is encrypted or not. For unencrypted connections no authentication data
 ///     is sent.
 /// </param>
 public PlatformApiEndpoint(string host, int port, bool insecure = false)
 {
     Host     = GaxPreconditions.CheckNotNullOrEmpty(host, nameof(host));
     Port     = GaxPreconditions.CheckArgumentRange(port, nameof(port), 1, 65535);
     Insecure = insecure;
 }
Beispiel #28
0
 /// <summary>
 /// Creates a new value using the provided latitude and longitude values.
 /// </summary>
 /// <param name="latitude">The latitude of the point in degrees, between -90 and 90 inclusive.</param>
 /// <param name="longitude">The longitude of the point in degrees, between -180 and 180 inclusive.</param>
 public GeoPoint(double latitude, double longitude)
 {
     _latitude  = GaxPreconditions.CheckArgumentRange(latitude, nameof(latitude), -90d, 90d);
     _longitude = GaxPreconditions.CheckArgumentRange(longitude, nameof(longitude), -180d, 180d);
 }
 /// <summary>
 /// Creates an instance with the given maximum number of attempts.
 /// </summary>
 /// <param name="maxAttempts">The number of times a transaction will be attempted before failing. Must be positive.</param>
 /// <returns>A new options object.</returns>
 public static TransactionOptions Create(int maxAttempts)
 {
     GaxPreconditions.CheckArgumentRange(maxAttempts, nameof(maxAttempts), 1, int.MaxValue);
     return(new TransactionOptions(maxAttempts));
 }
Beispiel #30
0
 public void CheckRangeTDouble_Valid(double value)
 {
     Assert.Equal(value, GaxPreconditions.CheckArgumentRange(value, nameof(value), RangeMin, RangeMax));
 }