#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - index      *
#  ###  ##        ####### ####### ##     **        $Revision: 2.1 $			*
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **      Carlo Strozzi (c) 1998     *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Adapted by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
# Original code: indextbl,v 1.13 1994/06/20 10:46:33 hobbs
#****************************************************************************

$0 =~ s-.*/-- ;
while ( $ARGV[0] =~ /^-/ ){				# Get args
    $_ = shift ;
    if( /^-a.*/ || /^--all$/ ){ $MTBL = shift ; next ; }
    if( /^-u.*/ || /^--update$/ ){ $UPD++ ; next ; }
    if( /^-x.*/ || /^--debug$/ ){ $xbug++ ; next ; }
    die "\n$0: unknown option: $_\n" ; 
}
if( $UPD ){
    &do_upd ;
}
else{
    die "\n$0: not enough info given, check the documentation\n"
	unless @ARGV >= 2 ;
    $tbl = shift ;
    ($base = $tbl) =~ s/\.rdb$// ;
    @COLS = @ARGV ;
    $dbx = "$base.x." . join( ".", @COLS ) ;
    &do_ndx ;
}
sub do_ndx {		# generate index file $dbx from $tbl on columns @COLS
    warn "gen ndx file $dbx ($tbl) on: @COLS\n" ;
    open( RR, $tbl ) || die "$0: can't open table: $tbl\n" ;
    @ndx = () ; @XFN = () ; @XFD = () ;
    $lln = 0 ;
    while( <RR> ){					# read rdbtbl header
	next if /^\s*#/ ;		# comment 
	chop ;
	if( ++$lln == 1 ){
	    @CN = split( /\t/, $_ );	# col names
	    next ; }
	@CD = split( /\t/, $_ );	# col definitions
	last ; }
    $lnpos = tell ;			# read point of first row
    for $col (@COLS){		# get, chk column name ndx
	for( $k=-1, $i=0 ; $i < @CN ; $i++ ){
	    if( $col eq $CN[$i] ){
		$k = $i ;
		push( @ndx, $i ) ;		# index in $tbl
		push( @XFN, $col ) ;	# new index file, col name
		push( @XFD, $CD[$i] ) ;	# new index file, col definition
		last ; } }
	die "$0: column name no match: $col\n" if $k == -1 ;
    }
    push( @XFN, "lpos" ) ;
    push( @XFD, "6n Line pos in $tbl" ) ;
    return if $xbug ;
    open( SS, "| nosql sort @XFN > $dbx" ) || die "$0: can't open pipe\n" ;
    # print SS "# NoSQL Index file for: $tbl\n" ;
    print SS join( "\t", @XFN ), "\n", join( "\t", @XFD ), "\n" ;
    $rcnt = 0 ;
    warn "Reading $tbl ...\n" ;
    while( <RR> ){					# read rdbtbl body
	$rcnt++ ;
	chop ;
	$lim = tr/\t/\t/ +1 ;
	@dat = () ;
	@a = split( /\t/, $_, $lim ) ;
	for (@ndx){
	    push( @dat, $a[$_] ) ; }
	print SS join( "\t", @dat ), "\t", $lnpos, "\n" ; # send to "nosql sort"
	warn "@dat  $lnpos\n" if $xbug ;
	$lnpos = tell ;		# read point of next row
    }
    warn "Rows: $rcnt, Sorting ...\n" ;
    close SS ;
}
sub do_upd { # get index file names, tbl names, col names, call do_ndx for each.
    unless( @ARGV ){
	opendir( DD, "." ) || die "$0: can't open curr dir\n" ;
	@ARGV = grep( /\.x\./, readdir( DD )) ; }
    for $dbx (@ARGV){
	($base = $dbx) =~ s/\.x\..*$// ;
	$tbl = "$base.rdb" ;
	next if $MTBL && $MTBL ne $tbl ;
	open( XF, $dbx ) || die "$0: can't open input: $dbx\n" ;
	while( <XF> ){
	    next if /^\s*#/ ;	# skip comments
	    @COLS = split( /\t/, $_ ) ;
	    pop( @COLS ) ;	# remove line pos col
	    close XF ;
	    last ; }
	# warn "$tbl, $dbx, @COLS\n" ; #<<<<<<<<<<<<
	&do_ndx ;
    }
}
