<commit_before>#include "Python.h"
#include <tctdb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>

static PyObject *
get(PyObject *self,PyObject *args){
    TCTDB *tdb;
    int ecode;
    const char *name;

    const char *dbname;
    const char *key;
    if (!PyArg_ParseTuple(args, "ss", &dbname, &key))
        return NULL;

    tdb = tctdbnew();
    if(!tctdbopen(tdb, dbname, TDBONOLCK | TDBOREADER)){
        ecode = tctdbecode(tdb);
        fprintf(stderr, "open error: %s\n", tctdberrmsg(ecode));
    }
    name = tctdbget3(tdb,key);
    if(!tctdbclose(tdb)){
        ecode = tctdbecode(tdb);
        fprintf(stderr, "close error: %s\n", tctdberrmsg(ecode));
    }
    tctdbdel(tdb);
    return Py_BuildValue("s",name);
}


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);
    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){
          // printf("%s", rbuf);
          tcmapiterinit(cols);
          PyDict_SetItemString(pDict, "kid", Py_BuildValue("s",rbuf));
          while((name = tcmapiternext2(cols)) != NULL){
              // printf("%s",tcmapget2(cols, name));
              PyDict_SetItemString(pDict, name, Py_BuildValue("s", tcmapget2(cols, name)));
              // printf("\t%s\t%s", name, tcmapget2(cols, name));
          }
          PyList_Append(pList,pDict);
          pDict = PyDict_New();
          // printf("\n");
          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);
}

PyMethodDef methods[] = {
  {"search", search, METH_VARARGS},
  {"get", get, METH_VARARGS},
  {NULL, NULL},
};

void initpycabinet(){
    PyObject* m;
    m = Py_InitModule("pycabinet", methods);
}
<commit_msg>让搜索结果按时间排序<commit_after>#include "Python.h"
#include <tctdb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>

static PyObject *
get(PyObject *self,PyObject *args){
    TCTDB *tdb;
    int ecode;
    const char *name;

    const char *dbname;
    const char *key;
    if (!PyArg_ParseTuple(args, "ss", &dbname, &key))
        return NULL;

    tdb = tctdbnew();
    if(!tctdbopen(tdb, dbname, TDBONOLCK | TDBOREADER)){
        ecode = tctdbecode(tdb);
        fprintf(stderr, "open error: %s\n", tctdberrmsg(ecode));
    }
    name = tctdbget3(tdb,key);
    if(!tctdbclose(tdb)){
        ecode = tctdbecode(tdb);
        fprintf(stderr, "close error: %s\n", tctdberrmsg(ecode));
    }
    tctdbdel(tdb);
    return Py_BuildValue("s",name);
}


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){
          // printf("%s", rbuf);
          tcmapiterinit(cols);
          PyDict_SetItemString(pDict, "kid", Py_BuildValue("s",rbuf));
          while((name = tcmapiternext2(cols)) != NULL){
              // printf("%s",tcmapget2(cols, name));
              PyDict_SetItemString(pDict, name, Py_BuildValue("s", tcmapget2(cols, name)));
              // printf("\t%s\t%s", name, tcmapget2(cols, name));
          }
          PyList_Append(pList,pDict);
          pDict = PyDict_New();
          // printf("\n");
          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);
}

PyMethodDef methods[] = {
  {"search", search, METH_VARARGS},
  {"get", get, METH_VARARGS},
  {NULL, NULL},
};

void initpycabinet(){
    PyObject* m;
    m = Py_InitModule("pycabinet", methods);
}

{'fraction_non_alphanumeric': 0.15984369184964645, 'fraction_numerical': 0.00297729810197246, 'mean_word_length': 2.785211267605634, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6774074', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2153, 'n_words': 407}