Class: Google::Cloud::Firestore::CollectionGroup

Inherits:
Query
  • Object
show all
Defined in:
lib/google/cloud/firestore/collection_group.rb

Overview

CollectionGroup

A collection group object is used for adding documents, getting document references, and querying for documents, including with partitions.

See Google::Cloud::Firestore::Client#col_group and Query.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Get a collection group
col_group = firestore.col_group "cities"

# Get and print all city documents
col_group.get do |city|
  puts "#{city.document_id} has #{city[:population]} residents."
end

Instance Method Summary collapse

Methods inherited from Query

#aggregate_query, #end_at, #end_before, from_json, #get, #limit, #limit_to_last, #listen, #offset, #order, #select, #start_after, #start_at, #to_json, #where

Instance Method Details

#partitions(partition_count, read_time: nil) ⇒ Array<QueryPartition>

Partitions a query by returning partition cursors that can be used to run the query in parallel. The returned partition cursors are split points that can be used as starting/end points for the query results.

Examples:

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

col_group = firestore.col_group "cities"

partitions = col_group.partitions 3

queries = partitions.map(&:to_query)

partition with read time

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

col_group = firestore.col_group "cities"

read_time = Time.now

partitions = col_group.partitions 3, read_time: read_time

queries = partitions.map(&:to_query)

Parameters:

  • partition_count (Integer)

    The desired maximum number of partition points. The number must be strictly positive. The actual number of partitions returned may be fewer.

  • read_time (Time) (defaults to: nil)

    Reads documents as they were at the given time. This may not be older than 270 seconds. Optional

Returns:

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/google/cloud/firestore/collection_group.rb', line 80

def partitions partition_count, read_time: nil
  ensure_service!

  raise ArgumentError, "partition_count must be > 0" unless partition_count.positive?

  # Partition queries require explicit ordering by __name__.
  query_with_default_order = order "__name__"
  # Since we are always returning an extra partition (with en empty endBefore cursor), we reduce the desired
  # partition count by one.
  partition_count -= 1

  grpc_partitions = if partition_count.positive?
                      # Retrieve all pages, since cursor order is not guaranteed and they must be sorted.
                      list_all partition_count, query_with_default_order, read_time
                    else
                      [] # Ensure that a single, empty QueryPartition is returned.
                    end
  cursor_values = grpc_partitions.map do |cursor|
    # Convert each cursor to a (single-element) array of Google::Cloud::Firestore::DocumentReference.
    cursor.values.map do |value|
      Convert.value_to_raw value, client
    end
  end
  # Sort the values of the returned cursor, which right now should only contain a single reference value (which
  # needs to be sorted one component at a time).
  cursor_values.sort! do |a, b|
    a.first <=> b.first
  end

  start_at = nil
  results = cursor_values.map do |end_before|
    partition = QueryPartition.new query_with_default_order, start_at, end_before
    start_at = end_before
    partition
  end
  # Always add a final QueryPartition with an empty end_before value.
  results << QueryPartition.new(query_with_default_order, start_at, nil)
  results
end