1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17package storage
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	gax "github.com/googleapis/gax-go/v2"
27	"google.golang.org/api/option"
28	"google.golang.org/api/option/internaloption"
29	gtransport "google.golang.org/api/transport/grpc"
30	storagepb "google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1"
31	"google.golang.org/grpc"
32	"google.golang.org/grpc/codes"
33	"google.golang.org/grpc/metadata"
34)
35
36var newBigQueryReadClientHook clientHook
37
38// BigQueryReadCallOptions contains the retry settings for each method of BigQueryReadClient.
39type BigQueryReadCallOptions struct {
40	CreateReadSession []gax.CallOption
41	ReadRows          []gax.CallOption
42	SplitReadStream   []gax.CallOption
43}
44
45func defaultBigQueryReadGRPCClientOptions() []option.ClientOption {
46	return []option.ClientOption{
47		internaloption.WithDefaultEndpoint("bigquerystorage.googleapis.com:443"),
48		internaloption.WithDefaultMTLSEndpoint("bigquerystorage.mtls.googleapis.com:443"),
49		internaloption.WithDefaultAudience("https://bigquerystorage.googleapis.com/"),
50		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
51		internaloption.EnableJwtWithScope(),
52		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
53		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
54			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
55	}
56}
57
58func defaultBigQueryReadCallOptions() *BigQueryReadCallOptions {
59	return &BigQueryReadCallOptions{
60		CreateReadSession: []gax.CallOption{
61			gax.WithRetry(func() gax.Retryer {
62				return gax.OnCodes([]codes.Code{
63					codes.DeadlineExceeded,
64					codes.Unavailable,
65				}, gax.Backoff{
66					Initial:    100 * time.Millisecond,
67					Max:        60000 * time.Millisecond,
68					Multiplier: 1.30,
69				})
70			}),
71		},
72		ReadRows: []gax.CallOption{
73			gax.WithRetry(func() gax.Retryer {
74				return gax.OnCodes([]codes.Code{
75					codes.Unavailable,
76				}, gax.Backoff{
77					Initial:    100 * time.Millisecond,
78					Max:        60000 * time.Millisecond,
79					Multiplier: 1.30,
80				})
81			}),
82		},
83		SplitReadStream: []gax.CallOption{
84			gax.WithRetry(func() gax.Retryer {
85				return gax.OnCodes([]codes.Code{
86					codes.DeadlineExceeded,
87					codes.Unavailable,
88				}, gax.Backoff{
89					Initial:    100 * time.Millisecond,
90					Max:        60000 * time.Millisecond,
91					Multiplier: 1.30,
92				})
93			}),
94		},
95	}
96}
97
98// internalBigQueryReadClient is an interface that defines the methods availaible from BigQuery Storage API.
99type internalBigQueryReadClient interface {
100	Close() error
101	setGoogleClientInfo(...string)
102	Connection() *grpc.ClientConn
103	CreateReadSession(context.Context, *storagepb.CreateReadSessionRequest, ...gax.CallOption) (*storagepb.ReadSession, error)
104	ReadRows(context.Context, *storagepb.ReadRowsRequest, ...gax.CallOption) (storagepb.BigQueryRead_ReadRowsClient, error)
105	SplitReadStream(context.Context, *storagepb.SplitReadStreamRequest, ...gax.CallOption) (*storagepb.SplitReadStreamResponse, error)
106}
107
108// BigQueryReadClient is a client for interacting with BigQuery Storage API.
109// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
110//
111// BigQuery Read API.
112//
113// The Read API can be used to read data from BigQuery.
114type BigQueryReadClient struct {
115	// The internal transport-dependent client.
116	internalClient internalBigQueryReadClient
117
118	// The call options for this service.
119	CallOptions *BigQueryReadCallOptions
120}
121
122// Wrapper methods routed to the internal client.
123
124// Close closes the connection to the API service. The user should invoke this when
125// the client is no longer required.
126func (c *BigQueryReadClient) Close() error {
127	return c.internalClient.Close()
128}
129
130// setGoogleClientInfo sets the name and version of the application in
131// the `x-goog-api-client` header passed on each request. Intended for
132// use by Google-written clients.
133func (c *BigQueryReadClient) setGoogleClientInfo(keyval ...string) {
134	c.internalClient.setGoogleClientInfo(keyval...)
135}
136
137// Connection returns a connection to the API service.
138//
139// Deprecated.
140func (c *BigQueryReadClient) Connection() *grpc.ClientConn {
141	return c.internalClient.Connection()
142}
143
144// CreateReadSession creates a new read session. A read session divides the contents of a
145// BigQuery table into one or more streams, which can then be used to read
146// data from the table. The read session also specifies properties of the
147// data to be read, such as a list of columns or a push-down filter describing
148// the rows to be returned.
149//
150// A particular row can be read by at most one stream. When the caller has
151// reached the end of each stream in the session, then all the data in the
152// table has been read.
153//
154// Data is assigned to each stream such that roughly the same number of
155// rows can be read from each stream. Because the server-side unit for
156// assigning data is collections of rows, the API does not guarantee that
157// each stream will return the same number or rows. Additionally, the
158// limits are enforced based on the number of pre-filtered rows, so some
159// filters can lead to lopsided assignments.
160//
161// Read sessions automatically expire 6 hours after they are created and do
162// not require manual clean-up by the caller.
163func (c *BigQueryReadClient) CreateReadSession(ctx context.Context, req *storagepb.CreateReadSessionRequest, opts ...gax.CallOption) (*storagepb.ReadSession, error) {
164	return c.internalClient.CreateReadSession(ctx, req, opts...)
165}
166
167// ReadRows reads rows from the stream in the format prescribed by the ReadSession.
168// Each response contains one or more table rows, up to a maximum of 100 MiB
169// per response; read requests which attempt to read individual rows larger
170// than 100 MiB will fail.
171//
172// Each request also returns a set of stream statistics reflecting the current
173// state of the stream.
174func (c *BigQueryReadClient) ReadRows(ctx context.Context, req *storagepb.ReadRowsRequest, opts ...gax.CallOption) (storagepb.BigQueryRead_ReadRowsClient, error) {
175	return c.internalClient.ReadRows(ctx, req, opts...)
176}
177
178// SplitReadStream splits a given ReadStream into two ReadStream objects. These
179// ReadStream objects are referred to as the primary and the residual
180// streams of the split. The original ReadStream can still be read from in
181// the same manner as before. Both of the returned ReadStream objects can
182// also be read from, and the rows returned by both child streams will be
183// the same as the rows read from the original stream.
184//
185// Moreover, the two child streams will be allocated back-to-back in the
186// original ReadStream. Concretely, it is guaranteed that for streams
187// original, primary, and residual, that original[0-j] = primary[0-j] and
188// original[j-n] = residual[0-m] once the streams have been read to
189// completion.
190func (c *BigQueryReadClient) SplitReadStream(ctx context.Context, req *storagepb.SplitReadStreamRequest, opts ...gax.CallOption) (*storagepb.SplitReadStreamResponse, error) {
191	return c.internalClient.SplitReadStream(ctx, req, opts...)
192}
193
194// bigQueryReadGRPCClient is a client for interacting with BigQuery Storage API over gRPC transport.
195//
196// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
197type bigQueryReadGRPCClient struct {
198	// Connection pool of gRPC connections to the service.
199	connPool gtransport.ConnPool
200
201	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
202	disableDeadlines bool
203
204	// Points back to the CallOptions field of the containing BigQueryReadClient
205	CallOptions **BigQueryReadCallOptions
206
207	// The gRPC API client.
208	bigQueryReadClient storagepb.BigQueryReadClient
209
210	// The x-goog-* metadata to be sent with each request.
211	xGoogMetadata metadata.MD
212}
213
214// NewBigQueryReadClient creates a new big query read client based on gRPC.
215// The returned client must be Closed when it is done being used to clean up its underlying connections.
216//
217// BigQuery Read API.
218//
219// The Read API can be used to read data from BigQuery.
220func NewBigQueryReadClient(ctx context.Context, opts ...option.ClientOption) (*BigQueryReadClient, error) {
221	clientOpts := defaultBigQueryReadGRPCClientOptions()
222	if newBigQueryReadClientHook != nil {
223		hookOpts, err := newBigQueryReadClientHook(ctx, clientHookParams{})
224		if err != nil {
225			return nil, err
226		}
227		clientOpts = append(clientOpts, hookOpts...)
228	}
229
230	disableDeadlines, err := checkDisableDeadlines()
231	if err != nil {
232		return nil, err
233	}
234
235	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
236	if err != nil {
237		return nil, err
238	}
239	client := BigQueryReadClient{CallOptions: defaultBigQueryReadCallOptions()}
240
241	c := &bigQueryReadGRPCClient{
242		connPool:           connPool,
243		disableDeadlines:   disableDeadlines,
244		bigQueryReadClient: storagepb.NewBigQueryReadClient(connPool),
245		CallOptions:        &client.CallOptions,
246	}
247	c.setGoogleClientInfo()
248
249	client.internalClient = c
250
251	return &client, nil
252}
253
254// Connection returns a connection to the API service.
255//
256// Deprecated.
257func (c *bigQueryReadGRPCClient) Connection() *grpc.ClientConn {
258	return c.connPool.Conn()
259}
260
261// setGoogleClientInfo sets the name and version of the application in
262// the `x-goog-api-client` header passed on each request. Intended for
263// use by Google-written clients.
264func (c *bigQueryReadGRPCClient) setGoogleClientInfo(keyval ...string) {
265	kv := append([]string{"gl-go", versionGo()}, keyval...)
266	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
267	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
268}
269
270// Close closes the connection to the API service. The user should invoke this when
271// the client is no longer required.
272func (c *bigQueryReadGRPCClient) Close() error {
273	return c.connPool.Close()
274}
275
276func (c *bigQueryReadGRPCClient) CreateReadSession(ctx context.Context, req *storagepb.CreateReadSessionRequest, opts ...gax.CallOption) (*storagepb.ReadSession, error) {
277	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
278		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
279		defer cancel()
280		ctx = cctx
281	}
282	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "read_session.table", url.QueryEscape(req.GetReadSession().GetTable())))
283	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
284	opts = append((*c.CallOptions).CreateReadSession[0:len((*c.CallOptions).CreateReadSession):len((*c.CallOptions).CreateReadSession)], opts...)
285	var resp *storagepb.ReadSession
286	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
287		var err error
288		resp, err = c.bigQueryReadClient.CreateReadSession(ctx, req, settings.GRPC...)
289		return err
290	}, opts...)
291	if err != nil {
292		return nil, err
293	}
294	return resp, nil
295}
296
297func (c *bigQueryReadGRPCClient) ReadRows(ctx context.Context, req *storagepb.ReadRowsRequest, opts ...gax.CallOption) (storagepb.BigQueryRead_ReadRowsClient, error) {
298	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "read_stream", url.QueryEscape(req.GetReadStream())))
299	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
300	var resp storagepb.BigQueryRead_ReadRowsClient
301	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
302		var err error
303		resp, err = c.bigQueryReadClient.ReadRows(ctx, req, settings.GRPC...)
304		return err
305	}, opts...)
306	if err != nil {
307		return nil, err
308	}
309	return resp, nil
310}
311
312func (c *bigQueryReadGRPCClient) SplitReadStream(ctx context.Context, req *storagepb.SplitReadStreamRequest, opts ...gax.CallOption) (*storagepb.SplitReadStreamResponse, error) {
313	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
314		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
315		defer cancel()
316		ctx = cctx
317	}
318	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
319	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
320	opts = append((*c.CallOptions).SplitReadStream[0:len((*c.CallOptions).SplitReadStream):len((*c.CallOptions).SplitReadStream)], opts...)
321	var resp *storagepb.SplitReadStreamResponse
322	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
323		var err error
324		resp, err = c.bigQueryReadClient.SplitReadStream(ctx, req, settings.GRPC...)
325		return err
326	}, opts...)
327	if err != nil {
328		return nil, err
329	}
330	return resp, nil
331}
332