Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

556 řádky
22 KiB

  1. // Copyright 2019 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. // Code generated by gapic-generator. DO NOT EDIT.
  15. package spanner
  16. import (
  17. "context"
  18. "math"
  19. "time"
  20. "github.com/golang/protobuf/proto"
  21. gax "github.com/googleapis/gax-go/v2"
  22. "google.golang.org/api/iterator"
  23. "google.golang.org/api/option"
  24. "google.golang.org/api/transport"
  25. spannerpb "google.golang.org/genproto/googleapis/spanner/v1"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // CallOptions contains the retry settings for each method of Client.
  31. type CallOptions struct {
  32. CreateSession []gax.CallOption
  33. GetSession []gax.CallOption
  34. ListSessions []gax.CallOption
  35. DeleteSession []gax.CallOption
  36. ExecuteSql []gax.CallOption
  37. ExecuteStreamingSql []gax.CallOption
  38. ExecuteBatchDml []gax.CallOption
  39. Read []gax.CallOption
  40. StreamingRead []gax.CallOption
  41. BeginTransaction []gax.CallOption
  42. Commit []gax.CallOption
  43. Rollback []gax.CallOption
  44. PartitionQuery []gax.CallOption
  45. PartitionRead []gax.CallOption
  46. }
  47. func defaultClientOptions() []option.ClientOption {
  48. return []option.ClientOption{
  49. option.WithEndpoint("spanner.googleapis.com:443"),
  50. option.WithScopes(DefaultAuthScopes()...),
  51. }
  52. }
  53. func defaultCallOptions() *CallOptions {
  54. retry := map[[2]string][]gax.CallOption{
  55. {"default", "idempotent"}: {
  56. gax.WithRetry(func() gax.Retryer {
  57. return gax.OnCodes([]codes.Code{
  58. codes.DeadlineExceeded,
  59. codes.Unavailable,
  60. }, gax.Backoff{
  61. Initial: 1000 * time.Millisecond,
  62. Max: 32000 * time.Millisecond,
  63. Multiplier: 1.3,
  64. })
  65. }),
  66. },
  67. {"long_running", "long_running"}: {
  68. gax.WithRetry(func() gax.Retryer {
  69. return gax.OnCodes([]codes.Code{
  70. codes.Unavailable,
  71. }, gax.Backoff{
  72. Initial: 1000 * time.Millisecond,
  73. Max: 32000 * time.Millisecond,
  74. Multiplier: 1.3,
  75. })
  76. }),
  77. },
  78. }
  79. return &CallOptions{
  80. CreateSession: retry[[2]string{"default", "idempotent"}],
  81. GetSession: retry[[2]string{"default", "idempotent"}],
  82. ListSessions: retry[[2]string{"default", "idempotent"}],
  83. DeleteSession: retry[[2]string{"default", "idempotent"}],
  84. ExecuteSql: retry[[2]string{"default", "idempotent"}],
  85. ExecuteStreamingSql: retry[[2]string{"streaming", "non_idempotent"}],
  86. ExecuteBatchDml: retry[[2]string{"default", "idempotent"}],
  87. Read: retry[[2]string{"default", "idempotent"}],
  88. StreamingRead: retry[[2]string{"streaming", "non_idempotent"}],
  89. BeginTransaction: retry[[2]string{"default", "idempotent"}],
  90. Commit: retry[[2]string{"long_running", "long_running"}],
  91. Rollback: retry[[2]string{"default", "idempotent"}],
  92. PartitionQuery: retry[[2]string{"default", "idempotent"}],
  93. PartitionRead: retry[[2]string{"default", "idempotent"}],
  94. }
  95. }
  96. // Client is a client for interacting with Cloud Spanner API.
  97. //
  98. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  99. type Client struct {
  100. // The connection to the service.
  101. conn *grpc.ClientConn
  102. // The gRPC API client.
  103. client spannerpb.SpannerClient
  104. // The call options for this service.
  105. CallOptions *CallOptions
  106. // The x-goog-* metadata to be sent with each request.
  107. xGoogMetadata metadata.MD
  108. }
  109. // NewClient creates a new spanner client.
  110. //
  111. // Cloud Spanner API
  112. //
  113. // The Cloud Spanner API can be used to manage sessions and execute
  114. // transactions on data stored in Cloud Spanner databases.
  115. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  116. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  117. if err != nil {
  118. return nil, err
  119. }
  120. c := &Client{
  121. conn: conn,
  122. CallOptions: defaultCallOptions(),
  123. client: spannerpb.NewSpannerClient(conn),
  124. }
  125. c.SetGoogleClientInfo()
  126. return c, nil
  127. }
  128. // Connection returns the client's connection to the API service.
  129. func (c *Client) Connection() *grpc.ClientConn {
  130. return c.conn
  131. }
  132. // Close closes the connection to the API service. The user should invoke this when
  133. // the client is no longer required.
  134. func (c *Client) Close() error {
  135. return c.conn.Close()
  136. }
  137. // SetGoogleClientInfo sets the name and version of the application in
  138. // the `x-goog-api-client` header passed on each request. Intended for
  139. // use by Google-written clients.
  140. func (c *Client) SetGoogleClientInfo(keyval ...string) {
  141. kv := append([]string{"gl-go", versionGo()}, keyval...)
  142. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  143. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  144. }
  145. // CreateSession creates a new session. A session can be used to perform
  146. // transactions that read and/or modify data in a Cloud Spanner database.
  147. // Sessions are meant to be reused for many consecutive
  148. // transactions.
  149. //
  150. // Sessions can only execute one transaction at a time. To execute
  151. // multiple concurrent read-write/write-only transactions, create
  152. // multiple sessions. Note that standalone reads and queries use a
  153. // transaction internally, and count toward the one transaction
  154. // limit.
  155. //
  156. // Cloud Spanner limits the number of sessions that can exist at any given
  157. // time; thus, it is a good idea to delete idle and/or unneeded sessions.
  158. // Aside from explicit deletes, Cloud Spanner can delete sessions for which no
  159. // operations are sent for more than an hour. If a session is deleted,
  160. // requests to it return NOT_FOUND.
  161. //
  162. // Idle sessions can be kept alive by sending a trivial SQL query
  163. // periodically, e.g., "SELECT 1".
  164. func (c *Client) CreateSession(ctx context.Context, req *spannerpb.CreateSessionRequest, opts ...gax.CallOption) (*spannerpb.Session, error) {
  165. ctx = insertMetadata(ctx, c.xGoogMetadata)
  166. opts = append(c.CallOptions.CreateSession[0:len(c.CallOptions.CreateSession):len(c.CallOptions.CreateSession)], opts...)
  167. var resp *spannerpb.Session
  168. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  169. var err error
  170. resp, err = c.client.CreateSession(ctx, req, settings.GRPC...)
  171. return err
  172. }, opts...)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return resp, nil
  177. }
  178. // GetSession gets a session. Returns NOT_FOUND if the session does not exist.
  179. // This is mainly useful for determining whether a session is still
  180. // alive.
  181. func (c *Client) GetSession(ctx context.Context, req *spannerpb.GetSessionRequest, opts ...gax.CallOption) (*spannerpb.Session, error) {
  182. ctx = insertMetadata(ctx, c.xGoogMetadata)
  183. opts = append(c.CallOptions.GetSession[0:len(c.CallOptions.GetSession):len(c.CallOptions.GetSession)], opts...)
  184. var resp *spannerpb.Session
  185. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  186. var err error
  187. resp, err = c.client.GetSession(ctx, req, settings.GRPC...)
  188. return err
  189. }, opts...)
  190. if err != nil {
  191. return nil, err
  192. }
  193. return resp, nil
  194. }
  195. // ListSessions lists all sessions in a given database.
  196. func (c *Client) ListSessions(ctx context.Context, req *spannerpb.ListSessionsRequest, opts ...gax.CallOption) *SessionIterator {
  197. ctx = insertMetadata(ctx, c.xGoogMetadata)
  198. opts = append(c.CallOptions.ListSessions[0:len(c.CallOptions.ListSessions):len(c.CallOptions.ListSessions)], opts...)
  199. it := &SessionIterator{}
  200. req = proto.Clone(req).(*spannerpb.ListSessionsRequest)
  201. it.InternalFetch = func(pageSize int, pageToken string) ([]*spannerpb.Session, string, error) {
  202. var resp *spannerpb.ListSessionsResponse
  203. req.PageToken = pageToken
  204. if pageSize > math.MaxInt32 {
  205. req.PageSize = math.MaxInt32
  206. } else {
  207. req.PageSize = int32(pageSize)
  208. }
  209. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  210. var err error
  211. resp, err = c.client.ListSessions(ctx, req, settings.GRPC...)
  212. return err
  213. }, opts...)
  214. if err != nil {
  215. return nil, "", err
  216. }
  217. return resp.Sessions, resp.NextPageToken, nil
  218. }
  219. fetch := func(pageSize int, pageToken string) (string, error) {
  220. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  221. if err != nil {
  222. return "", err
  223. }
  224. it.items = append(it.items, items...)
  225. return nextPageToken, nil
  226. }
  227. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  228. it.pageInfo.MaxSize = int(req.PageSize)
  229. return it
  230. }
  231. // DeleteSession ends a session, releasing server resources associated with it. This will
  232. // asynchronously trigger cancellation of any operations that are running with
  233. // this session.
  234. func (c *Client) DeleteSession(ctx context.Context, req *spannerpb.DeleteSessionRequest, opts ...gax.CallOption) error {
  235. ctx = insertMetadata(ctx, c.xGoogMetadata)
  236. opts = append(c.CallOptions.DeleteSession[0:len(c.CallOptions.DeleteSession):len(c.CallOptions.DeleteSession)], opts...)
  237. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  238. var err error
  239. _, err = c.client.DeleteSession(ctx, req, settings.GRPC...)
  240. return err
  241. }, opts...)
  242. return err
  243. }
  244. // ExecuteSql executes an SQL statement, returning all results in a single reply. This
  245. // method cannot be used to return a result set larger than 10 MiB;
  246. // if the query yields more data than that, the query fails with
  247. // a FAILED_PRECONDITION error.
  248. //
  249. // Operations inside read-write transactions might return ABORTED. If
  250. // this occurs, the application should restart the transaction from
  251. // the beginning. See [Transaction][google.spanner.v1.Transaction] for more
  252. // details.
  253. //
  254. // Larger result sets can be fetched in streaming fashion by calling
  255. // [ExecuteStreamingSql][google.spanner.v1.Spanner.ExecuteStreamingSql]
  256. // instead.
  257. func (c *Client) ExecuteSql(ctx context.Context, req *spannerpb.ExecuteSqlRequest, opts ...gax.CallOption) (*spannerpb.ResultSet, error) {
  258. ctx = insertMetadata(ctx, c.xGoogMetadata)
  259. opts = append(c.CallOptions.ExecuteSql[0:len(c.CallOptions.ExecuteSql):len(c.CallOptions.ExecuteSql)], opts...)
  260. var resp *spannerpb.ResultSet
  261. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  262. var err error
  263. resp, err = c.client.ExecuteSql(ctx, req, settings.GRPC...)
  264. return err
  265. }, opts...)
  266. if err != nil {
  267. return nil, err
  268. }
  269. return resp, nil
  270. }
  271. // ExecuteStreamingSql like [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql], except returns the
  272. // result set as a stream. Unlike
  273. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql], there is no limit on
  274. // the size of the returned result set. However, no individual row in the
  275. // result set can exceed 100 MiB, and no column value can exceed 10 MiB.
  276. func (c *Client) ExecuteStreamingSql(ctx context.Context, req *spannerpb.ExecuteSqlRequest, opts ...gax.CallOption) (spannerpb.Spanner_ExecuteStreamingSqlClient, error) {
  277. ctx = insertMetadata(ctx, c.xGoogMetadata)
  278. opts = append(c.CallOptions.ExecuteStreamingSql[0:len(c.CallOptions.ExecuteStreamingSql):len(c.CallOptions.ExecuteStreamingSql)], opts...)
  279. var resp spannerpb.Spanner_ExecuteStreamingSqlClient
  280. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  281. var err error
  282. resp, err = c.client.ExecuteStreamingSql(ctx, req, settings.GRPC...)
  283. return err
  284. }, opts...)
  285. if err != nil {
  286. return nil, err
  287. }
  288. return resp, nil
  289. }
  290. // ExecuteBatchDml executes a batch of SQL DML statements. This method allows many statements
  291. // to be run with lower latency than submitting them sequentially with
  292. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql].
  293. //
  294. // Statements are executed in order, sequentially.
  295. // [ExecuteBatchDmlResponse][Spanner.ExecuteBatchDmlResponse] will contain a
  296. // [ResultSet][google.spanner.v1.ResultSet] for each DML statement that has successfully executed. If a
  297. // statement fails, its error status will be returned as part of the
  298. // [ExecuteBatchDmlResponse][Spanner.ExecuteBatchDmlResponse]. Execution will
  299. // stop at the first failed statement; the remaining statements will not run.
  300. //
  301. // ExecuteBatchDml is expected to return an OK status with a response even if
  302. // there was an error while processing one of the DML statements. Clients must
  303. // inspect response.status to determine if there were any errors while
  304. // processing the request.
  305. //
  306. // See more details in
  307. // [ExecuteBatchDmlRequest][Spanner.ExecuteBatchDmlRequest] and
  308. // [ExecuteBatchDmlResponse][Spanner.ExecuteBatchDmlResponse].
  309. func (c *Client) ExecuteBatchDml(ctx context.Context, req *spannerpb.ExecuteBatchDmlRequest, opts ...gax.CallOption) (*spannerpb.ExecuteBatchDmlResponse, error) {
  310. ctx = insertMetadata(ctx, c.xGoogMetadata)
  311. opts = append(c.CallOptions.ExecuteBatchDml[0:len(c.CallOptions.ExecuteBatchDml):len(c.CallOptions.ExecuteBatchDml)], opts...)
  312. var resp *spannerpb.ExecuteBatchDmlResponse
  313. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  314. var err error
  315. resp, err = c.client.ExecuteBatchDml(ctx, req, settings.GRPC...)
  316. return err
  317. }, opts...)
  318. if err != nil {
  319. return nil, err
  320. }
  321. return resp, nil
  322. }
  323. // Read reads rows from the database using key lookups and scans, as a
  324. // simple key/value style alternative to
  325. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql]. This method cannot be
  326. // used to return a result set larger than 10 MiB; if the read matches more
  327. // data than that, the read fails with a FAILED_PRECONDITION
  328. // error.
  329. //
  330. // Reads inside read-write transactions might return ABORTED. If
  331. // this occurs, the application should restart the transaction from
  332. // the beginning. See [Transaction][google.spanner.v1.Transaction] for more
  333. // details.
  334. //
  335. // Larger result sets can be yielded in streaming fashion by calling
  336. // [StreamingRead][google.spanner.v1.Spanner.StreamingRead] instead.
  337. func (c *Client) Read(ctx context.Context, req *spannerpb.ReadRequest, opts ...gax.CallOption) (*spannerpb.ResultSet, error) {
  338. ctx = insertMetadata(ctx, c.xGoogMetadata)
  339. opts = append(c.CallOptions.Read[0:len(c.CallOptions.Read):len(c.CallOptions.Read)], opts...)
  340. var resp *spannerpb.ResultSet
  341. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  342. var err error
  343. resp, err = c.client.Read(ctx, req, settings.GRPC...)
  344. return err
  345. }, opts...)
  346. if err != nil {
  347. return nil, err
  348. }
  349. return resp, nil
  350. }
  351. // StreamingRead like [Read][google.spanner.v1.Spanner.Read], except returns the result set
  352. // as a stream. Unlike [Read][google.spanner.v1.Spanner.Read], there is no
  353. // limit on the size of the returned result set. However, no individual row in
  354. // the result set can exceed 100 MiB, and no column value can exceed
  355. // 10 MiB.
  356. func (c *Client) StreamingRead(ctx context.Context, req *spannerpb.ReadRequest, opts ...gax.CallOption) (spannerpb.Spanner_StreamingReadClient, error) {
  357. ctx = insertMetadata(ctx, c.xGoogMetadata)
  358. opts = append(c.CallOptions.StreamingRead[0:len(c.CallOptions.StreamingRead):len(c.CallOptions.StreamingRead)], opts...)
  359. var resp spannerpb.Spanner_StreamingReadClient
  360. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  361. var err error
  362. resp, err = c.client.StreamingRead(ctx, req, settings.GRPC...)
  363. return err
  364. }, opts...)
  365. if err != nil {
  366. return nil, err
  367. }
  368. return resp, nil
  369. }
  370. // BeginTransaction begins a new transaction. This step can often be skipped:
  371. // [Read][google.spanner.v1.Spanner.Read],
  372. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] and
  373. // [Commit][google.spanner.v1.Spanner.Commit] can begin a new transaction as a
  374. // side-effect.
  375. func (c *Client) BeginTransaction(ctx context.Context, req *spannerpb.BeginTransactionRequest, opts ...gax.CallOption) (*spannerpb.Transaction, error) {
  376. ctx = insertMetadata(ctx, c.xGoogMetadata)
  377. opts = append(c.CallOptions.BeginTransaction[0:len(c.CallOptions.BeginTransaction):len(c.CallOptions.BeginTransaction)], opts...)
  378. var resp *spannerpb.Transaction
  379. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  380. var err error
  381. resp, err = c.client.BeginTransaction(ctx, req, settings.GRPC...)
  382. return err
  383. }, opts...)
  384. if err != nil {
  385. return nil, err
  386. }
  387. return resp, nil
  388. }
  389. // Commit commits a transaction. The request includes the mutations to be
  390. // applied to rows in the database.
  391. //
  392. // Commit might return an ABORTED error. This can occur at any time;
  393. // commonly, the cause is conflicts with concurrent
  394. // transactions. However, it can also happen for a variety of other
  395. // reasons. If Commit returns ABORTED, the caller should re-attempt
  396. // the transaction from the beginning, re-using the same session.
  397. func (c *Client) Commit(ctx context.Context, req *spannerpb.CommitRequest, opts ...gax.CallOption) (*spannerpb.CommitResponse, error) {
  398. ctx = insertMetadata(ctx, c.xGoogMetadata)
  399. opts = append(c.CallOptions.Commit[0:len(c.CallOptions.Commit):len(c.CallOptions.Commit)], opts...)
  400. var resp *spannerpb.CommitResponse
  401. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  402. var err error
  403. resp, err = c.client.Commit(ctx, req, settings.GRPC...)
  404. return err
  405. }, opts...)
  406. if err != nil {
  407. return nil, err
  408. }
  409. return resp, nil
  410. }
  411. // Rollback rolls back a transaction, releasing any locks it holds. It is a good
  412. // idea to call this for any transaction that includes one or more
  413. // [Read][google.spanner.v1.Spanner.Read] or
  414. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] requests and ultimately
  415. // decides not to commit.
  416. //
  417. // Rollback returns OK if it successfully aborts the transaction, the
  418. // transaction was already aborted, or the transaction is not
  419. // found. Rollback never returns ABORTED.
  420. func (c *Client) Rollback(ctx context.Context, req *spannerpb.RollbackRequest, opts ...gax.CallOption) error {
  421. ctx = insertMetadata(ctx, c.xGoogMetadata)
  422. opts = append(c.CallOptions.Rollback[0:len(c.CallOptions.Rollback):len(c.CallOptions.Rollback)], opts...)
  423. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  424. var err error
  425. _, err = c.client.Rollback(ctx, req, settings.GRPC...)
  426. return err
  427. }, opts...)
  428. return err
  429. }
  430. // PartitionQuery creates a set of partition tokens that can be used to execute a query
  431. // operation in parallel. Each of the returned partition tokens can be used
  432. // by [ExecuteStreamingSql][google.spanner.v1.Spanner.ExecuteStreamingSql] to
  433. // specify a subset of the query result to read. The same session and
  434. // read-only transaction must be used by the PartitionQueryRequest used to
  435. // create the partition tokens and the ExecuteSqlRequests that use the
  436. // partition tokens.
  437. //
  438. // Partition tokens become invalid when the session used to create them
  439. // is deleted, is idle for too long, begins a new transaction, or becomes too
  440. // old. When any of these happen, it is not possible to resume the query, and
  441. // the whole operation must be restarted from the beginning.
  442. func (c *Client) PartitionQuery(ctx context.Context, req *spannerpb.PartitionQueryRequest, opts ...gax.CallOption) (*spannerpb.PartitionResponse, error) {
  443. ctx = insertMetadata(ctx, c.xGoogMetadata)
  444. opts = append(c.CallOptions.PartitionQuery[0:len(c.CallOptions.PartitionQuery):len(c.CallOptions.PartitionQuery)], opts...)
  445. var resp *spannerpb.PartitionResponse
  446. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  447. var err error
  448. resp, err = c.client.PartitionQuery(ctx, req, settings.GRPC...)
  449. return err
  450. }, opts...)
  451. if err != nil {
  452. return nil, err
  453. }
  454. return resp, nil
  455. }
  456. // PartitionRead creates a set of partition tokens that can be used to execute a read
  457. // operation in parallel. Each of the returned partition tokens can be used
  458. // by [StreamingRead][google.spanner.v1.Spanner.StreamingRead] to specify a
  459. // subset of the read result to read. The same session and read-only
  460. // transaction must be used by the PartitionReadRequest used to create the
  461. // partition tokens and the ReadRequests that use the partition tokens. There
  462. // are no ordering guarantees on rows returned among the returned partition
  463. // tokens, or even within each individual StreamingRead call issued with a
  464. // partition_token.
  465. //
  466. // Partition tokens become invalid when the session used to create them
  467. // is deleted, is idle for too long, begins a new transaction, or becomes too
  468. // old. When any of these happen, it is not possible to resume the read, and
  469. // the whole operation must be restarted from the beginning.
  470. func (c *Client) PartitionRead(ctx context.Context, req *spannerpb.PartitionReadRequest, opts ...gax.CallOption) (*spannerpb.PartitionResponse, error) {
  471. ctx = insertMetadata(ctx, c.xGoogMetadata)
  472. opts = append(c.CallOptions.PartitionRead[0:len(c.CallOptions.PartitionRead):len(c.CallOptions.PartitionRead)], opts...)
  473. var resp *spannerpb.PartitionResponse
  474. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  475. var err error
  476. resp, err = c.client.PartitionRead(ctx, req, settings.GRPC...)
  477. return err
  478. }, opts...)
  479. if err != nil {
  480. return nil, err
  481. }
  482. return resp, nil
  483. }
  484. // SessionIterator manages a stream of *spannerpb.Session.
  485. type SessionIterator struct {
  486. items []*spannerpb.Session
  487. pageInfo *iterator.PageInfo
  488. nextFunc func() error
  489. // InternalFetch is for use by the Google Cloud Libraries only.
  490. // It is not part of the stable interface of this package.
  491. //
  492. // InternalFetch returns results from a single call to the underlying RPC.
  493. // The number of results is no greater than pageSize.
  494. // If there are no more results, nextPageToken is empty and err is nil.
  495. InternalFetch func(pageSize int, pageToken string) (results []*spannerpb.Session, nextPageToken string, err error)
  496. }
  497. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  498. func (it *SessionIterator) PageInfo() *iterator.PageInfo {
  499. return it.pageInfo
  500. }
  501. // Next returns the next result. Its second return value is iterator.Done if there are no more
  502. // results. Once Next returns Done, all subsequent calls will return Done.
  503. func (it *SessionIterator) Next() (*spannerpb.Session, error) {
  504. var item *spannerpb.Session
  505. if err := it.nextFunc(); err != nil {
  506. return item, err
  507. }
  508. item = it.items[0]
  509. it.items = it.items[1:]
  510. return item, nil
  511. }
  512. func (it *SessionIterator) bufLen() int {
  513. return len(it.items)
  514. }
  515. func (it *SessionIterator) takeBuf() interface{} {
  516. b := it.items
  517. it.items = nil
  518. return b
  519. }