-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathnote_getter_options.nr
114 lines (100 loc) · 3.89 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
use dep::std::option::Option;
use crate::types::vec::BoundedVec;
use dep::protocol_types::constants::MAX_READ_REQUESTS_PER_CALL;
struct Select {
field_index: u8,
value: Field,
}
impl Select {
pub fn new(field_index: u8, value: Field) -> Self {
Select { field_index, value }
}
}
struct SortOrderEnum {
DESC: u2,
ASC: u2,
}
global SortOrder = SortOrderEnum {
DESC: 1,
ASC: 2,
};
struct Sort {
field_index: u8,
order: u2,
}
impl Sort {
pub fn new(field_index: u8, order: u2) -> Self {
Sort { field_index, order }
}
}
fn return_all_notes<Note, N>(
notes: [Option<Note>; MAX_READ_REQUESTS_PER_CALL],
_p: Field
) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL] {
notes
}
// docs:start:NoteGetterOptions
struct NoteGetterOptions<Note, N, FILTER_ARGS> {
selects: BoundedVec<Option<Select>, N>,
sorts: BoundedVec<Option<Sort>, N>,
limit: u32,
offset: u32,
filter: fn ([Option<Note>; MAX_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS,
}
// 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, FILTER_ARGS> NoteGetterOptions<Note, N, 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, Field> {
NoteGetterOptions {
selects: BoundedVec::new(Option::none()),
sorts: BoundedVec::new(Option::none()),
limit: MAX_READ_REQUESTS_PER_CALL as u32,
offset: 0,
filter: return_all_notes,
filter_args: 0,
}
}
// 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_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS,
) -> Self {
NoteGetterOptions {
selects: BoundedVec::new(Option::none()),
sorts: BoundedVec::new(Option::none()),
limit: MAX_READ_REQUESTS_PER_CALL as u32,
offset: 0,
filter,
filter_args,
}
}
// This method adds a `Select` criterion to the options.
// It takes a field_index indicating which field to select and a value representing the specific value to match in that field.
pub fn select(&mut self, field_index: u8, value: Field) -> Self {
self.selects.push(Option::some(Select::new(field_index, value)));
*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, field_index: u8, order: u2) -> Self {
self.sorts.push(Option::some(Sort::new(field_index, 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_READ_REQUESTS_PER_CALL as u32);
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
}
}