954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Associative array with libJudy

Hey guys. Trying to implement associative array with the help of libJudy. Here's small test, that doesn't work. Please, help me to find, where i did wrong?

#include <assert.h>
#include <Judy.h>

typedef struct {
	char *hash;
	int id;
	int views;
} Watch_t;

Pvoid_t dict = (Pvoid_t) NULL;
PWord_t rcw;

char idx[12];

void init_track(Watch_t *track) {
	assert(track);
	track->id = 0; track->views = 0;
}

int set_to_storage(const unsigned char *key, Watch_t *val) {
	PWord_t PV;

	JSLI(PV, dict, key);
	if (PV) {
		PV = (PWord_t) val;
		return 0;
	} else {
		return 1;
	}
}

Watch_t *get_from_storage(const unsigned char *key) {
	PWord_t PV;

	JSLG(PV, dict, key);

	if (PV) {
        	return ((Watch_t *) PV);
	} else {
		return NULL;
	}
}

int main(int argc, char **argv) {
	char *hash = "t8xr5nk8jczy";
	Watch_t *tr; Watch_t *track;
	int bool;

	track = (Watch_t *) malloc( sizeof(Watch_t) );
	init_track(track);

	track->hash = "t8xr5nk8jczy";
	track->id = 1; track->views = 16;

	bool = set_to_storage((unsigned char *) hash, track);

	tr = get_from_storage((unsigned char *) hash);
	assert(tr);

	fprintf(stderr, "Debug: hash=%s, track views=%d\n", tr->hash, tr->views);

    return 0;
}


Working under ubuntu 9.04 and compiler flags is:

gcc -Wall -lJudy test.c -o test


trying to run it with gdb returned no errors.
don't mention unused args. i didn't put here other voids, that are not important at the moment.

Oh, yeah. forgot to say, the wrong is that i couldn't get my data back from dict.

Quessir
Newbie Poster
6 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

who or what is libJudy? (don't tell me its a library because I already guessed that :) )

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Here u can find docs, examples, etc.
http://judy.sourceforge.net/

Quessir
Newbie Poster
6 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

why don't you just ask their support group? Its unlikely anyone here will know anything about that library, but then I could be wrong about that.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Because, i think, there is not the problem in libJudy. There's something with pointers or so.

Quessir
Newbie Poster
6 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

Did you try using track without pointers?

Watch_t *tr; Watch_t track;
	int bool;

	init_track( &track );

	track.hash = "t8xr5nk8jczy";
	track.id = 1; track.views = 16;
// etc etc
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Yes, but it doesn't change the result...

Quessir
Newbie Poster
6 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

In that case its not a pointer problem because you removed all pointers. So I have no clue what's wrong.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You