Esempio n. 1
0
/* perform get command */
static int procget(const char *path, const char *kbuf, int ksiz, TCCMP cmp, int omode,
        bool px, bool pz) {
    TCBDB *bdb = tcbdbnew();
    if (!INVALIDHANDLE(g_dbgfd)) tcbdbsetdbgfd(bdb, g_dbgfd);
    if (cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)) printerr(bdb);
    if (!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(bdb);
    if (!tcbdbopen(bdb, path, BDBOREADER | omode)) {
        printerr(bdb);
        tcbdbdel(bdb);
        return 1;
    }
    bool err = false;
    int vsiz;
    char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz);
    if (vbuf) {
        printdata(vbuf, vsiz, px);
        if (!pz) putchar('\n');
        tcfree(vbuf);
    } else {
        printerr(bdb);
        err = true;
    }
    if (!tcbdbclose(bdb)) {
        if (!err) printerr(bdb);
        err = true;
    }
    tcbdbdel(bdb);
    return err ? 1 : 0;
}
Esempio n. 2
0
int db_get_dbvalue(void *vhandle, const dbv_t *token, /*@out@*/ dbv_t *val)
{
    char *data;
    int dsiz;

    dbh_t *handle = vhandle;
    TCBDB *dbp = handle->dbp;

    data = tcbdbget(dbp, token->data, token->leng, &dsiz);

    if (data == NULL)
	return DS_NOTFOUND;

    if (val->leng < (unsigned)dsiz) {
	print_error(__FILE__, __LINE__,
		    "(tc) db_get_dbvalue( '%.*s' ), size error %lu: %lu",
		    CLAMP_INT_MAX(token->leng),
		    (char *)token->data, (unsigned long)val->leng,
		    (unsigned long)dsiz);
	exit(EX_ERROR);
    }

    val->leng = dsiz;		/* read count */
    memcpy(val->data, data, dsiz);

    free(data); /* not xfree() as allocated by tcbdbget() */

    return 0;
}
Esempio n. 3
0
/* get */
JNIEXPORT jbyteArray JNICALL Java_tokyocabinet_BDB_get
(JNIEnv *env, jobject self, jbyteArray key){
  if(!key){
    throwillarg(env);
    return NULL;
  }
  TCBDB *bdb = (TCBDB *)(intptr_t)(*env)->GetLongField(env, self, bdb_fid_ptr);
  jboolean ick;
  jbyte *kbuf = (*env)->GetByteArrayElements(env, key, &ick);
  if(!kbuf){
    throwoutmem(env);
    return NULL;
  }
  int ksiz = (*env)->GetArrayLength(env, key);
  int vsiz;
  char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz);
  jbyteArray val;
  if(vbuf){
    val = (*env)->NewByteArray(env, vsiz);
    if(!val){
      throwoutmem(env);
      return NULL;
    }
    (*env)->SetByteArrayRegion(env, val, 0, vsiz, (jbyte *)vbuf);
    tcfree(vbuf);
  } else {
    val = NULL;
  }
  (*env)->ReleaseByteArrayElements(env, key, kbuf, JNI_ABORT);
  return val;
}
Esempio n. 4
0
void *
mutt_hcache_fetch_raw (header_cache_t *h, const char *filename,
                       size_t(*keylen) (const char *fn))
{
#ifndef HAVE_DB4
  char path[_POSIX_PATH_MAX];
  int ksize;
#endif
#ifdef HAVE_QDBM
  char *data = NULL;
#elif HAVE_TC
  void *data;
  int sp;
#elif HAVE_GDBM
  datum key;
  datum data;
#elif HAVE_DB4
  DBT key;
  DBT data;
#endif
  
  if (!h)
    return NULL;
  
#ifdef HAVE_DB4
  if (filename[0] == '/')
    filename++;

  mutt_hcache_dbt_init(&key, (void *) filename, keylen(filename));
  mutt_hcache_dbt_empty_init(&data);
  data.flags = DB_DBT_MALLOC;
  
  h->db->get(h->db, NULL, &key, &data, 0);
  
  return data.data;
#else
  strncpy(path, h->folder, sizeof (path));
  safe_strcat(path, sizeof (path), filename);

  ksize = strlen (h->folder) + keylen (path + strlen (h->folder));  
#endif
#ifdef HAVE_QDBM
  data = vlget(h->db, path, ksize, NULL);
  
  return data;
#elif HAVE_TC
  data = tcbdbget(h->db, path, ksize, &sp);

  return data;
#elif HAVE_GDBM
  key.dptr = path;
  key.dsize = ksize;
  
  data = gdbm_fetch(h->db, key);
  
  return data.dptr;
#endif
}
Esempio n. 5
0
int main(int argc, char **argv){

  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " dbname record_num" << std::endl; 
    exit(1);
  }

  double t1, t2;
  int rnum = atoi(argv[2]);
  int ecode;
  char *key, *value;

  t1 = gettimeofday_sec();
  TCBDB *bdb = tcbdbnew();
  tcbdbtune(bdb, 5, -1, -1, -1, -1, 0);
  tcbdbsetcache(bdb, 300, 100);

  if (!tcbdbopen(bdb, argv[1], BDBOWRITER | BDBOCREAT)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "open error: %s\n", tcbdberrmsg(ecode));
  }

  for (int i = 0; i < rnum; ++i) {
    char key[9];
    memset(key, 0, 9);
    sprintf(key,"%08d", i);
    char *val = new char[307201]; // 100K
    memset(val, 0, 307201);
    sprintf(val, "%0102400d", i);
    sprintf(val + 102400, "%0102400d", i);
    sprintf(val + 102400*2, "%0102400d", i);

    int size;
    void *data = tcbdbget(bdb, key, strlen(key), &size);
    if (data != NULL) {
      if (strcmp(val, (char *) data) != 0) {
        std::cout << "[error] value incorrect." << std::endl;
      }
      free(data);
    } else {
      std::cout << "[error] entry not found." << std::endl;
    } 
    delete [] val;
  }

  if (!tcbdbclose(bdb)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "close error: %s\n", tcbdberrmsg(ecode));
  }
  tcbdbdel(bdb);
  t2 = gettimeofday_sec();
  std::cout << "get time: " << t2 - t1 << std::endl;

  return 0;
}
Esempio n. 6
0
static datum
ypdb_fetch (TCBDB *bdb, datum key)
{
  datum res;

  if (!(res.dptr = tcbdbget(bdb, key.dptr, key.dsize, &res.dsize)))
    {
      res.dptr = NULL;
      res.dsize = 0;
    }

  return res;
}
Esempio n. 7
0
static int _mapcache_cache_tc_get(mapcache_context *ctx, mapcache_tile *tile)
{
  int ret;
  struct tc_conn conn;
  mapcache_cache_tc *cache = (mapcache_cache_tc*)tile->tileset->cache;
  char *skey = mapcache_util_get_tile_key(ctx,tile,cache->key_template,NULL,NULL);
  conn = _tc_get_conn(ctx,tile,1);
  int size;
  if(GC_HAS_ERROR(ctx)) return MAPCACHE_FAILURE;
  tile->encoded_data = mapcache_buffer_create(0,ctx->pool);
  tile->encoded_data->buf = tcbdbget(conn.bdb, skey, strlen(skey), &size);
  if(tile->encoded_data->buf) {
    tile->encoded_data->avail = size;
    tile->encoded_data->size = size - sizeof(apr_time_t);
    apr_pool_cleanup_register(ctx->pool, tile->encoded_data->buf,(void*)free, apr_pool_cleanup_null);
    tile->mtime = *((apr_time_t*)(&tile->encoded_data->buf[tile->encoded_data->size]));
    ret = MAPCACHE_SUCCESS;
  } else {
    ret = MAPCACHE_CACHE_MISS;
  }
  _tc_release_conn(ctx,tile,conn);
  return ret;
}
static ngx_int_t
ngx_http_cloudrouter_init_upstream_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us) {
    struct sockaddr_in* sin;
    ngx_http_hs_main_conf_t    *hscf;
    ngx_http_cloudrouter_peer_t *peer;
    hs_route_t *route;
    int tcb_s;

    hscf = ngx_http_get_module_main_conf(r, ngx_http_hs_module);
    if (hscf == NULL || hscf->tcb_route_db == NULL) {
        return NGX_ERROR;
    }

    peer = ngx_pcalloc(r->pool, sizeof(ngx_http_cloudrouter_peer_t));
    if (peer == NULL) {
        ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "NULL peer");
        return NGX_ERROR;
    }

    peer->route = NULL;

    peer->socklen = sizeof(struct sockaddr_in);
    peer->sockaddr = (struct sockaddr*)ngx_pcalloc(r->pool, peer->socklen);
    if (peer->sockaddr == NULL) {
        ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "ALERT: peer->sockaddr alloc failed");
        return NGX_ERROR;
    }

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "init_peer");

    /* fallback = 404 host */
    ngx_str_set(&peer->name, "127.0.0.1");
    peer->inet_addr = ngx_inet_addr(peer->name.data, peer->name.len);
    peer->port_n = htons(8404);

    r->upstream->peer.data = peer;
    r->upstream->peer.free = ngx_http_cloudrouter_free_upstream_peer;
    r->upstream->peer.get = ngx_http_cloudrouter_get_upstream_peer;
    r->upstream->peer.tries = 1;

    // fetch config for client-supplied Host
    if (r->headers_in.server.len > 0) {
        route = (hs_route_t*)tcbdbget(hscf->tcb_route_db, r->headers_in.server.data, r->headers_in.server.len, &tcb_s);
    } else {
        route = NULL;
    }
    if (route == NULL) {
        // send user to 404 host
        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "CloudRouter: no route matched");
        return NGX_OK;
    } else {
        /* copy route into peer, managed memory */
        peer->route = ngx_pcalloc(r->pool, sizeof(hs_route_t));
        if (peer->route==NULL) {
            ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "ALERT: alloc failed");
            free(route);
            return NGX_ERROR;
        }
        (void)ngx_copy(peer->route, route, sizeof(hs_route_t));
        free(route); // gets malloc()'d by TC
        route = peer->route;

        peer->name.len = route->di_nlen;
        peer->name.data = ngx_pcalloc(r->pool, peer->name.len);
        (void)ngx_copy(peer->name.data, route->di_name, peer->name.len);

        ngx_shmtx_lock(&ngx_http_cloudrouter_shpool->mutex);

        ngx_http_cloudrouter_node_t *e = ngx_http_cloudrouter_get_locked(route);
        if (e && e->timestamp > (r->start_sec - NGX_HTTP_HS_CACHE_TIMEOUT)) {
            ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "cache hit");
            ngx_http_cloudrouter_remote_t *remote = e->remote;

            ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                           "cached values READ: %p:%d:%s[%d],(%uxD,%d)", e, e->timestamp,
                           e->di_name,
                           e->di_nlen,
                           remote->inet_addr,
                           remote->port_n);

            ngx_http_cloudrouter_set_hostandport(r,peer,e);

            ngx_shmtx_unlock(&ngx_http_cloudrouter_shpool->mutex);
        } else {
            /* entry has expired.
             * pretend it does not exist, the preconnect handler will overwrite it.
             */

            int current = 0;
            if (e) {
                ngx_http_cloudrouter_remote_t *remote = e->remote;
                while (remote != NULL) {
                    current += remote->current;
                    remote = remote->next;
                }
                ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "CloudRouter: cache entry expired");
            } else {
                ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "CloudRouter: no entry in cache");
            }
            ngx_shmtx_unlock(&ngx_http_cloudrouter_shpool->mutex);

            peer->sendbuf.data = ngx_pcalloc(r->pool, NGX_HTTP_CLOUDROUTER_PRECONNECT_SENDBUFSIZE);
            if (peer->sendbuf.data == NULL) {
                ngx_log_error(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "alloc peer->sendbuf.data failed, aborting request");
                return NGX_ERROR;
            }

            peer->sendbuf.len = ngx_sprintf(peer->sendbuf.data, "BLNC %*s %ud", peer->route->di_nlen, peer->route->di_name, current) - peer->sendbuf.data;

            ngx_shmtx_unlock(&ngx_http_cloudrouter_shpool->mutex);

            r->upstream->peer.preconnect = (ngx_event_preconnect_pt)ngx_http_cloudrouter_peer_preconnect;
        }
        return NGX_OK;
    }
    /* never reached */
}
Esempio n. 9
0
int main(int argc, char **argv){

  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " file dbname" << std::endl; 
    exit(1);
  }

  double t1, t2;
  FILE *fp = fopen(argv[1], "r");
  if (fp == NULL) {
    std::cerr << "failed to open " << argv[1] << std::endl;
    exit(1);
  }

  int ecode;
  char *key, *value;

  t1 = gettimeofday_sec();
  TCBDB *bdb = tcbdbnew();
  tcbdbtune(bdb, 5, -1, -1, -1, -1, 0);
  tcbdbsetcache(bdb, 300, 100);

  if (!tcbdbopen(bdb, argv[2], BDBOWRITER | BDBOCREAT)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "open error: %s\n", tcbdberrmsg(ecode));
  }

  char buf[256];
  while (fgets(buf, 256, fp)) {
    char *key = strchr(buf, ' ');
    if (key == NULL) { continue; }
    *key++ = '\0';
    char *valp = strchr(key, ' ');
    if (valp == NULL) { continue; }
    *valp++ = '\0';
    char *val = new char[102401]; // 100K
    sprintf(val, "%0102400d", atoi(valp));

    int size;
    void *data = tcbdbget(bdb, key, strlen(key), &size);
    if (data != NULL) {
      if (strcmp(val, (char *) data) != 0) {
        std::cout << "[error] value incorrect." << std::endl;
      }
      free(data);
    } else {
      std::cout << "[error] entry not found." << std::endl;
    } 
    delete [] val;
  }

  if (!tcbdbclose(bdb)) {
    ecode = tcbdbecode(bdb);
    fprintf(stderr, "close error: %s\n", tcbdberrmsg(ecode));
  }
  tcbdbdel(bdb);
  t2 = gettimeofday_sec();
  std::cout << "get time: " << t2 - t1 << std::endl;

  fclose(fp);

  return 0;
}