On Dec 24, 2007 11:57 AM, Martijn van Oosterhout <[EMAIL PROTECTED]> wrote:
> However, I still think we're getting ahead of ourselves. Attached is a
> patch which I beleive will cut the number of blank tiles created by
> clients by between 50 and 80%. I beleive that we can make the DB much
> smaller, because the size of the blank tile DB is *independant* of the
> size of the OSM database. If we get it right I think the database
> should stabilise at a size much smaller than what it is now...

And attached is another patch, this time for the server side, that
should cut the number of stored blanks by 50% again. I don't have PHP
installed so I havn't been able to syntax check this one so be
careful. If people could review these patches I'd be very grateful.

Have a nice day,
-- 
Martijn van Oosterhout <[EMAIL PROTECTED]> http://svana.org/kleptog/
Index: Upload/Run/index.php
===================================================================
--- Upload/Run/index.php	(revision 3473)
+++ Upload/Run/index.php	(working copy)
@@ -189,7 +189,7 @@
   else
     SaveMetadata($TileList, $UserID, $VersionID);
 
-  SaveBlankTiles($BlankTileList, $UserID);
+  SaveBlankTiles($BlankTileList, $UserID,$ValidTileset,$TilesetX, $TilesetY, $TilesetLayer);
 
   return($Count);
 }
@@ -220,7 +220,46 @@
 #------------------------------------------------------------------------------------
 # Save uploaded blank tiles in the database
 #------------------------------------------------------------------------------------
-function SaveBlankTiles($BlankTileList, $UserID){
+function SaveBlankTiles($BlankTileList, $UserID, $ValidTileset, $TilesetX, $TilesetY, $TilesetLayer){
+  # First we run through the set to find the predominant type
+  $CommonType = 0;
+  $ReplaceList = array();
+  
+  # This optimisation is only possible if we have a valid full tileset
+  if( $ValidTileset )
+  {
+    $CountTypes = array( -1 => 0, 1 => 0, 2 => 0 );
+    
+    foreach($BlankTileList as $SqlSnippet){
+
+      list($X, $Y, $Z, $Layer, $Type) = explode(",", $SqlSnippet);
+      # If we find levels <12, we set a flag to enable old behaviour
+      if( $Z < 12 ) {
+        $CommonType = -2;
+        break;
+      }
+      # Since we save storage by storing in level 12, if we are given the type we have to use it
+      if( $Z == 12 ) {
+        $CommonType = $Type;
+        break;
+      }
+      $CountType[$Type]++;
+    }
+    # Determine the common type, if we have a choice
+    if( $CommonType == 0 )
+    {
+      if( $CountType[1] > $CountType[2] ) {
+        $CommonType = 1;
+      } else {
+        $CommonType = 2;
+      }
+    }
+    # Store the "common tile" at zoom-12, now we don't have to store this type at any lower levels...
+    if( $CommonType > 0 ) {
+      array_push( $ReplaceList, sprintf("(%d, %d, %d, '%s', %d, now(), %d)", $TilesetX, $TilesetY, 12, $TilesetLayer, $CommonType, $UserID) );
+    }
+  }
+  
   # Each element in BlankTileList is a snippet of values (x,y,z,type,size) for each tile
   foreach($BlankTileList as $SqlSnippet){
 
@@ -230,25 +269,29 @@
       moveRequest($X, $Y, REQUEST_ACTIVE, REQUEST_DONE, 0);
     }
     
-    # Make a blank tile
-    if( $Type >= 0 )
+    # Make a blank tile. Level 12 and 15 are always stored, otherwise we only store tiles not equal to the "common tile"
+    if( $Type >= 0 && ($Z == 12 || $Z == 15 || $Type != $CommonType) )
     {
-      $Fields = "x, y, z, layer, type, date, user";
-      $Values = sprintf("%s, now(), %d", $SqlSnippet, $UserID);
-
-      $SQL = sprintf("replace into `tiles_blank` (%s) values (%s);", $Fields, $Values);
+      # Store the stuff to replace in the array
+      $Values = sprintf("(%s, now(), %d)", $SqlSnippet, $UserID);
+      array_push( $ReplaceList, $Values );
     }
     else
     {
       # Delete a blank tile
       $SQL = sprintf("delete from `tiles_blank` where `x`=%d AND `y`=%s AND `z`=%s AND `layer`=%d", $X, $Y, $Z, $Layer);
+      mysql_query($SQL);
     }
     DeleteRealTile($X,$Y,$Z,$Layer);
-
-    mysql_query($SQL);
     
     logSqlError();
   }
+  # Execute all queued replacements...
+  if( count($ReplaceList) > 0 ) {
+    $Fields = "x, y, z, layer, type, date, user";
+    $SQL = sprintf( "replace into `tiles_blank` (%s) values %s", $Fields, implode(",",$ReplaceList) );
+    mysql_query($SQL);
+  }
 }
 
 #------------------------------------------------------------------------------------
_______________________________________________
Tilesathome mailing list
[email protected]
http://lists.openstreetmap.org/cgi-bin/mailman/listinfo/tilesathome

Reply via email to