Beispiel #1
0
static PyObject *
tcmap2pydict(TCMAP *map)
{
    const char *kstr, *vstr;
    PyObject *dict, *value;
    
    dict = PyDict_New();
    
    if (dict == NULL)
    {
        PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for dict.");
        return NULL;
    }
    
    tcmapiterinit(map);
    kstr = tcmapiternext2(map);
    
    while (kstr != NULL)
    {
        vstr = tcmapget2(map, kstr);
        
        if (vstr == NULL)
        {
            Py_DECREF(dict);
            PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for map value.");
            return NULL;
        }
        
        value = PyString_FromString(vstr);
        
        if (value == NULL)
        {
            Py_DECREF(dict);
            PyErr_SetString(PyExc_MemoryError, "Could not allocate memory for dict value.");
            return NULL;
        }
        
        if (PyDict_SetItemString(dict, kstr, value) != 0)
        {
            Py_DECREF(value);
            Py_DECREF(dict);
            PyErr_SetString(PyExc_Exception, "Could not set dict item.");
            return NULL;
        }
        
        Py_DECREF(value);
        
        kstr = tcmapiternext2(map);
    }
    
    return dict;
}
Beispiel #2
0
void get_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
    const char *value, *name;
    char *uri, *json, *hash, *key; 
    struct evkeyvalq args;
    struct json_object *jsobj, *jsobj2, *jsobj3;
    TCMAP *cols;
    
    if (rdb == NULL) {
        evhttp_send_error(req, 503, "database not connected");
        return;
    }

    uri = evhttp_decode_uri(req->uri);
    evhttp_parse_query(uri, &args);
    free(uri);

    hash = (char *)evhttp_find_header(&args, "hash");
    key = (char *)evhttp_find_header(&args, "key");
    
    if (hash == NULL) {
        evhttp_send_error(req, 400, "hash is required");
        evhttp_clear_headers(&args);
        return;
    }
    
    jsobj = json_object_new_object();
    jsobj2 = json_object_new_object();
    cols = tcrdbtblget(rdb, hash, sizeof(hash));
    
    if (cols) {
        tcmapiterinit(cols);
        jsobj3 = json_object_new_object();
        
        
        if (key) {
            value = tcmapget2(cols, key);

            if (!value) {
                value = "";
            }
            
            json_object_object_add(jsobj2, key, json_object_new_string(value));
        } else {
            while ((name = tcmapiternext2(cols)) != NULL) {
                json_object_object_add(jsobj2, name, json_object_new_string(tcmapget2(cols, name)));
            }
        }
     
        json_object_object_add(jsobj, "status", json_object_new_string("ok"));
        json_object_object_add(jsobj, "results", jsobj2);
        
        tcmapdel(cols);
    } else {
        json_object_object_add(jsobj, "status", json_object_new_string("error"));
    }
   
    finalize_json(req, evb, &args, jsobj);
}
Beispiel #3
0
static PyObject *
search(PyObject *self, PyObject *args){
    TCTDB *tdb;
    int ecode, i, rsiz;
    const char *rbuf, *name;
    TCMAP *cols;
    TDBQRY *qry;
    TCLIST *res;

    const char *dbname;
    const char *sfield;
    const char *stext;
    const int *max;
    PyObject* pDict = PyDict_New();
    PyObject* pList = PyList_New(0);

    if (!PyArg_ParseTuple(args, "sssi", &dbname, &sfield, &stext,&max))
        return NULL;

    tdb = tctdbnew();
    
    if(!tctdbopen(tdb, dbname, TDBONOLCK | TDBOREADER)){
        ecode = tctdbecode(tdb);
        fprintf(stderr, "open error: %s\n", tctdberrmsg(ecode));
    }
    qry = tctdbqrynew(tdb);
    tctdbqryaddcond(qry, sfield, TDBQCSTREQ, stext);
    tctdbqrysetorder(qry, "savedate", TDBQOSTRDESC);
    tctdbqrysetlimit(qry, max, 0);
    res = tctdbqrysearch(qry);
    for(i = 0; i < tclistnum(res); i++){
        rbuf = tclistval(res, i, &rsiz);
        cols = tctdbget(tdb, rbuf, rsiz);
        if(cols){
          tcmapiterinit(cols);
          PyDict_SetItemString(pDict, "kid", Py_BuildValue("s",rbuf));
          while((name = tcmapiternext2(cols)) != NULL){
              PyDict_SetItemString(pDict, name, Py_BuildValue("s", tcmapget2(cols, name)));
          }
          PyList_Append(pList,pDict);
          pDict = PyDict_New();
          tcmapdel(cols);
        }
    }
    tclistdel(res);
    tctdbqrydel(qry);

    if(!tctdbclose(tdb)){
        ecode = tctdbecode(tdb);
        fprintf(stderr, "close error: %s\n", tctdberrmsg(ecode));
    }

    tctdbdel(tdb);

    return Py_BuildValue("O",pList);
}
Beispiel #4
0
static void sandbox_map_free(TCMAP *map)
{
	int sp;
	TCLIST **p;
	const char *pkbuf;

	// FIXME: free listeners
	tcmapiterinit(map);
	pkbuf = tcmapiternext2(map);
	while (pkbuf) {
		p = (TCLIST **)tcmapget(map, pkbuf, strlen(pkbuf), &sp);
		if (p) {
			tclistdel(*p);
			//free(p);
		}
		pkbuf = tcmapiternext2(map);
	}
	tcmapdel(map);
}
Beispiel #5
0
/* print system information */
static void sysprint(void){
  TCMAP *info = tcsysinfo();
  if(info){
    tcmapiterinit(info);
    const char *kbuf;
    while((kbuf = tcmapiternext2(info)) != NULL){
      iprintf("sys_%s: %s\n", kbuf, tcmapiterval2(kbuf));
    }
    tcmapdel(info);
  }
}
Beispiel #6
0
const char*
xtc_mapiternext2(void* map)
{
        return tcmapiternext2(map);
}