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 newBigQueryWriteClientHook clientHook
37
38// BigQueryWriteCallOptions contains the retry settings for each method of BigQueryWriteClient.
39type BigQueryWriteCallOptions struct {
40	CreateWriteStream       []gax.CallOption
41	AppendRows              []gax.CallOption
42	GetWriteStream          []gax.CallOption
43	FinalizeWriteStream     []gax.CallOption
44	BatchCommitWriteStreams []gax.CallOption
45	FlushRows               []gax.CallOption
46}
47
48func defaultBigQueryWriteClientOptions() []option.ClientOption {
49	return []option.ClientOption{
50		internaloption.WithDefaultEndpoint("bigquerystorage.googleapis.com:443"),
51		internaloption.WithDefaultMTLSEndpoint("bigquerystorage.mtls.googleapis.com:443"),
52		internaloption.WithDefaultAudience("https://bigquerystorage.googleapis.com/"),
53		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
54		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
55		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
56			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
57	}
58}
59
60func defaultBigQueryWriteCallOptions() *BigQueryWriteCallOptions {
61	return &BigQueryWriteCallOptions{
62		CreateWriteStream: []gax.CallOption{
63			gax.WithRetry(func() gax.Retryer {
64				return gax.OnCodes([]codes.Code{
65					codes.DeadlineExceeded,
66					codes.Unavailable,
67					codes.ResourceExhausted,
68				}, gax.Backoff{
69					Initial:    100 * time.Millisecond,
70					Max:        60000 * time.Millisecond,
71					Multiplier: 1.30,
72				})
73			}),
74		},
75		AppendRows: []gax.CallOption{
76			gax.WithRetry(func() gax.Retryer {
77				return gax.OnCodes([]codes.Code{
78					codes.Unavailable,
79					codes.ResourceExhausted,
80				}, gax.Backoff{
81					Initial:    100 * time.Millisecond,
82					Max:        60000 * time.Millisecond,
83					Multiplier: 1.30,
84				})
85			}),
86		},
87		GetWriteStream: []gax.CallOption{
88			gax.WithRetry(func() gax.Retryer {
89				return gax.OnCodes([]codes.Code{
90					codes.DeadlineExceeded,
91					codes.Unavailable,
92				}, gax.Backoff{
93					Initial:    100 * time.Millisecond,
94					Max:        60000 * time.Millisecond,
95					Multiplier: 1.30,
96				})
97			}),
98		},
99		FinalizeWriteStream: []gax.CallOption{
100			gax.WithRetry(func() gax.Retryer {
101				return gax.OnCodes([]codes.Code{
102					codes.DeadlineExceeded,
103					codes.Unavailable,
104				}, gax.Backoff{
105					Initial:    100 * time.Millisecond,
106					Max:        60000 * time.Millisecond,
107					Multiplier: 1.30,
108				})
109			}),
110		},
111		BatchCommitWriteStreams: []gax.CallOption{
112			gax.WithRetry(func() gax.Retryer {
113				return gax.OnCodes([]codes.Code{
114					codes.DeadlineExceeded,
115					codes.Unavailable,
116				}, gax.Backoff{
117					Initial:    100 * time.Millisecond,
118					Max:        60000 * time.Millisecond,
119					Multiplier: 1.30,
120				})
121			}),
122		},
123		FlushRows: []gax.CallOption{
124			gax.WithRetry(func() gax.Retryer {
125				return gax.OnCodes([]codes.Code{
126					codes.DeadlineExceeded,
127					codes.Unavailable,
128				}, gax.Backoff{
129					Initial:    100 * time.Millisecond,
130					Max:        60000 * time.Millisecond,
131					Multiplier: 1.30,
132				})
133			}),
134		},
135	}
136}
137
138// BigQueryWriteClient is a client for interacting with BigQuery Storage API.
139//
140// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
141type BigQueryWriteClient struct {
142	// Connection pool of gRPC connections to the service.
143	connPool gtransport.ConnPool
144
145	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
146	disableDeadlines bool
147
148	// The gRPC API client.
149	bigQueryWriteClient storagepb.BigQueryWriteClient
150
151	// The call options for this service.
152	CallOptions *BigQueryWriteCallOptions
153
154	// The x-goog-* metadata to be sent with each request.
155	xGoogMetadata metadata.MD
156}
157
158// NewBigQueryWriteClient creates a new big query write client.
159//
160// BigQuery Write API.
161//
162// The Write API can be used to write data to BigQuery.
163func NewBigQueryWriteClient(ctx context.Context, opts ...option.ClientOption) (*BigQueryWriteClient, error) {
164	clientOpts := defaultBigQueryWriteClientOptions()
165
166	if newBigQueryWriteClientHook != nil {
167		hookOpts, err := newBigQueryWriteClientHook(ctx, clientHookParams{})
168		if err != nil {
169			return nil, err
170		}
171		clientOpts = append(clientOpts, hookOpts...)
172	}
173
174	disableDeadlines, err := checkDisableDeadlines()
175	if err != nil {
176		return nil, err
177	}
178
179	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
180	if err != nil {
181		return nil, err
182	}
183	c := &BigQueryWriteClient{
184		connPool:         connPool,
185		disableDeadlines: disableDeadlines,
186		CallOptions:      defaultBigQueryWriteCallOptions(),
187
188		bigQueryWriteClient: storagepb.NewBigQueryWriteClient(connPool),
189	}
190	c.setGoogleClientInfo()
191
192	return c, nil
193}
194
195// Connection returns a connection to the API service.
196//
197// Deprecated.
198func (c *BigQueryWriteClient) Connection() *grpc.ClientConn {
199	return c.connPool.Conn()
200}
201
202// Close closes the connection to the API service. The user should invoke this when
203// the client is no longer required.
204func (c *BigQueryWriteClient) Close() error {
205	return c.connPool.Close()
206}
207
208// setGoogleClientInfo sets the name and version of the application in
209// the `x-goog-api-client` header passed on each request. Intended for
210// use by Google-written clients.
211func (c *BigQueryWriteClient) setGoogleClientInfo(keyval ...string) {
212	kv := append([]string{"gl-go", versionGo()}, keyval...)
213	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
214	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
215}
216
217// CreateWriteStream creates a write stream to the given table.
218// Additionally, every table has a special COMMITTED stream named ‘_default’
219// to which data can be written. This stream doesn’t need to be created using
220// CreateWriteStream. It is a stream that can be used simultaneously by any
221// number of clients. Data written to this stream is considered committed as
222// soon as an acknowledgement is received.
223func (c *BigQueryWriteClient) CreateWriteStream(ctx context.Context, req *storagepb.CreateWriteStreamRequest, opts ...gax.CallOption) (*storagepb.WriteStream, error) {
224	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
225		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
226		defer cancel()
227		ctx = cctx
228	}
229	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
230	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
231	opts = append(c.CallOptions.CreateWriteStream[0:len(c.CallOptions.CreateWriteStream):len(c.CallOptions.CreateWriteStream)], opts...)
232	var resp *storagepb.WriteStream
233	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
234		var err error
235		resp, err = c.bigQueryWriteClient.CreateWriteStream(ctx, req, settings.GRPC...)
236		return err
237	}, opts...)
238	if err != nil {
239		return nil, err
240	}
241	return resp, nil
242}
243
244// AppendRows appends data to the given stream.
245//
246// If offset is specified, the offset is checked against the end of
247// stream. The server returns OUT_OF_RANGE in AppendRowsResponse if an
248// attempt is made to append to an offset beyond the current end of the stream
249// or ALREADY_EXISTS if user provids an offset that has already been
250// written to. User can retry with adjusted offset within the same RPC
251// stream. If offset is not specified, append happens at the end of the
252// stream.
253//
254// The response contains the offset at which the append happened. Responses
255// are received in the same order in which requests are sent. There will be
256// one response for each successful request. If the offset is not set in
257// response, it means append didn’t happen due to some errors. If one request
258// fails, all the subsequent requests will also fail until a success request
259// is made again.
260//
261// If the stream is of PENDING type, data will only be available for read
262// operations after the stream is committed.
263func (c *BigQueryWriteClient) AppendRows(ctx context.Context, opts ...gax.CallOption) (storagepb.BigQueryWrite_AppendRowsClient, error) {
264	ctx = insertMetadata(ctx, c.xGoogMetadata)
265	opts = append(c.CallOptions.AppendRows[0:len(c.CallOptions.AppendRows):len(c.CallOptions.AppendRows)], opts...)
266	var resp storagepb.BigQueryWrite_AppendRowsClient
267	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
268		var err error
269		resp, err = c.bigQueryWriteClient.AppendRows(ctx, settings.GRPC...)
270		return err
271	}, opts...)
272	if err != nil {
273		return nil, err
274	}
275	return resp, nil
276}
277
278// GetWriteStream gets a write stream.
279func (c *BigQueryWriteClient) GetWriteStream(ctx context.Context, req *storagepb.GetWriteStreamRequest, opts ...gax.CallOption) (*storagepb.WriteStream, error) {
280	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
281		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
282		defer cancel()
283		ctx = cctx
284	}
285	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
286	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
287	opts = append(c.CallOptions.GetWriteStream[0:len(c.CallOptions.GetWriteStream):len(c.CallOptions.GetWriteStream)], opts...)
288	var resp *storagepb.WriteStream
289	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
290		var err error
291		resp, err = c.bigQueryWriteClient.GetWriteStream(ctx, req, settings.GRPC...)
292		return err
293	}, opts...)
294	if err != nil {
295		return nil, err
296	}
297	return resp, nil
298}
299
300// FinalizeWriteStream finalize a write stream so that no new data can be appended to the
301// stream. Finalize is not supported on the ‘_default’ stream.
302func (c *BigQueryWriteClient) FinalizeWriteStream(ctx context.Context, req *storagepb.FinalizeWriteStreamRequest, opts ...gax.CallOption) (*storagepb.FinalizeWriteStreamResponse, error) {
303	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
304		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
305		defer cancel()
306		ctx = cctx
307	}
308	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
309	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
310	opts = append(c.CallOptions.FinalizeWriteStream[0:len(c.CallOptions.FinalizeWriteStream):len(c.CallOptions.FinalizeWriteStream)], opts...)
311	var resp *storagepb.FinalizeWriteStreamResponse
312	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
313		var err error
314		resp, err = c.bigQueryWriteClient.FinalizeWriteStream(ctx, req, settings.GRPC...)
315		return err
316	}, opts...)
317	if err != nil {
318		return nil, err
319	}
320	return resp, nil
321}
322
323// BatchCommitWriteStreams atomically commits a group of PENDING streams that belong to the same
324// parent table.
325// Streams must be finalized before commit and cannot be committed multiple
326// times. Once a stream is committed, data in the stream becomes available
327// for read operations.
328func (c *BigQueryWriteClient) BatchCommitWriteStreams(ctx context.Context, req *storagepb.BatchCommitWriteStreamsRequest, opts ...gax.CallOption) (*storagepb.BatchCommitWriteStreamsResponse, error) {
329	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
330		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
331		defer cancel()
332		ctx = cctx
333	}
334	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
335	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
336	opts = append(c.CallOptions.BatchCommitWriteStreams[0:len(c.CallOptions.BatchCommitWriteStreams):len(c.CallOptions.BatchCommitWriteStreams)], opts...)
337	var resp *storagepb.BatchCommitWriteStreamsResponse
338	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
339		var err error
340		resp, err = c.bigQueryWriteClient.BatchCommitWriteStreams(ctx, req, settings.GRPC...)
341		return err
342	}, opts...)
343	if err != nil {
344		return nil, err
345	}
346	return resp, nil
347}
348
349// FlushRows flushes rows to a BUFFERED stream.
350// If users are appending rows to BUFFERED stream, flush operation is
351// required in order for the rows to become available for reading. A
352// Flush operation flushes up to any previously flushed offset in a BUFFERED
353// stream, to the offset specified in the request.
354// Flush is not supported on the _default stream, since it is not BUFFERED.
355func (c *BigQueryWriteClient) FlushRows(ctx context.Context, req *storagepb.FlushRowsRequest, opts ...gax.CallOption) (*storagepb.FlushRowsResponse, error) {
356	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
357		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
358		defer cancel()
359		ctx = cctx
360	}
361	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "write_stream", url.QueryEscape(req.GetWriteStream())))
362	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
363	opts = append(c.CallOptions.FlushRows[0:len(c.CallOptions.FlushRows):len(c.CallOptions.FlushRows)], opts...)
364	var resp *storagepb.FlushRowsResponse
365	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
366		var err error
367		resp, err = c.bigQueryWriteClient.FlushRows(ctx, req, settings.GRPC...)
368		return err
369	}, opts...)
370	if err != nil {
371		return nil, err
372	}
373	return resp, nil
374}
375