summaryrefslogtreecommitdiff
path: root/src/libdcache/dcache_search.c
blob: d01faa070db178e05849715d99803aebf1d76ac8 (plain)
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
/* ISC license. */

#include <stdint.h>

#include <skalibs/uint16.h>
#include <skalibs/avltree.h>

#include <shibari/dcache.h>
#include "dcache-internal.h"

int dcache_search (dcache *z, uint32_t *idx, char const *q, uint16_t qlen, uint16_t qtype, tai const *stamp)
{
  dcache_node *node ;
  uint32_t i ;
  char key[4 + qlen] ;
  uint16_pack_big(key, qtype) ;
  uint16_pack_big(key+2, qlen) ;
  memcpy(key+4, q, qlen) ;
  if (!avltree_search(&z->by_key, &key, &i)) return -1 ;
  node = DNODE(z, i) ;
  if (node->sa.len == node->sa.a && tai_less(&node->expire, stamp))
  {
    dcache_delete(z, i) ;
    return -1 ;
  }
  *idx = i ;
  return node->sa.len == node->sa.a ;
}