LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-21-2012, 03:08 AM   #1
vectrum
Member
 
Registered: Jul 2011
Posts: 59

Rep: Reputation: Disabled
GDBM firstkey and nextkey can't fetch records in the same order as were inserted


Is it a feature of GDBM that gdbm_firstkey() and gdbm_nextkey() fetches records not in proper order? Is it possible to extract the records in the order as we insert them? It gets the record this way; (Dev-C++ with mingw on win 2003)DB created. DB opened. [I][B]Here is the output:
Quote:

Storing 1 One
Storing 2 Two
Storing 3 Three
Storing 4 Four
done.
3 Three
2 Two
1 One
4 Four
And here is my code;
Quote:
#include<stdio.h>
#include<gdbm.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
GDBM_FILE dbf;
int i, ret;

char *keyval[] = {"1","2","3","4"};
char *dataval[] = {
"One",
"Two",
"Three",
"Four",
};

datum key, data, tkey;

dbf = gdbm_open("arraydb", 512, GDBM_NEWDB,0666, NULL);
if(dbf == NULL){
printf("ERROR: Could not open the DB file.\n");
exit(1);
} else {
printf("DB created.\n");
printf("DB opened.\n");
}

for(i = 0; i<4; ++i){
key.dsize = strlen(keyval[i]) + 1;
key.dptr = keyval[i];
data.dsize = strlen(dataval[i]) + 1;
data.dptr = dataval[i];

if((ret = gdbm_store (dbf, key, data, GDBM_INSERT)) != 0){
printf("Return values is %d : Storing failed...\n",ret);
exit(2);
}
printf ("Storing %s %s \n",key.dptr, data.dptr);

}
gdbm_close (dbf);
printf ("done.\n");

dbf = gdbm_open("arraydb", 0, GDBM_READER, 0644, 0);
if (!dbf)
{
fprintf (stderr, "Database %s either doesn't exist or is not a gdbm file.\n", "arraydb");
exit (3);
}
for(i=1,key=gdbm_firstkey(dbf); key.dptr!=NULL; tkey=key,key=gdbm_nextkey(dbf, key),free(tkey.dptr),i++){
//for(key = gdbm_firstkey(dbf); data.dptr; key = gdbm_nextkey(dbf,key)){

data = gdbm_fetch(dbf, key);

if (data.dsize > 0) {
printf ("%s %s\n", key.dptr,data.dptr);
//free (data.dptr);

} else {
printf ("Key %s not found.\n", key.dptr);
}
}

gdbm_close (dbf);
system("pause");

return 0;
}
I think gdbm_store() uses malloc() so it allocates
memory at random and hash the keys and data but I'm
not sure. Thank you
 
Old 10-22-2012, 02:47 AM   #2
aetratus
LQ Newbie
 
Registered: Jul 2006
Location: Internet
Distribution: Fedora, CentOS, Archlinux
Posts: 27

Rep: Reputation: 6
Your answer is here: http://www.gnu.org.ua/software/gdbm/manual/gdbm.txt
 
1 members found this post helpful.
Old 10-22-2012, 03:41 AM   #3
vectrum
Member
 
Registered: Jul 2011
Posts: 59

Original Poster
Rep: Reputation: Disabled
Thank you for your tip but it would be better if you've referred to the exact position of the gnu gdbm manual where it's been mentioned. I think the following is the point you want me to understand;
From gdbm manual:
Quote:
9. Sequential access to records.
Quote:
The next two functions allow for accessing all items in the database. This access is not key sequential, but it is guaranteed to visit every key in the database once. The order has to do with the hash values. gdbm_firstkey starts the visit of all keys in the database. gdbm_nextkey finds and reads the next entry in the hash structure for dbf.

Last edited by vectrum; 10-24-2012 at 07:53 AM.
 
Old 10-23-2012, 02:46 AM   #4
aetratus
LQ Newbie
 
Registered: Jul 2006
Location: Internet
Distribution: Fedora, CentOS, Archlinux
Posts: 27

Rep: Reputation: 6
Yes, and my apology for not quoting it directly, it sort of jumped out at me and I had a bad moment between not wanting to presume you'd read it nor appear condescending! (shoots foot)
 
Old 10-23-2012, 09:30 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,935

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Never used gdbm yet, but this sounds wrong: there is no way the process the records sequentially -- in the sequence of the keys, I mean?
 
Old 10-24-2012, 08:07 AM   #6
vectrum
Member
 
Registered: Jul 2011
Posts: 59

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by aetratus View Post
Yes, and my apology for not quoting it directly, it sort of jumped out at me and I had a bad moment between not wanting to presume you'd read it nor appear condescending! (shoots foot)
Please don't make me embarrass. You've done a great job 'case I have had to find myself and the effort I made will help me, (as the old adage says; we remember a for longer period what I learn in harder way or something or something or...).
Thanks again.

Last edited by vectrum; 10-24-2012 at 08:08 AM.
 
Old 10-24-2012, 08:16 AM   #7
vectrum
Member
 
Registered: Jul 2011
Posts: 59

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Never used gdbm yet, but this sounds wrong: there is no way the process the records sequentially -- in the sequence of the keys, I mean?
Yes, I thought firstkey() pointed to the record that had been
inserted at first so, in that case record number one would have come first. But I didn't realized that gdbm database is a hash table(I assume it is and I don't know how hashing algo works).

Thank you.
 
  


Reply

Tags
gdm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] MX Records / A Records / CNAME Records - Advice Please fusion1275 Linux - Newbie 15 01-18-2011 05:06 AM
Distinguish between gdbm 64b to gdbm 32b in rpm spec file. byaron Red Hat 1 10-13-2010 07:35 PM
mplayer fetch subtitles option: --fetch-subs frenchn00b Linux - General 1 11-10-2009 12:21 AM
Reading caller detail records(CDR ) records eliufoo Linux - Networking 2 12-17-2008 12:31 AM
GDBM Need for zinf install... What's GDBM??? Stevetgn Linux - Software 5 07-16-2004 07:00 AM


All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration