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