⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libgist.cc

📁 Libgist is an implementation of the Generalized Search Tree, a template index structure that makes i
💻 CC
📖 第 1 页 / 共 5 页
字号:
    JNIEnv *env,    jclass cl){    DBGPRINT(1, cout << "libFlush()" << endl);    //printf("flushing\n");    Gist_env = env;    rc_t status = Gist_gist->flush();    CHECKSTATUS(status, env, "gist::flush failed");}JNIEXPORT void JNICALLJava_Libgist_libSave(    JNIEnv *env,    jclass cl,    jstring filename){    DBGPRINT(1, cout << "libSave()" << endl);    // first save all changes, then make a copy of the file    //printf("save\n");    Gist_env = env;    rc_t status = Gist_gist->flush();    CHECKSTATUS(status, env, "gist::flush failed");    const char* dest = env->GetStringUTFChars(filename, NULL);    assert(Gist_filename != NULL);    char buf[1024];    sprintf(buf, "cp %s %s", Gist_filename, dest);    env->ReleaseStringUTFChars(filename, dest);    int res = system(buf);    if (res == -1) {        // something went wrong        jclass excCl = (env)->FindClass("LibgistException");	if (excCl == NULL) {	    return;	}	(env)->ThrowNew(excCl, strerror(errno));	return;    }}JNIEXPORT void JNICALLJava_Libgist_libInsert(    JNIEnv *env,    jclass cl,    jstring jkey,    jstring jdata){    DBGPRINT(1, cout << "libInsert()" << endl);    char _key[gist_p::max_tup_sz];    void* key = (void *) _key;    char _data[gist_p::max_tup_sz];    void* data = (void *) _data;    int klen, dlen;    Gist_env = env;    gist_ext_t::gist_ext_ids extId = Gist_gist->extension()->myId;    gist_ext_t* ext = gist_ext_t::gist_ext_list[extId];    // parse key    const char *keyStr = env->GetStringUTFChars(jkey, NULL);    rc_t status = ext->parsePred(keyStr, key, klen);    CHECKSTATUS(status, env, "could not parse key");    // parse data    const char *dataStr = env->GetStringUTFChars(jdata, NULL);    status = ext->parseData(dataStr, data, dlen);    CHECKSTATUS(status, env, "could not parse data");    printf("inserting %s, %s\n", keyStr, dataStr);    printf("parsed: ");    vec_t keyv(key, klen);    ext->printPred(cout, keyv, 0);    printf(", ");    vec_t datav(data, dlen);    ext->printData(cout, datav);    printf("\n");    status = Gist_gist->insert(key, klen, data, dlen);    CHECKSTATUS(status, env, "gist::insert failed");    env->ReleaseStringUTFChars(jkey, keyStr);    env->ReleaseStringUTFChars(jdata, dataStr);}JNIEXPORT void JNICALLJava_Libgist_libRemove(    JNIEnv *env,    jclass cl,    jstring query){    DBGPRINT(1, cout << "libRemove()" << endl);    gist_query_t* q;    Gist_env = env;    gist_ext_t::gist_ext_ids extId = Gist_gist->extension()->myId;    gist_ext_t* ext = gist_ext_t::gist_ext_list[extId];    // parse query    const char* qStr = env->GetStringUTFChars(query, NULL);    rc_t status = ext->parseQuery(qStr, q);    CHECKSTATUS(status, env, "could not parse query");    //printf("removing %s\n", qStr);    status = Gist_gist->remove(q);    CHECKSTATUS(status, env, "gist::remove failed");    env->ReleaseStringUTFChars(query, qStr);    delete q;}JNIEXPORT void JNICALLJava_Libgist_libFetch(    JNIEnv *env,    jclass cl,    jstring query,    jint limit,    jobject procResult){    DBGPRINT(1, cout << "libFetch()" << endl);    gist_query_t* q;    Gist_env = env;    gist_ext_t::gist_ext_ids extId = Gist_gist->extension()->myId;    gist_ext_t* ext = gist_ext_t::gist_ext_list[extId];    // parse query    const char* qStr = env->GetStringUTFChars(query, NULL);    rc_t status = ext->parseQuery(qStr, q);    CHECKSTATUS(status, env, "could not parse query");    // find processing function    jclass objCl = env->GetObjectClass(procResult);    jmethodID procId = env->GetMethodID(objCl, "processItem",        "(Ljava/lang/String;Ljava/lang/String;)V");    if (procId == 0) {        // what's wrong now?	ERRORRETURN("Couldn't find ResultProcessor.processItem method ID");    }    //printf("searching %s\n", qStr);    gist_cursor_t cursor;    status = Gist_gist->fetch_init(cursor, q, limit);    CHECKSTATUS(status, env, "gist::fetch_init failed");    bool eof = false;    char _key[gist_p::max_tup_sz];    void* key = (void *) _key;    char _data[gist_p::max_tup_sz];    void* data = (void *) _data;    smsize_t klen, dlen;    for (;;) {	klen = gist_p::max_tup_sz;	dlen = gist_p::max_tup_sz;        status = Gist_gist->fetch(cursor, key, klen, data, dlen, eof);	CHECKSTATUS(status, env, "gist::fetch failed");	if (eof) break; // done	// convert to jstrings	vec_t keyv(key, klen);	char buf[gist_p::max_tup_sz];	(void) memset(buf, 0, sizeof(buf));	ostrstream predStream(buf, gist_p::max_tup_sz);	ext->printPred(predStream, keyv, 0);	//cout << "fetched " <<  buf << ": ";	jstring jkey = env->NewStringUTF(buf);	HANDLEEXC(env);	(void) memset(buf, 0, sizeof(buf));	ostrstream dataStream(buf, gist_p::max_tup_sz);	vec_t datav(data, dlen);	ext->printData(dataStream, datav);	//cout << buf << endl;;	jstring jdata = env->NewStringUTF(buf);	HANDLEEXC(env);	env->CallVoidMethod(procResult, procId, jkey, jdata);	HANDLEEXC(env);    }    env->ReleaseStringUTFChars(query, qStr);    delete q;}JNIEXPORT void JNICALLJava_Libgist_libCheck(    JNIEnv *env,    jclass cl){}JNIEXPORT jint JNICALLJava_Libgist_libDump(    JNIEnv *env,    jclass cl,    jobject textArea,    jint pgNo){    char buf[DUMPBUFLEN];    (void) memset(buf, 0, DUMPBUFLEN);    ostrstream dumpStr(buf, DUMPBUFLEN);    Gist_gist->dump(dumpStr, pgNo);    // transfer content of buf to text area    jstring bufString = env->NewStringUTF(buf);    env->CallVoidMethod(textArea, Gist_taAppendId, bufString);    return strlen(buf);}JNIEXPORT jint JNICALLJava_Libgist_libPickSplit(    JNIEnv* env,    jclass cl,    jint pgno,    jintArray splitEntries){    jsize maxEntries = env->GetArrayLength(splitEntries);    jint* entriesCopy = env->GetIntArrayElements(splitEntries, NULL);    gist::AlignedPred x, y;    vec_t leftBp(x.pred, gist_p::max_tup_sz);    vec_t rightBp(y.pred, gist_p::max_tup_sz);    assert(Gist_gist != NULL);    int entries[gist_p::max_scnt]; // jint is long int, so we need to copy those    int numSplit;    rc_t status = Gist_gist->pickSplit((shpid_t) pgno, entries, numSplit, leftBp, rightBp);    CHECKSTATUSR(status, env, "gist::pickSplit failed", 0);    int i;    for (i = 0; i < numSplit; i++) {        entriesCopy[i] = (jint) entries[i];    }    env->ReleaseIntArrayElements(splitEntries, entriesCopy, 0);    return numSplit;}static int jintCmp(    const void* a,    const void* b){    const jint* l = (jint*) a;    const jint* r = (jint*) b;    return ((*l < *r) ? -1 : ((*l > *r) ? 1 : 0));}/* * Tree info functions */JNIEXPORT jint JNICALLJava_Libgist_libGetPageCount(    JNIEnv *en,    jclass cl){    DBGPRINT(1, cout << "libGetPageCount()" << endl);    assert(Gist_gist != NULL);    return Gist_gist->pageCount();}JNIEXPORT jint JNICALLJava_Libgist_libGetRoot(    JNIEnv *en,    jclass cl){    DBGPRINT(1, cout << "libGetRoot()" << endl);    assert(Gist_indexStruct != NULL);    return Gist_indexStruct->rootNo;}JNIEXPORT jint JNICALLJava_Libgist_libGetParent(    JNIEnv *env,    jclass cl,    jint pgNo){    DBGPRINT(1, cout << "libGetParent()" << endl);    assert(Gist_indexStruct != NULL);    return ((amdb_idxstruct::NodeStruct *) Gist_indexStruct->nodeMap[pgNo])->parent;}JNIEXPORT jint JNICALLJava_Libgist_libGetLevel(    JNIEnv *env,    jclass cl,    jint pgNo){    assert(Gist_indexStruct != NULL);    amdb_idxstruct::NodeStruct* ns =         (amdb_idxstruct::NodeStruct*) Gist_indexStruct->nodeMap[pgNo];    if (ns == NULL) {        return(-1);    } else {        return(ns->level);    }}JNIEXPORT jint JNICALLJava_Libgist_libGetParentPos(    JNIEnv *env,    jclass cl,    jint pgNo){    DBGPRINT(1, cout << "libGetParentPos()" << endl);    assert(Gist_indexStruct != NULL);    if (pgNo == (jint) Gist_indexStruct->rootNo) return -1;    // We can't look at the position of the pgNo pointer in the amdb_idxstruct,    // because NodeStruct.children might not reflect the actual order of the entries    // on the page (due to relocateChild(), I believe).    shpid_t parent =        ((amdb_idxstruct::NodeStruct *) Gist_indexStruct->nodeMap[pgNo])->parent;    Vector children; // vector<shpid_t>    // gist::getChildren() returns the children in the order they're in on the page    int dummy;    rc_t status = Gist_gist->getChildren(parent, dummy, &children);    CHECKSTATUSR(status, env, "gist::getChildren failed", -1);    // locate child pointer    int pos = 0;    Vector::iterator it;    for (it = children.begin(); it != children.end(); it++, pos++) {        shpid_t childNo = (shpid_t) *it;	if ((shpid_t) pgNo == childNo) return pos;    }    assert(0); // pgNo not a child of parent?    return 0;}JNIEXPORT jint JNICALLJava_Libgist_libGetChildren(    JNIEnv *env,    jclass cl,    jint parent,    jintArray children){    DBGPRINT(1, cout << "libGetChildren()" << endl);    amdb_idxstruct::NodeStruct* ns =        (amdb_idxstruct::NodeStruct *) Gist_indexStruct->nodeMap[parent];    if (ns == NULL) {        // request for a non-existent node	ERRORRETURNV("node doesn't exist", 0);    }    jsize maxChildren = env->GetArrayLength(children);    jint* childCopy = env->GetIntArrayElements(children, NULL);    if (childCopy == NULL) {        ERRORRETURNV("couldn't copy child array", 0);    }    jint* pos = childCopy;    amdb_idxstruct::ChildIterator it;    for (it = ns->children.begin(); it != ns->children.end(); it++, pos++) {	*pos = (int) *it;    }    env->ReleaseIntArrayElements(children, childCopy, 0);    return(ns->children.size());}JNIEXPORT jint JNICALLJava_Libgist_libMaxChildren(    JNIEnv* env,    jclass cl){    return gist_p::max_scnt;}JNIEXPORT jint JNICALLJava_Libgist_libGetPath(    JNIEnv *env,    jclass cl,    jint pgno,    jintArray path){    DBGPRINT(1, cout << "libGetPath()" << endl);    jsize pLen = env->GetArrayLength(path);    jint* pathCopy = env->GetIntArrayElements(path, NULL);    int cnt = 0;    for (;;) {        pathCopy[cnt] = pgno;	cnt++;	if ((unsigned int) pgno == Gist_indexStruct->rootNo) break;	amdb_idxstruct::NodeStruct* ns =	    (amdb_idxstruct::NodeStruct *) Gist_indexStruct->nodeMap[pgno];	if (ns == NULL) {	    // request for non-existent node	    ERRORRETURNV("node doesn't exist", 0);	}	pgno = ns->parent;    }    env->ReleaseIntArrayElements(path, pathCopy, 0);    return cnt;}JNIEXPORT jint JNICALLJava_Libgist_libGetNonce(    JNIEnv *env,    jclass cl,    jint pgno){    DBGPRINT(1, cout << "libGetNonce()" << endl);    amdb_idxstruct::NodeStruct* ns =	(amdb_idxstruct::NodeStruct *) Gist_indexStruct->nodeMap[pgno];    if (ns == NULL) {	// request for non-existent node	ERRORRETURNV("node doesn't exist", 0);    }    return ns->nonce;}/* * Profiling */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -