Exemplo n.º 1
0
int main(){
	
	printf("ok, it is done\n");
	lru_cache_t * lru;
	hash_map_t * hash_map;
	int res = hash_map_init(&hash_map, &simple_hash_function, &key_cmp_cbf, &key_free_cbf, & key_clone_cbf);
	printf("hash init result: %d\n", res);
	int rest = lru_cache_init(&lru, &simple_hash_function, &key_cmp_cbf, &key_free_cbf, &key_clone_cbf);
	printf("lru init result: %d\n", rest);
	lru_dump(lru, &value_to_string, &key_to_string);
	int i = 1;
	test_key_t key;
	test_value_t value;
	test_value_t * vp;
	int ret = 0;
	while(i){
		printf("0: quit\n1: insert\n2: lookup\n3: delete\nyour choice:");
		scanf("%d", &i);
		switch(i){
		case 1:
			printf("key:");
			scanf("%19s", key.key);
			printf("got key: %s\nvalue:", key.key);
			scanf("%19s", value.value);
			if(value.value[0] == '0') i=0;
			ret = lru_cache_insert(lru, (const void *)(&key), (const void *)(&value), &value_clone_cbf, &value_free_cbf); 
			if(!ret){
				printf("insert (%s, %s) failed", key.key, value.value);
			}
			break;
		case 3:
			ret = lru_delete_auto(lru);
			if(!ret){
				printf("delete failed\n");
			}
			break;
		case 2:
			printf("key:");
			scanf("%19s", key.key);
			ret = lru_cache_get(lru, (const void *) &key, (void **)(&vp));
			if(!ret || !vp){
				printf("key does not exist\n");
			}
			else{
				printf("key=%s, value=%s\n", key_to_string((const void *)&key), value_to_string((const void *)vp));
			}
			break;
		default:
			i = 0;
			break;
		}
		lru_dump(lru, value_to_string, key_to_string);
	}
	printf("quit cache; test hash map");
	i = 1;
	while(i){
		printf("0: quit\n1: insert\n2: lookup\n3: delete\nyour choice:");
		scanf("%d", &i);
		switch(i){
		case 1:
			printf("key:");
			scanf("%19s", key.key);
			printf("got key: %s\nvalue:", key.key);
			scanf("%19s", value.value);
			if(value.value[0] == '0') i=0;
			ret = hash_map_insert(hash_map, (const void *)(&key), (const void *)(&value), &value_clone_cbf, &value_free_cbf); 
			if(!ret){
				printf("insert (%s, %s) failed", key.key, value.value);
			}
			break;
		case 3:
			printf("key:");
			scanf("%19s", key.key);
			ret = hash_map_delete_entry(hash_map, (const void *) &key);
			if(!ret){
				printf("delete failed\n");
			}
			break;
		case 2:
			printf("key:");
			scanf("%19s", key.key);
			ret = hash_map_lookup(hash_map, (const void *) &key, (void **)(&vp));
			if(!ret || !vp){
				printf("key does not exist\n");
			}
			else{
				printf("key=%s, value=%s\n", key_to_string((const void *)&key), value_to_string((const void *)vp));
			}
			break;
		default:
			i = 0;
			break;
		}
		hash_map_dump(hash_map, &key_to_string, &value_to_string);
	}
	hash_map_fini(hash_map);
	printf("quit hash map\n");
	
	return 0;
}
Exemplo n.º 2
0
int main (int argc, char *argv[])
{
  hash_map_t *map = NULL;
  struct thread_info *tinfo = NULL;
  pthread_attr_t attr;
  pthread_barrier_t barrier = {0,};
  int ret = -1;
  int tcount = 100, idx = 0;

  map = hash_map_init ();
  if (map == NULL) {
    printf ("failed to initialize hash map\n");
  }

  hh_random_init ();

  ret = pthread_attr_init(&attr);
  if (ret != 0)
    printf ("ERROR: failed to initialize pthread attr\n");

  pthread_barrier_init (&barrier, NULL, tcount + 1);

  tinfo = calloc (tcount, sizeof(struct thread_info));
  if (tinfo == NULL)
    printf ("ERROR: calloc failed\n");

  for (idx = 0; idx < tcount; idx++) {
    tinfo[idx].action = (hh_random() % 3) + 1;

    if (tinfo[idx].action == ACTION_INSERTION)
      tinfo[idx].size = 100 + idx;
    else
      tinfo[idx].size = 23;

    tinfo[idx].map = map;

    tinfo[idx].barrier = &barrier;
  }

  for (idx = 0; idx < tcount; idx++)
    pthread_create (&tinfo[idx].thread_id, &attr, &thread_start, &tinfo[idx]);

  /* tinfo[0].action = ACTION_INSERTION; */
  /* tinfo[0].size = 100; */

  /* tinfo[1].action = ACTION_REMOVAL; */
  /* tinfo[1].size = 20; */

  /* tinfo[2].action = ACTION_RETRIEVAL; */
  /* tinfo[2].size = 20; */

  /* tinfo[0].map = tinfo[1].map = tinfo[2].map = map; */

  /* tinfo[0].barrier = tinfo[1].barrier = tinfo[2].barrier = &barrier; */

  /* //  insertions (map, 100); */
  /* pthread_create (&tinfo[0].thread_id, &attr, &thread_start, &tinfo[0]); */

  /* //  removals (map, 20); */
  /* pthread_create (&tinfo[1].thread_id, &attr, &thread_start, &tinfo[1]); */

  /* //  retrievals (map, 10); */
  /* pthread_create (&tinfo[2].thread_id, &attr, &thread_start, &tinfo[2]); */

  pthread_barrier_wait (&barrier);

  for (idx = 0; idx < tcount; idx++)
    pthread_join (tinfo[idx].thread_id, NULL);


  /* pthread_join (tinfo[0].thread_id, NULL); */
  /* pthread_join (tinfo[1].thread_id, NULL); */
  /* pthread_join (tinfo[2].thread_id, NULL); */

  hash_map_dump (map);

  return 0;
}