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