]> err.no Git - mapper/commitdiff
Keep some simple statistics on how the place cache is working.
authorKaj-Michael Lang <milang@onion.tal.org>
Mon, 18 Feb 2008 12:34:12 +0000 (14:34 +0200)
committerKaj-Michael Lang <milang@onion.tal.org>
Mon, 18 Feb 2008 12:34:12 +0000 (14:34 +0200)
Check the sql statements before finalizing.

src/osm-db.c

index 9e31ac11fbec4bfc91468545e2aa184d626cb379..98e41a119ef62546485c6aded142bec0d06eba4c 100644 (file)
@@ -71,7 +71,12 @@ static GTimer *dbt;
 static GtkProgressBar *dbpw=NULL;
 
 /* Cache hash tables */
-static GHashTable *place_cache;
+struct osm_place_cache {
+       GHashTable *cache;
+       guint hit;
+       guint miss;
+};
+static struct osm_place_cache pcache;
 
 static guint way_dist_range=OSM_RANGE_WAY;
 
@@ -141,7 +146,6 @@ if (w!=NULL)
 gboolean
 osm_db_prepare(sqlite3 *db)
 {
-/* Place */
 /* Select nearest place inside lat,lon+-range */
 if (sqlite3_prepare_v2(db, "select name,(($LAT-ilat)*($LAT-ilat))+(($LON-ilon)*($LON-ilon)) as dist,"
                                        " ilat,ilon,places.nid,isin_p,isin_c "
@@ -224,19 +228,34 @@ return TRUE;
 void
 osm_deinit(void)
 {
-if (osmdb!=NULL) {
-       sqlite3_finalize(sql.select_way_ref);
-       sqlite3_finalize(sql.select_way_name);
-       sqlite3_finalize(sql.select_way_next_seg);
-       sqlite3_finalize(sql.select_way_prev_seg);
-       sqlite3_finalize(sql.select_way_name_search);
-       sqlite3_finalize(sql.select_way2);
-       sqlite3_finalize(sql.select_place);
-       sqlite3_finalize(sql.select_near_place);
+if (osmdb) {
+       if (sql.select_way_ref)
+               sqlite3_finalize(sql.select_way_ref);
+       if (sql.select_way_name)
+               sqlite3_finalize(sql.select_way_name);
+       if (sql.select_way_next_seg)
+               sqlite3_finalize(sql.select_way_next_seg);
+       if (sql.select_way_prev_seg)
+               sqlite3_finalize(sql.select_way_prev_seg);
+       if (sql.select_way_name_search)
+               sqlite3_finalize(sql.select_way_name_search);
+       if (sql.select_way2)
+               sqlite3_finalize(sql.select_way2);
+       if (sql.select_place)
+               sqlite3_finalize(sql.select_place);
+       if (sql.select_near_place)
+               sqlite3_finalize(sql.select_near_place);
+       if (sql.select_node_next)
+               sqlite3_finalize(sql.select_node_next);
+       if (sql.select_node_prev)
+               sqlite3_finalize(sql.select_node_prev);
 }
 osmdb=NULL;
 osm_db_ok=FALSE;
-g_hash_table_destroy(place_cache);
+memset(&sql, 0, sizeof(sql));
+g_hash_table_destroy(pcache.cache);
+pcache.hit=0;
+pcache.miss=0;
 g_timer_destroy(dbt);
 }
 
@@ -244,7 +263,7 @@ gboolean
 osm_init(sqlite3 **db)
 {
 osm_db_ok=FALSE;
-place_cache=g_hash_table_new(g_direct_hash, g_direct_equal);
+pcache.cache=g_hash_table_new(g_direct_hash, g_direct_equal);
 dbt=g_timer_new();
 
 if (!db || !*db) {
@@ -253,6 +272,7 @@ if (!db || !*db) {
 }
 
 osmdb=*db;
+memset(&sql, 0, sizeof(sql));
 if (osm_db_prepare(osmdb)==FALSE) {
        g_printerr("Failed to prepare OSM SQL statements:");
        g_printf("SQLITE: %s\n", sqlite3_errmsg(osmdb));
@@ -344,21 +364,33 @@ return g_slice_new0(osm_place);
 static osm_place *
 osm_place_cache_lookup(guint32 id)
 {
-return g_hash_table_lookup(place_cache, GINT_TO_POINTER(id));
+osm_place *r;
+r=g_hash_table_lookup(pcache.cache, GINT_TO_POINTER(id));
+if (r) 
+       pcache.hit++; 
+else 
+       pcache.miss++;
+
+g_debug("OSM: Cache %d/%d", pcache.hit, pcache.miss);
+return r;
 }
 
 static void
 osm_place_cache_add(osm_place *p)
 {
 if (osm_place_cache_lookup(p->id)==NULL)
-       g_hash_table_insert(place_cache, GINT_TO_POINTER(p->id), p);
+       g_hash_table_insert(pcache.cache, GINT_TO_POINTER(p->id), p);
 }
 
 static void
 osm_place_cache_gc(void)
 {
-gint r;
-r=g_hash_table_foreach_remove(place_cache, osm_place_remove, NULL);
+guint r;
+
+r=g_hash_table_foreach_remove(pcache.cache, osm_place_remove, NULL);
+g_debug("OSM: Cache cleared (%d)", r);
+pcache.hit=0;
+pcache.miss=0;
 }
 
 static void
@@ -383,14 +415,13 @@ osm_place *n;
 n=*nr;
 n=osm_place_cache_lookup(id);
 if (n) {
-       g_print("*P!\n");
        osm_place_update_distance(n, lat, lon);
        return TRUE;
 }
 n=NULL;
 
 /* XXX: better place for this */
-if (g_hash_table_size(place_cache)>OSM_PLACE_CACHE_MAX_ITEMS)
+if (g_hash_table_size(pcache.cache)>OSM_PLACE_CACHE_MAX_ITEMS)
        osm_place_cache_gc();
 
 sqlite3_clear_bindings(sql.select_place);