Example #1
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;
}
Example #2
0
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;

}
Example #3
0
v8::Local<v8::Object> decodeObject(const bson *bb)
{
    bson_iterator it;
    bson_iterator_from_buffer(&it, bson_data(bb));

    return decodeObject(&it, false);
}
Example #4
0
uint8_t
XTDBRemoveFromIndex(XTDBHandle* handle,BinaryStr* key,BinaryStr* data) {
    bson_oid_t oid;
    bson_iterator q;
    bson_type qType;
    uint8_t ret = True;
    _S_FN(indexRemove);

    bson_iterator_from_buffer(&q, data->data);

    while((qType = bson_iterator_next(&q))) {
        const char* keyVal = bson_iterator_key(&q);
        //printf("%s\n",keyVal);
        DataBaseBE* db;
        BSONElem qVal;
        BSONElemInitFromItr(&qVal,&q);
        db = XTDBGetIndex(handle,keyVal);
        if (db) {
            bson outBson;
            BinaryStr out;
            ConvertToBStr(&qVal,&outBson);
            BsonToBStr(&outBson,&out);
            ret = DBDeleteKeyVal(db,&out,key);
            if (!ret) {
                handle->error = XTDB_NO_ENTRY;
                bson_destroy(&outBson);
                break;
            }
            bson_destroy(&outBson);
        }
    }
    _E_FN(indexRemove);
    return ret;
    //return DBSet(handle->mainDB,&key,&val,False);
}
Example #5
0
v8::Local<v8::Object> decodeObject(const char *buffer)
{
    bson_iterator it;
    bson_iterator_from_buffer(&it, buffer);

    return decodeObject(&it, false);
}
Example #6
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;
}
Example #7
0
INLINE int sizeof_bson P1(const char *, data){
	int size = 0;
	bson_iterator i;
	bson_iterator_from_buffer( &i, data );
	while(bson_iterator_next( &i )){
		size++;
	}
	return size;
}
Example #8
0
int lua_from_bson(lua_State *L) {
    lua_settop(L, 1);
    size_t slen = 0;
    const void *bsdata = luaL_checklstring(L, lua_gettop(L), &slen);
    if (slen <= 4 || !bsdata) {
        return luaL_error(L, "Invalid bson string at argument #1");
    }
    bson_iterator it;
    bson_iterator_from_buffer(&it, bsdata);
    lua_push_bson_table(L, &it);
    return 1;
}
Example #9
0
INT32 bson2csv( CHAR delChar, CHAR delField, CHAR *pbson,
                CHAR **ppBuffer, INT32 *pCSVSize,
                BOOLEAN includeBinary,
                BOOLEAN includeRegex )
{
   INT32 rc = SDB_OK ;
   BOOLEAN isFirst = TRUE ;
   bson_type fieldType ;
   bson_iterator it ;

   bson_iterator_from_buffer( &it, pbson ) ;


   while ( bson_iterator_next( &it ) )
   {
      fieldType = bson_iterator_type( &it ) ;
      if ( BSON_EOO == fieldType )
      {
         break ;
      }
      if ( isFirst )
      {
         isFirst = FALSE ;
      }
      else
      {
         rc = _appendString( delChar, &delField, 1, ppBuffer, pCSVSize ) ;
         if ( rc )
         {
            goto error ;
         }
      }
      if ( BSON_UNDEFINED == fieldType )
      {
         continue ;
      }
      rc = _appendValue( delChar, &it, ppBuffer, pCSVSize,
                         includeBinary,
                         includeRegex ) ;
      if ( rc )
      {
         goto error ;
      }
   }
done:
   return rc ;
error:
   goto done ;
}
Example #10
0
int
QueryGetIndexesToUse(BinaryStr* query,ListNode* head){
   LIST_INIT(head);
   bson_iterator q,o;
   bson_type qType,tType;

   bson_iterator_from_buffer(&q, query->data);
   while((qType =bson_iterator_next(&q))) {
      const char* key = bson_iterator_key(&q);
      if (qType != BSON_ARRAY && qType != BSON_OBJECT ) {
         ListNode* node2 = (ListNode*) malloc(sizeof(*node2));
         node2->data = strdup(key);
         LIST_ADD_AFTER(node2,LIST_LAST(head));
      } 
   }
   return 0;
}
Example #11
0
static void mongo_replset_check_seed( mongo *conn ) {
    bson out;
    bson hosts;
    const char *data;
    bson_iterator it;
    bson_iterator it_sub;
    const char *host_string;
    mongo_host_port *host_port = NULL;

    out.data = NULL;

    hosts.data = NULL;

    if( mongo_simple_int_command( conn, "admin", "ismaster", 1, &out ) == MONGO_OK ) {

        if( bson_find( &it, &out, "hosts" ) ) {
            data = bson_iterator_value( &it );
            bson_iterator_from_buffer( &it_sub, data );

            /* Iterate over host list, adding each host to the
             * connection's host list. */
            while( bson_iterator_next( &it_sub ) ) {
                host_string = bson_iterator_string( &it_sub );

                host_port = bson_malloc( sizeof( mongo_host_port ) );
                mongo_parse_host( host_string, host_port );

                if( host_port ) {
                    mongo_replset_add_node( &conn->replset->hosts,
                                            host_port->host, host_port->port );

                    bson_free( host_port );
                    host_port = NULL;
                }
            }
        }
    }

    bson_destroy( &out );
    bson_destroy( &hosts );
    mongo_close_socket( conn->sock );
    conn->sock = 0;
    conn->connected = 0;

}
Example #12
0
INLINE void bson_to_array P2(const char *, data, svalue_t *, v){
	int size;
    array_t *av;
    svalue_t *sv;
    bson_iterator i;

    size = sizeof_bson(data);
    av = allocate_array(size);
    sv = av->item;
    bson_iterator_from_buffer( &i, data );
    while(bson_iterator_next( &i ) && size--){
		bson_to_v(&i,sv);
		sv++;
    }
    v->type = T_ARRAY;
    v->u.arr = av;
    
}
Example #13
0
// Checked
uint8_t
XTDBInsertToIndex(XTDBHandle* handle,BinaryStr* key,BinaryStr* data) {
    bson_oid_t oid;
    bson_iterator q;
    bson_type qType;
    uint8_t ret = True;
    _S_FN(indexInsert);

    if (!tcmaprnum(handle->indexes)) {
        _E_FN(indexInsert);
        return True;
    }

    bson_iterator_from_buffer(&q, data->data);
    while((qType = bson_iterator_next(&q))) {
        const char* keyVal = bson_iterator_key(&q);
        //printf("keyval %s\n",keyVal);
        DataBaseBE* db;
        BSONElem qVal;
        BSONElemInitFromItr(&qVal,&q);
        db = XTDBGetIndex(handle,keyVal);
        if (db) {
            bson outBson;
            BinaryStr out;
            if (qType == BSON_ARRAY) {
                assert(0);
                bson_iterator si;
                bson_type t;
                bson_iterator_subiterator(&q,&si);
                while ((t=bson_iterator_next(&si))) {
                    if (ConvertToBStr(&qVal,&outBson)) {
                        handle->error = XTDB_NO_MEM;
                        return False;
                    }
                    BsonToBStr(&outBson,&out);
                    ret = DBSet(db,&out,key,True);
                    bson_destroy(&outBson);
                    if (ret) {
                        handle->error = XTDB_IO_ERR;
                        goto error;
                    }
                }
            } else {

                if (ConvertToBStr(&qVal,&outBson)) {
                    handle->error = XTDB_NO_MEM;
                    return False;
                }
                BsonToBStr(&outBson,&out);
                ret = DBSet(db,&out,key,True);
                bson_destroy(&outBson);
                if (!ret) {
                    handle->error = XTDB_IO_ERR;
                    goto error;
                }
            }
        }

    }
    _E_FN(indexInsert);

    return ret;
error:
    // roll back is required?
    // can simply return error unnecessarily inserted elements will be eventually corrected when index is accessed
    return ret;
    //return DBSet(handle->mainDB,&key,&val,False);
}
Example #14
0
Tcl_Obj *
mongotcl_bsontolist_raw (Tcl_Interp *interp, Tcl_Obj *listObj, const char *data , int depth) {
    bson_iterator i;
    const char *key;
    bson_timestamp_t ts;
    char oidhex[25];
    bson scope;

	if (data == NULL) {
		return listObj;
	}

    bson_iterator_from_buffer(&i, data);

    while (bson_iterator_next (&i)) {
        bson_type t = bson_iterator_type (&i);
        if (t == 0) {
            break;
		}

        key = bson_iterator_key (&i);

		switch (t) {
			case BSON_DOUBLE: {
				append_list_type_object (interp, listObj, "double", key, Tcl_NewDoubleObj (bson_iterator_double (&i)));
				break;
			}

			case BSON_STRING: {
				append_list_type_object (interp, listObj, "string", key, Tcl_NewStringObj (bson_iterator_string (&i), -1));
				break;
			}

			case BSON_SYMBOL: {
				append_list_type_object (interp, listObj, "symbol", key, Tcl_NewStringObj (bson_iterator_string (&i), -1));
				break;
			}

			case BSON_OID: {
				bson_oid_to_string( bson_iterator_oid( &i ), oidhex );
				append_list_type_object (interp, listObj, "oid", key, Tcl_NewStringObj (oidhex, -1));
				break;
			}

			case BSON_BOOL: {
			append_list_type_object (interp, listObj, "bool", key, Tcl_NewBooleanObj (bson_iterator_bool (&i)));
				break;
			}

			case BSON_DATE: {
				append_list_type_object (interp, listObj, "date", key, Tcl_NewLongObj ((long) bson_iterator_date(&i)));
				break;
			}

			case BSON_BINDATA: {
				unsigned char *bindata = (unsigned char *)bson_iterator_bin_data (&i);
				int binlen = bson_iterator_bin_len (&i);

				append_list_type_object (interp, listObj, "bin", key, Tcl_NewByteArrayObj (bindata, binlen));
				break;
			}

			case BSON_UNDEFINED: {
				append_list_type_object (interp, listObj, "undefined", key, Tcl_NewObj ());
				break;
			}

			case BSON_NULL: {
				append_list_type_object (interp, listObj, "null", key, Tcl_NewObj ());
				break;
			}

			case BSON_REGEX: {
				append_list_type_object (interp, listObj, "regex", key, Tcl_NewStringObj (bson_iterator_regex (&i), -1));
				break;
			}

			case BSON_CODE: {
				append_list_type_object (interp, listObj, "code", key, Tcl_NewStringObj (bson_iterator_code (&i), -1));
				break;
			}

			case BSON_CODEWSCOPE: {
				bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) );
				/* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */
				bson_iterator_code_scope( &i, &scope );
				bson_printf( "\n\t SCOPE: " );
				bson_print( &scope );
				/* bson_destroy( &scope ); */ /* review - causes free error */
				break;
			}

			case BSON_INT: {
				append_list_type_object (interp, listObj, "int", key, Tcl_NewIntObj (bson_iterator_int (&i)));
				break;
			}

			case BSON_LONG: {
				append_list_type_object (interp, listObj, "long", key, Tcl_NewLongObj ((uint64_t)bson_iterator_long (&i)));
				break;
			}

			case BSON_TIMESTAMP: {
				char string[64];

				ts = bson_iterator_timestamp (&i);
				snprintf(string, sizeof(string), "%d:%d", ts.i, ts.t);
				append_list_type_object (interp, listObj, "timestamp", key, Tcl_NewStringObj (bson_iterator_string (&i), -1));
				break;
			}

			case BSON_ARRAY: {
				Tcl_Obj *subList = Tcl_NewObj ();

				subList = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1);
				append_list_type_object (interp, listObj, "array", key, subList);
				break;
			}

			case BSON_OBJECT: {
				Tcl_Obj *subList = Tcl_NewObj ();

				subList = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1);
				append_list_type_object (interp, listObj, "object", key, subList);
				break;
			}

			default: {
				append_list_type_object (interp, listObj, "unknown", key, Tcl_NewIntObj (t));
				break;
			}
		}
    }
    return listObj;
}
Example #15
0
XTDBCursor*
XTDBFind(XTDBHandle* handle,bson* query) {
    BinaryStr key,data,keyData;
    bson_iterator q;
    bson_type qType;
    XTDBCursor * cur ;
    ListNode idxNameList;
    char * minKey = NULL;
    uint64_t minWeight = -1,weight;
    DataBaseBE* db = NULL,*minDB;
    BinaryStr idxVal,minIndexVal;
    BinaryStr origQuery;
    bson outBson;

    _S_FN(find);
    cur= malloc(sizeof(XTDBCursor));
    if (!cur) {
        handle->error = XTDB_NO_MEM;
        return NULL;
    }
    memset(cur,0,sizeof(*cur));
    cur->first = True;
    BsonToBStr(query,&origQuery);
    if (BinaryStrDup(&cur->query,&origQuery)) {

        handle->error = XTDB_NO_MEM;
        goto error;
    }
    bson_iterator_from_buffer(&q,query->data);
    QueryGetIndexesToUse(&cur->query,&idxNameList);

    minIndexVal.data = NULL;
    minIndexVal.len = 0;

    while (!LIST_ISEMPTY(&idxNameList)) {
        ListNode* node = LIST_FIRST(&idxNameList);
        char* key = node->data;
        LIST_REMOVE(&idxNameList,LIST_FIRST(&idxNameList));
        qType = bson_find(&q,query,key);
        assert(qType);
        BSONElem qVal;
        BSONElemInitFromItr(&qVal,&q);
        db = XTDBGetIndex(handle,key);
        if (db) {
            if (ConvertToBStr(&qVal,&outBson)) {
                handle->error = XTDB_NO_MEM;
                goto error;
            }
            BsonToBStr(&outBson,&idxVal);
            weight = DBCount(db,&idxVal);
            if (minWeight >  weight) {
                //BinaryStrFree(&minIndexVal);
                BStrToBson(&minIndexVal,&outBson);
                bson_destroy(&outBson);
                minIndexVal = idxVal;
                free(minKey);
                minWeight = weight;
                minKey = strdup(key);

                minDB = db;
            } else {
                bson_destroy(&outBson);
            }
        }
        free(node);
        free(key);
    }

    cur->mdbHandle = handle;
    if (minWeight == -1) {
        cur->dbIter = DBIter(handle->mainDB);
        if (!cur->dbIter) {
            handle->error = XTDB_NO_MEM;
            goto error;
        }
        cur->usingIndex = NO_INDEX;
        cur->curDB = handle->mainDB;
    } else {
        assert(minDB);
        cur->curDB = minDB;
        //printf("using index %s %d \n",minKey,minWeight);
        cur->usingIndex = OTHER_INDEX;
        if (!DBGetList(minDB,&minIndexVal,&cur->dbIter)) {
            handle->error = DBGetLastError(minDB);
            if (handle->error == XTDB_NO_ENTRY) {
                handle->error = XTDB_OK;
            } else {
                goto error;
            }
        }
        BinaryStrFree(&minIndexVal);

        if (!cur->dbIter) {
            //printf("Warning Value not found in index '%s'\n",idxVal);
        } else {
            uint32_t count = DBListLen(minDB,cur->dbIter);
            if (count >10) {
            }

        }
        //assert(cur->dbIter);
        cur->idx = 0;
        free(minKey);
    }

    _E_FN(find);
    return cur;
error:
    _E_FN(find);
    XTDBCursorFree(cur);
    return NULL;
}
Example #16
0
MONGO_EXPORT void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ) {
    check_mongo_object( (void*)sub );
    check_mongo_object( (void*)i );
    bson_iterator_from_buffer( sub, bson_iterator_value( i ) );
}
Example #17
0
INLINE void bson_to_mapping P2(const char *, data, svalue_t *, v){
	int id;
	int oi;
	int mask;
	int size;
	int count = 0;
	const char *key_c;
	bson_iterator i;
	mapping_t *m;
	svalue_t key, value;
	mapping_node_t **a, *elt, *elt2;

	size = sizeof_bson(data);
	if (!size) {
		v->type = T_MAPPING;
		v->u.map = allocate_mapping(0);
		return;
    }
    m = allocate_mapping(size);
    a = m->table;                    
    mask = m->table_size;
	bson_iterator_from_buffer( &i, data );
	while(bson_iterator_next( &i )){
		key_c = bson_iterator_key( &i );
		key.u.string = make_shared_string(key_c);
    	key.type = T_STRING;
    	key.subtype = STRING_SHARED;
		bson_to_v(&i,&value);
		oi = MAP_POINTER_HASH(key.u.number);
		id = oi & mask;
		if ((elt2 = elt = a[id])) {
		    do {
			/* This should never happen, but don't bail on it */
				if (msameval(&key, elt->values)) {
				    free_svalue(&key, "bson_to_map: duplicate key");
				    free_svalue(elt->values+1, "bson_to_map: replaced value");
				    *(elt->values+1) = value;
				    break;
				}
		    } while ((elt = elt->next));
		    if (elt)
				continue;
		} else if (!(--m->unfilled)) {
		    if (growMap(m)) {
				a = m->table;
				if (oi & ++mask) 
					elt2 = a[id |= mask];
				mask <<= 1;
				mask--;
		    } else {
				add_map_stats(m, count);
				free_mapping(m);
				free_svalue(&key, "bson_to_map: out of memory");
				free_svalue(&value, "bson_to_map: out of memory");
				error("Out of memory\n");
		    }
		}
	
		if (++count > MAX_MAPPING_SIZE) {
		    add_map_stats(m, count -1);
		    free_mapping(m);
		    free_svalue(&key, "bson_to_map: mapping too large");
		    free_svalue(&value, "bson_to_map: mapping too large");
		    mapping_too_large();
		}
		elt = new_map_node();
		*elt->values = key;
		*(elt->values + 1) = value;
		(a[id] = elt)->next = elt2;
	}
	add_map_stats(m, count);
    v->type = T_MAPPING;
    v->u.map = m;
}
Example #18
0
int
mongotcl_bsontoarray_raw (Tcl_Interp *interp, char *arrayName, char *typeArrayName, const char *data , int depth) {
    bson_iterator i;
    const char *key;
    bson_timestamp_t ts;
    char oidhex[25];
	Tcl_Obj *obj;
	char *type;

	if (data == NULL) {
		return TCL_OK;
	}

    bson_iterator_from_buffer(&i, data);

    while (bson_iterator_next (&i)) {
        bson_type t = bson_iterator_type (&i);
        if (t == 0) {
            break;
		}

        key = bson_iterator_key (&i);

        switch (t) {
			case BSON_DOUBLE: {
				obj = Tcl_NewDoubleObj (bson_iterator_double (&i));
				type = "double";
				break;
		}

			case BSON_SYMBOL: {
				obj = Tcl_NewStringObj (bson_iterator_string (&i), -1);
				type = "symbol";
				break;
			}

			case BSON_STRING: {
				obj = Tcl_NewStringObj (bson_iterator_string (&i), -1);
				type = "string";
				break;
			}

			case BSON_OID: {
				bson_oid_to_string( bson_iterator_oid( &i ), oidhex );
				obj = Tcl_NewStringObj (oidhex, -1);
				type = "oid";
				break;
			}

			case BSON_BOOL: {
				obj = Tcl_NewBooleanObj (bson_iterator_bool (&i));
				type = "bool";
				break;
			}

			case BSON_DATE: {
				obj = Tcl_NewLongObj ((long) bson_iterator_date(&i));
				type = "date";
				break;
			}

			case BSON_BINDATA: {
				unsigned char *bindata = (unsigned char *)bson_iterator_bin_data (&i);
				int binlen = bson_iterator_bin_len (&i);

				obj = Tcl_NewByteArrayObj (bindata, binlen);
				type = "bin";
				break;
			}

			case BSON_UNDEFINED: {
				obj = Tcl_NewObj ();
				type = "undefined";
				break;
			}

			case BSON_NULL: {
				obj = Tcl_NewObj ();
				type = "null";
				break;
			}

			case BSON_REGEX: {
				obj = Tcl_NewStringObj (bson_iterator_regex (&i), -1);
				type = "regex";
				break;
			}

			case BSON_CODE: {
				obj = Tcl_NewStringObj (bson_iterator_code (&i), -1);
				type = "code";
				break;
			}

			case BSON_CODEWSCOPE: {
				// bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) );
				/* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */
				// bson_iterator_code_scope( &i, &scope );
				// bson_printf( "\n\t SCOPE: " );
				// bson_print( &scope );
				/* bson_destroy( &scope ); */ /* review - causes free error */
				break;
			}

			case BSON_INT: {
				obj = Tcl_NewIntObj (bson_iterator_int (&i));
				type = "int";
				break;
			}

			case BSON_LONG: {
				obj = Tcl_NewLongObj ((uint64_t)bson_iterator_long (&i));
				type = "long";
				break;
			}

			case BSON_TIMESTAMP: {
				char string[64];

				ts = bson_iterator_timestamp (&i);
				snprintf(string, sizeof(string), "%d:%d", ts.i, ts.t);
				obj = Tcl_NewStringObj (bson_iterator_string (&i), -1);
				type = "timestamp";
				break;
			}

			case BSON_ARRAY: {
				obj = Tcl_NewObj();
				obj = mongotcl_bsontolist_raw (interp, obj, bson_iterator_value (&i), depth + 1);
				type = "array";

				break;
			}

			case BSON_OBJECT: {
				Tcl_Obj *subList = Tcl_NewObj ();

				obj = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1);
				type = "object";
				break;
			}

			default: {
				obj = Tcl_NewIntObj (t);
				type = "unknown";
				break;
			}
		}

		if (Tcl_SetVar2Ex (interp, arrayName, key, obj, TCL_LEAVE_ERR_MSG) == NULL) {
			return TCL_ERROR;
		}

		if (typeArrayName != NULL) {
			if (Tcl_SetVar2Ex (interp, typeArrayName, key, Tcl_NewStringObj (type, -1), TCL_LEAVE_ERR_MSG) == NULL) {
				return TCL_ERROR;
			}
		}
    }
	return TCL_OK; 
}
Example #19
0
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;
}
Example #20
0
void bson_print_raw( const char *data , int depth ) {
    bson_iterator i;
    const char *key;
    int temp;
    bson_timestamp_t ts;
    char oidhex[25];
    bson scope;
    bson_iterator_from_buffer( &i, data );

    while ( bson_iterator_next( &i ) ) {
        bson_type t = bson_iterator_type( &i );
        if ( t == 0 )
            break;
        key = bson_iterator_key( &i );

        for ( temp=0; temp<=depth; temp++ )
            printf( "\t" );
        bson_printf( "%s : %d \t " , key , t );
        switch ( t ) {
        case BSON_DOUBLE:
            printf( "%f" , bson_iterator_double( &i ) );
            break;
        case BSON_STRING:
            printf( "%s" , bson_iterator_string( &i ) );
            break;
        case BSON_SYMBOL:
            printf( "SYMBOL: %s" , bson_iterator_string( &i ) );
            break;
        case BSON_OID:
            bson_oid_to_string( bson_iterator_oid( &i ), oidhex );
            printf( "%s" , oidhex );
            break;
        case BSON_BOOL:
            printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" );
            break;
        case BSON_DATE:
            printf( "%ld" , ( long int )bson_iterator_date( &i ) );
            break;
        case BSON_BINDATA:
            printf( "BSON_BINDATA" );
            break;
        case BSON_UNDEFINED:
            printf( "BSON_UNDEFINED" );
            break;
        case BSON_NULL:
            printf( "BSON_NULL" );
            break;
        case BSON_REGEX:
            printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) );
            break;
        case BSON_CODE:
            printf( "BSON_CODE: %s", bson_iterator_code( &i ) );
            break;
        case BSON_CODEWSCOPE:
            printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) );
            bson_init( &scope );
            bson_iterator_code_scope( &i, &scope );
            printf( "\n\t SCOPE: " );
            bson_print( &scope );
            break;
        case BSON_INT:
            printf( "%d" , bson_iterator_int( &i ) );
            break;
        case BSON_LONG:
            printf( "%lld" , ( long long int )bson_iterator_long( &i ) );
            break;
        case BSON_TIMESTAMP:
            ts = bson_iterator_timestamp( &i );
            printf( "i: %d, t: %d", ts.i, ts.t );
            break;
        case BSON_OBJECT:
        case BSON_ARRAY:
            printf( "\n" );
            bson_print_raw( bson_iterator_value( &i ) , depth + 1 );
            break;
        default:
            bson_errprintf( "can't print type : %d\n" , t );
        }
        printf( "\n" );
    }
}
Example #21
0
static void bson_print_xstr(TCXSTR* xstr, const char *data, int depth) {
    bson_iterator i;
    const char *key;
    int temp;
    bson_timestamp_t ts;
    char oidhex[25];
    bson scope;
    bson_iterator_from_buffer(&i, data);

    while (bson_iterator_next(&i)) {
        bson_type t = bson_iterator_type(&i);
        if (t == 0)
            break;
        key = bson_iterator_key(&i);

        for (temp = 0; temp <= depth; temp++) {
            tcxstrprintf(xstr, ".");
        }
        tcxstrprintf(xstr, "%s(%d)=", key, t);
        switch (t) {
            case BSON_DOUBLE:
                tcxstrprintf(xstr, "%f", bson_iterator_double(&i));
                break;
            case BSON_STRING:
                tcxstrprintf(xstr, "%s", bson_iterator_string(&i));
                break;
            case BSON_SYMBOL:
                tcxstrprintf(xstr, "SYMBOL: %s", bson_iterator_string(&i));
                break;
            case BSON_OID:
                bson_oid_to_string(bson_iterator_oid(&i), oidhex);
                tcxstrprintf(xstr, "%s", oidhex);
                break;
            case BSON_BOOL:
                tcxstrprintf(xstr, "%s", bson_iterator_bool(&i) ? "true" : "false");
                break;
            case BSON_DATE:
                tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i));
                break;
            case BSON_BINDATA:
                tcxstrprintf(xstr, "BSON_BINDATA");
                break;
            case BSON_UNDEFINED:
                tcxstrprintf(xstr, "BSON_UNDEFINED");
                break;
            case BSON_NULL:
                tcxstrprintf(xstr, "BSON_NULL");
                break;
            case BSON_REGEX:
                tcxstrprintf(xstr, "BSON_REGEX: %s", bson_iterator_regex(&i));
                break;
            case BSON_CODE:
                tcxstrprintf(xstr, "BSON_CODE: %s", bson_iterator_code(&i));
                break;
            case BSON_CODEWSCOPE:
                tcxstrprintf(xstr, "BSON_CODE_W_SCOPE: %s", bson_iterator_code(&i));
                /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */
                bson_iterator_code_scope(&i, &scope);
                tcxstrprintf(xstr, "\n  SCOPE: ");
                bson_print_xstr(xstr, scope.data, 0);
                /* bson_destroy( &scope ); */ /* review - causes free error */
                break;
            case BSON_INT:
                tcxstrprintf(xstr, "%d", bson_iterator_int(&i));
                break;
            case BSON_LONG:
                tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i));
                break;
            case BSON_TIMESTAMP:
                ts = bson_iterator_timestamp(&i);
                tcxstrprintf(xstr, "i: %d, t: %d", ts.i, ts.t);
                break;
            case BSON_OBJECT:
            case BSON_ARRAY:
                tcxstrprintf(xstr, "\n");
                bson_print_xstr(xstr, bson_iterator_value(&i), depth + 1);
                break;
            default:
                fprintf(stderr, "can't print type : %d\n", t);
        }
        tcxstrprintf(xstr, "\n");
    }
}
Example #22
0
static char *
bson_to_json_recurse(StringInfo buf, const char *data, bool is_object)
{
	bson_iterator i;
	const char *key;
	char oidhex[25];
	char *str;
	bool first = true;

	bson_iterator_from_buffer(&i, data);

	while (bson_iterator_next(&i))
	{
		bson_type t = bson_iterator_type(&i);
		if (t == 0)
			break;

		if (!first)
			appendStringInfoChar(buf, ',');
		first = false;

		if (is_object)
		{
			key = bson_iterator_key(&i);

			appendStringInfo(buf, "\"%s\"", key);
			appendStringInfoChar(buf, ':');
		}
		switch (t)
		{
			case BSON_DOUBLE:
				appendStringInfo(buf, "%f", bson_iterator_double(&i));
				break;
			case BSON_STRING:
				str = quote_string(bson_iterator_string(&i));
				appendStringInfoString(buf, str);
				break;
			case BSON_OID:
				bson_oid_to_string(bson_iterator_oid(&i), oidhex);
				str = quote_string(oidhex);
				appendStringInfoString(buf, str);
				break;
			case BSON_BOOL:
				appendStringInfoString(buf, bson_iterator_bool(&i) ? "true" : "false");
				break;
			case BSON_INT:
				appendStringInfo(buf, "%d", bson_iterator_int(&i));
				break;
			case BSON_LONG:
				appendStringInfo(buf, "%lld", (uint64_t) bson_iterator_long(&i));
				break;
			case BSON_NULL:
				appendStringInfoString(buf, "null");
				break;
			case BSON_OBJECT:
				appendStringInfoChar(buf, '{');
				bson_to_json_recurse(buf, bson_iterator_value(&i), true);
				appendStringInfoChar(buf, '}');
				break;
			case BSON_ARRAY:
				appendStringInfoChar(buf, '[');
				bson_to_json_recurse(buf, bson_iterator_value(&i), false);
				appendStringInfoChar(buf, ']');
				break;
			case BSON_DATE:
			case BSON_TIMESTAMP:
			case BSON_SYMBOL:
			case BSON_BINDATA:
			case BSON_UNDEFINED:
			case BSON_REGEX:
			case BSON_CODE:
			case BSON_CODEWSCOPE:
				ereport(ERROR,
						(errmsg("unsupported bson type: %d", t)));
				break;
			default:
				elog(ERROR, "unknown bson type: %d", t);
				break;
		}
	}

	return buf->data;
}
Example #23
0
void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ) {
    bson_iterator_from_buffer( sub, bson_iterator_value( i ) );
}
Example #24
0
char* Namespace::bson_to_json(char* val, const char *data, int depth,
		bool isObject, int bsonSize, int &jsonSize, int &cursor)
{

	char *object;

	int bufSize;

	char keybuf[512];
	char buf[512];
	int c = 0;

	bson_iterator i;
	const char *key;
	int temp;
	bson_timestamp_t ts;
	char oidhex[25];
	bson scope;
	bson_iterator_from_buffer(&i, data);
	//bson_iterator_init(&i, data);

	if (isObject)
	{
		memcpy(&val[cursor], "{", 1);
		cursor += 1;
	}
	else
	{
		memcpy(&val[cursor], "[", 1);
		cursor += 1;
	}

	while (bson_iterator_next(&i) && i.progress < bsonSize)
	{
		if (c > 0)
		{
			memcpy(&val[cursor], ",", 1);
			cursor += 1;
		}
		c++;

		bson_type t = bson_iterator_type(&i);
		if (t == 0)
		{
			break;
		}
		key = bson_iterator_key(&i);

		if (key[0] != '\0')
		{
			sprintf(keybuf, "\"%s\":", key);
			key = &keybuf[0];
		}

		switch (t)
		{
		case BSON_DOUBLE:
			sprintf(buf, "%s%f", key, bson_iterator_double(&i));
			bufSize = strlen(buf);
			memcpy(&val[cursor], buf, bufSize);
			cursor += bufSize;
			break;
		case BSON_STRING:
			sprintf(buf, "%s\"%s\"", key, bson_iterator_string(&i));
			bufSize = strlen(buf);
			memcpy(&val[cursor], buf, bufSize);
			cursor += bufSize;
			break;
		case BSON_OID:
			bson_oid_to_string(bson_iterator_oid(&i), oidhex);
			sprintf(buf, "%s\"%s\"", key, oidhex);
			bufSize = strlen(buf);
			memcpy(&val[cursor], buf, bufSize);
			cursor += bufSize;
			break;
		case BSON_BOOL:
			sprintf(buf, "%s%s", key,
					bson_iterator_bool(&i) ? "true" : "false");
			bufSize = strlen(buf);
			memcpy(&val[cursor], buf, bufSize);
			cursor += bufSize;
			break;
		case BSON_NULL:
			sprintf(buf, "%snull", key);
			bufSize = strlen(buf);
			memcpy(&val[cursor], buf, bufSize);
			cursor += bufSize;
			break;
		case BSON_INT:
			sprintf(buf, "%s%d", key, bson_iterator_int(&i));
			bufSize = strlen(buf);
			memcpy(&val[cursor], buf, bufSize);
			cursor += bufSize;
			break;
		case BSON_OBJECT:
			sprintf(buf, "%s\0", key);
			memcpy(&val[cursor], buf, strlen(buf));
			cursor += strlen(buf);

			object = bson_to_json(val, bson_iterator_value(&i), depth + 1, true,
					bsonSize - cursor, jsonSize, cursor);

			break;
		case BSON_ARRAY:
			sprintf(buf, "%s", key);
			bufSize = strlen(buf);
			memcpy(&val[cursor], buf, bufSize);
			cursor += bufSize;

			object = bson_to_json(val, bson_iterator_value(&i), depth + 1,
					false, bsonSize - cursor, jsonSize, cursor);

			break;
		default:
			LOGE("can't print type");
			FORCE_LOG_INT("type: ", t);

			memcpy(&val[cursor], "}", 1);
			cursor += 1;
			jsonSize = cursor;
			return val;
		}
	}

	if (isObject)
	{
		memcpy(&val[cursor], "}", 1);
		cursor += 1;
		memcpy(&val[cursor], "\0", 1);

	}
	else
	{
		memcpy(&val[cursor], "]", 1);
		cursor += 1;
	}

	jsonSize = cursor;
	return val;
}