Skip to content

Commit

Permalink
feat(shm/kv) add nelts member representing number of entries
Browse files Browse the repository at this point in the history
Iterating a tree in batches without knowing the number of elements in
it implies an additional traversal through its elements after the last
batch is retrieved. This last traversal returns no elements which signals
the end of the iteration.

Knowing the number of entries in the tree renders the additional traverse
unnecessary, when the last batch is retrieved it can be verified that
the tree has been fully iterated.
  • Loading branch information
casimiro authored and thibaultcha committed Sep 19, 2024
1 parent 8c6a796 commit cc96200
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/common/shm/ngx_wa_shm_kv.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ ngx_wa_shm_kv_init(ngx_wa_shm_t *shm)
shm->data = kv;
shm->shpool->log_nomem = 0;

kv->nelts = 0;

if (shm->eviction == NGX_WA_SHM_EVICTION_LRU) {
ngx_queue_init(&kv->eviction.lru_queue);

Expand Down Expand Up @@ -193,6 +195,8 @@ queue_expire(ngx_wa_shm_t *shm, ngx_queue_t *queue, ngx_queue_t *q)

ngx_slab_free_locked(shm->shpool, node);

kv->nelts--;

return NGX_OK;
}

Expand Down Expand Up @@ -287,6 +291,7 @@ ngx_wa_shm_kv_set_locked(ngx_wa_shm_t *shm, ngx_str_t *key,
ngx_rbtree_delete(&kv->rbtree, &n->key.node);
ngx_slab_free_locked(shm->shpool, n);
*written = 1;
kv->nelts--;

} else {
*written = 0;
Expand Down Expand Up @@ -343,6 +348,9 @@ ngx_wa_shm_kv_set_locked(ngx_wa_shm_t *shm, ngx_str_t *key,
n->cas = old->cas;
ngx_rbtree_delete(&kv->rbtree, &old->key.node);
ngx_slab_free_locked(shm->shpool, old);

} else {
kv->nelts++;
}

ngx_memcpy(n->key.str.data, key->data, key->len);
Expand Down
1 change: 1 addition & 0 deletions src/common/shm/ngx_wa_shm_kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
typedef struct {
ngx_rbtree_t rbtree;
ngx_rbtree_node_t sentinel;
ngx_uint_t nelts;
union {
ngx_queue_t lru_queue;
ngx_queue_t slru_queues[0];
Expand Down

0 comments on commit cc96200

Please sign in to comment.