UnQLite Users Forum

unqlite_kv_cursor_key() return iData length of maximum value 8

append delete mactavish

unqlite_kv_cursor_key(...) as well as unqlite_kv_cursor_data(...) always returns a data of maximum size 8. rest of the characters if present appears as unreadable text. Is there any limit as to how much length of iData can be, or is it set to fetch a maximum length of 8 ?

Reply RSS

Replies

append delete #1. chm

Could you provide any code snippet on how you obtained this result. It seems something (probably the cursors) not properly initialized.

append delete #2. mactavish

My value is a single digit. It gets printed correctly. But key is 9 characters long. So after the 8th character garbage value is printed.

Sample <key, value> pair : <1234phy_s, 4>

unqlite_kv_fetch() is working perfectly fine, so there is no error in storing the values, i.e., unqlite_kv_store()

Code snippet :

// taking all data out together

returnCode = unqlite_kv_cursor_init(dbHandle, &dbCursor);

if (returnCode != UNQLITE_OK) {

printf("Database cursor initialization error\n");

unqlite_close(dbHandle);

return KV_CURSOR_INIT_ERROR;

}

printf("starting to fetch data : \n");

int i;

for (i = 0, unqlite_kv_cursor_first_entry(dbCursor); unqlite_kv_cursor_valid_entry(dbCursor); unqlite_kv_cursor_next_entry(dbCursor), i++) {

char *key;
char *value;

char key_ext[5];
char value_ext[2];

unqlite_int64 keyLen;

unqlite_int64 dataLen;

//int keyLen;
//int dataLen;

// Fetching length of key

unqlite_kv_cursor_key(dbCursor, NULL, &keyLen);

printf("key length %u\n", keyLen);

key = malloc(sizeof(char) * 12);

*// gives output of 8.... ->*

memset(key, 0, sizeof(key));

unqlite_kv_cursor_key(dbCursor, key, &keyLen);


*//Erroneous output...... ->*

printf("\nKey = %s\n", key);

key_ext[0] = key[0];
key_ext[1] = key[1];
key_ext[2] = key[2];
key_ext[3] = key[3];
//key_ext[4] = key[4];
//key_ext[5] = key[5];
key_ext[6] = '\0';

printf("key extracted = %s\n", key_ext);

int key_int;

key_int = atoi(key_ext);

printf("key integer format = %d\n", key_int);

int cid, srid, rid;

cid = key_int%100;
key_int /= 100;

srid = key_int % 10;
key_int /= 10;

rid = key_int;

printf("rack id = %d, sub rack id = %d, card id = %d\n", rid, srid, cid);

unqlite_kv_cursor_data(dbCursor, NULL, &dataLen);

*// gives output of 8.... ->*

printf("value length %u\n", dataLen);

value = malloc(sizeof(char) * 12);

memset(value, 0, sizeof(value));

unqlite_kv_cursor_data(dbCursor, value, &dataLen);

* //Erroneous output...... if value exceeds length > 8 .....->*

printf("\nValue = %s\n", value);

value_ext[0] = value[0];
value_ext[1] = '\0';

printf("value extracted = %s\n", value_ext);

int value_int;
value_int = atoi(value_ext);

printf ("value in integer format = %d\n", value_int);

}

append delete #3. chm

The way you allocate memory to store the key and its associated value is completely wrong. This is a newbie error. You can't zero a dynamically allocated memory chunk like you did:

%C
unqlite_kv_cursor_key(dbCursor, NULL, &keyLen); 
 key = malloc(sizeof(char) * 12);

*// gives output of 8.... ->*

memset(key, 0, sizeof(key)); 
---------------
 value = malloc(sizeof(char) * 12);

memset(value, 0, sizeof(value)); 

%

The correct way to do it is as follows:

%C

unqlite_kv_cursor_key(dbCursor, NULL, &keyLen); 

/* Allocate a buffer big enough to hold the entire output */
key = malloc(keyLen); //Use keyLen only
memset(key, 0, keyLen); 

/* Copy the data */
unqlite_kv_cursor_key(dbCursor, key, &keyLen); 
%
append delete #4. Atul

This post was deleted by its owner

Reply

(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: chm_at_symisc, devel_at_symisc