12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use std:: cell:: { Cell , RefCell } ;
15
+ use std:: cell:: RefCell ;
16
16
use std:: collections:: { HashMap , VecDeque } ;
17
- use std:: rc:: Rc ;
18
- use std:: sync:: atomic:: { AtomicBool , Ordering } ;
19
- use std:: sync:: Arc ;
20
- use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
17
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
21
18
22
- use swimos:: agent:: event_handler:: { ConstHandler , UnitHandler } ;
23
19
use swimos:: {
24
20
agent:: event_handler:: Sequentially ,
25
21
agent:: lanes:: CommandLane ,
@@ -50,14 +46,23 @@ impl ExampleLifecycle {
50
46
51
47
#[ lifecycle( ExampleAgent , no_clone) ]
52
48
impl ExampleLifecycle {
49
+ #[ on_command( add) ]
50
+ pub fn add (
51
+ & self ,
52
+ context : HandlerContext < ExampleAgent > ,
53
+ cmd : & str ,
54
+ ) -> impl EventHandler < ExampleAgent > {
55
+ context. update ( ExampleAgent :: HISTORY , now ( ) , cmd. to_string ( ) )
56
+ }
57
+
53
58
#[ on_update( history) ]
54
59
pub fn on_update (
55
60
& self ,
56
61
context : HandlerContext < ExampleAgent > ,
57
62
map : & HashMap < u64 , String > ,
58
63
key : u64 ,
59
64
_prev : Option < String > ,
60
- _new_value : & String ,
65
+ _new_value : & str ,
61
66
) -> impl EventHandler < ExampleAgent > {
62
67
self . policy . on_event ( context, key, map. clone ( ) )
63
68
}
@@ -71,19 +76,25 @@ pub enum RetentionPolicy {
71
76
max : usize ,
72
77
} ,
73
78
Time {
74
- start : u64 ,
75
79
interval : u64 ,
76
80
keys : RefCell < VecDeque < u64 > > ,
77
81
} ,
78
- Recency {
79
- start : u64 ,
80
- max : u64 ,
81
- keys : RefCell < VecDeque < u64 > > ,
82
- scheduled : Arc < AtomicBool > ,
83
- } ,
84
82
}
85
83
86
84
impl RetentionPolicy {
85
+ #[ allow( dead_code) ]
86
+ pub fn count ( max : usize ) -> RetentionPolicy {
87
+ RetentionPolicy :: Count { max }
88
+ }
89
+
90
+ #[ allow( dead_code) ]
91
+ pub fn time ( interval : u64 ) -> RetentionPolicy {
92
+ RetentionPolicy :: Time {
93
+ interval,
94
+ keys : RefCell :: new ( Default :: default ( ) ) ,
95
+ }
96
+ }
97
+
87
98
fn on_event (
88
99
& self ,
89
100
context : HandlerContext < ExampleAgent > ,
@@ -104,18 +115,15 @@ impl RetentionPolicy {
104
115
} ;
105
116
handler. discard ( ) . boxed ( )
106
117
}
107
- RetentionPolicy :: Time {
108
- start,
109
- interval,
110
- keys,
111
- } => {
118
+ RetentionPolicy :: Time { interval, keys } => {
112
119
let timestamps = & mut * keys. borrow_mut ( ) ;
113
120
timestamps. push_back ( key) ;
114
121
115
122
let mut to_remove = Vec :: new ( ) ;
123
+ let start = now ( ) ;
116
124
117
125
timestamps. retain ( |timestamp| {
118
- if * start - * timestamp > * interval {
126
+ if start - * timestamp > * interval {
119
127
to_remove. push ( context. remove ( ExampleAgent :: HISTORY , * timestamp) ) ;
120
128
false
121
129
} else {
@@ -131,59 +139,6 @@ impl RetentionPolicy {
131
139
132
140
handler. discard ( ) . boxed ( )
133
141
}
134
- RetentionPolicy :: Recency {
135
- keys,
136
- scheduled,
137
- max,
138
- start,
139
- } => {
140
- let timestamps = & mut * keys. borrow_mut ( ) ;
141
- timestamps. push_back ( key) ;
142
-
143
- loop {
144
- let handler = if !scheduled. swap ( true , Ordering :: Acquire ) {
145
- let handler = context
146
- . effect ( move || {
147
- scheduled. store ( false , Ordering :: Release ) ;
148
-
149
- let mut to_remove = Vec :: new ( ) ;
150
-
151
- timestamps. retain ( |timestamp| {
152
- if * start - * timestamp > * max {
153
- to_remove. push ( * timestamp) ;
154
- false
155
- } else {
156
- true
157
- }
158
- } ) ;
159
-
160
- to_remove
161
- } )
162
- . and_then ( |to_remove : Vec < u64 > | {
163
- let handler = if to_remove. is_empty ( ) {
164
- None
165
- } else {
166
- Some ( Sequentially :: new ( to_remove. into_iter ( ) . map (
167
- |timestamp| {
168
- context. remove ( ExampleAgent :: HISTORY , timestamp)
169
- } ,
170
- ) ) )
171
- } ;
172
-
173
- handler. discard ( ) . boxed ( )
174
- } ) ;
175
-
176
- Some (
177
- context
178
- . run_after ( Duration :: from_millis ( * max) , handler)
179
- . discard ( ) ,
180
- )
181
- } else {
182
- None
183
- } ;
184
- handler. boxed ( )
185
- }
186
- }
187
142
}
188
143
}
189
144
}
0 commit comments