int test_bson_init_finished( void ) { bson b; ALLOW_AND_REQUIRE_MALLOC_BEGIN; bson_init( &b ); ALLOW_AND_REQUIRE_MALLOC_END; bson_append_double( &b, "d", 3.14 ); bson_append_string( &b, "s", "hello" ); bson_finish( &b ); ASSERT( bson_size( &b ) == 29 ); // 29 determined by running this code bson b2; bson_init_finished_data( &b2, (char *) bson_data( &b ), 0 ); ASSERT( bson_size( &b ) == bson_size( &b2 ) ); bson_destroy( &b2 ); ALLOW_AND_REQUIRE_MALLOC_BEGIN; bson_init_finished_data_with_copy( &b2, (char *) bson_data( &b ) ); ALLOW_AND_REQUIRE_MALLOC_END; ASSERT( bson_size( &b ) == bson_size( &b2 ) ); ALLOW_AND_REQUIRE_FREE_BEGIN; bson_destroy( &b2 ); ALLOW_AND_REQUIRE_FREE_END; bson_init_finished_data( &b2, (char *) bson_data( &b ), 1 ); ASSERT( bson_size( &b ) == bson_size( &b2 ) ); ALLOW_AND_REQUIRE_FREE_BEGIN; bson_destroy( &b2 ); ALLOW_AND_REQUIRE_FREE_END; return 0; }
int ComputeOperator(BSONElem* val,BSONElem* query){ bson_iterator q,o; bson_type qType,tType; bson_iterator_from_buffer(&q, query->value); bson_iterator_from_buffer(&o, val->value); bson qBson,oBson; bson_init_finished_data(&qBson,(char*)query->value); bson_init_finished_data(&oBson,(char*)val->value); while((qType = bson_iterator_next(&q))) { const char* key = bson_iterator_key(&q); BSONElem qVal; BSONElemInitFromItr(&qVal,&q); int cVal = 0,r; uint8_t cont = False; int error; QueryOpDesc* desc = HTFind(opTable,key,strlen(key)+1); if (desc) { cont = desc->fn(val,&qVal,&error); if (!cont) { return False; } } else { // no such operator return False; } } //printf("Returning true\n"); return True; }
MONGO_EXPORT int mongo_cursor_next( mongo_cursor *cursor ) { char *next_object; char *message_end; if( ! ( cursor->flags & MONGO_CURSOR_QUERY_SENT ) ) if( mongo_cursor_op_query( cursor ) != MONGO_OK ) return MONGO_ERROR; if( !cursor->reply ) return MONGO_ERROR; /* no data */ if ( cursor->reply->fields.num == 0 ) { /* Special case for tailable cursors. */ if( cursor->reply->fields.cursorID ) { if( ( mongo_cursor_get_more( cursor ) != MONGO_OK ) || cursor->reply->fields.num == 0 ) { return MONGO_ERROR; } } else return MONGO_ERROR; } /* first */ if ( cursor->current.data == NULL ) { bson_init_finished_data( &cursor->current, &cursor->reply->objs ); return MONGO_OK; } next_object = cursor->current.data + bson_size( &cursor->current ); message_end = ( char * )cursor->reply + cursor->reply->head.len; if ( next_object >= message_end ) { if( mongo_cursor_get_more( cursor ) != MONGO_OK ) return MONGO_ERROR; /* If there's still a cursor id, then the message should be pending. */ if( cursor->reply->fields.num == 0 && cursor->reply->fields.cursorID ) { cursor->err = MONGO_CURSOR_PENDING; return MONGO_ERROR; } bson_init_finished_data( &cursor->current, &cursor->reply->objs ); } else { bson_init_finished_data( &cursor->current, next_object ); } return MONGO_OK; }
uint8_t XTDBUpdateIndex(XTDBHandle* handle,BinaryStr* key,BinaryStr* newData,BinaryStr* oldData) { XTDBUpdateIndexArgs args; _S_FN(indexUpdate); bson_iterator_from_buffer(&args.oldItr,oldData->data); bson_iterator_from_buffer(&args.newItr,newData->data); bson_init_finished_data(&args.oldBson,(char*)oldData->data); bson_init_finished_data(&args.newBson,(char*)newData->data); args.key = *key; uint8_t ret = XTDBForAllIDX(handle,XTDBUpdateIndexIter,NULL,NULL,&args); _E_FN(indexUpdate); return ret; }
MONGO_EXPORT void bson_iterator_subobject_init( const bson_iterator *i, bson *sub, bson_bool_t copyData ) { const char *data = bson_iterator_value( i ); if( copyData ) bson_init_finished_data_with_copy( sub, data ); else bson_init_finished_data( sub, (char *)data, 0 ); }
int QueryMatch(BinaryStr* query, BinaryStr* obj ) { bson_iterator q,o; bson_type qType,tType; bson qBson,oBson; BSONElem objElem; bson_iterator_from_buffer(&q, query->data); bson_iterator_from_buffer(&o, obj->data); bson_init_finished_data(&qBson,(char*)query->data); bson_init_finished_data(&oBson,(char*)obj->data); while((qType = bson_iterator_next(&q))) { // for all keys in query char *newKey; const char* key = bson_iterator_key(&q); char* keyEnd; BSONElem qVal,oVal; newKey = strdup(key); if (!newKey) { return False; } keyEnd = strchr(newKey,'.'); if (keyEnd) { *keyEnd = '\0'; keyEnd ++; } else { //printf("No dot \n"); keyEnd = newKey+strlen(newKey); } BSONElemInitFromItr(&qVal,&q); tType = bson_find(&o,&oBson,newKey) ; if (!tType) { return False; } BSONElemInitFromItr(&oVal,&o); if (!QueryElemMatch(keyEnd,&oVal,&qVal)) { free(newKey); return False; } free(newKey); } return True; }
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; }
// Checked uint8_t XTDBCursorNext(XTDBCursor* cursor, BinaryStr* outKey, bson* outVal) { BinaryStr val; uint8_t rv; rv = XTDBCursorNextBStr(cursor,outKey,&val); if (rv) { bson_init_finished_data(outVal,(char*)val.data); } return rv; }
MONGO_EXPORT void bson_iterator_code_scope_init( const bson_iterator *i, bson *scope, bson_bool_t copyData ) { if ( bson_iterator_type( i ) == BSON_CODEWSCOPE ) { int codeLen = bson_finished_data_size( bson_iterator_value( i )+4 ); const char * scopeData = bson_iterator_value( i )+8+codeLen; if( copyData ) bson_init_finished_data_with_copy( scope, scopeData ); else bson_init_finished_data( scope, (char *)scopeData, 0 ); } else { bson_init_empty( scope ); } }
void CSMatrix<Scalar>::import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt, bool invert_storage) { this->free(); switch (fmt) { case EXPORT_FORMAT_MATRIX_MARKET: throw Exceptions::MethodNotOverridenException("CSMatrix<Scalar>::import_from_file - Matrix Market"); break; case EXPORT_FORMAT_MATLAB_MATIO: { #ifdef WITH_MATIO mat_t *matfp; matvar_t *matvar; matfp = Mat_Open(filename, MAT_ACC_RDONLY); if (!matfp) throw Exceptions::IOException(Exceptions::IOException::Read, filename); matvar = Mat_VarRead(matfp, var_name); if (matvar) { mat_sparse_t *sparse = (mat_sparse_t *)matvar->data; this->nnz = sparse->nir; this->Ax = malloc_with_check<CSMatrix<Scalar>, Scalar>(this->nnz, this); this->Ai = malloc_with_check<CSMatrix<Scalar>, int>(this->nnz, this); this->size = sparse->njc; this->Ap = malloc_with_check<CSMatrix<Scalar>, int>(this->size + 1, this); void* data = nullptr; if (Hermes::Helpers::TypeIsReal<Scalar>::value) data = sparse->data; else { std::complex<double>* complex_data = malloc_with_check<CSMatrix<Scalar>, std::complex<double> >(this->nnz, this); double* real_array = (double*)((mat_complex_split_t*)sparse->data)->Re; double* imag_array = (double*)((mat_complex_split_t*)sparse->data)->Im; for (int i = 0; i < this->nnz; i++) complex_data[i] = std::complex<double>(real_array[i], imag_array[i]); data = (void*)complex_data; } memcpy(this->Ax, data, this->nnz * sizeof(Scalar)); if (!Hermes::Helpers::TypeIsReal<Scalar>::value) free_with_check(data); memcpy(this->Ap, sparse->jc, this->size * sizeof(int)); this->Ap[this->size] = this->nnz; memcpy(this->Ai, sparse->ir, this->nnz * sizeof(int)); if (invert_storage) this->switch_orientation(); } Mat_Close(matfp); if (!matvar) throw Exceptions::IOException(Exceptions::IOException::Read, filename); #else throw Exceptions::Exception("MATIO not included."); #endif } break; case EXPORT_FORMAT_PLAIN_ASCII: throw Exceptions::MethodNotOverridenException("CSMatrix<Scalar>::import_from_file - Simple format"); #ifdef WITH_BSON case EXPORT_FORMAT_BSON: { FILE *fpr; fpr = fopen(filename, "rb"); // file size: fseek(fpr, 0, SEEK_END); int size = ftell(fpr); rewind(fpr); // allocate memory to contain the whole file: char *datar = malloc_with_check<char>(size); fread(datar, size, 1, fpr); fclose(fpr); bson br; bson_init_finished_data(&br, datar, 0); bson_iterator it; bson sub; bson_find(&it, &br, "size"); this->size = bson_iterator_int(&it); bson_find(&it, &br, "nnz"); this->nnz = bson_iterator_int(&it); this->Ap = malloc_with_check<CSMatrix<Scalar>, int>(this->size + 1, this); this->Ai = malloc_with_check<CSMatrix<Scalar>, int>(nnz, this); this->Ax = malloc_with_check<CSMatrix<Scalar>, Scalar>(nnz, this); // coeffs bson_iterator it_coeffs; bson_find(&it_coeffs, &br, "Ap"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); int index_coeff = 0; while (bson_iterator_next(&it)) this->Ap[index_coeff++] = bson_iterator_int(&it); this->Ap[this->size] = this->nnz; bson_find(&it_coeffs, &br, "Ai"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); index_coeff = 0; while (bson_iterator_next(&it)) this->Ai[index_coeff++] = bson_iterator_int(&it); bson_find(&it_coeffs, &br, "Ax"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); index_coeff = 0; while (bson_iterator_next(&it)) this->Ax[index_coeff++] = bson_iterator_double(&it); if (!Hermes::Helpers::TypeIsReal<Scalar>::value) { bson_find(&it_coeffs, &br, "Ax-imag"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); index_coeff = 0; while (bson_iterator_next(&it)) ((std::complex<double>)this->Ax[index_coeff++]).imag(bson_iterator_double(&it)); } if (invert_storage) this->switch_orientation(); bson_destroy(&br); free_with_check(datar); } #endif break; } }
MONGO_EXPORT bson_bool_t bson_init_empty( bson *obj ) { bson_init_finished_data( obj, bson_shared_empty_data, 0 ); return BSON_OK; }
int QueryMatch2(BinaryStr* query, BinaryStr* obj ) { bson_iterator q,o; bson_type qType,tType; bson qBson,oBson; bson_iterator_from_buffer(&q, query->data); bson_iterator_from_buffer(&o, obj->data); bson_init_finished_data(&qBson,(char*)query->data); bson_init_finished_data(&oBson,(char*)obj->data); while((qType = bson_iterator_next(&q))) { // for all keys in query const char* key = bson_iterator_key(&q); //printf("Key %s \n",key); BSONElem val1,val2; BSONElemInit(&val1,bson_iterator_type(&q),(char*) bson_iterator_value(&q),0,&q); tType = bson_find(&o,&oBson,key); if (!tType) { BSONElemInit(&val2,BSON_EOO,NULL,0,NULL); if (!CompareValue(&val2,&val1)) { return False; } continue; } else if (tType == BSON_OBJECT && qType == BSON_OBJECT) { BinaryStr qData,oData; qData.data = (char*) bson_iterator_value(&q); oData.data = (char*) bson_iterator_value(&o); if (!QueryMatch(&qData,&oData)) { return False; } } else if (tType == BSON_ARRAY && qType != BSON_OBJECT && qType != BSON_ARRAY) { bson_iterator si; bson_type t; bson_iterator_subiterator(&o,&si); uint8_t found = False; while ((t=bson_iterator_next(&si))){ //BSONElemInit(&val2,bson_iterator_type(&si),(char*) bson_iterator_value(&si),0,&si); BSONElemInitFromItr(&val2,&si); //BSONElemInit(&val2,bson_iterator_type(&si),(char*) bson_iterator_value(&si),0,&si); if (CompareValue(&val2,&val1)) { found = True; break; } } if (!found) { return False; } } else { //BSONElemInit(&val2,bson_iterator_type(&o),(char*) bson_iterator_value(&o),0,&o); BSONElemInitFromItr(&val2,&o); if (!CompareValue(&val2,&val1)) { return False; } } } return True; }
void SimpleVector<Scalar>::import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt) { switch (fmt) { case EXPORT_FORMAT_PLAIN_ASCII: { std::vector<Scalar> data; std::ifstream input(filename); if (input.bad()) throw Exceptions::IOException(Exceptions::IOException::Read, filename); std::string lineData; while (getline(input, lineData)) { Scalar d; std::stringstream lineStream(lineData); lineStream >> d; data.push_back(d); } this->alloc(data.size()); memcpy(this->v, &data[0], sizeof(Scalar)*data.size()); } break; case EXPORT_FORMAT_MATLAB_MATIO: #ifdef WITH_MATIO mat_t *matfp; matvar_t *matvar; matfp = Mat_Open(filename, MAT_ACC_RDONLY); if (!matfp) { throw Exceptions::IOException(Exceptions::IOException::Read, filename); return; } matvar = Mat_VarRead(matfp, var_name); if (matvar) { this->alloc(matvar->dims[0]); if (Hermes::Helpers::TypeIsReal<Scalar>::value) memcpy(this->v, matvar->data, sizeof(Scalar)*this->size); else { std::complex<double>* complex_data = malloc_with_check<SimpleVector<Scalar>, std::complex<double> >(this->size, this); double* real_array = (double*)((mat_complex_split_t*)matvar->data)->Re; double* imag_array = (double*)((mat_complex_split_t*)matvar->data)->Im; for (int i = 0; i < this->size; i++) complex_data[i] = std::complex<double>(real_array[i], imag_array[i]); memcpy(this->v, complex_data, sizeof(Scalar)*this->size); free_with_check(complex_data); } } Mat_Close(matfp); if (!matvar) throw Exceptions::IOException(Exceptions::IOException::Read, filename); #else throw Exceptions::Exception("MATIO not included."); #endif break; case EXPORT_FORMAT_MATRIX_MARKET: throw Hermes::Exceptions::MethodNotImplementedException("SimpleVector<Scalar>::import_from_file - Matrix Market"); break; #ifdef WITH_BSON case EXPORT_FORMAT_BSON: { FILE *fpr; fpr = fopen(filename, "rb"); // file size: fseek(fpr, 0, SEEK_END); int size = ftell(fpr); rewind(fpr); // allocate memory to contain the whole file: char *datar = malloc_with_check<char>(size); fread(datar, size, 1, fpr); fclose(fpr); bson br; bson_init_finished_data(&br, datar, 0); bson_iterator it; bson sub; bson_find(&it, &br, "size"); this->size = bson_iterator_int(&it); this->v = malloc_with_check<SimpleVector<Scalar>, Scalar>(this->size, this); bson_iterator it_coeffs; bson_find(&it_coeffs, &br, "v"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); int index_coeff = 0; while (bson_iterator_next(&it)) this->v[index_coeff++] = bson_iterator_double(&it); if (!Hermes::Helpers::TypeIsReal<Scalar>::value) { bson_find(&it_coeffs, &br, "v-imag"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); index_coeff = 0; while (bson_iterator_next(&it)) ((std::complex<double>)this->v[index_coeff++]).imag(bson_iterator_double(&it)); } bson_destroy(&br); free_with_check(datar); } break; #endif } }
int uvmongo_message_read(uvmongo_t * m, char * msg, size_t buflen) { uvmongo_header_t * header; uvmongo_message_t * message; uvmongo_reply_fields_t * fields; uvmongo_reply_t * reply; unsigned int len, next; header = (uvmongo_header_t *) msg; fields = (uvmongo_reply_fields_t *)(msg+16); bson_little_endian32(&len, &header->msglen); reply = (uvmongo_reply_t *) bson_malloc(sizeof(uvmongo_reply_t) - sizeof(char) + len - 16 - 20); reply->header.msglen = len; bson_little_endian32(&reply->header.req_id, &header->req_id); bson_little_endian32(&reply->header.res_to, &header->res_to); bson_little_endian32(&reply->header.opcode, &header->opcode); bson_little_endian32(&reply->fields.flag, &fields->flag); bson_little_endian64(&reply->fields.cursorID, &fields->cursorID); bson_little_endian32(&reply->fields.start, &fields->start); bson_little_endian32(&reply->fields.num, &fields->num); reply->objs = msg+16+20; next = 0; int msg_name_len = Length(reply->header.res_to); char msg_name[msg_name_len]; snprintf(msg_name, sizeof(msg_name), "%d", reply->header.res_to); message = (uvmongo_message_t *)(hash_get(m->msgs, msg_name)); if (message == NULL) { fprintf(stderr, "could not found request id: %s", msg_name); return UVMONGO_ERROR; } do { bson res[1]; bson_init_finished_data(res, reply->objs + next, 1); if (!res) { break; } if (message->on_data) { message->on_data(m, res, message->privdata); } next += bson_size(res); if (next >= reply->header.msglen - 36) { break; } } while (next); /* * Handle Get more */ if (reply->fields.cursorID == 0 || reply->fields.num < message->cursor->limit) { /* * Here, we could call `response_cb` to notify user-land this response * has been ended. */ if (message->on_drain) { message->on_drain(m, message->privdata); } uvmongo_cursor_free(message->cursor); } else { uvmongo_cursor_t * cursor = uvmongo_cursor_new(message->cursor->coll); uvmongo_cursor_set_id(cursor, reply->fields.cursorID); uvmongo_cursor_set_limit(cursor, message->cursor->limit); uvmongo_message_t * new_msg = uvmongo_message_serialize_more(cursor); new_msg->cursor = cursor; uvmongo_message_set_callback(new_msg, message->on_data, message->on_drain, message->privdata); uvmongo_message_send(m, new_msg); } /* * check the buffer size is bigger than reply length, * that means multiple replies has been received. */ if (buflen > reply->header.msglen) { uvmongo_message_read(m, msg + reply->header.msglen, buflen - reply->header.msglen); } return UVMONGO_OK; }