Exemple #1
0
/* metasearch */
JNIEXPORT jobject JNICALL Java_tokyocabinet_TDBQRY_metasearch
(JNIEnv *env, jobject self, jobjectArray others, jint type){
  TDBQRY *qry = (TDBQRY *)(intptr_t)(*env)->GetLongField(env, self, tdbqry_fid_ptr);
  int onum = (*env)->GetArrayLength(env, others);
  TDBQRY *qrys[onum+1];
  int qnum = 0;
  qrys[qnum++] = qry;
  jclass clsqry = (*env)->GetObjectClass(env, self);
  for(int i = 0; i < onum; i++){
    jobject oqry = (*env)->GetObjectArrayElement(env, others, i);
    if((*env)->IsInstanceOf(env, oqry, clsqry))
      qrys[qnum++] = (TDBQRY *)(intptr_t)(*env)->GetLongField(env, oqry, tdbqry_fid_ptr);
  }
  TCLIST *tkeys = tctdbmetasearch(qrys, qnum, type);
  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;
}
Exemple #2
0
/* perform search command */
static int procsearch(const char *path, TCLIST *conds, const char *oname, const char *otype,
                      int omode, int max, int skip, bool pv, bool px, bool kw, bool ph, int bt,
                      bool rm, const char *mtype){
  TCTDB *tdb = tctdbnew();
  if(g_dbgfd != INVALID_HANDLE_VALUE) tctdbsetdbgfd(tdb, g_dbgfd);
  if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb);
  if(!tctdbopen(tdb, path, (rm ? TDBOWRITER : TDBOREADER) | omode)){
    printerr(tdb);
    tctdbdel(tdb);
    return 1;
  }
  bool err = false;
  TDBQRY *qry = tctdbqrynew(tdb);
  int cnum = tclistnum(conds);
  for(int i = 0; i < cnum - 2; i += 3){
    const char *name = tclistval2(conds, i);
    const char *opstr = tclistval2(conds, i + 1);
    const char *expr  = tclistval2(conds, i + 2);
    int op = tctdbqrystrtocondop(opstr);
    if(op >= 0) tctdbqryaddcond(qry, name, op, expr);
  }
  if(oname){
    int type = tctdbqrystrtoordertype(otype);
    if(type >= 0) tctdbqrysetorder(qry, oname, type);
  }
  tctdbqrysetlimit(qry, max, skip);
  if(rm){
    double stime = tctime();
    if(!tctdbqrysearchout(qry)){
      printerr(tdb);
      err = true;
    }
    double etime = tctime();
    if(ph){
      TCLIST *hints = tcstrsplit(tctdbqryhint(qry), "\n");
      int hnum = tclistnum(hints);
      for(int i = 0; i < hnum; i++){
        const char *hint = tclistval2(hints, i);
        if(*hint == '\0') continue;
        printf("\t:::: %s\n", hint);
      }
      tclistdel(hints);
      printf("\t:::: number of records: %d\n", tctdbqrycount(qry));
      printf("\t:::: elapsed time: %.5f\n", etime - stime);
    }
  } else if(bt > 0){
    double sum = 0;
    for(int i = 1; i <= bt; i++){
      double stime = tctime();
      TCLIST *res = tctdbqrysearch(qry);
      double etime = tctime();
      tclistdel(res);
      printf("%d: %.5f sec.\n", i, etime - stime);
      sum += etime - stime;
    }
    printf("----\n");
    printf("total: %.5f sec. (%.5f s/q = %.5f q/s)\n", sum, sum / bt, bt / sum);
  } else {
    double stime = tctime();
    TCLIST *res;
    TCLIST *hints;
    int count;
    int mtnum = mtype ? tctdbmetastrtosettype(mtype) : -1;
    if(mtnum >= 0){
      TDBQRY *qrys[cnum/3+1];
      int qnum = 0;
      for(int i = 0; i < cnum - 2; i += 3){
        const char *name = tclistval2(conds, i);
        const char *opstr = tclistval2(conds, i + 1);
        const char *expr  = tclistval2(conds, i + 2);
        int op = tctdbqrystrtocondop(opstr);
        if(op >= 0){
          qrys[qnum] = tctdbqrynew(tdb);
          tctdbqryaddcond(qrys[qnum], name, op, expr);
          if(oname){
            int type = tctdbqrystrtoordertype(otype);
            if(type >= 0) tctdbqrysetorder(qrys[qnum], oname, type);
          }
          tctdbqrysetlimit(qrys[qnum], max, skip);
          qnum++;
        }
      }
      res = tctdbmetasearch(qrys, qnum, mtnum);
      hints = qnum > 0 ? tcstrsplit(tctdbqryhint(qrys[0]), "\n") : tclistnew2(1);
      count = qnum > 0 ? tctdbqrycount(qrys[0]) : 0;
      for(int i = 0; i < qnum; i++){
        tctdbqrydel(qrys[i]);
      }
    } else {
      res = tctdbqrysearch(qry);
      hints = tcstrsplit(tctdbqryhint(qry), "\n");
      count = tctdbqrycount(qry);
    }
    double etime = tctime();
    if(max < 0) max = INT_MAX;
    int rnum = tclistnum(res);
    for(int i = 0; i < rnum && max > 0; i++){
      int pksiz;
      const char *pkbuf = tclistval(res, i, &pksiz);
      if(kw){
        TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
        if(cols){
          TCLIST *texts = tctdbqrykwic(qry, cols, NULL, 16, TCKWMUTAB);
          int tnum = tclistnum(texts);
          for(int j = 0; j < tnum && max > 0; j++){
            int tsiz;
            const char *text = tclistval(texts, j, &tsiz);
            printdata(pkbuf, pksiz, px);
            putchar('\t');
            printdata(text, tsiz, px);
            putchar('\n');
            max--;
          }
          tclistdel(texts);
          tcmapdel(cols);
        }
      } else {
        printdata(pkbuf, pksiz, px);
        if(pv){
          TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
          if(cols){
            tcmapiterinit(cols);
            const char *kbuf;
            int ksiz;
            while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){
              int vsiz;
              const char *vbuf = tcmapiterval(kbuf, &vsiz);
              putchar('\t');
              printdata(kbuf, ksiz, px);
              putchar('\t');
              printdata(vbuf, vsiz, px);
            }
            tcmapdel(cols);
          }
        }
        putchar('\n');
        max--;
      }
    }
    if(ph){
      int hnum = tclistnum(hints);
      for(int i = 0; i < hnum; i++){
        const char *hint = tclistval2(hints, i);
        if(*hint == '\0') continue;
        printf("\t:::: %s\n", hint);
      }
      printf("\t:::: number of records: %d\n", count);
      printf("\t:::: elapsed time: %.5f\n", etime - stime);
    }
    tclistdel(hints);
    tclistdel(res);
  }
  tctdbqrydel(qry);
  if(!tctdbclose(tdb)){
    if(!err) printerr(tdb);
    err = true;
  }
  tctdbdel(tdb);
  return err ? 1 : 0;
}