示例#1
0
文件: pycabinet.c 项目: Frish/ntfs
static PyObject *
list2(PyObject *self,PyObject *args){
    const char *dbname;
    if (!PyArg_ParseTuple(args, "s", &dbname))
        return NULL;
    TCADB *adb = openadb(dbname);
    PyObject* pList = PyList_New(0);
    if(!tcadbiterinit(adb)){
        return NULL;
    }
    BDBCUR *cur = tcbdbcurnew(adb->bdb);
    TCXSTR *key = tcxstrnew();
    TCXSTR *val = tcxstrnew();
    if(!tcbdbcurfirst(cur) && tcbdbecode(adb->bdb) != TCENOREC){
        return NULL;
    }
    while(tcbdbcurrec(cur, key, val)){
        PyObject* pList1 = PyList_New(0);
        PyList_Append(pList1,Py_BuildValue("s",tcxstrptr(key)));
        PyList_Append(pList1,Py_BuildValue("s",tcxstrptr(val)));
        PyList_Append(pList,pList1);
        if(!tcbdbcurnext(cur) && tcbdbecode(adb->bdb) != TCENOREC){
            return NULL;
        }
    }
    tcxstrdel(val);
    tcxstrdel(key);
    tcbdbcurdel(cur);
    closeadb(adb);
    return Py_BuildValue("O",pList);
}
static void do_mc_list(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum){
    ttservlog(g_serv, TTLOGDEBUG, "doing mc_list command");
    TCADB *adb = arg->adb;
    if(tnum < 1){
        ttsockprintf(sock, "CLIENT_ERROR error\r\n");
        return;
    }
    TCXSTR *xstr = tcxstrnew();
    TCXSTR * head = tcxstrnew();

    pthread_cleanup_push((void (*)(void *))tcxstrdel, xstr);
    pthread_cleanup_push((void (*)(void *))tcxstrdel, head);

    tcadbiterinit(adb);
    char * key;
    int list_size;
    while((key=tcadbiternext2(adb))!=NULL){
        tcxstrcat(xstr, key, strlen(key));
        tcxstrcat(xstr,"\r\n",2);
    }
    list_size=tcxstrsize(xstr);
    tcxstrprintf(head,"LIST %d\r\n",list_size);
    tcxstrcat(xstr, "END\r\n",5);

    if( ttsocksend(sock, tcxstrptr(head), tcxstrsize(head)) && ttsocksend(sock,tcxstrptr(xstr),tcxstrsize(xstr)) ){
        req->keep = true;
    } else {
        ttservlog(g_serv, TTLOGINFO, "do_mc_list: response failed");
        return ;
    }
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
}
示例#3
0
/* Calls the given function for each of the key/value pairs */
static void
tc_db_foreach (void *db, void (*fp) (TCADB * m, void *k, int s, void *u),
               void *user_data)
{
  TCADB *adb = db;
  int ksize = 0;
  void *key;

  tcadbiterinit (adb);
  while ((key = tcadbiternext (adb, &ksize)) != NULL)
    (*fp) (adb, key, ksize, user_data);
}
示例#4
0
文件: pycabinet.c 项目: Frish/ntfs
static PyObject *
list(PyObject *self,PyObject *args){
    const char *dbname;
    if (!PyArg_ParseTuple(args, "s", &dbname))
        return NULL;
    TCADB *adb = openadb(dbname);
    PyObject* pDict = PyDict_New();
    if(!tcadbiterinit(adb)){
        return NULL;
    }
    int ksiz;
    char *kbuf;
    while((kbuf = tcadbiternext(adb, &ksiz)) != NULL){
        int vsiz;
        char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz);
        PyDict_SetItemString(pDict,kbuf,Py_BuildValue("s",vbuf));
        tcfree(vbuf);
        tcfree(kbuf);
    }
    closeadb(adb);
    return Py_BuildValue("O",pDict);
}
示例#5
0
文件: tcamgr.c 项目: Fleurer/nanodb
/* perform list command */
static int proclist(const char *name, int sep, int max, bool pv, bool px, const char *fmstr){
  TCADB *adb = tcadbnew();
  ADBSKEL skel;
  if(*name == '@'){
    setskeltran(&skel);
    if(!tcadbsetskel(adb, &skel)){
      printerr(adb);
      skel.del(skel.opq);
      tcadbdel(adb);
      return 1;
    }
    name++;
  } else if(*name == '%'){
    if(!tcadbsetskelmulti(adb, 8)){
      printerr(adb);
      tcadbdel(adb);
      return 1;
    }
    name++;
  }
  if(!tcadbopen(adb, name)){
    printerr(adb);
    tcadbdel(adb);
    return 1;
  }
  bool err = false;
  if(fmstr){
    TCLIST *keys = tcadbfwmkeys2(adb, fmstr, max);
    for(int i = 0; i < tclistnum(keys); i++){
      int ksiz;
      const char *kbuf = tclistval(keys, i, &ksiz);
      printdata(kbuf, ksiz, px, sep);
      if(pv){
        int vsiz;
        char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz);
        if(vbuf){
          putchar('\t');
          printdata(vbuf, vsiz, px, sep);
          tcfree(vbuf);
        }
      }
      putchar('\n');
    }
    tclistdel(keys);
  } else {
    if(!tcadbiterinit(adb)){
      printerr(adb);
      err = true;
    }
    int ksiz;
    char *kbuf;
    int cnt = 0;
    while((kbuf = tcadbiternext(adb, &ksiz)) != NULL){
      printdata(kbuf, ksiz, px, sep);
      if(pv){
        int vsiz;
        char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz);
        if(vbuf){
          putchar('\t');
          printdata(vbuf, vsiz, px, sep);
          tcfree(vbuf);
        }
      }
      putchar('\n');
      tcfree(kbuf);
      if(max >= 0 && ++cnt >= max) break;
    }
  }
  if(!tcadbclose(adb)){
    if(!err) printerr(adb);
    err = true;
  }
  tcadbdel(adb);
  return err ? 1 : 0;
}