uint8_t XTDBFindById(XTDBHandle* handle,BinaryStr* oid,bson* outVal) { BinaryStr outBStr; uint8_t rv; rv = DBGet(handle->mainDB,oid,&outBStr); if (rv) { bson_init_finished_data(outVal,(char*)outBStr.data); } return rv; }
// XXX revisit after DB eror uint8_t XTDBCreateIndex(XTDBHandle* handle,char* fieldName) { String idxDBName; String descKey; bson idxDesc; BinaryStr key,value,data; DataBaseBE* db; int rc; StrInit(&descKey); if(StrAppendFmt(&descKey,"index.value.%s",fieldName)) { handle->error = XTDB_NO_MEM; return False; } StrToBStr(descKey.ptr,&key); if (DBGet(handle->descDB,&key,&value)) { // Index already exists printf("Index already exists.\n"); StrFree(&descKey); BinaryStrFree(&value); handle->error= XTDB_INDEX_EXISTS; return False; } bson_init(&idxDesc); if (bson_append_string(&idxDesc,"name",fieldName)) { handle->error = XTDB_NO_MEM; bson_destroy(&idxDesc); StrFree(&descKey); return False; } if (bson_finish(&idxDesc)) { handle->error = XTDB_NO_MEM; bson_destroy(&idxDesc); StrFree(&descKey); return False; } BsonToBStr(&idxDesc,&value); if (!DBSet(handle->descDB,&key,&value,False)) { printf("Error Adding to index names list\n"); handle->error = DBGetLastError(handle->descDB); bson_destroy(&idxDesc); StrFree(&descKey); return False; } StrFree(&descKey); StrToBStr(IDXFIELDNAME,&key); StrToBStr(fieldName,&value); if (!DBSet(handle->descDB,&key,&value,True)) { //printf("Error Adding to index names list\n"); handle->error = DBGetLastError(handle->descDB); //handle->error = XTDB_IO_ERR; bson_destroy(&idxDesc); return False; } bson_destroy(&idxDesc); if (GetIndexDBName(handle,fieldName,&idxDBName)) { handle->error = XTDB_NO_MEM; return False; } db = DBInit(STR(&idxDBName),BDBOWRITER | BDBOCREAT); StrFree(&idxDBName); if (!db) { handle->error = XTDB_NO_MEM; return False; } rc = DBOpen(db,BDBOCREAT|BDBOWRITER); if (!rc) { handle->error = DBGetLastError(db); DBFree(db); return False; } DBClose(db); DBFree(db); if (!XTDBAddIndex(handle,fieldName)) { // AddIndex failed return printf("Error Adding to index names list\n"); return False; } assert(XTDBGetIndex(handle,fieldName)); void* iter = DBIter(handle->mainDB); while (DBIterCur(handle->mainDB,iter,&key,&data)) { //printf("Adding value to index\n"); bson obj; BStrToBson(&data,&obj); if (!XTDBInsertToIndex(handle,&key,&data)) { return False; } BinaryStrFree(&key); BinaryStrFree(&data); DBIterNext(handle->mainDB,iter); } DBIterFree(handle->mainDB,iter); return True; }
// Checked static inline uint8_t XTDBCursorNextBStr(XTDBCursor* cursor, BinaryStr* outKey, BinaryStr* outVal) { XTDBHandle* handle = cursor->mdbHandle; _S_FN(curNext); if (cursor->usingIndex) { if (!cursor->dbIter) { return False; } if (cursor->first) { cursor->first = False; } else { // cursor->idx++; } int64_t listLen = DBListLen(cursor->curDB,cursor->dbIter); while (cursor->idx < listLen ) { BinaryStr oid,bsonVal; DBListVal(cursor->curDB,cursor->dbIter,cursor->idx,&oid); if (cursor->usingIndex == OTHER_INDEX) { DataBaseBE* mainDB = cursor->mdbHandle->mainDB; if (!DBGet(mainDB,&oid,&bsonVal)) { // value not in main db ideally we should delete from the index cursor->idx++; continue; } } else { // using main db as index so we directly get oid bsonVal = oid; } _S_FN(queryMatch); if (QueryMatch(&cursor->query,&bsonVal)) { if (!(outKey->data = malloc(oid.len))) { cursor->error = XTDB_NO_MEM; return False; } outKey->len = oid.len; memcpy((char*)outKey->data,oid.data,oid.len); /* assert(outVal->data = malloc(newVal.len)); memcpy((void*)outVal->data,newVal.data,newVal.len);*/ *outVal = bsonVal; //cursor->idx++; _E_FN(curNext); _E_FN(queryMatch); return True; } else { _E_FN(queryMatch); BinaryStrFree(&bsonVal); } cursor->idx++; } _E_FN(curNext); cursor->error = XTDB_OK; return False; } else { if (cursor->first) { cursor->first = False; } else { // cursor->error = XTDB_OK; if (handle->gen != cursor->gen) { if (!cursor->nextKeyStatus) { return False; } DBIterFree(cursor->curDB,cursor->dbIter); if (!(cursor->dbIter = DBIter(handle->mainDB)) ) { cursor->error = XTDB_NO_MEM; return False; } assert(cursor->dbIter); if (!DBIterJump(cursor->curDB,cursor->dbIter,&cursor->lastKey)) { return False; } BinaryStrFree(&cursor->lastKey); cursor->nextKeyStatus = False; cursor->gen = handle->gen; } } while (DBIterCur(cursor->curDB,cursor->dbIter,outKey,outVal)) { //return True; if (QueryMatch(&cursor->query,outVal)) { //DBIterNext(cursor->dbIter); if (!(cursor->lastKey.data = malloc(outKey->len))) { cursor->error = XTDB_NO_MEM; return False; } cursor->lastKey.len =outKey->len; memcpy((void*)cursor->lastKey.data,outKey->data,outKey->len); if (DBIterNext(cursor->curDB,cursor->dbIter)) { // save the next key before returning the value used in next call of this function cursor->nextKeyStatus = DBIterCur(cursor->curDB,cursor->dbIter,&cursor->lastKey,NULL); } _E_FN(curNext); return True; } //printf(" **** Query not matched ****\n"); BinaryStrFree(outKey); BinaryStrFree(outVal); DBIterNext(cursor->curDB,cursor->dbIter); } _E_FN(curNext); return False; } return False; }
uint /* */ DerefPointer( uint ptr , uint mid ) /* 240 112*/ { /* */ uint sfx; /* stack frame index. */ uint *p; /* */ uint read; /* number of bytes read by DBGet */ uint size; /* */ uchar type; /* type of pointer 219*/ uint derefPtr; /* dereferneced pointer 219*/ int IsStackAddr = 0; sfx = StackFrameIndex( ExprScope ); IsStackAddr = TestBit(ptr,STACKADDRBIT); if( IsStackAddr ) { if( sfx == 0 ) goto BadAddr; ptr = StackBPRelToAddr( ptr , sfx ); if( ptr == NULL ) goto BadAddr; } else if( (ptr >> REGADDCHECKPOS) == REGISTERTYPEADDR ){ /*205*/ if( sfx != 1 ) /* If not in the executing frame 112*/ goto BadAddr; /*112*/ } /*112*/ type = GetPtrType(mid,ExprTid); /*813240*/ /* get the type of pointer 219*/ if ( type == PTR_0_16 ) /* 219*/ size = 2; /* set the size of ptr depending 219*/ else /* on type of ptr. 219*/ size = 4; /* 219*/ /*****************************************************************************/ /* At this point, we are ready to return the fruits of our labor. We get */ /* "pointer size" bytes from the users stack at location ptr if we can. */ /* */ /*****************************************************************************/ p = (uint *)DBGet(ptr, size, &read ); if( p == NULL ) goto BadAddr; derefPtr = (uint) *p; /* get the value of deref pointer 219*/ /*****************************************************************************/ /* convert the deref pointer into correct flat adddress depending on type 219*/ /*****************************************************************************/ switch (type) /*219*/ { /*219*/ case PTR_0_16: /*219*/ derefPtr = Data_SelOff2Flat(DgroupDS,LoFlat(derefPtr)); break; /*219*/ /*219*/ case PTR_16_16: /*219*/ derefPtr = Data_SelOff2Flat( HiFlat(derefPtr) , LoFlat(derefPtr) ); break; /*219*/ } /*219*/ /*219*/ return( derefPtr ); /*219*/ BadAddr: return( NULL ); }