-
Notifications
You must be signed in to change notification settings - Fork 217
/
note_getter_options.nr
178 lines (157 loc) · 5.54 KB
/
note_getter_options.nr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use dep::std::option::Option;
use dep::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::ToField};
use crate::note::note_interface::NoteInterface;
struct PropertySelector {
index: u8,
offset: u8,
length: u8,
}
struct ComparatorEnum {
EQ: u8,
NEQ: u8,
LT: u8,
LTE: u8,
GT: u8,
GTE: u8,
}
global Comparator = ComparatorEnum {
EQ: 1,
NEQ: 2,
LT: 3,
LTE: 4,
GT: 5,
GTE: 6,
};
struct Select {
property_selector: PropertySelector,
value: Field,
comparator: u8,
}
impl Select {
pub fn new(property_selector: PropertySelector, value: Field, comparator: u8) -> Self {
Select { property_selector, value, comparator }
}
}
struct SortOrderEnum {
DESC: u8,
ASC: u8,
}
global SortOrder = SortOrderEnum {
DESC: 1,
ASC: 2,
};
struct Sort {
property_selector: PropertySelector,
order: u8,
}
impl Sort {
pub fn new(property_selector: PropertySelector, order: u8) -> Self {
Sort { property_selector, order }
}
}
struct NoteStatusEnum {
ACTIVE: u8,
ACTIVE_OR_NULLIFIED: u8,
}
global NoteStatus = NoteStatusEnum {
ACTIVE: 1,
ACTIVE_OR_NULLIFIED: 2,
// TODO 4217: add 'NULLIFIED'
};
fn return_all_notes<Note, N>(
notes: [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
_p: Field
) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] {
notes
}
// docs:start:NoteGetterOptions
struct NoteGetterOptions<Note, N, M, FILTER_ARGS> {
selects: BoundedVec<Option<Select>, N>,
sorts: BoundedVec<Option<Sort>, N>,
limit: u32,
offset: u32,
filter: fn ([Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS,
status: u8,
}
// docs:end:NoteGetterOptions
// When retrieving notes using the NoteGetterOptions, the configurations are applied in a specific sequence to ensure precise and controlled data retrieval.
// The database-level configurations are applied first:
// `selects` to specify fields, `sorts` to establish sorting criteria, `offset` to skip items, and `limit` to cap the result size.
// And finally, a custom filter to refine the outcome further.
impl<Note, N, M, FILTER_ARGS> NoteGetterOptions<Note, N, M, FILTER_ARGS> {
// This function initializes a NoteGetterOptions that simply returns the maximum number of notes allowed in a call.
pub fn new() -> NoteGetterOptions<Note, N, M, Field> where Note: NoteInterface<N, M> {
NoteGetterOptions {
selects: BoundedVec::new(),
sorts: BoundedVec::new(),
limit: MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32,
offset: 0,
filter: return_all_notes,
filter_args: 0,
status: NoteStatus.ACTIVE
}
}
// This function initializes a NoteGetterOptions with a filter, which takes the notes returned from the database and filter_args as its parameters.
// `filter_args` allows you to provide additional data or context to the custom filter.
pub fn with_filter(
filter: fn([Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS
) -> Self where Note: NoteInterface<N, M> {
NoteGetterOptions {
selects: BoundedVec::new(),
sorts: BoundedVec::new(),
limit: MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32,
offset: 0,
filter,
filter_args,
status: NoteStatus.ACTIVE
}
}
// This method adds a `Select` criterion to the options.
// It takes a property_selector indicating which field to select,
// a value representing the specific value to match in that field, and
// a comparator (For possible values of comparators, please see the Comparator enum above)
pub fn select<T>(
&mut self,
property_selector: PropertySelector,
value: T,
comparator: Option<u8>
) -> Self where T: ToField {
self.selects.push(
Option::some(
Select::new(
property_selector,
value.to_field(),
comparator.unwrap_or(Comparator.EQ)
)
)
);
*self
}
// This method adds a `Sort` criterion to the options.
// It takes a field_index indicating which field to sort by and an order (SortOrder) to determine the sorting direction.
pub fn sort(&mut self, property_selector: PropertySelector, order: u8) -> Self {
self.sorts.push(Option::some(Sort::new(property_selector, order)));
*self
}
// This method lets you set a limit for the maximum number of notes to be retrieved in a single query result.
pub fn set_limit(&mut self, limit: u32) -> Self {
assert(limit <= MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32);
// By requesting that the limit is a constant, we guarantee that it will be possible to loop over it, reducing
// gate counts when a limit has been set.
assert_constant(limit);
self.limit = limit;
*self
}
// This method sets the offset value, which determines where to start retrieving notes in the query results.
pub fn set_offset(&mut self, offset: u32) -> Self {
self.offset = offset;
*self
}
// This method sets the status value, which determines whether to retrieve active or nullified notes.
pub fn set_status(&mut self, status: u8) -> Self {
self.status = status;
*self
}
}