From: Kaj-Michael Lang Date: Thu, 2 Aug 2007 09:01:27 +0000 (+0300) Subject: Add function db_close to finalize prepared statements and close database connection. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5adb7323dad3b71d00d219fea35c5093ff607c30;p=mapper Add function db_close to finalize prepared statements and close database connection. Silence some warnings. init/deinit functions handle storage init/deinit --- diff --git a/src/osm.c b/src/osm.c index 348473d..f852f29 100644 --- a/src/osm.c +++ b/src/osm.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -251,6 +252,9 @@ gint wsegcnt; node *osm_find_node(guint32 nid); void osm_free_way_data(way *w); void print_way(way *w); +gboolean db_open(void); +void db_prepare(void); +gboolean db_insert_node(node *n); /****************************************************/ /* Functions */ @@ -266,6 +270,23 @@ if (SQLITE_OK != (sqlite3_open(OSM_DB_FILE, &db))) { return TRUE; } +void +db_close(void) +{ +sqlite3_finalize(sql.insert_poi); +sqlite3_finalize(sql.delete_osm_poi); + +sqlite3_finalize(sql.insert_node); +sqlite3_finalize(sql.select_node); +sqlite3_finalize(sql.delete_nodes); +sqlite3_finalize(sql.update_node); + +sqlite3_finalize(sql.insert_place); +sqlite3_finalize(sql.delete_place); + +sqlite3_close(db); +} + void db_prepare(void) { @@ -333,6 +354,7 @@ sqlite3_bind_int(sql.insert_node, 3, lon); sqlite3_step(sql.insert_node); sqlite3_reset(sql.insert_node); sqlite3_clear_bindings(sql.insert_node); + return TRUE; } @@ -374,6 +396,7 @@ sqlite3_bind_int(sql.insert_poi, 8, n->data->isin); sqlite3_step(sql.insert_poi); sqlite3_reset(sql.insert_poi); sqlite3_clear_bindings(sql.insert_poi); + return TRUE; } @@ -486,7 +509,8 @@ db_insert_way(way *w) if (!w) return FALSE; -if (w->type==WAY_UNWAYED) +/* Skip things we can't use */ +if (w->type==WAY_UNWAYED || w->type>WAY_ROAD_END) return TRUE; wsegcnt=0; @@ -799,7 +823,7 @@ way *t; gchar **isin; gchar **place; -isin=g_hash_table_lookup(osm_way_isin, w->id); +isin=g_hash_table_lookup(osm_way_isin, GINT_TO_POINTER(w->id)); if (!isin) return 0; @@ -821,10 +845,12 @@ return 0; /***********************************************************************/ void -osm_node_save_node(gint key, node *value, gpointer user_data) +osm_node_save_node(gint key, gpointer value, gpointer user_data) { +node *n=(node *)value; + dbnode_cnt++; -db_insert_node(value); +db_insert_node(n); if (dbnode_cnt % 26214==0) g_printf("Nodes: %f%%\n",((float)dbnode_cnt/(float)node_cnt)*100); } @@ -1006,8 +1032,6 @@ switch (t) { break; case IS_NODE: { - gint i; - if (!osm_node_tags) return; @@ -1183,17 +1207,47 @@ switch (t) { /************************************************************************/ +static void +storage_init(void) +{ +osm_nodes=g_hash_table_new(g_direct_hash, g_direct_equal); +osm_segments=g_hash_table_new(g_direct_hash, g_direct_equal); + +osm_place_country=g_hash_table_new(g_str_hash, g_str_equal); +osm_place_city=g_hash_table_new(g_str_hash, g_str_equal); +osm_place_suburb=g_hash_table_new(g_str_hash, g_str_equal); +osm_place_village=g_hash_table_new(g_str_hash, g_str_equal); +osm_place_region=g_hash_table_new(g_str_hash, g_str_equal); +osm_node_isin=g_hash_table_new(g_direct_hash, g_direct_equal); +} + +static void +storage_free(void) +{ +g_hash_table_destroy(osm_nodes); +g_hash_table_destroy(osm_segments); + +g_hash_table_destroy(osm_place_country); +g_hash_table_destroy(osm_place_city); +g_hash_table_destroy(osm_place_suburb); +g_hash_table_destroy(osm_place_village); +g_hash_table_destroy(osm_place_region); +g_hash_table_destroy(osm_node_isin); +} + void -osm_planet_parse_init(void) +osm_planet_parser_init(void) { xp=XML_ParserCreate(NULL); XML_SetElementHandler(xp, _osm_tag_start, _osm_tag_end); +storage_init(); } void -osm_planet_parse_deinit(void) +osm_planet_parser_deinit(void) { XML_ParserFree(xp); +storage_free(); } gboolean @@ -1258,34 +1312,6 @@ g_printf("Way size: %d\n", sizeof(way)); g_printf("WayD size: %d\n", sizeof(way_data)); } -static void -storage_init(void) -{ -osm_nodes=g_hash_table_new(g_direct_hash, g_direct_equal); -osm_segments=g_hash_table_new(g_direct_hash, g_direct_equal); - -osm_place_country=g_hash_table_new(g_str_hash, g_str_equal); -osm_place_city=g_hash_table_new(g_str_hash, g_str_equal); -osm_place_suburb=g_hash_table_new(g_str_hash, g_str_equal); -osm_place_village=g_hash_table_new(g_str_hash, g_str_equal); -osm_place_region=g_hash_table_new(g_str_hash, g_str_equal); -osm_node_isin=g_hash_table_new(g_direct_hash, g_direct_equal); -} - -static void -storage_free(void) -{ -g_hash_table_destroy(osm_nodes); -g_hash_table_destroy(osm_segments); - -g_hash_table_destroy(osm_place_country); -g_hash_table_destroy(osm_place_city); -g_hash_table_destroy(osm_place_suburb); -g_hash_table_destroy(osm_place_village); -g_hash_table_destroy(osm_place_region); -g_hash_table_destroy(osm_node_isin); -} - /************************************************************************/ int main (int argc, char **argv) @@ -1315,11 +1341,8 @@ db_prepare(); print_memory_usage(); -storage_init(); - -osm_planet_parse_init(); +osm_planet_parser_init(); osm_planet_parse_file(argv[1]); -osm_planet_parse_deinit(); g_printf("Planet loaded.\nTotal nodes %d, POIs: %d, Segments %d and Ways %d.\n", node_cnt, noded_cnt, seg_cnt, way_cnt); @@ -1328,10 +1351,9 @@ g_printf("Villages/Hamlets: %d\n", g_hash_table_size(osm_place_village)); g_printf("Suburbs: %d\n", g_hash_table_size(osm_place_suburb)); osm_planet_save_to_db(); +osm_planet_parser_deinit(); +db_close(); -storage_free(); - -sqlite3_close(db); sync(); return 0; }