static VALUE cQuery_search(VALUE vself){ VALUE vqry, vary; RDBQRY *qry; TCLIST *res; vqry = rb_iv_get(vself, RDBQRYVNDATA); Data_Get_Struct(vqry, RDBQRY, qry); res = tcrdbqrysearch(qry); vary = listtovary(res); tclistdel(res); return vary; }
static PyObject * RTDBQuery_search(RTDBQuery *self) { TCLIST *result; PyObject *pyresult; Py_BEGIN_ALLOW_THREADS result = tcrdbqrysearch(self->rqry); Py_END_ALLOW_THREADS pyresult = tclist_to_tuple(result); tclistdel(result); return pyresult; }
/* search */ JNIEXPORT jobject JNICALL Java_tokyotyrant_RDBQRY_search (JNIEnv *env, jobject self){ RDBQRY *qry = (RDBQRY *)(intptr_t)(*env)->GetLongField(env, self, tcrdbqry_fid_ptr); TCLIST *tkeys = tcrdbqrysearch(qry); jclass clslist = (*env)->FindClass(env, CLSARRAYLIST); jmethodID midinit = (*env)->GetMethodID(env, clslist, "<init>", "()V"); jobject pkeys = (*env)->NewObject(env, clslist, midinit); jmethodID midadd = (*env)->GetMethodID(env, clslist, "add", "(L" CLSOBJECT ";)Z"); for(int i = 0; i < tclistnum(tkeys); i++){ int ksiz; const char *kbuf = tclistval(tkeys, i, &ksiz); jbyteArray pkey = (*env)->NewByteArray(env, ksiz); (*env)->SetByteArrayRegion(env, pkey, 0, ksiz, (jbyte *)kbuf); (*env)->CallVoidMethod(env, pkeys, midadd, pkey); (*env)->DeleteLocalRef(env, pkey); } tclistdel(tkeys); return pkeys; }
void* xtcrdb_qrysearch(void* query) { return tcrdbqrysearch(query); }
void search_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx) { char *uri, *json; double lat, lng, distance, minlat, minlng, maxlat, maxlng, miles, lat2, lng2; int x1, x2, y1, y2, id, max; int total; struct evkeyvalq args; int ecode, pksiz, i, rsiz; char pkbuf[256]; char minx[8]; char miny[8]; char maxx[8]; char maxy[8]; const char *rbuf, *name, *buf; RDBQRY *query; TCLIST *result; TCMAP *cols; Geo_Result *georesultPtr, **georesults; struct json_object *jsobj, *jsobj2, *jsarr; 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); argtof(&args, "lat", &lat, 0); argtof(&args, "lng", &lng, 0); argtof(&args, "miles", &miles, 0); argtoi(&args, "max", &max, 1); geo_box(lat, lng, miles, &minlat, &minlng, &maxlat, &maxlng); x1 = (minlat * 10000) + 1800000; y1 = (minlng * 10000) + 1800000; x2 = (maxlat * 10000) + 1800000; y2 = (maxlng * 10000) + 1800000; sprintf(minx, "%d", x1); sprintf(miny, "%d", y1); sprintf(maxx, "%d", x2); sprintf(maxy, "%d", y2); query = tcrdbqrynew(rdb); tcrdbqryaddcond(query, "x", RDBQCNUMGT, minx); tcrdbqryaddcond(query, "x", RDBQCNUMLT, maxx); tcrdbqryaddcond(query, "y", RDBQCNUMGT, miny); tcrdbqryaddcond(query, "y", RDBQCNUMLT, maxy); tcrdbqrysetorder(query, "x", RDBQONUMASC); cols = tcmapnew(); result = tcrdbqrysearch(query); total = tclistnum(result); georesults = malloc(sizeof(Geo_Result *) * total); for(i = 0; i < total; i++){ rbuf = tclistval(result, i, &rsiz); cols = tcrdbtblget(rdb, rbuf, rsiz); if (cols) { georesultPtr = malloc(sizeof(*georesultPtr)); tcmapiterinit(cols); buf = tcmapget2(cols, "lat"); lat2 = atof(buf); georesultPtr->latitude = lat2; buf = tcmapget2(cols, "lng"); lng2 = atof(buf); georesultPtr->longitude = lng2; id = atoi(rbuf); georesultPtr->id = id; georesultPtr->data = strdup(tcmapget2(cols, "data")); distance = geo_distance(lat, lng, lat2, lng2); georesultPtr->distance = distance; georesults[i] = georesultPtr; tcmapdel(cols); } } tclistdel(result); tcrdbqrydel(query); qsort(georesults, total, sizeof(Geo_Result *), CmpElem); jsobj = json_object_new_object(); jsarr = json_object_new_array(); for(i = 0; i < total; i++){ georesultPtr = georesults[i]; if (i < max) { jsobj2 = json_object_new_object(); json_object_object_add(jsobj2, "id", json_object_new_int(georesultPtr->id)); json_object_object_add(jsobj2, "data", json_object_new_string(georesultPtr->data)); json_object_object_add(jsobj2, "latitude", json_object_new_double(georesultPtr->latitude)); json_object_object_add(jsobj2, "longitude", json_object_new_double(georesultPtr->longitude)); json_object_object_add(jsobj2, "distance", json_object_new_double(georesultPtr->distance)); json_object_array_add(jsarr, jsobj2); } free(georesultPtr->data); free(georesultPtr); } free(georesults); json_object_object_add(jsobj, "total", json_object_new_int(total)); json_object_object_add(jsobj, "results", jsarr); finalize_json(req, evb, &args, jsobj); }