Querying Mongodb Key and Value using C driver - mongodb

mongo_cursor *cursor=mongo_find(conn,TEST_NS,query,NULL,0,0,0);
count_matched=0;
bson *doc;
while(mongo_cursor_next(cursor)==MONGO_OK)
{
count_matched++;
doc=(bson *)mongo_cursor_bson(cursor);
bson_iterator_init(&it,doc);
while(bson_iterator_next(&it) != BSON_EOO)
{
fprintf(stderr,"%s : %s\n\n",bson_iterator_key(&it),bson_iterator_string(&it));
}
}
This code is working perfectly and i can see the matched documents (Key + Value) but now i want to save the matched document's key and value to a string. Can any tell me how i can save the return value of key and value in to a string?
One document includes (all strings)
Total Key=10
Total value=10
and i want to save 10 document's key and value at one time. I am using C driver of mongodb.

The following code shows how you would be doing copy of the key and values from the bson iterator into your key-value arrays temp_key and temp_value. The specific block of code is in between the comments marked START and END.
Additionally, you can find documentation for accessing BSON document contents at http://api.mongodb.org/c/current/bson.html .
mongo_cursor *cursor = mongo_find(&conn, TEST_NS, &query, NULL, 0, 0, 0);
int count_matched = 0;
bson *doc;
// Assuming you are just looking for 100 key / value pair of max length of 99 characters
const unsigned KV_ARRAY_LENGTH = 100;
const unsigned MAX_KV_LENGTH = 105;
char temp_key[KV_ARRAY_LENGTH][MAX_KV_LENGTH + 1], temp_value[KV_ARRAY_LENGTH][MAX_KV_LENGTH + 1];
int i = 0;
while (mongo_cursor_next(cursor) == MONGO_OK) {
count_matched++;
doc=(bson *)mongo_cursor_bson(cursor);
bson_iterator it;
bson_iterator_init(&it,doc);
while (bson_iterator_next(&it) != BSON_EOO) {
fprintf(stderr,"%s : %s\n", bson_iterator_key(&it), bson_iterator_string(&it));
/******* START - Code to capture key-value into appropriate array */
if (i < KV_ARRAY_LENGTH) {
/* - Collect key-value pairs only if there is space in the array
* - Key / Value woud be captured only till the max amount of space available for them i.e. MAX_KV_LENGTH in this case
* */
strncpy(temp_key[i], bson_iterator_key(&it), MAX_KV_LENGTH);
strncpy(temp_value[i], bson_iterator_string(&it), MAX_KV_LENGTH);
temp_key[i][MAX_KV_LENGTH] = temp_value[i][MAX_KV_LENGTH] = '\0';
++i;
} else {
/* whatever need to be done if there is no room in the array */
}
/******* END - Code to capture key-value into appropriate array */
}
}
/* Test iterating through the key-value pair constructed in query iteration */
fprintf(stdout, "--- Fields collected ---\n");
int keyIndex = 0;
for ( ; keyIndex < i; ++keyIndex) {
fprintf(stdout, "{key: %s, value: %s}\n", temp_key[keyIndex], temp_value[keyIndex]);
}

mongo_cursor *cursor=mongo_find(conn,TEST_NS,query,NULL,0,0,0);
count_matched=0;
bson *doc;
//Answer
const char* temp_key[100][100],temp_value[100][100];
int i=0;
while(mongo_cursor_next(cursor)==MONGO_OK)
{
count_matched++;
doc=(bson *)mongo_cursor_bson(cursor);
bson_iterator_init(&it,doc);
while(bson_iterator_next(&it) != BSON_EOO)
{
fprintf(stderr,"%s : %s\n\n",bson_iterator_key(&it),bson_iterator_string(&it));
temp[i][0]=bson_iterator_key[&it]; //Answer
temp_value[i][0]=bson_iterator_key[&it]; //Answer
i++; //Answer
}
}
Just for the record, this is the rough sketch and i know about corruption of the temp variables and their overflow but i will remove it according to my code.

Related

Xilinx Echo Server Data Variable

I want to have my Zedboard return a numeric value using the Xilinx lwIP example as a base but no matter what I do I can't figure out what stores the data received or transmitted.
I have found the void type payload but I don't know what to do with it.
Snapshot of one instance of payload and a list of lwIP files
Below is the closest function to my goal:
err_t recv_callback(void *arg, struct tcp_pcb *tpcb,
struct pbuf *p, err_t err){
/* do not read the packet if we are not in ESTABLISHED state */
if (!p) {
tcp_close(tpcb);
tcp_recv(tpcb, NULL);
return ERR_OK;
}
/* indicate that the packet has been received */
tcp_recved(tpcb, p->len);
/* echo back the payload */
/* in this case, we assume that the payload is < TCP_SND_BUF */
if (tcp_sndbuf(tpcb) > p->len) {
err = tcp_write(tpcb, p->payload, p->len, 1);
//I need to change p->paylod but IDK where it is given a value.
} else
xil_printf("no space in tcp_sndbuf\n\r");
/* free the received pbuf */
pbuf_free(p);
return ERR_OK;
}
Any guidance is appreciated.
Thanks,
Turtlemii
-I cheated and just made sure that the function has access to Global_tpcb from echo.c
-tcp_write() reads in an address and displays each char it seems.
void Print_Code()
{
/* Prepare for TRANSMISSION */
char header[] = "\rSwitch: 1 2 3 4 5 6 7 8\n\r"; //header text
char data_t[] = " \n\r\r"; //area for storing the
data
unsigned char mask = 10000000; //mask to decode switches
swc_value = XGpio_DiscreteRead(&SWCInst, 1); //Save switch values
/* Write switch values to the LEDs for visual. */
XGpio_DiscreteWrite(&LEDInst, LED_CHANNEL, swc_value);
for (int i =0; i<=7; i++) //load data_t with switch values (0/1)
{
data_t[8+2*i] = '0' + ((swc_value & mask)/mask); //convert one bit to 0/1
mask = mask >> 1;//move to next bit
}
int len_header = *(&header + 1) - header; //find the length of the
header string
int len_data = *(&data_t + 1) - data_t; //find the length of the data string
tcp_write(Global_tpcb, &header, len_header, 1); //print the header
tcp_write(Global_tpcb, &data_t, len_data, 1); //print the data
}

CS50 pset 3: Tideman sort_pairs function

I need some assistance in understanding the logic behind this function. This is my current sort_pairs function in Tideman:
// Sort pairs in decreasing order by the strength of victory
void sort_pairs(void)
{
qsort(pairs, pair_count, sizeof(pair), compare);
return;
}
// Function for sort_pairs
int compare(const void *a, const void *b)
{
const pair *p1 = (const pair *) a;
const pair *p2 = (const pair *) b;
if (p1->winner < p2->winner)
{
return -1;
}
else if (p1->winner > p2->winner)
{
return 1;
}
else
{
return 0;
}
}
This does not clear check50 and I looked online to find how to approach this problem. It seems that most functions compare the values from the preferences array instead (eg preferences[pairs[i].winner][pairs[i].loser]) . My previous functions vote, record_preferences, and add_pairs all clear check50. I have not advanced beyond sort_pairs yet.
Why can't I compare the strength of victory directly from the pairs array instead since I already have the data stored there?
You don't need to make this so complex, you can use your own sorting here. Let's try a simple insertion sort-
void sort_pairs()
{
pair temp;
for (int i = 1, j; i < pair_count; i++)
{
temp = pairs[i];
j = i - 1;
for (; j >= 0 && preferences[pairs[j].winner][pairs[j].loser] < preferences[temp.winner][temp.loser]; j--)
{
pairs[j + 1] = pairs[j];
}
pairs[j + 1] = temp;
}
}
The pair struct looks like-
typedef struct
{
int winner;
int loser;
}
pair;
Explanation:-
We go through each pair of elements inside the pairs array - starting at 1 since I'm going to compare with the previous element (j = i - 1)
Now we check all the previous elements from the current element and compare them with the key - preferences[pairs[INDEX].winner][pairs[INDEX].loser]
This is the key you should be sorting by. preferences[WINNER_ID][LOSER_ID] means the amount of people that prefer WINNER_ID over LOSER_ID.
And that's pretty much it!, it's simply a insertion sort but the key is the important part.

how can this Repeated string concatenation function

using UnityEngine;
using System.Collections;
public class NewMonoBehaviour1 : MonoBehaviour
{
void ConcatExample(int[] intArray)
{
string line = intArray[0].ToString(); // the line is the var of the first in array
for(i =1;i <intArray.Length; i++) // the length is unknown ?
{
line += ", " + intArray[i].ToString(); //
}
return line;
//each time allocate new in original place
}
}
How can this function work ? the length of array is unknown , so how the for loop works ?Besides, this is void function but shouldn't return anythings right ,or is there any exceptional case ,finally,according to the unity manual, it is said that the function will keep producing a string but with new contents in the same place , resulting in consuming large memory space .Why ?thx
What makes you think that the Length should be unknown? It is a property that any array simply has
Gets the total number of elements in all the dimensions of the Array.
Of course it is not unknown the moment you call your method with an according parameter!
The return line; will not even compile since as you say the method is of type void so it can not return anything. It should probably be private string ConcatExample
Then what the unity manual (don't know where exactly you read this) means lies in
line += ", " + intArray[i].ToString();
under the hood every string in c# is an immutable char[]. So everytime you do a string concatenation via stringC = stringA + stringB what happens under the hood is basically something similar to
char[] stringC = new char[stringA.Length + stringB.Length];
for(var iA = 0; iA < stringA.Length; iA++)
{
stringC[i] = stringA[i];
}
for(var iB = 0; iB < stringB.Length; iB++)
{
stringC[iB + stringA.Length] = stringB[iB];
}
so whenever dealing with loops especially with large data it is strongly recommended to rather use a StringBuilder like
private string ConcatExample(int[] intArray)
{
var builder = new StringBuilder(intArray[0]);
for(i =1; i < intArray.Length; i++)
{
builder.Append(", ").Append(intArray[i].ToString());
}
return builder.ToString();
}
The length of the array will be the length of the array of ints you pass into the function as an argument.
say you pass it
Int[] ints = {1,2,3}
ConcatExample(ints); //the length of the array is now 3
add a debug.log() function to the ConcatExample method
void ConcatExample(int[] intArray)
{
string line = intArray[0].ToString();
for (int i = 1; i < intArray.Length; i++)
{
line += ", " + intArray[i].ToString(); //
Debug.Log(line);
}
}
debug.log would produce the following in the console
1, 2
1, 2, 3
and finally the return line; at the end would just result in an error because yes you are correct void returns nothing
This function CANNOT work, unless it gets the data it expects. A NULL passed to this function, for example, would generate a runtime null-reference exception. Passing a valid integer array, of length zero would generate an invalid index error on the first line.
You are correct, the function returns nothing, and appears pointless. In fact, I would have expected return line; to generate a complier error.
The string type appears "dynamic" meaning, it will indeed allocate more and more memory as needed. Technically, it is actually the string "+" operator, (a function that takes two strings as parameters) that is allocating this space. This function returns a new string, of the appropriate size. The garbage collector will DEallocate "old" strings when they are no longer referenced by any variables.

Losing values with iterative realloc in C

I am working in C with Netbeans8.0
I have to read files in an iterative approach to get list of words. That is, in single iteration a file is read into an array of strings and then merge this array into a single array.
void merge_array(char** a,int* M, char** b,int N)
{
//............. Add extra memory to a ..............*/
void *tmp = realloc(a, (*M+N) * sizeof(*a));
if (tmp == NULL)
{
perror("Merging -> Could not reallocate");
exit(EXIT_FAILURE);
}
a = tmp;
memset(a+(*M), 0, N*sizeof(*a));
//............. copy strings in b to a ..............*/
int i,j=0;
for(i=*M; i<((*M)+N); i++)
{
size_t wlen = strlen(b[j]);
a[i] = malloc((wlen+1) * sizeof(char));
if (a[i] == NULL)
{
perror("Failed to replicate string");
exit(EXIT_FAILURE);
}
memcpy(a[i], b[j], wlen+1);
j++;
}
(*M) = (*M)+N; // resetting the count
printf("Confirm - %s, %d\n",a[0],*M);
}
Above function reads the contents of a file. In main above function is called iteratively and merged into a single array named 'termlist'. Main code is given below
char** termlist;
int termCount=0;
while(files[i]){
char **word_array;
int wdCnt,a;
char* tmp = (char*) malloc(strlen(path)*sizeof(char));
strcpy(tmp,path); strcat(tmp,files[i]); strcpy(files[i],tmp);
printf("\n\n******* Reading file %s...\n",files[i]);
word_array = getTerms_fscanf(files[i],&a); //reading contents of file
wdCnt = a;
if(i==0) // before reading the first file initializing the termlist
{
termlist = (char**) malloc(wdCnt*sizeof(char*));
}
merge_array(termlist,&termCount,word_array,wdCnt);
printf("CHECK - %s, %d\n",termlist[0],termCount);
free(word_array);
++i;
}
Now the problem is that,
After 1st two iterations, Inside function everything works fine but in main values of termlist[0], termlist[1] turns out to be junk.. That is first 2 words read from first file is lost. The 3rd iteration returns with failure at merge_array function call.
Output is
******* Reading F:/Netbeans C/Test Docs/doc1.txt...
Confirm - tour, 52
CHECK - tour, 52
******* Reading F:/Netbeans C/Test Docs/doc2.txt...
Confirm - tour, 71
CHECK - Ôk'aÔk'a`œ€`œ€äk'aäk'aìk'aìk'aôk'aôk'aük'aük'ah“€, 71
I am not able to identify problem with this.. Please help with this..

MongoDB: How can I add a new hash field directly from the console?

I have objects like:
{ "_id" : ObjectId( "4e00e83608146e71e6edba81" ),
....
"text" : "Text now exists in the database"}
and I can add hash fields through java using the com.mongodb.util.Hash.longHash method to create
{ "_id" : ObjectId( "4e00e83608146e71e6edba81" ),
....
"text" : "Text now exists in the database",
"tHash" : -4375633875013353634 }
But this is quite slow. I would like to be able to do something within the database like:
db.foo.find( {} ).forEach( function (x) {
x.tHash = someFunction(x.text); // create a long hash compatible with com.mongodb.util.Hash.longHash
db.foo.save(x);
});
Does anyone know how I can call this long hash within the Javascript function?
First define a nice hashCode function to use. JavaScript does not have a hashCode function by default on all objects so you will need to write one. Or just use this one:
var hashCode = function(s) {
if (s == null) return 0;
if (s.length == 0) return 1;
var hash = 0;
for (var i = 0; i < s.length; i++) {
hash = ((hash << 5) - hash) + s.charCodeAt(i);
hash = hash & hash; // Convert to 32bit integer
}
return hash;
};
Alternatively use another hash function like MD5 - there are scripts that can generate them for you.
I gave up trying to replicate the Mongo Java driver Hash.longHash method in Javascript, since JS treats everything as a float and doesn't handle the overflow like Java does. I found some examples of replicating the Java hashCode function in JS and so I did this:
longHash = function(s){
var hash = 0;
if (s.length == 0) return hash;
for (i = 0; i < s.length; i++) {
char = s.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return NumberInt(hash);
};
db.foo.find( {} ).forEach( function (x) {
x.cHash = longHash(x.c);
db.foo.save(x);
});
which at least let me do a integer level hash code on the existing data. This will be enough to narrow down data for indexing.
Update: I just updated with by returning a NumberInt type instead. By default the hash was a Javascript number and was stored in Mongo as a Double taking much more space than necessary. The NumberInt is a 32-bit signed integer, and NumberLong is a 64-bit version.