You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

82 lines
2.6 KiB

  1. // Copyright 2017 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. // http://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. package firestore
  15. import (
  16. "context"
  17. "errors"
  18. pb "google.golang.org/genproto/googleapis/firestore/v1"
  19. )
  20. // A WriteBatch holds multiple database updates. Build a batch with the Create, Set,
  21. // Update and Delete methods, then run it with the Commit method. Errors in Create,
  22. // Set, Update or Delete are recorded instead of being returned immediately. The
  23. // first such error is returned by Commit.
  24. type WriteBatch struct {
  25. c *Client
  26. err error
  27. writes []*pb.Write
  28. }
  29. func (b *WriteBatch) add(ws []*pb.Write, err error) *WriteBatch {
  30. if b.err != nil {
  31. return b
  32. }
  33. if err != nil {
  34. b.err = err
  35. return b
  36. }
  37. b.writes = append(b.writes, ws...)
  38. return b
  39. }
  40. // Create adds a Create operation to the batch.
  41. // See DocumentRef.Create for details.
  42. func (b *WriteBatch) Create(dr *DocumentRef, data interface{}) *WriteBatch {
  43. return b.add(dr.newCreateWrites(data))
  44. }
  45. // Set adds a Set operation to the batch.
  46. // See DocumentRef.Set for details.
  47. func (b *WriteBatch) Set(dr *DocumentRef, data interface{}, opts ...SetOption) *WriteBatch {
  48. return b.add(dr.newSetWrites(data, opts))
  49. }
  50. // Delete adds a Delete operation to the batch.
  51. // See DocumentRef.Delete for details.
  52. func (b *WriteBatch) Delete(dr *DocumentRef, opts ...Precondition) *WriteBatch {
  53. return b.add(dr.newDeleteWrites(opts))
  54. }
  55. // Update adds an Update operation to the batch.
  56. // See DocumentRef.Update for details.
  57. func (b *WriteBatch) Update(dr *DocumentRef, data []Update, opts ...Precondition) *WriteBatch {
  58. return b.add(dr.newUpdatePathWrites(data, opts))
  59. }
  60. // Commit applies all the writes in the batch to the database atomically. Commit
  61. // returns an error if there are no writes in the batch, if any errors occurred in
  62. // constructing the writes, or if the Commmit operation fails.
  63. func (b *WriteBatch) Commit(ctx context.Context) ([]*WriteResult, error) {
  64. if b.err != nil {
  65. return nil, b.err
  66. }
  67. if len(b.writes) == 0 {
  68. return nil, errors.New("firestore: cannot commit empty WriteBatch")
  69. }
  70. return b.c.commit(ctx, b.writes)
  71. }