static void process_truncate( bdb_drv_t* pdrv, ErlIOVec *ev) {

    u_int32_t count;

    if (pdrv->pcfg == NULL) {
        return_error_tuple(pdrv, "Database not opened!");
        return;
    }
        if (pdrv->pcfg->pdb == NULL) {
            return_error_tuple(pdrv, "Database not opened!");
            return;
        }


    int ret;

    ret = pdrv->pcfg->pdb->truncate(pdrv->pcfg->pdb, NULL, &count, 0);

    if (ret) {
        return_error_tuple(pdrv, db_strerror(ret));
    } else {
        return_ok(pdrv);
    }

    return;

}
static void process_compact( bdb_drv_t* pdrv, ErlIOVec *ev) {


    if (pdrv->pcfg == NULL) {
        return_error_tuple(pdrv, "Database not opened!");
        return;
    }

    if (pdrv->pcfg->pdb == NULL) {
        return_error_tuple(pdrv, "Database not opened!");
        return;
    }

    int ret;

    ret = pdrv->pcfg->pdb->compact(pdrv->pcfg->pdb, NULL, NULL, NULL, NULL, 0, NULL);

    if (ret) {
        return_error_tuple(pdrv, db_strerror(ret));
    } else {
        return_ok(pdrv);
    }

    return;

}
static void process_flush( bdb_drv_t* pdrv, ErlIOVec *ev) {


    if (pdrv->pcfg == NULL) {
        return_error_tuple(pdrv, "Database not opened!");
        return;
    }

    if (pdrv->pcfg->pdb == NULL) {
        return_error_tuple(pdrv, "Database not opened!");
        return;
    }

    int ret;

    //if ((ret = pdrv->pcfg->pdb->sync(pdrv->pcfg->pdb, 0))) {
    if ((ret = pdrv->pcfg->penv->memp_sync(pdrv->pcfg->penv, NULL))) {
        return_error_tuple(pdrv, db_strerror(ret));
    } else {
        return_ok(pdrv);
    }

    return;

}
Ejemplo n.º 4
0
static void redis_drv_srem(redis_drv_t *redis_drv, Reader *const reader)
{
  int rc;
  REDIS redis = redis_drv->redis;
  
  const uint64_t *keysize = NULL;
  const char *key = NULL;
  const uint64_t *valuesize = NULL;
  const char *value = NULL;
  
  if(read_binary(reader, &key, &keysize) && read_binary(reader, &value, &valuesize)){
    rc = credis_srem(redis, key, value);
    if(rc > -1){
      return_ok(redis_drv, REDIS_DRV_OK);
    }
  }
}
static void set (u_int32_t key_size, void* praw_key, u_int32_t data_size, void* praw_data, bdb_drv_t *pdrv) {

    DB* pdb = pdrv->pcfg->pdb;

    DBT key;
    DBT data;

    int ret, count;

    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    key.data = praw_key,
    key.size = key_size,
    
    data.data = praw_data,
    data.size = data_size,

    count = 0;

    while (1) {

        ret = pdb->put(pdb, 0, &key, &data, 0);
    
        if        (ret == 0) {
            return_ok(pdrv);
            break;
        } else if (ret == DB_LOCK_DEADLOCK) {
            if (count < 5) {
                count = count + 1;
                usleep(1000);
            } else {
                return_error_tuple(pdrv, db_strerror(ret));
                break;
            }

        } else {
            return_error_tuple(pdrv, db_strerror(ret));
            break;
        }

    }
        
    return;

}
Ejemplo n.º 6
0
static void redis_drv_del(redis_drv_t *redis_drv, Reader *const reader)
{
  int rc;
  
  REDIS redis = redis_drv->redis;
  
  const uint64_t *keysize = NULL;
  const char *key = NULL;
  
  if(read_binary(reader, &key, &keysize)){
    rc = credis_del(redis, key);
    if(rc > -1){
      return_ok(redis_drv, REDIS_DRV_OK);
    }
  }
  
  return_error(redis_drv, REDIS_DRV_ERROR);
}
Ejemplo n.º 7
0
int		com_avance(t_server *s, char **tab, int sock)
{
  t_client	*client;

  UNUSED(tab);
  client = s->client;
  while (client != NULL)
    {
      if (client->sock == sock && client->type == 1)
	{
	  avance(s, client);
	  if (s->sock_graph > 0)
	    send_position(client, s->sock_graph);
	  return (return_ok(sock));
	}
      client = client->next;
    }
  return (0);
}
static void del (u_int32_t key_size, void* praw_key, bdb_drv_t *pdrv) {

    DB* pdb = pdrv->pcfg->pdb;

    DBT key;

    int ret, count;

    memset(&key, 0, sizeof(DBT));

    key.data   = praw_key,
    key.size   = key_size,

    count = 0;

    while (1) {

        ret = pdb->del(pdb, NULL, &key, 0);

        if        ((ret == 0) || (ret == DB_NOTFOUND)) {
            return_ok(pdrv);
            break;
        } else if (ret == DB_LOCK_DEADLOCK) {
            if (count < 5) {
                count = count + 1;
                usleep(1000);
            } else {
                return_error_tuple(pdrv, db_strerror(ret));
                break;
            }

        } else {
            return_error_tuple(pdrv, db_strerror(ret));
            break;
        }

    }

    return;

}
Ejemplo n.º 9
0
bool stop(ad_http_t *http, char *id, char **body, size_t *size) {
    system("sudo /etc/init.d/lyftcube stop");
    printf("Shutting down lyftcube ...\n");

    return return_ok(body, size);
}
Ejemplo n.º 10
0
bool start(ad_http_t *http, char *id, char **body, size_t *size) {
    system("sudo /etc/init.d/lyftcube start");
    printf("Starting lyftcube ...\n");

    return return_ok(body, size);
}
Ejemplo n.º 11
0
static void open_db(bdb_drv_t* pdrv, ErlIOVec *ev) {

    drv_cfg* pcfg;

    DB* pdb;
    DB_ENV* penv;
    u_int32_t open_flags, page_size_bytes, cache_size_bytes, bulk_get_buffer_size_bytes;
    int ret;

    ErlDrvBinary* data = ev->binv[1];

    char *bytes = data->orig_bytes;

    int txn_enabled         = bytes[1];
    int db_type             = bytes[2];

    cache_size_bytes           = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4) ));
    page_size_bytes            = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4 + 4) ));
    bulk_get_buffer_size_bytes = (uint32_t) ntohl(* ((uint32_t*) (bytes + 4 + 4 + 4) ));

    char* lv_start = bytes + 4 + 4 + 4 + 4;

    char *db_name_len_bytes = lv_start;
    char *db_name_bytes     = lv_start + 4;
    uint32_t db_name_length = (uint32_t) ntohl(* ((uint32_t*) db_name_len_bytes) );

    char *data_dir_len_bytes = db_name_bytes + db_name_length;
    char *data_dir_bytes     = data_dir_len_bytes + 4;
    uint32_t data_dir_length = (uint32_t) ntohl(* ((uint32_t*) data_dir_len_bytes) );

    pcfg = (drv_cfg*) malloc(sizeof(drv_cfg));

    pcfg->buffer = (char*) malloc(bulk_get_buffer_size_bytes);
    if (pcfg->buffer == NULL) {
        return_error_tuple(pdrv, "Could not allocate memory for operation!");
        return;
    }  

    pcfg->penv = NULL;
    pcfg->pdb  = NULL;

    pcfg->txn_enabled = txn_enabled;

    pcfg->data_dir             = malloc(data_dir_length + 1);
    pcfg->db_name              = malloc(db_name_length + 1);
    pcfg->page_size_bytes      = page_size_bytes;

    pcfg->data_dir[data_dir_length] = 0;
    pcfg->db_name[db_name_length] = 0;

    if (db_type == 'B') {
        pcfg->db_type = DB_BTREE;
    } else {
        pcfg->db_type = DB_HASH;
    }

    memcpy(pcfg->data_dir, data_dir_bytes, data_dir_length);
    memcpy(pcfg->db_name, db_name_bytes, db_name_length);

    pcfg->bulk_get_buffer_size_bytes = bulk_get_buffer_size_bytes;

    ret = db_env_create(&penv, 0);
    if (ret != 0) {
        return_error_tuple(pdrv, db_strerror(ret));
        return;
    } 

    penv->app_private = pcfg;

    
    open_flags = DB_CREATE | DB_INIT_MPOOL;

    if (pcfg->txn_enabled) {
        open_flags |= DB_INIT_LOCK | DB_THREAD | DB_INIT_TXN | DB_INIT_LOG | DB_REGISTER | DB_RECOVER;
    }

    //penv->set_msgcall(penv, (FILE*)stderr);
    //penv->set_verbose(penv, DB_VERB_DEADLOCK | DB_VERB_RECOVERY, 1);

    ret = penv->set_cachesize(penv, 0, cache_size_bytes, 1);
    if (ret != 0) {
        return_error_tuple(pdrv, db_strerror(ret));
        return;
    }

    ret = penv->open(penv, pcfg->data_dir, open_flags, 0);
    if (ret != 0) {
        return_error_tuple(pdrv, db_strerror(ret));
        return;
    }

    //Only open the table if not in replication mode.... for rep mode the table will be opened in event handler ...

    ret = db_create(&pdb, penv, 0);
    if (ret != 0) {
        return_error_tuple(pdrv, db_strerror(ret));
        return;
    }

    if (pcfg->db_type == DB_BTREE) {

        ret = pdb->set_flags(pdb, DB_RECNUM);
        if (ret != 0) {
            return_error_tuple(pdrv, db_strerror(ret));
            return;
        }
    }

    ret = pdb->set_pagesize(pdb, page_size_bytes);
    if (ret != 0) {
        return_error_tuple(pdrv, db_strerror(ret));
        return;
    }

    pdb->set_errpfx(pdb, pcfg->db_name);

    pcfg->db_open_flags = DB_CREATE;
 
    if (pcfg->txn_enabled) {
        pcfg->db_open_flags |= DB_THREAD; 
    }

    if ((ret = pdb->open(pdb, NULL, pcfg->db_name, pcfg->db_name, pcfg->db_type, pcfg->db_open_flags, 0)) != 0) {
        return_error_tuple(pdrv, db_strerror(ret));
        return;
    }

    pcfg->pdb  = pdb;

    pcfg->penv = penv;

    pdrv->pcfg = pcfg;

    return_ok(pdrv);

    return;

}