null object C

34 views
Skip to first unread message

Guilherme Souza

unread,
Aug 3, 2012, 2:51:15 PM8/3/12
to mongod...@googlegroups.com
guys I'm having a weird problem my C code was inserting in mongodb a null object, I have an array with objects inside and all objects that i try to insert are null, follow the code:


#define MONGO_HAVE_UNISTD
#define MONGO_HAVE_STDINT

#include "mongo.h"
#include "templateadmin.h"
#include <stdlib.h>
#include <string.h>

void vModel_InsertService(ST_TemplateAdmin* stTemplate){

    mongo mgConn[1];
    bson bsInsert[1];
   
    mongo_write_concern mwConc[1];
    mongo_write_concern_init(mwConc);
    mwConc->w = 1;
    mongo_write_concern_finish(mwConc);
   
    int imachinesize = stTemplate->sttemplateadminimage->imachinesize;
    int isoftwaresize = stTemplate->sttemplateadminsoftware->iappsize;
    int ialternativessize = 0;
    int icounter = 0;

    bson_init(bsInsert);
    bson_append_start_array(bsInsert, "environment");
    while(imachinesize > icounter){
       
        bson_append_start_object(bsInsert, "machine");

            bson_append_string(bsInsert, "distribuition", stTemplate->sttemplateadminimage->sttemplateadminmachine[icounter]->szdistribution);
            bson_append_string(bsInsert, "version", stTemplate->sttemplateadminimage->sttemplateadminmachine[icounter]->szversion);
            bson_append_string(bsInsert, "name", stTemplate->sttemplateadminimage->sttemplateadminmachine[icounter]->szname);
            bson_append_string(bsInsert, "type", stTemplate->sttemplateadminimage->sttemplateadminmachine[icounter]->sztype);
            bson_append_int(bsInsert, "disk", stTemplate->sttemplateadminimage->sttemplateadminmachine[icounter]->idisk);
            bson_append_int(bsInsert, "memory", stTemplate->sttemplateadminimage->sttemplateadminmachine[icounter]->imemory);
            bson_append_int(bsInsert, "processor", stTemplate->sttemplateadminimage->sttemplateadminmachine[icounter]->iprocessor);
        bson_append_finish_object(bsInsert);
        icounter++;

    }
    bson_append_finish_array(bsInsert);
    icounter = 0;

    bson_append_start_array(bsInsert, "software");

    int internalcounter = 0;

    while(isoftwaresize > icounter){

        bson_append_start_object(bsInsert, "app");
            bson_append_string(bsInsert, "description", stTemplate->sttemplateadminsoftware->sttemplateadminapp[icounter]->szdescription);
            bson_append_string(bsInsert, "package", stTemplate->sttemplateadminsoftware->sttemplateadminapp[icounter]->szpackage);
            bson_append_string(bsInsert, "version", stTemplate->sttemplateadminsoftware->sttemplateadminapp[icounter]->szversion);
       
            bson_append_start_array(bsInsert, "dependences");
            int iaux = stTemplate->sttemplateadminsoftware->sttemplateadminapp[icounter]->sttemplateadmindependences->iitemsize;
            while(iaux > internalcounter){
                bson_append_string(bsInsert, "dependence_package",stTemplate->sttemplateadminsoftware->sttemplateadminapp[icounter]->sttemplateadmindependences->lstitems[internalcounter]);
                internalcounter++;
            }
            internalcounter = 0;
            bson_append_finish_array(bsInsert);

            bson_append_string(bsInsert, "main_download", stTemplate->sttemplateadminsoftware->sttemplateadminapp[icounter]->sttemplateadmindownload->szmain);
       
            iaux = stTemplate->sttemplateadminsoftware->sttemplateadminapp[icounter]->sttemplateadmindownload->ialternativesize;

            bson_append_start_array(bsInsert, "alternatives_downloads");
            while(iaux > internalcounter){
                bson_append_string(bsInsert, "downloads", stTemplate->sttemplateadminsoftware->sttemplateadminapp[icounter]->sttemplateadmindownload->lstalternative[internalcounter]);
                internalcounter++;

            }
            internalcounter = 0;
            bson_append_finish_array(bsInsert);   

        bson_append_finish_object(bsInsert);

        icounter++;
    }
    bson_append_finish_array(bsInsert);

    bson_finish(bsInsert);

    bson_print(bsInsert);

    int iStatus = mongo_connect(mgConn, "127.0.0.1", 27017);
    if(iStatus != MONGO_OK){
        switch(mgConn->err)
        {
            case MONGO_CONN_NO_SOCKET: printf("Socket not found\n"); fflush(stdout); exit(1);
            case MONGO_CONN_FAIL: printf("Connection Failed\n"); fflush(stdout); exit(1);
            case MONGO_CONN_NOT_MASTER: printf("Not master\n"); fflush(stdout); exit(1);
        }
    }

    if(mongo_insert(mgConn, "OSA.Template", bsInsert, mwConc) != MONGO_OK){
        switch(mgConn->err)
        {
            case MONGO_ERROR: printf("Mongo Error\n"); fflush(stdout); exit(1);
            case MONGO_BSON_INVALID: printf("Invalid BSON\n"); fflush(stdout); exit(1);
        }
    }

    mongo_destroy(mgConn);
}

ideas?

Thanks a lot!

Gary Murakami

unread,
Aug 8, 2012, 9:45:07 AM8/8/12
to mongod...@googlegroups.com
I suggest that you at least check the results of bson_finish for BSON_OK or BSON_ERROR.  Your code is complex enough that I suspect that you have an error somewhere.  Please do the check and respond back.  Also I suggest that you comment out code until you have something that works, then start adding elements back in iteratively with testing, fixing any problems iteratively until you have a completely working solution.

-Gary
Reply all
Reply to author
Forward
0 new messages