ic_db/store/query/aggregate/jobs/extrema.rs
1//! Retained extrema over the synchronous comparator and shared job lifecycle.
2
3use super::super::Extremum;
4use super::{AggregateJobs, JobHandle, JobStep};
5use crate::{Entity, Error, GroupPage, GroupValue, Timestamp, Value, store::Query};
6use ic_storage::Memory;
7
8impl<E: Entity, M: Memory> Query<'_, E, M> {
9 /// Retain minima per exact key record, without scanning input. R selects one
10 /// exact ordered field, as in `group_min`; key and operand may overlap.
11 /// Retain all-absent groups with None. Keep at most 32 groups within the
12 /// complete 16 KiB job frame, including keys, winners and progress.
13 /// Group predicates, order and total limit bind the job. No automatic spill.
14 /// # Errors
15 /// Refuses unsupported shapes, page modifiers, expiry or occupied capacity.
16 /// # Panics
17 /// Supplier mutation failures trap the enclosing message.
18 pub fn start_group_min<K: Value, R: Value>(
19 self,
20 jobs: &mut AggregateJobs<M>,
21 now: Timestamp,
22 expires: Timestamp,
23 ) -> Result<JobHandle, Error> {
24 observe!(start, {
25 let admitted = self.group_extremum_plan::<K, R>(Extremum::Min)?;
26
27 self.start_group_value(jobs, now, expires, admitted)
28 })
29 }
30
31 /// Retain maxima per exact key record with `start_group_min`'s bounds.
32 /// Minimum and maximum have separate job identities.
33 /// # Errors
34 /// The same query, expiry and capacity errors as `start_group_min`.
35 /// # Panics
36 /// Supplier mutation failures trap the enclosing message.
37 pub fn start_group_max<K: Value, R: Value>(
38 self,
39 jobs: &mut AggregateJobs<M>,
40 now: Timestamp,
41 expires: Timestamp,
42 ) -> Result<JobHandle, Error> {
43 observe!(start, {
44 let admitted = self.group_extremum_plan::<K, R>(Extremum::Max)?;
45
46 self.start_group_value(jobs, now, expires, admitted)
47 })
48 }
49
50 /// Advance grouped minima with the retained revision, retry and expiry contract.
51 /// Finish all input before group filtering, ordering and limiting. Absent keys
52 /// form a group; all-absent operands yield None. Use `group_page` for completed
53 /// result pagination, retaining its continuation when mapping rows.
54 /// # Errors
55 /// Refuses mismatched queries/operations, corrupt winners, group/frame bounds
56 /// and oversized output without changing saved progress. Selection cannot
57 /// hide state exhaustion. No prefix winner is returned as an exact result.
58 /// # Panics
59 /// Supplier mutation failures trap the enclosing message.
60 pub fn resume_group_min<K: Value, R: Value>(
61 self,
62 jobs: &mut AggregateJobs<M>,
63 handle: &JobHandle,
64 sequence: u64,
65 now: Timestamp,
66 ) -> Result<JobStep<GroupPage<GroupValue<K, R>>>, Error> {
67 observe!(resume, {
68 let admitted = self.group_extremum_plan::<K, R>(Extremum::Min)?;
69
70 self.resume_group_value(jobs, handle, sequence, now, admitted)
71 })
72 }
73
74 /// Advance grouped maxima with `resume_group_min`'s exactness and bounds.
75 /// Equal operands retain their first encountered value.
76 /// # Errors
77 /// The same query, checkpoint, resource and output errors as `resume_group_min`.
78 /// # Panics
79 /// Supplier mutation failures trap the enclosing message.
80 pub fn resume_group_max<K: Value, R: Value>(
81 self,
82 jobs: &mut AggregateJobs<M>,
83 handle: &JobHandle,
84 sequence: u64,
85 now: Timestamp,
86 ) -> Result<JobStep<GroupPage<GroupValue<K, R>>>, Error> {
87 observe!(resume, {
88 let admitted = self.group_extremum_plan::<K, R>(Extremum::Max)?;
89
90 self.resume_group_value(jobs, handle, sequence, now, admitted)
91 })
92 }
93
94 /// Retain a minimum over one exact single-field result record, without scanning.
95 /// Uses `min`'s ordered types and absence semantics and `start_count`'s expiry,
96 /// revision and capacity bounds. Persist only the winning record, within the
97 /// complete 16 KiB job frame; no partial result or automatic spill.
98 /// # Errors
99 /// Refuses unsupported projections/query shapes, expiry or occupied capacity.
100 /// # Panics
101 /// Supplier mutation failures trap the enclosing message.
102 pub fn start_min<R: Value>(
103 self,
104 jobs: &mut AggregateJobs<M>,
105 now: Timestamp,
106 expires: Timestamp,
107 ) -> Result<JobHandle, Error> {
108 observe!(start, {
109 let admitted = self.extremum_plan::<R>(Extremum::Min)?;
110 self.start_value(jobs, now, expires, admitted)
111 })
112 }
113
114 /// Retain a maximum with `start_min`'s types and bounds. Minimum and maximum
115 /// have distinct job identities, even when they select the same field.
116 /// # Errors
117 /// The same admission, expiry and capacity errors as `start_min`.
118 /// # Panics
119 /// Supplier mutation failures trap the enclosing message.
120 pub fn start_max<R: Value>(
121 self,
122 jobs: &mut AggregateJobs<M>,
123 now: Timestamp,
124 expires: Timestamp,
125 ) -> Result<JobHandle, Error> {
126 observe!(start, {
127 let admitted = self.extremum_plan::<R>(Extremum::Max)?;
128 self.start_value(jobs, now, expires, admitted)
129 })
130 }
131
132 /// Advance a retained minimum with `resume_count`'s bounded work, retry,
133 /// revision and expiry contract. Ignore absent operands; empty/all-absent
134 /// input completes with None. Return the winning record only after exhaustion.
135 /// Equal values retain the first encountered value. A completed result stays
136 /// fixed after source writes, until expiry or cancellation.
137 /// # Errors
138 /// Refuses changed queries/operations, malformed winner bytes, work/state
139 /// bounds or oversized completed output. Refusal preserves the checkpoint.
140 /// A comparison key exceeding 4096 bytes refuses even for a losing candidate.
141 /// # Panics
142 /// Supplier mutation failures trap the enclosing message.
143 pub fn resume_min<R: Value>(
144 self,
145 jobs: &mut AggregateJobs<M>,
146 handle: &JobHandle,
147 sequence: u64,
148 now: Timestamp,
149 ) -> Result<JobStep<Option<R>>, Error> {
150 observe!(resume, {
151 let admitted = self.extremum_plan::<R>(Extremum::Min)?;
152
153 self.resume_value(jobs, handle, sequence, now, admitted)
154 })
155 }
156
157 /// Advance a retained maximum with `resume_min`'s exactness and bounds.
158 /// # Errors
159 /// The same query, resource and checkpoint errors as `resume_min`.
160 /// # Panics
161 /// Supplier mutation failures trap the enclosing message.
162 pub fn resume_max<R: Value>(
163 self,
164 jobs: &mut AggregateJobs<M>,
165 handle: &JobHandle,
166 sequence: u64,
167 now: Timestamp,
168 ) -> Result<JobStep<Option<R>>, Error> {
169 observe!(resume, {
170 let admitted = self.extremum_plan::<R>(Extremum::Max)?;
171
172 self.resume_value(jobs, handle, sequence, now, admitted)
173 })
174 }
175}