@@ -35,7 +35,9 @@ class IntrusiveStringSet {
35
35
: buckets_it_(it), end_(end), entry_(entry) {
36
36
}
37
37
38
- iterator (Buckets::iterator it, Buckets::iterator end) : buckets_it_(it), end_(end) {
38
+ iterator (Buckets::iterator it, Buckets::iterator end, std::uint32_t time_now,
39
+ std::uint32_t * set_size)
40
+ : buckets_it_(it), end_(end), time_now_(time_now), set_size_(set_size) {
39
41
SetEntryIt ();
40
42
}
41
43
@@ -54,9 +56,11 @@ class IntrusiveStringSet {
54
56
// void Advance();
55
57
56
58
iterator& operator ++() {
57
- // TODO add expiration logic
58
- if (entry_)
59
+ if (entry_) {
60
+ if (entry_.ExpireIfNeeded (time_now_, &buckets_it_->obj_malloc_used_ ))
61
+ (*set_size_)--;
59
62
++entry_;
63
+ }
60
64
if (!entry_) {
61
65
++buckets_it_;
62
66
SetEntryIt ();
@@ -95,14 +99,16 @@ class IntrusiveStringSet {
95
99
Buckets::iterator buckets_it_;
96
100
Buckets::iterator end_;
97
101
IntrusiveStringList::iterator entry_;
102
+ std::uint32_t time_now_;
103
+ std::uint32_t * set_size_;
98
104
};
99
105
100
106
iterator begin () {
101
- return iterator (entries_.begin (), entries_.end ());
107
+ return iterator (entries_.begin (), entries_.end (), time_now_, &size_ );
102
108
}
103
109
104
110
iterator end () {
105
- return iterator (entries_.end (), entries_.end ());
111
+ return iterator (entries_.end (), entries_.end (), time_now_, &size_ );
106
112
}
107
113
108
114
explicit IntrusiveStringSet (PMR_NS::memory_resource* mr = PMR_NS::get_default_resource())
@@ -111,14 +117,18 @@ class IntrusiveStringSet {
111
117
112
118
static constexpr uint32_t kMaxBatchLen = 32 ;
113
119
114
- ISLEntry Add (std::string_view str, uint32_t ttl_sec = UINT32_MAX) {
120
+ ISLEntry Add (std::string_view str, bool keepttl = true , uint32_t ttl_sec = UINT32_MAX) {
115
121
if (entries_.empty () || size_ >= entries_.size ()) {
116
122
Reserve (Capacity () * 2 );
117
123
}
118
124
uint64_t hash = Hash (str);
119
125
const auto bucket_id = BucketId (hash);
120
126
121
127
if (auto item = FindInternal (bucket_id, str, hash); item.first != IntrusiveStringList::end ()) {
128
+ // Update ttl if found
129
+ if (!keepttl) {
130
+ item.first .SetExpiryTime (EntryTTL (ttl_sec), &entries_[item.second ].obj_malloc_used_ );
131
+ }
122
132
return {};
123
133
}
124
134
@@ -160,8 +170,9 @@ class IntrusiveStringSet {
160
170
Reserve (span.size ());
161
171
unsigned res = 0 ;
162
172
for (auto & s : span) {
163
- res++;
164
- Add (s, ttl_sec);
173
+ if (Add (s, keepttl, ttl_sec)) {
174
+ res++;
175
+ }
165
176
}
166
177
return res;
167
178
}
@@ -173,7 +184,7 @@ class IntrusiveStringSet {
173
184
other->set_time (time_now ());
174
185
for (uint32_t bucket_id = 0 ; bucket_id < entries_.size (); ++bucket_id) {
175
186
for (auto it = entries_[bucket_id].begin (); it != entries_[bucket_id].end (); ++it) {
176
- other->Add (it->Key (), it.HasExpiry () ? it->ExpiryTime () : UINT32_MAX);
187
+ other->Add (it->Key (), true , it.HasExpiry () ? it->ExpiryTime () : UINT32_MAX);
177
188
}
178
189
}
179
190
}
@@ -229,7 +240,7 @@ class IntrusiveStringSet {
229
240
uint64_t hash = Hash (str);
230
241
auto bucket_id = BucketId (hash);
231
242
auto item = FindInternal (bucket_id, str, hash);
232
- return entries_[item.second ].Erase (str);
243
+ return entries_[item.second ].Erase (str, &size_ );
233
244
}
234
245
235
246
iterator Find (std::string_view member) {
@@ -239,7 +250,11 @@ class IntrusiveStringSet {
239
250
uint64_t hash = Hash (member);
240
251
auto bucket_id = BucketId (hash);
241
252
auto res = FindInternal (bucket_id, member, hash);
242
- return {res.first ? entries_.begin () + res.second : entries_.end (), entries_.end (), res.first };
253
+ return {
254
+ res.first ? entries_.begin () + res.second : entries_.end (),
255
+ entries_.end (),
256
+ res.first ,
257
+ };
243
258
}
244
259
245
260
bool Contains (std::string_view member) {
@@ -369,7 +384,6 @@ class IntrusiveStringSet {
369
384
std::uint32_t capacity_log_ = 0 ;
370
385
std::uint32_t size_ = 0 ; // number of elements in the set.
371
386
std::uint32_t time_now_ = 0 ;
372
-
373
387
Buckets entries_;
374
388
};
375
389
0 commit comments