Class: Google::Cloud::Firestore::CollectionGroup
- 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.
Instance Method Summary collapse
-
#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.
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.
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 |