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/v1beta2"
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.
114//
115// New code should use the v1 Read API going forward, if they don’t use Write
116// API at the same time.
117type BigQueryReadClient struct {
118	// The internal transport-dependent client.
119	internalClient internalBigQueryReadClient
120
121	// The call options for this service.
122	CallOptions *BigQueryReadCallOptions
123}
124
125// Wrapper methods routed to the internal client.
126
127// Close closes the connection to the API service. The user should invoke this when
128// the client is no longer required.
129func (c *BigQueryReadClient) Close() error {
130	return c.internalClient.Close()
131}
132
133// setGoogleClientInfo sets the name and version of the application in
134// the `x-goog-api-client` header passed on each request. Intended for
135// use by Google-written clients.
136func (c *BigQueryReadClient) setGoogleClientInfo(keyval ...string) {
137	c.internalClient.setGoogleClientInfo(keyval...)
138}
139
140// Connection returns a connection to the API service.
141//
142// Deprecated.
143func (c *BigQueryReadClient) Connection() *grpc.ClientConn {
144	return c.internalClient.Connection()
145}
146
147// CreateReadSession creates a new read session. A read session divides the contents of a
148// BigQuery table into one or more streams, which can then be used to read
149// data from the table. The read session also specifies properties of the
150// data to be read, such as a list of columns or a push-down filter describing
151// the rows to be returned.
152//
153// A particular row can be read by at most one stream. When the caller has
154// reached the end of each stream in the session, then all the data in the
155// table has been read.
156//
157// Data is assigned to each stream such that roughly the same number of
158// rows can be read from each stream. Because the server-side unit for
159// assigning data is collections of rows, the API does not guarantee that
160// each stream will return the same number or rows. Additionally, the
161// limits are enforced based on the number of pre-filtered rows, so some
162// filters can lead to lopsided assignments.
163//
164// Read sessions automatically expire 6 hours after they are created and do
165// not require manual clean-up by the caller.
166func (c *BigQueryReadClient) CreateReadSession(ctx context.Context, req *storagepb.CreateReadSessionRequest, opts ...gax.CallOption) (*storagepb.ReadSession, error) {
167	return c.internalClient.CreateReadSession(ctx, req, opts...)
168}
169
170// ReadRows reads rows from the stream in the format prescribed by the ReadSession.
171// Each response contains one or more table rows, up to a maximum of 100 MiB
172// per response; read requests which attempt to read individual rows larger
173// than 100 MiB will fail.
174//
175// Each request also returns a set of stream statistics reflecting the current
176// state of the stream.
177func (c *BigQueryReadClient) ReadRows(ctx context.Context, req *storagepb.ReadRowsRequest, opts ...gax.CallOption) (storagepb.BigQueryRead_ReadRowsClient, error) {
178	return c.internalClient.ReadRows(ctx, req, opts...)
179}
180
181// SplitReadStream splits a given ReadStream into two ReadStream objects. These
182// ReadStream objects are referred to as the primary and the residual
183// streams of the split. The original ReadStream can still be read from in
184// the same manner as before. Both of the returned ReadStream objects can
185// also be read from, and the rows returned by both child streams will be
186// the same as the rows read from the original stream.
187//
188// Moreover, the two child streams will be allocated back-to-back in the
189// original ReadStream. Concretely, it is guaranteed that for streams
190// original, primary, and residual, that original[0-j] = primary[0-j] and
191// original[j-n] = residual[0-m] once the streams have been read to
192// completion.
193func (c *BigQueryReadClient) SplitReadStream(ctx context.Context, req *storagepb.SplitReadStreamRequest, opts ...gax.CallOption) (*storagepb.SplitReadStreamResponse, error) {
194	return c.internalClient.SplitReadStream(ctx, req, opts...)
195}
196
197// bigQueryReadGRPCClient is a client for interacting with BigQuery Storage API over gRPC transport.
198//
199// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
200type bigQueryReadGRPCClient struct {
201	// Connection pool of gRPC connections to the service.
202	connPool gtransport.ConnPool
203
204	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
205	disableDeadlines bool
206
207	// Points back to the CallOptions field of the containing BigQueryReadClient
208	CallOptions **BigQueryReadCallOptions
209
210	// The gRPC API client.
211	bigQueryReadClient storagepb.BigQueryReadClient
212
213	// The x-goog-* metadata to be sent with each request.
214	xGoogMetadata metadata.MD
215}
216
217// NewBigQueryReadClient creates a new big query read client based on gRPC.
218// The returned client must be Closed when it is done being used to clean up its underlying connections.
219//
220// BigQuery Read API.
221//
222// The Read API can be used to read data from BigQuery.
223//
224// New code should use the v1 Read API going forward, if they don’t use Write
225// API at the same time.
226func NewBigQueryReadClient(ctx context.Context, opts ...option.ClientOption) (*BigQueryReadClient, error) {
227	clientOpts := defaultBigQueryReadGRPCClientOptions()
228	if newBigQueryReadClientHook != nil {
229		hookOpts, err := newBigQueryReadClientHook(ctx, clientHookParams{})
230		if err != nil {
231			return nil, err
232		}
233		clientOpts = append(clientOpts, hookOpts...)
234	}
235
236	disableDeadlines, err := checkDisableDeadlines()
237	if err != nil {
238		return nil, err
239	}
240
241	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
242	if err != nil {
243		return nil, err
244	}
245	client := BigQueryReadClient{CallOptions: defaultBigQueryReadCallOptions()}
246
247	c := &bigQueryReadGRPCClient{
248		connPool:           connPool,
249		disableDeadlines:   disableDeadlines,
250		bigQueryReadClient: storagepb.NewBigQueryReadClient(connPool),
251		CallOptions:        &client.CallOptions,
252	}
253	c.setGoogleClientInfo()
254
255	client.internalClient = c
256
257	return &client, nil
258}
259
260// Connection returns a connection to the API service.
261//
262// Deprecated.
263func (c *bigQueryReadGRPCClient) Connection() *grpc.ClientConn {
264	return c.connPool.Conn()
265}
266
267// setGoogleClientInfo sets the name and version of the application in
268// the `x-goog-api-client` header passed on each request. Intended for
269// use by Google-written clients.
270func (c *bigQueryReadGRPCClient) setGoogleClientInfo(keyval ...string) {
271	kv := append([]string{"gl-go", versionGo()}, keyval...)
272	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
273	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
274}
275
276// Close closes the connection to the API service. The user should invoke this when
277// the client is no longer required.
278func (c *bigQueryReadGRPCClient) Close() error {
279	return c.connPool.Close()
280}
281
282func (c *bigQueryReadGRPCClient) CreateReadSession(ctx context.Context, req *storagepb.CreateReadSessionRequest, opts ...gax.CallOption) (*storagepb.ReadSession, error) {
283	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
284		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
285		defer cancel()
286		ctx = cctx
287	}
288	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "read_session.table", url.QueryEscape(req.GetReadSession().GetTable())))
289	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
290	opts = append((*c.CallOptions).CreateReadSession[0:len((*c.CallOptions).CreateReadSession):len((*c.CallOptions).CreateReadSession)], opts...)
291	var resp *storagepb.ReadSession
292	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
293		var err error
294		resp, err = c.bigQueryReadClient.CreateReadSession(ctx, req, settings.GRPC...)
295		return err
296	}, opts...)
297	if err != nil {
298		return nil, err
299	}
300	return resp, nil
301}
302
303func (c *bigQueryReadGRPCClient) ReadRows(ctx context.Context, req *storagepb.ReadRowsRequest, opts ...gax.CallOption) (storagepb.BigQueryRead_ReadRowsClient, error) {
304	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "read_stream", url.QueryEscape(req.GetReadStream())))
305	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
306	var resp storagepb.BigQueryRead_ReadRowsClient
307	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
308		var err error
309		resp, err = c.bigQueryReadClient.ReadRows(ctx, req, settings.GRPC...)
310		return err
311	}, opts...)
312	if err != nil {
313		return nil, err
314	}
315	return resp, nil
316}
317
318func (c *bigQueryReadGRPCClient) SplitReadStream(ctx context.Context, req *storagepb.SplitReadStreamRequest, opts ...gax.CallOption) (*storagepb.SplitReadStreamResponse, error) {
319	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
320		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
321		defer cancel()
322		ctx = cctx
323	}
324	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
325	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
326	opts = append((*c.CallOptions).SplitReadStream[0:len((*c.CallOptions).SplitReadStream):len((*c.CallOptions).SplitReadStream)], opts...)
327	var resp *storagepb.SplitReadStreamResponse
328	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
329		var err error
330		resp, err = c.bigQueryReadClient.SplitReadStream(ctx, req, settings.GRPC...)
331		return err
332	}, opts...)
333	if err != nil {
334		return nil, err
335	}
336	return resp, nil
337}
338