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.
 
 
 

142 lines
4.0 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. "math/rand"
  18. "os"
  19. "sync"
  20. "time"
  21. )
  22. // A CollectionRef is a reference to Firestore collection.
  23. type CollectionRef struct {
  24. c *Client
  25. // The full resource path of the collection's parent. Typically Parent.Path,
  26. // or c.path if Parent is nil. May be different if this CollectionRef was
  27. // created from a stored reference to a different project/DB. Always
  28. // includes /documents - that is, the parent is minimally considered to be
  29. // "<db>/documents".
  30. //
  31. // For example, "projects/P/databases/D/documents/coll-1/doc-1".
  32. parentPath string
  33. // The shorter resource path of the collection. A collection "coll-2" in
  34. // document "doc-1" in collection "coll-1" would be: "coll-1/doc-1/coll-2".
  35. selfPath string
  36. // Parent is the document of which this collection is a part. It is
  37. // nil for top-level collections.
  38. Parent *DocumentRef
  39. // The full resource path of the collection: "projects/P/databases/D/documents..."
  40. Path string
  41. // ID is the collection identifier.
  42. ID string
  43. // Use the methods of Query on a CollectionRef to create and run queries.
  44. Query
  45. }
  46. func newTopLevelCollRef(c *Client, dbPath, id string) *CollectionRef {
  47. return &CollectionRef{
  48. c: c,
  49. ID: id,
  50. parentPath: dbPath + "/documents",
  51. selfPath: id,
  52. Path: dbPath + "/documents/" + id,
  53. Query: Query{
  54. c: c,
  55. collectionID: id,
  56. path: dbPath + "/documents/" + id,
  57. parentPath: dbPath + "/documents",
  58. },
  59. }
  60. }
  61. func newCollRefWithParent(c *Client, parent *DocumentRef, id string) *CollectionRef {
  62. selfPath := parent.shortPath + "/" + id
  63. return &CollectionRef{
  64. c: c,
  65. Parent: parent,
  66. ID: id,
  67. parentPath: parent.Path,
  68. selfPath: selfPath,
  69. Path: parent.Path + "/" + id,
  70. Query: Query{
  71. c: c,
  72. collectionID: id,
  73. path: parent.Path + "/" + id,
  74. parentPath: parent.Path,
  75. },
  76. }
  77. }
  78. // Doc returns a DocumentRef that refers to the document in the collection with the
  79. // given identifier.
  80. func (c *CollectionRef) Doc(id string) *DocumentRef {
  81. if c == nil {
  82. return nil
  83. }
  84. return newDocRef(c, id)
  85. }
  86. // NewDoc returns a DocumentRef with a uniquely generated ID.
  87. func (c *CollectionRef) NewDoc() *DocumentRef {
  88. return c.Doc(uniqueID())
  89. }
  90. // Add generates a DocumentRef with a unique ID. It then creates the document
  91. // with the given data, which can be a map[string]interface{}, a struct or a
  92. // pointer to a struct.
  93. //
  94. // Add returns an error in the unlikely event that a document with the same ID
  95. // already exists.
  96. func (c *CollectionRef) Add(ctx context.Context, data interface{}) (*DocumentRef, *WriteResult, error) {
  97. d := c.NewDoc()
  98. wr, err := d.Create(ctx, data)
  99. if err != nil {
  100. return nil, nil, err
  101. }
  102. return d, wr, nil
  103. }
  104. // DocumentRefs returns references to all the documents in the collection, including
  105. // missing documents. A missing document is a document that does not exist but has
  106. // sub-documents.
  107. func (c *CollectionRef) DocumentRefs(ctx context.Context) *DocumentRefIterator {
  108. return newDocumentRefIterator(ctx, c, nil)
  109. }
  110. const alphanum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  111. var (
  112. rngMu sync.Mutex
  113. rng = rand.New(rand.NewSource(time.Now().UnixNano() ^ int64(os.Getpid())))
  114. )
  115. func uniqueID() string {
  116. var b [20]byte
  117. rngMu.Lock()
  118. for i := 0; i < len(b); i++ {
  119. b[i] = alphanum[rng.Intn(len(alphanum))]
  120. }
  121. rngMu.Unlock()
  122. return string(b[:])
  123. }