/* Our polling thread */ gpointer apollo_orbiter(gpointer gp_queue) { FinishedNotify * pn = gp_queue; g_async_queue_ref(pn->queue); while(TRUE) { g_print("\n-----------\n\n"); gpointer thread_data = g_async_queue_pop(pn->queue); if(thread_data == THREAD_TERMINATOR) { g_printerr("\n-- Terminating --\n"); break; } else { GlyrQuery * q = thread_data; GlyrMemCache * head = glyr_get(q,NULL,NULL); if(head != NULL) { g_print("//////// ITEM %d ////////\n",++pn->counter); glyr_cache_print(head); glyr_free_list(head); g_print("/////////////////////////\n"); } glyr_query_destroy(q); } } g_async_queue_unref(pn->queue); return NULL; }
END_TEST //-------------------- START_TEST (test_sorted_rating) { const int N = 10; GlyrDatabase * db = setup_db(); GlyrQuery q; glyr_query_init (&q); setup (&q,GLYR_GET_LYRICS,N); for (int i = 0; i < N; ++i) { int rate = (i / 2) + 1; GlyrMemCache * ct = glyr_cache_new(); fail_if (ct == NULL); glyr_cache_set_data (ct,g_strdup_printf ("MyLyrics %d",i),-1); ct->dsrc = g_strdup ("http://MyLyrics.com"); ct->rating = rate; glyr_db_insert (db,&q,ct); glyr_cache_free (ct); } fail_unless (count_db_items (db) == N); GlyrMemCache * list = glyr_db_lookup (db,&q); GlyrMemCache * iter = list; fail_if (list == NULL); double last_timestamp = DBL_MAX; int last_rating = INT_MAX; while (iter) { glyr_cache_print (iter); fail_unless (last_rating >= iter->rating); if (last_rating == iter->rating) fail_unless (last_timestamp >= iter->timestamp); last_timestamp = iter->timestamp; last_rating = iter->rating; iter = iter->next; } glyr_free_list (list); }
static int foreach_callback(GlyrQuery *q, GlyrMemCache *item, void *userptr) { GlyrDatabase *db = (GlyrDatabase *) userptr; g_assert(item); g_assert(db); if(item->rating == -1) { g_printerr("----------------\n"); glyr_cache_print(item); if(do_delete) { glyr_db_delete(db, q); } } return 0; }
int main (void) { /* Init this thing, the only two methods not being threadsafe */ glyr_init(); /* Also clear ressources on exit */ atexit (glyr_cleanup); /* This struct is used to store all settings you do via glyr_opt_* */ GlyrQuery q; /* We also should set it to standard values */ glyr_query_init (&q); /* We want lyrics, well, I want. */ glyr_opt_type (&q,GLYR_GET_LYRICS); /* Set random artist/title - you could also omit the album line */ glyr_opt_artist (&q, (char*) "Die Ärzte"); glyr_opt_album (&q, (char*) "Die Bestie in Menschengestalt"); glyr_opt_title (&q, (char*) "FaFaFa"); /* If any error occured it will be saved here, or GLYRE_OK */ /* You could have also passed a NULL pointer to glyr_get() if you're not interested in this */ GLYR_ERROR err; /* Now get the job done! The 3rd */ GlyrMemCache * head = glyr_get (&q,&err,NULL); /* The results are stored in the GlyrMemCache struct - you are most likely only interested in the fields data, size and type*/ if (head != NULL) { /* head contains also a pointer to the next element, you can use it therefore as linkedlist */ // puts(head->data); glyr_cache_print (head); /* We have to free it again also, you can pass ANY pointer of the list, it works in both directions */ glyr_free_list (head); } /* glyr_query_init may allocate memory - free it. */ glyr_query_destroy (&q); return err; }
static void print_item(GlyrMemCache *cacheditem, int num) { fprintf(stderr, "\n------- ITEM #%d --------\n", num); glyr_cache_print(cacheditem); fprintf(stderr, "\n------------------------\n"); }