/// <inheritdoc />
        public override async Task <IDictionary <string, string> > ClearDatasetLabelsAsync(DatasetReference datasetReference, ModifyLabelsOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));

            int retries    = options?.Retries ?? ModifyLabelsOptions.DefaultRetries;
            var getOptions = new GetDatasetOptions {
                ETag = options?.ETag
            };

            while (true)
            {
                // Outside the catch, as we don't need to retry if *this* fails the precondition.
                // If the dataset originally has the "right" version but then changes before we patch it, we'll end up retrying this
                // part as well, and it will immediately fail.
                var dataset = await GetDatasetAsync(datasetReference, getOptions, cancellationToken).ConfigureAwait(false);

                try
                {
                    var existingLabels = dataset.Resource.Labels;
                    if (existingLabels == null || existingLabels.Count == 0)
                    {
                        return(new Dictionary <string, string>());
                    }
                    var labels = existingLabels.Keys.ToDictionary(key => key, key => (string)null);
                    await dataset.PatchAsync(new Dataset { Labels = labels }, matchETag : true, cancellationToken : cancellationToken).ConfigureAwait(false);

                    return(existingLabels);
                }
                catch (GoogleApiException e) when(retries > 0 && e.HttpStatusCode == HttpStatusCode.PreconditionFailed)
                {
                    retries--;
                    // No delay here: we're only trying to catch the case where another change is made between Get/Patch.
                }
            }
        }
        // Most of the work is done here...

        /// <inheritdoc />
        public override IDictionary <string, string> ModifyDatasetLabels(DatasetReference datasetReference, IDictionary <string, string> labels, ModifyLabelsOptions options = null)
        {
            GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));
            GaxPreconditions.CheckNotNull(labels, nameof(labels));

            int retries    = options?.Retries ?? ModifyLabelsOptions.DefaultRetries;
            var getOptions = new GetDatasetOptions {
                ETag = options?.ETag
            };

            while (true)
            {
                // Outside the catch, as we don't need to retry if *this* fails the precondition.
                // If the dataset originally has the "right" version but then changes before we patch it, we'll end up retrying this
                // part as well, and it will immediately fail.
                var dataset = GetDataset(datasetReference, getOptions);
                try
                {
                    var existingLabels = dataset.Resource.Labels ?? new Dictionary <string, string>();

                    var differences = GetDifferences(labels, existingLabels);
                    // If everything's already as we want it to be, we don't need to patch.
                    if (differences.Count != 0)
                    {
                        dataset.Patch(new Dataset {
                            Labels = differences
                        }, matchETag: true);
                    }

                    var oldValues = new Dictionary <string, string>();
                    foreach (var entry in labels)
                    {
                        existingLabels.TryGetValue(entry.Key, out string existingValue);
                        oldValues[entry.Key] = existingValue; // May be null
                    }
                    return(oldValues);
                }
                catch (GoogleApiException e) when(retries > 0 && e.HttpStatusCode == HttpStatusCode.PreconditionFailed)
                {
                    retries--;
                    // No delay here: we're only trying to catch the case where another change is made between Get/Patch.
                }
            }
        }
 /// <inheritdoc />
 public override BigQueryDataset GetOrCreateDataset(DatasetReference datasetReference, GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null)
 {
     GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));
     try
     {
         return(GetDataset(datasetReference, getOptions));
     }
     catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
     {
         return(CreateDataset(datasetReference, createOptions));
     }
 }
        /// <inheritdoc />
        public override BigQueryDataset GetDataset(DatasetReference datasetReference, GetDatasetOptions options = null)
        {
            GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));
            var request = Service.Datasets.Get(datasetReference.ProjectId, datasetReference.DatasetId);

            options?.ModifyRequest(request);
            return(new BigQueryDataset(this, request.Execute()));
        }
 /// <inheritdoc />
 public override async Task <BigQueryDataset> GetOrCreateDatasetAsync(DatasetReference datasetReference, GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));
     try
     {
         return(await GetDatasetAsync(datasetReference, getOptions, cancellationToken).ConfigureAwait(false));
     }
     catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
     {
         return(await CreateDatasetAsync(datasetReference, createOptions, cancellationToken).ConfigureAwait(false));;
     }
 }
        /// <inheritdoc />
        public override async Task <BigQueryDataset> GetDatasetAsync(DatasetReference datasetReference, GetDatasetOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));
            var request = Service.Datasets.Get(datasetReference.ProjectId, datasetReference.DatasetId);

            options?.ModifyRequest(request);
            return(new BigQueryDataset(this, await request.ExecuteAsync(cancellationToken).ConfigureAwait(false)));
        }
 /// <inheritdoc />
 public override async Task <BigQueryDataset> GetOrCreateDatasetAsync(DatasetReference datasetReference, Dataset resource = null, GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null, CancellationToken cancellationToken = default)
 {
     GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));
     GaxPreconditions.CheckArgument(
         resource?.DatasetReference == null || datasetReference.ReferencesSameAs(resource.DatasetReference),
         nameof(resource.DatasetReference),
         $"If {nameof(resource.DatasetReference)} is specified, it must be the same as {nameof(datasetReference)}");
     try
     {
         return(await GetDatasetAsync(datasetReference, getOptions, cancellationToken).ConfigureAwait(false));
     }
     catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
     {
         return(await CreateDatasetAsync(datasetReference, resource, createOptions, cancellationToken).ConfigureAwait(false));;
     }
 }
Exemple #8
0
 /// <summary>
 /// Retrieves a dataset given a project ID and dataset ID.
 /// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="GetDataset(DatasetReference,GetDatasetOptions)"/>.
 /// </summary>
 /// <param name="projectId">The project ID. Must not be null.</param>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The requested dataset.</returns>
 public virtual BigQueryDataset GetDataset(string projectId, string datasetId, GetDatasetOptions options = null) =>
 GetDataset(GetDatasetReference(projectId, datasetId), options);
Exemple #9
0
 /// <summary>
 /// Asynchronously attempts to fetch the specified dataset within the given project, creating it if it doesn't exist.
 /// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="GetOrCreateDatasetAsync(DatasetReference,GetDatasetOptions,CreateDatasetOptions,CancellationToken)"/>.
 /// </summary>
 /// <param name="projectId">The project ID. Must not be null.</param>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>A task representing the asynchronous operation. When complete, the result is
 /// the existing or new dataset.</returns>
 public virtual Task <BigQueryDataset> GetOrCreateDatasetAsync(string projectId, string datasetId,
                                                               GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null, CancellationToken cancellationToken = default(CancellationToken)) =>
 GetOrCreateDatasetAsync(GetDatasetReference(projectId, datasetId), getOptions, createOptions, cancellationToken);
Exemple #10
0
 /// <summary>
 /// Attempts to fetch the specified dataset within this client's project, creating it if it doesn't exist.
 /// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="GetOrCreateDataset(DatasetReference, Dataset, GetDatasetOptions, CreateDatasetOptions)"/>.
 /// </summary>
 /// <remarks>
 /// If <paramref name="resource" /><see cref="Dataset.DatasetReference" /> is specified, then it must be the same as the <see cref="DatasetReference" /> obtained from the other parameters.
 /// </remarks>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="resource">The dataset resource representation to use for the creation. May be null in which case default values will be used.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The existing or new dataset.</returns>
 public virtual BigQueryDataset GetOrCreateDataset(string datasetId, Dataset resource = null, GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null) =>
 GetOrCreateDataset(GetDatasetReference(datasetId), resource, getOptions, createOptions);
        /// <inheritdoc />
        public override async Task <BigQueryDataset> GetDatasetAsync(DatasetReference datasetReference, GetDatasetOptions options = null, CancellationToken cancellationToken = default)
        {
            var request = CreateGetDatasetRequest(datasetReference, options);

            return(new BigQueryDataset(this, CheckETag(await request.ExecuteAsync(cancellationToken).ConfigureAwait(false), options?.ETag)));
        }
        /// <inheritdoc />
        public override BigQueryDataset GetDataset(DatasetReference datasetReference, GetDatasetOptions options = null)
        {
            var request = CreateGetDatasetRequest(datasetReference, options);

            return(new BigQueryDataset(this, CheckETag(request.Execute(), options?.ETag)));
        }
        // Request creation
        private GetRequest CreateGetDatasetRequest(DatasetReference datasetReference, GetDatasetOptions options)
        {
            GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));
            var request = Service.Datasets.Get(datasetReference.ProjectId, datasetReference.DatasetId);

            options?.ModifyRequest(request);
            RetryHandler.MarkAsRetriable(request);
            return(request);
        }
Exemple #14
0
 /// <summary>
 /// Attempts to fetch the specified dataset within the given project, creating it if it doesn't exist.
 /// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="GetOrCreateDataset(DatasetReference,GetDatasetOptions,CreateDatasetOptions)"/>.
 /// </summary>
 /// <param name="projectId">The project ID. Must not be null.</param>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The existing or new dataset.</returns>
 public virtual BigQueryDataset GetOrCreateDataset(string projectId, string datasetId, GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null) =>
 GetOrCreateDataset(GetDatasetReference(projectId, datasetId), getOptions, createOptions);
Exemple #15
0
 /// <summary>
 /// Attempts to fetch the specified dataset, creating it if it doesn't exist.
 /// </summary>
 /// <remarks>
 /// If <paramref name="resource" /><see cref="Dataset.DatasetReference" /> is specified, then it must be the same as the <see cref="DatasetReference" /> obtained from the other parameters.
 /// </remarks>
 /// <param name="datasetReference">A fully-qualified identifier for the dataset. Must not be null.</param>
 /// <param name="resource">The dataset resource representation to use for the creation. May be null in which case default values will be used.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The existing or new dataset.</returns>
 public virtual BigQueryDataset GetOrCreateDataset(DatasetReference datasetReference, Dataset resource = null, GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null) =>
 throw new NotImplementedException();
Exemple #16
0
 /// <summary>
 /// Asynchronously retrieves a dataset within this client's project given the dataset ID.
 /// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="GetDatasetAsync(DatasetReference,GetDatasetOptions,CancellationToken)"/>.
 /// </summary>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A task representing the asynchronous operation. When complete, the result is
 /// the requested dataset.</returns>
 public virtual Task <BigQueryDataset> GetDatasetAsync(string datasetId, GetDatasetOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) =>
 GetDatasetAsync(GetDatasetReference(datasetId), options, cancellationToken);
Exemple #17
0
 /// <summary>
 /// Asynchronously attempts to fetch the specified dataset within this client's project, creating it if it doesn't exist.
 /// This method just creates a <see cref="DatasetReference"/> and delegates to <see cref="GetOrCreateDatasetAsync(DatasetReference, Dataset, GetDatasetOptions, CreateDatasetOptions, CancellationToken)"/>.
 /// </summary>
 /// <remarks>
 /// If <paramref name="resource" /><see cref="Dataset.DatasetReference" /> is specified, then it must be the same as the <see cref="DatasetReference" /> obtained from the other parameters.
 /// </remarks>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="resource">The dataset resource representation to use for the creation. May be null in which case default values will be used.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A task representing the asynchronous operation. When complete, the result is
 /// the existing or new dataset.</returns>
 public virtual Task <BigQueryDataset> GetOrCreateDatasetAsync(string datasetId, Dataset resource = null, GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null, CancellationToken cancellationToken = default) =>
 GetOrCreateDatasetAsync(GetDatasetReference(datasetId), resource, getOptions, createOptions, cancellationToken);
Exemple #18
0
 /// <summary>
 /// Asynchronously attempts to fetch the specified dataset, creating it if it doesn't exist.
 /// </summary>
 /// <param name="datasetReference">A fully-qualified identifier for the dataset. Must not be null.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A task representing the asynchronous operation. When complete, the result is
 /// the existing or new dataset.</returns>
 public virtual Task <BigQueryDataset> GetOrCreateDatasetAsync(DatasetReference datasetReference, GetDatasetOptions getOptions = null,
                                                               CreateDatasetOptions createOptions = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
Exemple #19
0
 /// <summary>
 /// Asynchronously retrieves the specified dataset.
 /// </summary>
 /// <param name="datasetReference">A fully-qualified identifier for the dataset. Must not be null.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A task representing the asynchronous operation. When complete, the result is
 /// the requested dataset.</returns>
 public virtual Task <BigQueryDataset> GetDatasetAsync(DatasetReference datasetReference, GetDatasetOptions options = null, CancellationToken cancellationToken = default) =>
 throw new NotImplementedException();
Exemple #20
0
 /// <summary>
 /// Retrieves a dataset.
 /// </summary>
 /// <param name="datasetReference">A fully-qualified identifier for the dataset. Must not be null.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The requested dataset.</returns>
 public virtual BigQueryDataset GetDataset(DatasetReference datasetReference, GetDatasetOptions options = null)
 {
     throw new NotImplementedException();
 }
 /// <inheritdoc />
 public override BigQueryDataset GetOrCreateDataset(DatasetReference datasetReference, Dataset resource = null, GetDatasetOptions getOptions = null, CreateDatasetOptions createOptions = null)
 {
     GaxPreconditions.CheckNotNull(datasetReference, nameof(datasetReference));
     GaxPreconditions.CheckArgument(
         resource?.DatasetReference == null || datasetReference.ReferencesSameAs(resource.DatasetReference),
         nameof(resource.DatasetReference),
         $"If {nameof(resource.DatasetReference)} is specified, it must be the same as {nameof(datasetReference)}");
     try
     {
         return(GetDataset(datasetReference, getOptions));
     }
     catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
     {
         return(CreateDataset(datasetReference, resource, createOptions));
     }
 }