#!/usr/bin/perl -w use strict; use lib 'lib'; my($cfg_file, $class) = @ARGV or die "usage: $0 "; use MT; use DB_File; use File::Spec; use MT::ConfigMgr; use MT::Object; my $mt = new MT(Config => $cfg_file); my $cfg = MT::ConfigMgr->instance; eval "use $class;"; die "Loading '$class' failed: $@" if $@; my $props = $class->properties; my $indexes = $props->{indexes}; unless ($indexes && keys %$indexes) { print "No indexes to rebuild... exiting.\n"; exit; } print "Indexes:\n"; my %idx; for my $idx_col (keys %$indexes) { my $idx_file = File::Spec->catfile($cfg->DataSource, $class->datasource . '.' . $idx_col . '.idx'); print "\t$idx_col => $idx_file\n"; tie %{ $idx{$idx_col} }, 'DB_File', $idx_file, O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Tie to '$idx_file' failed: $!"; %{ $idx{$idx_col} } = (); } print "\n"; my $iter = $class->load_iter; while (my $obj = $iter->()) { my $id = $obj->id; for my $idx_col (keys %$indexes) { my $idx = $idx{$idx_col}; my $col_value = $obj->$idx_col() || ''; my @cur = split /$;/, $idx->{$col_value} || ''; my %ids = map { $_ => 1 } @cur; next if exists $ids{$id}; $col_value ||= 0 if ($idx_col =~ m/(id|status|number)$/ || $idx_col =~ m/_is_/); $idx->{$col_value} = join $;, keys %ids, $id; } } for my $idx_col (keys %$indexes) { untie %{ $idx{$idx_col} }; }