Exemple #1
0
void *test_thread(void *arg)
{
    char key[64];
    char value[128];
    void *v;
    int knum = *(int*)arg;
    while(1) {
        int krand = get_rand() % knum;
        int ksize = snprintf(key, 64, "%ld%ld%ld", krand, krand, krand);
        int vsize = snprintf(value, 128, "%ld%ld%ld%ld%d%ld%ld%ld%ld",
                krand, krand, krand, krand, krand, krand, krand, krand);
        int op = optable[rand() & 0x07];
        int expire = 600 + 20 * (rand() % 1000);
        switch(op) {
            case SETOP:
                if (cdb_set2(db, key, ksize, value, vsize, CDB_OVERWRITE | CDB_INSERTCACHE, expire) < 0)
                    printf("ERROR! %s:%d\n", __FILE__, __LINE__);
                break;
            case GETOP:
                if (cdb_get(db, key, ksize, &v, &vsize) == -1)
                    printf("ERROR! %s:%d\n", __FILE__, __LINE__);
                if (v)
                    cdb_free_val(&v);
                break;
            case DELOP:
                if (cdb_del(db, key, ksize) == -1)
                    printf("ERROR! %s:%d\n", __FILE__, __LINE__);
                break;
            default:
                break;
        }
    }
}
int
cdb_read(const struct cdb *cdbp, void *buf, unsigned len, unsigned pos)
{
  const void *data = cdb_get(cdbp, len, pos);
  if (!data) return -1;
  memcpy(buf, data, len);
  return 0;
}
Exemple #3
0
static enum nss_status
__nss_cdb_dobyid(struct nss_cdb *dbp, unsigned long id,
                 void *result, char *buf, size_t bufl, int *errnop) {
  int r;
  unsigned len;
  const char *data;

  if ((r = cdb_find(&dbp->cdb, buf, sprintf(buf, ":%lu", id))) < 0)
    return *errnop = errno, NSS_STATUS_UNAVAIL;
  len = cdb_datalen(&dbp->cdb);
  if (!r || len < 2)
    return *errnop = ENOENT, NSS_STATUS_NOTFOUND;
  if (!(data = (const char*)cdb_get(&dbp->cdb, len, cdb_datapos(&dbp->cdb))))
    return *errnop = errno, NSS_STATUS_UNAVAIL;

  return __nss_cdb_dobyname(dbp, data, len, result, buf, bufl, errnop);
}