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.
 
 
 

130 lines
3.4 KiB

  1. // Copyright 2018 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 datastore
  15. import (
  16. "fmt"
  17. pb "google.golang.org/genproto/googleapis/datastore/v1"
  18. )
  19. // A Mutation represents a change to a Datastore entity.
  20. type Mutation struct {
  21. key *Key // needed for transaction PendingKeys and to dedup deletions
  22. mut *pb.Mutation
  23. err error
  24. }
  25. func (m *Mutation) isDelete() bool {
  26. _, ok := m.mut.Operation.(*pb.Mutation_Delete)
  27. return ok
  28. }
  29. // NewInsert creates a mutation that will save the entity src into the datastore with
  30. // key k, returning an error if k already exists.
  31. // See Client.Put for valid values of src.
  32. func NewInsert(k *Key, src interface{}) *Mutation {
  33. if !k.valid() {
  34. return &Mutation{err: ErrInvalidKey}
  35. }
  36. p, err := saveEntity(k, src)
  37. if err != nil {
  38. return &Mutation{err: err}
  39. }
  40. return &Mutation{
  41. key: k,
  42. mut: &pb.Mutation{Operation: &pb.Mutation_Insert{Insert: p}},
  43. }
  44. }
  45. // NewUpsert creates a mutation that saves the entity src into the datastore with key
  46. // k, whether or not k exists. See Client.Put for valid values of src.
  47. func NewUpsert(k *Key, src interface{}) *Mutation {
  48. if !k.valid() {
  49. return &Mutation{err: ErrInvalidKey}
  50. }
  51. p, err := saveEntity(k, src)
  52. if err != nil {
  53. return &Mutation{err: err}
  54. }
  55. return &Mutation{
  56. key: k,
  57. mut: &pb.Mutation{Operation: &pb.Mutation_Upsert{Upsert: p}},
  58. }
  59. }
  60. // NewUpdate creates a mutation that replaces the entity in the datastore with key k,
  61. // returning an error if k does not exist. See Client.Put for valid values of src.
  62. func NewUpdate(k *Key, src interface{}) *Mutation {
  63. if !k.valid() {
  64. return &Mutation{err: ErrInvalidKey}
  65. }
  66. if k.Incomplete() {
  67. return &Mutation{err: fmt.Errorf("datastore: can't update the incomplete key: %v", k)}
  68. }
  69. p, err := saveEntity(k, src)
  70. if err != nil {
  71. return &Mutation{err: err}
  72. }
  73. return &Mutation{
  74. key: k,
  75. mut: &pb.Mutation{Operation: &pb.Mutation_Update{Update: p}},
  76. }
  77. }
  78. // NewDelete creates a mutation that deletes the entity with key k.
  79. func NewDelete(k *Key) *Mutation {
  80. if !k.valid() {
  81. return &Mutation{err: ErrInvalidKey}
  82. }
  83. if k.Incomplete() {
  84. return &Mutation{err: fmt.Errorf("datastore: can't delete the incomplete key: %v", k)}
  85. }
  86. return &Mutation{
  87. key: k,
  88. mut: &pb.Mutation{Operation: &pb.Mutation_Delete{Delete: keyToProto(k)}},
  89. }
  90. }
  91. func mutationProtos(muts []*Mutation) ([]*pb.Mutation, error) {
  92. // If any of the mutations have errors, collect and return them.
  93. var merr MultiError
  94. for i, m := range muts {
  95. if m.err != nil {
  96. if merr == nil {
  97. merr = make(MultiError, len(muts))
  98. }
  99. merr[i] = m.err
  100. }
  101. }
  102. if merr != nil {
  103. return nil, merr
  104. }
  105. var protos []*pb.Mutation
  106. // Collect protos. Remove duplicate deletions (see deleteMutations).
  107. seen := map[string]bool{}
  108. for _, m := range muts {
  109. if m.isDelete() {
  110. ks := m.key.String()
  111. if seen[ks] {
  112. continue
  113. }
  114. seen[ks] = true
  115. }
  116. protos = append(protos, m.mut)
  117. }
  118. return protos, nil
  119. }