File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed
crates/nostr-database/src/collections Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 92
92
* nostr: impl ` FromIterator<Tag> ` for ` Tags ` ([ Yuki Kishimoto] )
93
93
* nostr: add ` EventDeletionRequest ` struct ([ Yuki Kishimoto] )
94
94
* nostr: add ` notifications ` field to NIP47 ` GetInfoResponse ` ([ Yuki Kishimoto] )
95
+ * database: add ` Events::force_insert ` ([ Yuki Kishimoto] )
95
96
* pool: event verification cache ([ Yuki Kishimoto] )
96
97
* pool: add ` AdmitPolicy ` trait ([ Yuki Kishimoto] )
97
98
* ffi: add Mac Catalyst support in Swift package ([ Yuki Kishimoto] )
Original file line number Diff line number Diff line change @@ -70,11 +70,22 @@ impl Events {
70
70
/// Insert [`Event`]
71
71
///
72
72
/// If the set did not previously contain an equal value, `true` is returned.
73
+ /// If the collection is full, the older events will be discarded.
74
+ /// Use [`Events::force_insert`] to always make sure the event is inserted.
73
75
#[ inline]
74
76
pub fn insert ( & mut self , event : Event ) -> bool {
75
77
self . set . insert ( event) . inserted
76
78
}
77
79
80
+ /// Force insert [`Event`]
81
+ ///
82
+ /// Use [`Events::insert`] to respect the max collection capacity (if any).
83
+ /// If the collection capacity is full, this method will increase it.
84
+ #[ inline]
85
+ pub fn force_insert ( & mut self , event : Event ) -> bool {
86
+ self . set . force_insert ( event) . inserted
87
+ }
88
+
78
89
/// Insert events
79
90
#[ inline]
80
91
pub fn extend < I > ( & mut self , events : I )
Original file line number Diff line number Diff line change @@ -237,6 +237,28 @@ where
237
237
}
238
238
}
239
239
240
+ /// Force insert the value
241
+ ///
242
+ /// If the capacity is full, automatically increases the capacity.
243
+ pub fn force_insert ( & mut self , value : T ) -> InsertResult < T > {
244
+ let inserted: bool = self . set . insert ( value) ;
245
+
246
+ // If successfully inserted, check if the capacity must be increased
247
+ if inserted {
248
+ if let Capacity :: Bounded { max, .. } = self . capacity {
249
+ if self . set . len ( ) >= max {
250
+ self . capacity = Capacity :: bounded ( max + 1 ) ;
251
+ }
252
+ }
253
+ }
254
+
255
+ // Insert value
256
+ InsertResult {
257
+ inserted,
258
+ pop : None ,
259
+ }
260
+ }
261
+
240
262
/// Extend with values
241
263
pub fn extend < I > ( & mut self , values : I )
242
264
where
You can’t perform that action at this time.
0 commit comments