--- bitcache.inc.dist	2008-05-09 05:15:59.000000000 -0600
+++ bitcache.inc	2008-07-13 12:48:15.940240000 -0600
@@ -152,7 +152,47 @@ class Bitcache_Repository implements Ite
   }
 
   protected function path($id = NULL) {
-    return $this->path . ($id ? '/' . $id : '');
+    //return $this->path . ($id ? '/' . $id : '');
+    
+    // implementing two-level hash directory structure to keep
+    // down the number of files per dir in large repositories
+    if(!$id){
+    	return $this->path;
+    }
+    
+    $d1 = substr($id,0,1);
+    $d2 = substr($id,1,1);
+    
+    $path = $this->path . '/' . $d1 . '/' . $d2;
+    if(!is_dir($path)){
+    	mkdir($path, 0700, TRUE);
+    }
+    
+    return $path . '/' . $id;
+    
+  }
+  
+  /**
+  	* move a respository build with a one-level directory structure to a two-tier
+  	* hash structure.  Safe (if inefficient) to call multiple times.
+  	*/
+  function rehash(){
+  	$dir = opendir($this->path);
+  	
+  	$moves = array();
+  	while(($file = readdir($dir)) != false){
+  		$filepath = $this->path . '/' . $file;
+  		if(!is_file($filepath)){
+  			// directory, which we can ignore
+  			continue;
+  		}
+  		// file, which we need to move into our structure
+  		$moves[] = $file;
+  		$this->put_file($file, $filepath, true);
+  	}
+  	closedir($dir);
+  	
+  	return $moves;
   }
 }
 
