1// Copyright 2020 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/transport"
29	storagepb "google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1beta2"
30	"google.golang.org/grpc"
31	"google.golang.org/grpc/codes"
32	"google.golang.org/grpc/metadata"
33)
34
35// BigQueryReadCallOptions contains the retry settings for each method of BigQueryReadClient.
36type BigQueryReadCallOptions struct {
37	CreateReadSession []gax.CallOption
38	ReadRows          []gax.CallOption
39	SplitReadStream   []gax.CallOption
40}
41
42func defaultBigQueryReadClientOptions() []option.ClientOption {
43	return []option.ClientOption{
44		option.WithEndpoint("bigquerystorage.googleapis.com:443"),
45		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
46		option.WithScopes(DefaultAuthScopes()...),
47		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
48			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
49	}
50}
51
52func defaultBigQueryReadCallOptions() *BigQueryReadCallOptions {
53	return &BigQueryReadCallOptions{
54		CreateReadSession: []gax.CallOption{
55			gax.WithRetry(func() gax.Retryer {
56				return gax.OnCodes([]codes.Code{
57					codes.DeadlineExceeded,
58					codes.Unavailable,
59				}, gax.Backoff{
60					Initial:    100 * time.Millisecond,
61					Max:        60000 * time.Millisecond,
62					Multiplier: 1.30,
63				})
64			}),
65		},
66		ReadRows: []gax.CallOption{
67			gax.WithRetry(func() gax.Retryer {
68				return gax.OnCodes([]codes.Code{
69					codes.Unavailable,
70				}, gax.Backoff{
71					Initial:    100 * time.Millisecond,
72					Max:        60000 * time.Millisecond,
73					Multiplier: 1.30,
74				})
75			}),
76		},
77		SplitReadStream: []gax.CallOption{
78			gax.WithRetry(func() gax.Retryer {
79				return gax.OnCodes([]codes.Code{
80					codes.DeadlineExceeded,
81					codes.Unavailable,
82				}, gax.Backoff{
83					Initial:    100 * time.Millisecond,
84					Max:        60000 * time.Millisecond,
85					Multiplier: 1.30,
86				})
87			}),
88		},
89	}
90}
91
92// BigQueryReadClient is a client for interacting with BigQuery Storage API.
93//
94// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
95type BigQueryReadClient struct {
96	// The connection to the service.
97	conn *grpc.ClientConn
98
99	// The gRPC API client.
100	bigQueryReadClient storagepb.BigQueryReadClient
101
102	// The call options for this service.
103	CallOptions *BigQueryReadCallOptions
104
105	// The x-goog-* metadata to be sent with each request.
106	xGoogMetadata metadata.MD
107}
108
109// NewBigQueryReadClient creates a new big query read client.
110//
111// BigQuery Read API.
112//
113// The Read API can be used to read data from BigQuery.
114func NewBigQueryReadClient(ctx context.Context, opts ...option.ClientOption) (*BigQueryReadClient, error) {
115	conn, err := transport.DialGRPC(ctx, append(defaultBigQueryReadClientOptions(), opts...)...)
116	if err != nil {
117		return nil, err
118	}
119	c := &BigQueryReadClient{
120		conn:        conn,
121		CallOptions: defaultBigQueryReadCallOptions(),
122
123		bigQueryReadClient: storagepb.NewBigQueryReadClient(conn),
124	}
125	c.setGoogleClientInfo()
126
127	return c, nil
128}
129
130// Connection returns the client's connection to the API service.
131func (c *BigQueryReadClient) Connection() *grpc.ClientConn {
132	return c.conn
133}
134
135// Close closes the connection to the API service. The user should invoke this when
136// the client is no longer required.
137func (c *BigQueryReadClient) Close() error {
138	return c.conn.Close()
139}
140
141// setGoogleClientInfo sets the name and version of the application in
142// the `x-goog-api-client` header passed on each request. Intended for
143// use by Google-written clients.
144func (c *BigQueryReadClient) setGoogleClientInfo(keyval ...string) {
145	kv := append([]string{"gl-go", versionGo()}, keyval...)
146	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
147	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
148}
149
150// CreateReadSession creates a new read session. A read session divides the contents of a
151// BigQuery table into one or more streams, which can then be used to read
152// data from the table. The read session also specifies properties of the
153// data to be read, such as a list of columns or a push-down filter describing
154// the rows to be returned.
155//
156// A particular row can be read by at most one stream. When the caller has
157// reached the end of each stream in the session, then all the data in the
158// table has been read.
159//
160// Data is assigned to each stream such that roughly the same number of
161// rows can be read from each stream. Because the server-side unit for
162// assigning data is collections of rows, the API does not guarantee that
163// each stream will return the same number or rows. Additionally, the
164// limits are enforced based on the number of pre-filtered rows, so some
165// filters can lead to lopsided assignments.
166//
167// Read sessions automatically expire 24 hours after they are created and do
168// not require manual clean-up by the caller.
169func (c *BigQueryReadClient) CreateReadSession(ctx context.Context, req *storagepb.CreateReadSessionRequest, opts ...gax.CallOption) (*storagepb.ReadSession, error) {
170	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "read_session.table", url.QueryEscape(req.GetReadSession().GetTable())))
171	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
172	opts = append(c.CallOptions.CreateReadSession[0:len(c.CallOptions.CreateReadSession):len(c.CallOptions.CreateReadSession)], opts...)
173	var resp *storagepb.ReadSession
174	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
175		var err error
176		resp, err = c.bigQueryReadClient.CreateReadSession(ctx, req, settings.GRPC...)
177		return err
178	}, opts...)
179	if err != nil {
180		return nil, err
181	}
182	return resp, nil
183}
184
185// ReadRows reads rows from the stream in the format prescribed by the ReadSession.
186// Each response contains one or more table rows, up to a maximum of 100 MiB
187// per response; read requests which attempt to read individual rows larger
188// than 100 MiB will fail.
189//
190// Each request also returns a set of stream statistics reflecting the current
191// state of the stream.
192func (c *BigQueryReadClient) ReadRows(ctx context.Context, req *storagepb.ReadRowsRequest, opts ...gax.CallOption) (storagepb.BigQueryRead_ReadRowsClient, error) {
193	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "read_stream", url.QueryEscape(req.GetReadStream())))
194	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
195	opts = append(c.CallOptions.ReadRows[0:len(c.CallOptions.ReadRows):len(c.CallOptions.ReadRows)], opts...)
196	var resp storagepb.BigQueryRead_ReadRowsClient
197	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
198		var err error
199		resp, err = c.bigQueryReadClient.ReadRows(ctx, req, settings.GRPC...)
200		return err
201	}, opts...)
202	if err != nil {
203		return nil, err
204	}
205	return resp, nil
206}
207
208// SplitReadStream splits a given ReadStream into two ReadStream objects. These
209// ReadStream objects are referred to as the primary and the residual
210// streams of the split. The original ReadStream can still be read from in
211// the same manner as before. Both of the returned ReadStream objects can
212// also be read from, and the rows returned by both child streams will be
213// the same as the rows read from the original stream.
214//
215// Moreover, the two child streams will be allocated back-to-back in the
216// original ReadStream. Concretely, it is guaranteed that for streams
217// original, primary, and residual, that original[0-j] = primary[0-j] and
218// original[j-n] = residual[0-m] once the streams have been read to
219// completion.
220func (c *BigQueryReadClient) SplitReadStream(ctx context.Context, req *storagepb.SplitReadStreamRequest, opts ...gax.CallOption) (*storagepb.SplitReadStreamResponse, error) {
221	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
222	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
223	opts = append(c.CallOptions.SplitReadStream[0:len(c.CallOptions.SplitReadStream):len(c.CallOptions.SplitReadStream)], opts...)
224	var resp *storagepb.SplitReadStreamResponse
225	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
226		var err error
227		resp, err = c.bigQueryReadClient.SplitReadStream(ctx, req, settings.GRPC...)
228		return err
229	}, opts...)
230	if err != nil {
231		return nil, err
232	}
233	return resp, nil
234}
235