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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use super::Stream;
use crate::execution_step::value_types::Iterable;
use crate::execution_step::value_types::IterableItem;
use crate::execution_step::value_types::IterableVecResolvedCall;
use crate::execution_step::ValueAggregate;

use air_interpreter_data::GenerationIdx;

pub(crate) type IterableValue = Box<dyn for<'ctx> Iterable<'ctx, Item = IterableItem<'ctx>>>;

/// Tracks a state of a stream by storing last generation of every value type.
#[derive(Debug, Clone, Copy)]
pub struct StreamCursor {
    pub previous_start_idx: GenerationIdx,
    pub current_start_idx: GenerationIdx,
    pub new_start_idx: GenerationIdx,
}

/// Intended to generate values for recursive stream handling.
///
/// It could be considered as a simple state machine which should be started with
/// fold_started and then continued with next_iteration:
///    met_fold_start  - met_iteration_end - ... met_iteration_end - Exhausted
///          |                  |
///      Exhausted          Exhausted
#[derive(Debug, Clone, Copy)]
pub(crate) struct RecursiveStreamCursor {
    cursor: StreamCursor,
}

pub(crate) enum RecursiveCursorState {
    Continue(Vec<IterableValue>),
    Exhausted,
}

impl RecursiveStreamCursor {
    pub fn new() -> Self {
        Self {
            cursor: StreamCursor::empty(),
        }
    }

    pub fn met_fold_start(&mut self, stream: &mut Stream<ValueAggregate>) -> RecursiveCursorState {
        let state = self.cursor_state(stream);
        self.cursor = stream.cursor();

        if state.should_continue() {
            // add a new generation to made all consequence "new" (meaning that they are just executed on this peer)
            // write operation to this stream to write to this new generation
            stream.new_values().add_new_empty_generation();
        }

        state
    }

    pub fn met_iteration_end(&mut self, stream: &mut Stream<ValueAggregate>) -> RecursiveCursorState {
        let state = self.cursor_state(stream);

        // remove last generation if it empty to track cursor state
        remove_last_generation_if_empty(stream);
        self.cursor = stream.cursor();
        // add new last generation to store new values into this generation
        stream.new_values().add_new_empty_generation();

        state
    }

    fn cursor_state(&self, stream: &Stream<ValueAggregate>) -> RecursiveCursorState {
        let slice_iter = stream.slice_iter(self.cursor);
        let iterable = Self::slice_iter_to_iterable(slice_iter);

        RecursiveCursorState::from_iterable_values(iterable)
    }

    fn slice_iter_to_iterable<'value>(iter: impl Iterator<Item = &'value [ValueAggregate]>) -> Vec<IterableValue> {
        iter.map(|iterable| {
            let foldable = IterableVecResolvedCall::init(iterable.to_vec());
            let foldable: IterableValue = Box::new(foldable);
            foldable
        })
        .collect::<Vec<_>>()
    }
}

fn remove_last_generation_if_empty(stream: &mut Stream<ValueAggregate>) {
    if stream.new_values().last_generation_is_empty() {
        stream.new_values().remove_last_generation();
    }
}

impl StreamCursor {
    pub(crate) fn empty() -> Self {
        Self {
            previous_start_idx: GenerationIdx::from(0),
            current_start_idx: GenerationIdx::from(0),
            new_start_idx: GenerationIdx::from(0),
        }
    }

    pub(crate) fn new(
        previous_start_idx: GenerationIdx,
        current_start_idx: GenerationIdx,
        new_start_idx: GenerationIdx,
    ) -> Self {
        Self {
            previous_start_idx,
            current_start_idx,
            new_start_idx,
        }
    }
}

impl RecursiveCursorState {
    pub(crate) fn from_iterable_values(values: Vec<IterableValue>) -> Self {
        if values.is_empty() {
            Self::Exhausted
        } else {
            Self::Continue(values)
        }
    }

    pub(crate) fn should_continue(&self) -> bool {
        matches!(self, Self::Continue(_))
    }
}

#[cfg(test)]
mod test {
    use super::IterableValue;
    use super::RecursiveCursorState;
    use super::RecursiveStreamCursor;
    use super::Stream;
    use super::ValueAggregate;
    use crate::execution_step::Generation;
    use crate::execution_step::ServiceResultAggregate;
    use crate::JValue;

    use air_interpreter_cid::CID;

    fn create_value(value: impl Into<JValue>) -> ValueAggregate {
        ValueAggregate::from_service_result(
            ServiceResultAggregate::new(value.into(), <_>::default(), 0.into()),
            CID::new("some fake cid").into(),
        )
    }

    fn iterables_unwrap(cursor_state: RecursiveCursorState) -> Vec<IterableValue> {
        match cursor_state {
            RecursiveCursorState::Continue(iterables) => iterables,
            RecursiveCursorState::Exhausted => panic!("cursor is exhausted"),
        }
    }

    #[test]
    fn fold_started_empty_if_no_values() {
        let mut stream = Stream::new();
        let mut recursive_stream = RecursiveStreamCursor::new();
        let cursor_state = recursive_stream.met_fold_start(&mut stream);

        assert!(!cursor_state.should_continue())
    }

    #[test]
    fn next_iteration_empty_if_no_values() {
        let mut stream = Stream::new();
        let mut recursive_stream = RecursiveStreamCursor::new();
        let cursor_state = recursive_stream.met_iteration_end(&mut stream);

        assert!(!cursor_state.should_continue())
    }

    #[test]
    fn next_iteration_empty_if_no_values_added() {
        let mut stream = Stream::new();
        let mut recursive_stream = RecursiveStreamCursor::new();

        let value = create_value("1");
        stream.add_value(value, Generation::current(0)).unwrap();

        let cursor_state = recursive_stream.met_fold_start(&mut stream);
        let iterables = iterables_unwrap(cursor_state);
        assert_eq!(iterables.len(), 1);

        let cursor_state = recursive_stream.met_iteration_end(&mut stream);
        assert!(!cursor_state.should_continue());
    }

    #[test]
    fn one_recursive_iteration() {
        let mut stream = Stream::new();
        let mut recursive_stream = RecursiveStreamCursor::new();

        let value = create_value("1");
        stream.add_value(value.clone(), Generation::current(0)).unwrap();

        let cursor_state = recursive_stream.met_fold_start(&mut stream);
        let iterables = iterables_unwrap(cursor_state);
        assert_eq!(iterables.len(), 1);

        stream.add_value(value.clone(), Generation::new()).unwrap();
        stream.add_value(value, Generation::new()).unwrap();

        let cursor_state = recursive_stream.met_iteration_end(&mut stream);
        let iterables = iterables_unwrap(cursor_state);
        assert_eq!(iterables.len(), 1);

        let cursor_state = recursive_stream.met_iteration_end(&mut stream);
        assert!(!cursor_state.should_continue());
    }

    #[test]
    fn add_value_into_prev_and_current() {
        let mut stream = Stream::new();
        let mut recursive_stream = RecursiveStreamCursor::new();

        let value = create_value("1");
        stream.add_value(value.clone(), Generation::current(0)).unwrap();

        let cursor_state = recursive_stream.met_fold_start(&mut stream);
        assert!(cursor_state.should_continue());

        stream.add_value(value.clone(), Generation::previous(0)).unwrap();

        let cursor_state = recursive_stream.met_iteration_end(&mut stream);
        assert!(cursor_state.should_continue());

        stream.add_value(value, Generation::current(1)).unwrap();

        let cursor_state = recursive_stream.met_iteration_end(&mut stream);
        assert!(cursor_state.should_continue());

        let cursor_state = recursive_stream.met_iteration_end(&mut stream);
        assert!(!cursor_state.should_continue());
    }
}