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