Exemplo n.º 1
0
int
osm_db_guid2lid_guids(IN osm_db_domain_t * const p_g2l,
		      OUT cl_qlist_t * p_guid_list)
{
	char *p_key;
	cl_list_t keys;
	osm_db_guid_elem_t *p_guid_elem;

	cl_list_construct(&keys);
	cl_list_init(&keys, 10);

	if (osm_db_keys(p_g2l, &keys))
		return 1;

	while ((p_key = cl_list_remove_head(&keys)) != NULL) {
		p_guid_elem =
		    (osm_db_guid_elem_t *) malloc(sizeof(osm_db_guid_elem_t));
		CL_ASSERT(p_guid_elem != NULL);

		p_guid_elem->guid = __osm_unpack_guid(p_key);
		cl_qlist_insert_head(p_guid_list, &p_guid_elem->item);
	}

	cl_list_destroy(&keys);
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
	osm_db_t db;
	osm_log_t log;
	osm_db_domain_t *p_dbd;
	cl_list_t keys;
	cl_list_iterator_t kI;
	char *p_key;
	char *p_val;
	int i;

	cl_list_construct(&keys);
	cl_list_init(&keys, 10);

	osm_log_init_v2(&log, TRUE, 0xff, "/var/log/osm_db_test.log", 0, FALSE);

	osm_db_construct(&db);
	if (osm_db_init(&db, &log)) {
		printf("db init failed\n");
		exit(1);
	}

	p_dbd = osm_db_domain_init(&db, "lid_by_guid");

	if (osm_db_restore(p_dbd)) {
		printf("failed to restore\n");
	}

	if (osm_db_keys(p_dbd, &keys)) {
		printf("failed to get keys\n");
	} else {
		kI = cl_list_head(&keys);
		while (kI != cl_list_end(&keys)) {
			p_key = cl_list_obj(kI);
			kI = cl_list_next(kI);

			p_val = osm_db_lookup(p_dbd, p_key);
			printf("key = %s val = %s\n", p_key, p_val);
		}
	}

	cl_list_remove_all(&keys);

	/* randomly add and remove numbers */
	for (i = 0; i < 10; i++) {
		int k;
		float v;
		int is_add;
		char val_buf[16];
		char key_buf[16];

		k = floor(1.0 * rand() / RAND_MAX * 100);
		v = rand();
		sprintf(key_buf, "%u", k);
		sprintf(val_buf, "%u", v);

		is_add = (rand() < RAND_MAX / 2);

		if (is_add) {
			osm_db_update(p_dbd, key_buf, val_buf);
		} else {
			osm_db_delete(p_dbd, key_buf);
		}
	}
	if (osm_db_keys(p_dbd, &keys)) {
		printf("failed to get keys\n");
	} else {
		kI = cl_list_head(&keys);
		while (kI != cl_list_end(&keys)) {
			p_key = cl_list_obj(kI);
			kI = cl_list_next(kI);

			p_val = osm_db_lookup(p_dbd, p_key);
			printf("key = %s val = %s\n", p_key, p_val);
		}
	}
	if (osm_db_store(p_dbd))
		printf("failed to store\n");

	osm_db_destroy(&db);
	cl_list_destroy(&keys);
}