/** * \brief This function tries to load tag group data from MongoDB */ static void vs_mongo_taggroup_load_data(struct VSTagGroup *tg, bson *bson_version) { bson_iterator version_data_iter; /* Try to get tags of tag group */ if( bson_find(&version_data_iter, bson_version, "tags") == BSON_OBJECT ) { struct VSTag *tag; bson bson_tag; bson_iterator tags_iter, tag_iter; const char *key; int tag_id, data_type = -1, count = -1, tag_custom_type = -1; bson_iterator_subiterator(&version_data_iter, &tags_iter); while( bson_iterator_next(&tags_iter) == BSON_OBJECT ) { key = bson_iterator_key(&tags_iter); sscanf(key, "%d", &tag_id); bson_iterator_subobject_init(&tags_iter, &bson_tag, 0); if( bson_find(&tag_iter, &bson_tag, "data_type") == BSON_INT) { data_type = bson_iterator_int(&tag_iter); } if( bson_find(&tag_iter, &bson_tag, "count") == BSON_INT) { count = bson_iterator_int(&tag_iter); } if( bson_find(&tag_iter, &bson_tag, "custom_type") == BSON_INT) { tag_custom_type = bson_iterator_int(&tag_iter); } if(data_type != -1 && count != -1 && tag_custom_type != -1) { /* Create tag with specific ID */ tag = vs_tag_create(tg, tag_id, data_type, count, tag_custom_type); if(tag != NULL) { tag->state = ENTITY_CREATED; vs_mongo_tag_load_data(tag, &bson_tag); tag->flag = TAG_INITIALIZED; } } } } }
int main(int argc, char *argv[]) { const char * test_server = (argc > 1 ? argv[1] : TEST_SERVER); bson_iterator it[1], it2[1]; bson b[1]; bson sub[1]; bson copy[1]; bson_type type; bson_init( b ); bson_append_string( b, "foo", "hello" ); { bson_append_start_object( b, "o" ); bson_append_string( b, "bar", "goodbye" ); bson_append_finish_object( b ); } bson_iterator_init( it, b ); bson_iterator_next( it ); type = bson_iterator_next( it ); ASSERT( BSON_OBJECT == type ); bson_iterator_subobject_init( it, sub, 0 ); ASSERT( sub->finished == 1 ); bson_iterator_init( it2, sub ); type = bson_iterator_next( it2 ); ASSERT( BSON_STRING == type ); type = bson_iterator_next( it2 ); ASSERT( BSON_EOO == type ); bson_copy( copy, sub ); ASSERT( 1 == copy->finished ); ASSERT( 0 == copy->stackPos ); ASSERT( 0 == copy->err ); bson_destroy( copy ); bson_destroy( b ); return 0; }
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; } }
int main() { bson b, sub, out; bson_iterator it; mongo conn; mongo_cursor cursor; int result; /* Create a rich document like this one: * * { _id: ObjectId("4d95ea712b752328eb2fc2cc"), * user_id: ObjectId("4d95ea712b752328eb2fc2cd"), * * items: [ * { sku: "col-123", * name: "John Coltrane: Impressions", * price: 1099, * }, * * { sku: "young-456", * name: "Larry Young: Unity", * price: 1199 * } * ], * * address: { * street: "59 18th St.", * zip: 10010 * }, * * total: 2298 * } */ bson_init( &b ); bson_append_new_oid( &b, "_id" ); bson_append_new_oid( &b, "user_id" ); bson_append_start_array( &b, "items" ); bson_append_start_object( &b, "0" ); bson_append_string( &b, "name", "John Coltrane: Impressions" ); bson_append_int( &b, "price", 1099 ); bson_append_finish_object( &b ); bson_append_start_object( &b, "1" ); bson_append_string( &b, "name", "Larry Young: Unity" ); bson_append_int( &b, "price", 1199 ); bson_append_finish_object( &b ); bson_append_finish_object( &b ); bson_append_start_object( &b, "address" ); bson_append_string( &b, "street", "59 18th St." ); bson_append_int( &b, "zip", 10010 ); bson_append_finish_object( &b ); bson_append_int( &b, "total", 2298 ); /* Finish the BSON obj. */ bson_finish( &b ); printf("Here's the whole BSON object:\n"); bson_print( &b ); /* Advance to the 'items' array */ bson_find( &it, &b, "items" ); /* Get the subobject representing items */ bson_iterator_subobject_init( &it, &sub, 0 ); /* Now iterate that object */ printf("And here's the inner sub-object by itself.\n"); bson_print( &sub ); bson_destroy( &sub ); /* Now make a connection to MongoDB. */ if( mongo_client( &conn, "127.0.0.1", 27017 ) != MONGO_OK ) { switch( conn.err ) { case MONGO_CONN_SUCCESS: break; case MONGO_CONN_NO_SOCKET: printf( "FAIL: Could not create a socket!\n" ); break; case MONGO_CONN_FAIL: printf( "FAIL: Could not connect to mongod. Make sure it's listening at 127.0.0.1:27017.\n" ); break; default: printf( "MongoDB connection error number %d.\n", conn.err ); break; } exit( 1 ); } /* Insert the sample BSON document. */ if( mongo_insert( &conn, "test.records", &b, NULL ) != MONGO_OK ) { printf( "FAIL: Failed to insert document with error %d\n", conn.err ); exit( 1 ); } /* Query for the sample document. */ mongo_cursor_init( &cursor, &conn, "test.records" ); mongo_cursor_set_query( &cursor, bson_shared_empty() ); if( mongo_cursor_next( &cursor ) != MONGO_OK ) { printf( "FAIL: Failed to find inserted document." ); exit( 1 ); } printf( "Found saved BSON object:\n" ); bson_print( (bson *)mongo_cursor_bson( &cursor ) ); mongo_cmd_drop_collection( &conn, "test", "records", NULL ); mongo_cursor_destroy( &cursor ); bson_destroy( &b ); mongo_destroy( &conn ); return 0; }
/** * \brief This function tries to load tag group from MongoDB * * \param[in] *vs_ctx The verse server context * \param[in] *oid The pointer at ObjectID of tag group in MongoDB * \param[in] *node The node containing tag group * \param[in] taggroup_id The tag group ID that is requested from database * \param[in] version The version of tag group id that is requested * from database. When version is equal to -1, then * current version is loaded from MongoDB. * * \return This function returns pointer at tag group, when tag group is found. * Otherwise it returns NULL. */ struct VSTagGroup *vs_mongo_taggroup_load_linked(struct VS_CTX *vs_ctx, bson_oid_t *oid, struct VSNode *node, uint16 taggroup_id, uint32 req_version) { struct VSTagGroup *tg = NULL; bson query; mongo_cursor cursor; uint32 node_id = -1, tmp_taggroup_id = -1, current_version = -1, custom_type = -1; int found = 0; bson_iterator tg_data_iter; const bson *bson_tg; bson_init(&query); bson_append_oid(&query, "_id", oid); bson_finish(&query); mongo_cursor_init(&cursor, vs_ctx->mongo_conn, vs_ctx->mongo_tg_ns); mongo_cursor_set_query(&cursor, &query); /* ObjectID should be unique */ while( mongo_cursor_next(&cursor) == MONGO_OK ) { bson_tg = mongo_cursor_bson(&cursor); /* Try to get node id */ if( bson_find(&tg_data_iter, bson_tg, "node_id") == BSON_INT ) { node_id = bson_iterator_int(&tg_data_iter); } /* Try to get tag group id */ if( bson_find(&tg_data_iter, bson_tg, "taggroup_id") == BSON_INT ) { tmp_taggroup_id = bson_iterator_int(&tg_data_iter); } /* ObjectID is ALMOST unique. So it is check, if node id and * tag group id matches */ if(node_id == node->id && tmp_taggroup_id == taggroup_id) { found = 1; break; } } /* When tag group was found, then load required data from MongoDB */ if(found == 1) { /* Try to get current version of tag group */ if( bson_find(&tg_data_iter, bson_tg, "current_version") == BSON_INT ) { current_version = bson_iterator_int(&tg_data_iter); } /* Try to get custom type of tag group */ if( bson_find(&tg_data_iter, bson_tg, "custom_type") == BSON_INT ) { custom_type = bson_iterator_int(&tg_data_iter); } /* Create tag group with specific ID */ if((int)current_version != -1 && (int)custom_type != -1) { tg = vs_taggroup_create(node, taggroup_id, custom_type); if(tg != NULL) { tg->state = ENTITY_CREATED; /* Save ObjectID to tag group */ memcpy(&tg->oid, oid, sizeof(bson_oid_t)); /* Try to get versions of tag group */ if( bson_find(&tg_data_iter, bson_tg, "versions") == BSON_OBJECT ) { bson bson_versions; bson_iterator version_iter; char str_num[15]; /* Initialize sub-object of versions */ bson_iterator_subobject_init(&tg_data_iter, &bson_versions, 0); sprintf(str_num, "%u", req_version); /* Try to find required version of tag group */ if( bson_find(&version_iter, &bson_versions, str_num) == BSON_OBJECT ) { bson bson_version; /* Set version of tag group */ tg->version = tg->saved_version = current_version; bson_iterator_subobject_init(&version_iter, &bson_version, 0); /* Try to load tags */ vs_mongo_taggroup_load_data(tg, &bson_version); } } } } } bson_destroy(&query); mongo_cursor_destroy(&cursor); return tg; }
int main(int argc, char **argv) { bson b, sub; bson_iterator it; /* Create a rich document like this one: * * { _id: ObjectId("4d95ea712b752328eb2fc2cc"), * user_id: ObjectId("4d95ea712b752328eb2fc2cd"), * * items: [ * { sku: "col-123", * name: "John Coltrane: Impressions", * price: 1099, * }, * * { sku: "young-456", * name: "Larry Young: Unity", * price: 1199 * } * ], * * address: { * street: "59 18th St.", * zip: 10010 * }, * * total: 2298 * } */ bson_init( &b ); bson_append_new_oid( &b, "_id" ); bson_append_new_oid( &b, "user_id" ); bson_append_start_array( &b, "items" ); bson_append_start_object( &b, "0" ); bson_append_string( &b, "name", "John Coltrane: Impressions" ); bson_append_int( &b, "price", 1099 ); bson_append_finish_object( &b ); bson_append_start_object( &b, "1" ); bson_append_string( &b, "name", "Larry Young: Unity" ); bson_append_int( &b, "price", 1199 ); bson_append_finish_object( &b ); bson_append_finish_object( &b ); bson_append_start_object( &b, "address" ); bson_append_string( &b, "street", "59 18th St." ); bson_append_int( &b, "zip", 10010 ); bson_append_finish_object( &b ); bson_append_int( &b, "total", 2298 ); bson_finish( &b ); /* Advance to the 'items' array */ bson_find( &it, &b, "items" ); /* Get the subobject representing items */ bson_iterator_subobject_init( &it, &sub, 0 ); /* Now iterate that object */ bson_print( &sub ); bson_destroy( &b ); return 0; }
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 } }