Index: scripts/mknmz.in
===================================================================
--- scripts/mknmz.in	(リビジョン 2)
+++ scripts/mknmz.in	(作業コピー)
@@ -39,6 +39,8 @@
 use File::Copy;
 use DirHandle;
 use File::Basename;
+use Fcntl;
+use DB_File;
 
 use vars qw($SYSTEM);
 # It exists only for back compatibility.
@@ -97,6 +99,9 @@
     my ($output_dir, @targets) = parse_options();
     my ($docid_base, $total_files_num) = prep($output_dir, @targets);
 
+    init_index_hash();
+    init_phrase_hash();
+
     my $swap = 1;
     my $docid_count = 0;
     my $file_count = 0;
@@ -147,10 +152,7 @@
 	    last if $ReceiveTERM;
 	    if ($processed_files_size > $conf::ON_MEMORY_MAX) {
 		if (%KeyIndex) {
-		    $key_count = write_index();
-		    print _("Writing index files..."); 
-		    write_phrase_hash();
-		    print "\n";
+		    $key_count = flush_index();
 		}
 		$processed_files_size = 0;
 		$checkpoint = 1, last if $CheckPoint{'on'} && defined(<$fh_flist>);
@@ -166,10 +168,7 @@
 	    $file_count, $key_count) if $checkpoint;
 
     if (%KeyIndex) {
-	$key_count = write_index();
-	print _("Writing index files...");
-	write_phrase_hash();
-	print "\n";
+	$key_count = flush_index();
     }
 
     $key_count = get_total_keys() unless $key_count;
@@ -2324,8 +2323,7 @@
     util::fclose($fh_tmp_p);
     util::fclose($fh_tmp_pi);
 
-    %PhraseHash = ();
-    %PhraseHashLast = ();
+    init_phrase_hash();
 }
 
 # Dr. Knuth's  ``hash'' from (UNIX MAGAZINE May 1998)
@@ -2590,8 +2588,7 @@
 	$key_count++;
 	$cnt++;
     }
-    %KeyIndex = ();
-    %KeyIndexLast = ();
+    init_index_hash();
 
     util::fclose($fh_wi);
     util::fclose($fh_w) if (defined $fh_w);
@@ -2662,6 +2659,34 @@
     print STDERR "Warning: signal $signame occured.\n";
 }
 
+# performance up for large index
+
+sub flush_index() {
+    my $kc = write_index();
+    print _("Writing index files..."); 
+    write_phrase_hash();
+    print "\n";
+    return $kc
+}
+
+sub init_index_hash() {
+    my $kif = util::tmpnam("KeyIndex");
+    my $kilf = util::tmpnam("KeyIndexLast");
+    untie %KeyIndex if tied(%KeyIndex) =~ /^DB_File/;
+    untie %KeyIndexLast if tied(%KeyIndexLast) =~ /^DB_File/;
+    tie %KeyIndex, 'DB_File', $kif, O_RDWR|O_CREAT, 0644, $DB_HASH;
+    tie %KeyIndexLast, 'DB_File', $kilf, O_RDWR|O_CREAT, 0644, $DB_HASH;
+}
+
+sub init_phrase_hash() {
+    my $phf = util::tmpnam("PhraseHash");
+    my $phlf = util::tmpnam("PhraseHashLast");
+    untie %PhraseHash if tied(%PhraseHash) =~ /^DB_File/;
+    untie %PhraseHashLast if tied(%PhraseHashLast) =~ /^DB_File/;
+    tie %PhraseHash, 'DB_File', $phf, O_RDWR|O_CREAT, 0644, $DB_HASH;
+    tie %PhraseHashLast, 'DB_File', $phlf, O_RDWR|O_CREAT, 0644, $DB_HASH;
+}
+
 #
 # For avoiding "used only once: possible typo at ..." warnings.
 #

