home *** CD-ROM | disk | FTP | other *** search
-
- /* i4reindex.c (c)Copyright Sequiter Software Inc., 1987, 1988, 1989. All rights reserved.
-
- Reindexes an Index File
- */
-
- #include "d4base.h"
- #include "u4error.h"
-
- #ifndef UNIX
- #include <io.h>
- #include <stdlib.h>
- #endif
-
- #ifdef TURBO
- #include <stdlib.h>
- #else
- #include <malloc.h>
- #endif
-
- #include <string.h>
-
- typedef struct
- {
- char *ptr ; /* Pointer to the current buffer position */
- char *end ; /* Pointer to the end buffer position */
- long disk ; /* Current Disk Position, Offset from Start;
- -1 Means nothing is on disk. */
- } BUFFER ;
-
- #define NUM_BUFFER 600
-
- static int add_key(KEY *), read_buffer(int), reindex(int), build_sort(int),
- index_info( int ) ;
- static long build_index(void) ;
- static char *get_key(void) ;
- extern INDEX *v4index ;
- extern BLOCK *v4block ;
- extern BASE *v4base ;
- extern int v4cur_base ;
- extern int v4last_base ;
-
- static KEY key_value ;
- static BUFFER *buffer_ptr ;
- static INDEX *index_ptr ;
- static int block_on ;
- static int block_max ;
- static long file_block ;
- static int group_len ;
- static int key_len ;
- static int key_len_4 ;
- static int keys_max ;
- static int keys_mem ;
- static int dos_file ;
- static int i_small ;
- static char *name ;
- static char temp_ptr[10];
- static char i_type ;
-
- static char * large ;
- static unsigned int buf_size, spool_size ;
- static int num_spool, temp_file ;
-
- static long qsort_times ;
- static long qsort_len ;
- static long num_read_buffer ;
- static long bytes_read_buffer ;
- static long disk_read_buffer ;
-
- static int compare( char *, char * ) ;
-
- static int compare( one, two )
- char *one,*two ;
- {
- return( memcmp(one, two, key_len_4) ) ;
- }
-
- #ifndef CLIPPER
- static int compare_doub( double *, double * ) ;
-
- static int compare_doub( one, two )
- double *one, *two ;
- {
- if ( *one <= *two )
- return( -1 ) ;
- else
- return( 1 ) ;
- }
- #endif
-
-
- static int build_sort( index_ref )
- int index_ref ;
- {
- long on_rec, count ;
- unsigned int on_key ;
- int rc, i, base_file, base_length, base_length2 ;
- char *eval_ptr, *large_ptr, *save_ptr, *on_ptr ;
- BASE *base_ptr ;
-
- base_ptr = v4base + v4cur_base ;
- i_type = i4type( index_ref ) ;
-
- num_read_buffer = 0 ;
- disk_read_buffer= 0 ;
- bytes_read_buffer= 0 ;
-
- count = d4reccount() ;
- on_key = 0 ;
- num_spool = 0 ;
-
- large_ptr = large ;
-
- base_file = base_ptr->dos_file ;
- base_length = base_ptr->buffer_len ;
- base_length2= base_length << 1 ;
- save_ptr = base_ptr->buffer ;
- on_ptr = save_ptr - base_length ;
-
- lseek( base_file, (long) d4ptr()->header_len, 0) ;
- qsort_times = 0 ;
- qsort_len = 0 ;
-
- for ( on_rec = 1; on_rec <= count; on_rec++)
- {
- base_ptr->rec_num = on_rec ;
-
- if ( on_rec & 1L ) /* On Odd Records */
- {
- rc = read( base_file, on_ptr, base_length2) ;
- if ( rc != base_length2 )
- {
- if ( on_rec != count || rc < base_length )
- {
- char buffer[34] ;
- ltoa( on_rec, buffer, 10) ;
- u4error( E_READ, base_ptr->name, "Record: ", buffer, (char *) 0) ;
- return( -1) ;
- }
- }
- base_ptr->buffer = on_ptr ;
- eval_ptr = i4eval( index_ref) ;
- base_ptr->buffer = save_ptr ;
- }
- else
- eval_ptr = i4eval( index_ref) ;
-
- if ( eval_ptr == (char *) 0) return( -1) ;
-
- memmove( large_ptr, eval_ptr, key_len) ;
- memmove( large_ptr+key_len, (char *)&on_rec, 4) ;
- large_ptr += key_len_4 ;
- on_key++ ;
-
- if ( on_key >= keys_mem && on_rec != count)
- {
- /* Buffer Full, Sort and Spool to Disk */
- #ifdef CLIPPER
- u4sort( large, on_key, key_len_4, compare) ;
- #else
- if ( i_type == 'C' )
- u4sort( large, on_key, key_len_4, compare) ;
- else
- u4sort( large, on_key, key_len_4, compare_doub ) ;
- #endif
-
- qsort_times++ ;
- qsort_len += on_key*key_len_4 ;
-
- if ( num_spool == 0 )
- {
- /* Open the temporary File */
- strcpy( temp_ptr, "D3XXXXXX") ;
- mktemp( temp_ptr ) ;
- if ( (temp_file = u4open( temp_ptr, 1 )) < 0 ) return -1 ;
- lseek( temp_file, 0L, 0) ;
- }
-
- if ( write( temp_file, large, on_key*key_len_4) != on_key*key_len_4)
- {
- u4error( E_WRITE, temp_ptr, "Temporary Working File", (char *) 0 ) ;
- return( -1 ) ;
- }
-
- num_spool++ ;
-
- large_ptr = large ;
- on_key = 0 ;
- }
- }
-
- /* Sort the buffer and spool if necessary */
- #ifdef CLIPPER
- u4sort( large, on_key, key_len_4, compare) ;
- #else
- if ( i_type == 'C' )
- u4sort( large, on_key, key_len_4, compare) ;
- else
- u4sort( large, on_key, key_len_4, compare_doub ) ;
- #endif
- qsort_times++ ;
- qsort_len += on_key*key_len_4 ;
-
- if ( num_spool > 0 )
- {
- if ( write( temp_file, large, on_key*key_len_4) != on_key*key_len_4)
- {
- u4error( E_WRITE, temp_ptr, "Temporary Work File", (char *) 0) ;
- return( -1) ;
- }
- num_spool++ ;
-
- spool_size = keys_mem * key_len_4 ;
- buf_size = (keys_mem / num_spool) * key_len_4 ;
- if ( num_spool > NUM_BUFFER || keys_mem < num_spool )
- {
- u4error( E_INTERNAL, "BUILD_SORT", (char *) 0) ;
- return( -1) ;
- }
-
- for ( i=0; i< num_spool; i++ )
- {
- buffer_ptr[i].disk = i*((long)spool_size) ;
- buffer_ptr[i].end = large+ buf_size*(i+1) ;
- read_buffer( i ) ;
- }
- }
- else
- {
- /* One buffer was large enough. */
- buffer_ptr->disk = -1 ;
- buffer_ptr->ptr = large ;
- buffer_ptr->end = large + count * key_len_4 ;
- if ( count > 0) num_spool = 1 ;
- }
-
- i_small = 0 ;
- return( 0) ;
- }
-
-
- static int read_buffer( i_buf )
- int i_buf ;
- {
- unsigned int bytes_read, ask_read, buffer_number ;
-
- num_read_buffer++ ;
-
- /* Is there anything still on disk ? */
- if ( buffer_ptr[i_buf].disk < 0 )
- {
- if ( num_spool > 0)
- {
- /* Nothing left for Buffer */
- memmove( (char *) (buffer_ptr+i_buf),
- (char *) (buffer_ptr+num_spool-1), sizeof(BUFFER) ) ;
- num_spool-- ;
- }
- return( 0) ;
- }
-
- disk_read_buffer++ ;
-
- /* Calculate the buffer number from the current disk offset */
- buffer_number = (unsigned int) (buffer_ptr[i_buf].disk / spool_size) ;
- buffer_ptr[i_buf].ptr = large+ buffer_number* buf_size ;
-
- ask_read = (unsigned int) ((long)spool_size* (buffer_number+1) - buffer_ptr[i_buf].disk) ;
- if (ask_read > buf_size) ask_read = buf_size ;
-
- /* Read in the rest of the buffer from disk */
-
- lseek( temp_file, buffer_ptr[i_buf].disk, 0 ) ;
- bytes_read = read( temp_file, buffer_ptr[i_buf].ptr, ask_read) ;
- if (bytes_read< (key_len+4) || bytes_read == 0xFFFF)
- {
- /* Nothing left for Buffer */
- memmove( (char *) (buffer_ptr+i_buf),
- (char *) (buffer_ptr+num_spool-1), sizeof(BUFFER) ) ;
- num_spool-- ;
- return( 0 ) ;
- }
-
- buffer_ptr[i_buf].disk += bytes_read ;
- if (bytes_read < buf_size ||
- buffer_ptr[i_buf].disk/spool_size != buffer_number)
- {
- buffer_ptr[i_buf].disk = -1 ;
- }
-
- bytes_read_buffer += bytes_read ;
-
- buffer_ptr[i_buf].end = buffer_ptr[i_buf].ptr + bytes_read ;
-
- return( 0) ;
- }
-
-
- static char *get_key() /* Returns the next key in sorted order */
- {
- char *current ;
- int i ;
-
- if ( buffer_ptr[i_small].ptr >= buffer_ptr[i_small].end )
- read_buffer( i_small) ;
- if ( num_spool <= 0 ) return( (char *) 0) ;
-
- current = buffer_ptr[0].ptr ;
- i_small = 0 ;
- for ( i= 1; i< num_spool; i++)
- {
- #ifndef CLIPPER
- if ( i_type == 'C' )
- {
- #endif
- if ( memcmp( buffer_ptr[i].ptr, current, key_len) < 0 )
- {
- current = buffer_ptr[i].ptr ;
- i_small = i ;
- }
- #ifndef CLIPPER
- }
- else
- {
- if ( compare_doub( (double *) buffer_ptr[i].ptr,
- (double *) current ) < 0 )
- {
- current = buffer_ptr[i].ptr ;
- i_small = i ;
- }
- }
- #endif
- }
- buffer_ptr[i_small].ptr += key_len_4 ;
-
- return( current ) ;
- }
-
-
- #ifdef CLIPPER
- static long build_index()
- {
- BLOCK *block_ptr ;
- KEY *key_ptr;
- char *from_ptr, last_ptr[MAX_KEY_SIZE+2] ;
- int i, first_time, i_block ;
- long num_recs ;
-
- num_recs = 0 ;
- first_time = 1 ;
-
- block_on = block_max = -1 ;
- block_ptr= v4block + block_on ;
-
- file_block = BLOCK_SIZE ;
- lseek( dos_file, (long)BLOCK_SIZE, 0) ;
-
- while( 1)
- {
- from_ptr = get_key() ; /* Retrieves Keys in Sorted Order */
-
- if ( index_ptr->unique )
- {
- if ( ! first_time )
- {
- while ( from_ptr != (char *) 0 )
- {
- extern int v4unique_error ;
- if ( memcmp(from_ptr, last_ptr, key_len) != 0 ) break ;
-
- /* Filter out any repeat keys. */
- if ( v4unique_error )
- {
- u4error( E_UNIQUE, index_ptr->name, (char *) 0) ;
- return( -1) ;
- }
- from_ptr = get_key() ;
- }
- }
- if ( from_ptr != (char *) 0 )
- memmove( last_ptr, from_ptr, sizeof(last_ptr ) ) ;
-
- first_time = 0 ;
- }
-
- if ( from_ptr == (char *) 0 )
- {
- /* Observe
- 1. Full Blocks will have been written if the entry which
- points the the block has been entered.
- 2. Either the bottom block may be written and give a starting
- block pointer or the last block written is a good starting
- block pointer.
- 3. There may be one stranded entry which may be added later.
- 4. file_block is the next address to be written
- */
-
- /* The key_value is not yet active */
- memset( &key_value, 0, (size_t) sizeof(key_value) ) ;
-
- /* Check for no Entries */
- if ( block_max < 0 )
- {
- add_key( &key_value ) ;
- v4block[0].num_keys = 0 ;
-
- if( write( dos_file, (char *)&v4block->num_keys, BLOCK_SIZE) != BLOCK_SIZE)
- {
- u4error( E_WRITE, name, (char *) 0) ;
- return( -1) ;
- }
- file_block += BLOCK_SIZE ;
- }
- else
- {
- if ( v4block[0].num_keys == 0 )
- {
- while ( v4block[++block_on].num_keys == 0 ) ;
-
- block_ptr = v4block + block_on ;
- memcpy( &key_value, (char *) &block_ptr->num_keys +
- block_ptr->pointers[block_ptr->num_keys-1], group_len) ;
- block_ptr->num_keys-- ;
- if ( block_ptr->num_keys != 0 ) block_on-- ;
- }
- }
-
- while( block_on < block_max )
- {
- block_on++ ;
- block_ptr = v4block+ block_on ;
-
- if ( block_ptr->num_keys > 0 )
- {
- key_ptr = (KEY *) ((char *) &block_ptr->num_keys+
- block_ptr->pointers[block_ptr->num_keys]) ;
- if ( block_on > 0 )
- key_ptr->file_block = file_block - BLOCK_SIZE ;
-
- if ( write( dos_file, (char *)&block_ptr->num_keys, BLOCK_SIZE) != BLOCK_SIZE)
- {
- u4error( E_WRITE, name, (char *) 0) ;
- return( -1) ;
- }
- file_block += BLOCK_SIZE ;
- }
- }
- index_ptr->root = file_block - BLOCK_SIZE ;
- index_ptr->virtual_eof = index_ptr->eof = 0L ;
-
- return( num_recs ) ;
- }
- else
- num_recs++ ;
-
- memcpy( key_value.value, from_ptr, key_len ) ;
- memcpy( &key_value.rec_num, from_ptr+key_len, sizeof(long) ) ;
- key_value.file_block = 0 ;
- if( add_key( &key_value ) < 0 ) return -1 ;
- }
- }
-
- #else
-
-
- static long build_index()
- {
- BLOCK *block_ptr ;
- KEY *key_ptr ;
- char *from_ptr, last_ptr[102] ;
- int i, first_time ;
- long num_recs ;
-
- num_recs = 0 ;
- first_time = 1 ;
-
- block_on = block_max = 0 ;
- block_ptr= v4block + block_on ;
- memset( ((char *)block_ptr)+2*sizeof(int), 0,
- (size_t) (sizeof(BLOCK)-2*sizeof(int)) ) ;
-
- block_ptr->num_keys = keys_max ;
- file_block = 1 ;
- lseek( dos_file, (long)BLOCK_SIZE, 0) ;
-
- while( 1)
- {
- key_ptr = (KEY *) ((char *)&block_ptr->key - group_len) ;
- for( i=0; i< keys_max; i++)
- {
- from_ptr = get_key() ; /* Retrieves Keys in Sorted Order */
- if ( index_ptr->unique )
- {
- if ( ! first_time )
- {
- while ( from_ptr != (char *) 0 )
- {
- extern int v4unique_error ;
-
- if ( memcmp(from_ptr, last_ptr, key_len) != 0 ) break ;
-
- /* Filter out any repeat keys. */
- if ( v4unique_error )
- {
- u4error( E_UNIQUE, index_ptr->name, (char *) 0) ;
- return( -1) ;
- }
- from_ptr = get_key() ;
- }
- }
- if ( from_ptr != (char *) 0 )
- memmove( last_ptr, from_ptr, sizeof(last_ptr ) ) ;
- first_time = 0 ;
- }
-
- if ( from_ptr == (char *) 0 && (i > 0 || block_max == block_on))
- {
- block_ptr->num_keys = i ;
- key_ptr = (KEY *) ((char *)&block_ptr->key+
- i * group_len) ;
- break ;
- }
-
- if ( from_ptr == (char *) 0)
- {
- while( ++block_on)
- {
- block_ptr = v4block+ block_on ;
-
- key_ptr = (KEY *) ((char *)&block_ptr->key+
- block_ptr->num_keys * group_len) ;
-
- if ( block_ptr->num_keys > 0)
- {
- if( write( dos_file, (char *)&block_ptr->num_keys, BLOCK_SIZE) != BLOCK_SIZE)
- {
- u4error( E_WRITE, name, (char *) 0) ;
- return( -1) ;
- }
- key_ptr->file_block = file_block++ ;
- }
- if ( block_on >= block_max)
- {
- index_ptr->root = file_block -1 ;
- index_ptr->eof = file_block ;
- return( num_recs ) ;
- }
- if ( block_ptr->num_keys >= 0 )
- add_key( key_ptr ) ;
- }
- }
- else
- num_recs++ ;
-
- key_ptr = (KEY *) ((char *)key_ptr + group_len) ;
- memmove( key_ptr->value, from_ptr, key_len ) ;
- memmove( (char *) &key_ptr->rec_num, from_ptr+key_len, 4) ;
- }
- if( write( dos_file, (char *)&block_ptr->num_keys, BLOCK_SIZE) != BLOCK_SIZE)
- {
- u4error( E_WRITE, name, (char *) 0) ;
- return( -1L ) ;
- }
- key_ptr->file_block = file_block++ ;
- if( add_key(key_ptr ) < 0) return( -1L ) ;
- key_ptr->file_block = 0 ;
- }
- }
-
- #endif
-
-
- #ifdef CLIPPER
-
- static int add_key( key_ptr )
- KEY *key_ptr ;
- {
- BLOCK *block_ptr ;
- int i, i_block ;
-
- block_on ++ ;
- block_ptr = v4block+ block_on ;
-
- if ( block_on > block_max )
- {
- /* Initialization for this block level */
- memset( ((char *)block_ptr)+2*sizeof(int), 0,
- (size_t) (sizeof(BLOCK)-2*sizeof(int)) ) ;
-
- i_block = keys_max*2 + 4 ;
- for (i=0; i<= keys_max; i++, i_block+= group_len)
- block_ptr->pointers[i] = i_block ;
-
- block_ptr->num_keys = 0 ;
- block_max = block_on ;
- }
-
- if ( block_ptr->num_keys >= keys_max )
- {
- memmove( (char *)&block_ptr->num_keys +
- block_ptr->pointers[block_ptr->num_keys],
- (char *) &key_ptr->file_block, 4) ;
-
- if( write(dos_file, (char *) &block_ptr->num_keys, BLOCK_SIZE) != BLOCK_SIZE)
- {
- u4error( E_WRITE, name, (char *) 0) ;
- return( -1) ;
- }
- key_ptr->file_block = file_block ;
- file_block += BLOCK_SIZE ;
- if( add_key( key_ptr) < 0) return( -1) ;
-
- block_ptr->num_keys = 0 ;
- }
- else
- {
- memmove( (char *)&block_ptr->num_keys +
- block_ptr->pointers[block_ptr->num_keys],
- (char *) key_ptr, group_len) ;
- block_ptr->num_keys++ ;
- }
-
- block_on -- ;
- return(0) ;
- }
-
- #else
-
- static int add_key( key_ptr )
- KEY *key_ptr ;
- {
- BLOCK *block_ptr ;
-
- block_on ++ ;
- block_ptr = v4block+ block_on ;
-
- if ( block_on > block_max )
- {
- /* Initialization for this block level */
- memset( ((char *)block_ptr)+2*sizeof(int), 0,
- (size_t) (sizeof(BLOCK)-2*sizeof(int)) ) ;
- block_ptr->num_keys = -1 ;
- block_max = block_on ;
- }
-
- key_ptr->rec_num = 0 ;
-
- if ( ++block_ptr->num_keys >= keys_max )
- {
- memmove( (char *)&block_ptr->key + block_ptr->num_keys*group_len,
- (char *) key_ptr, 4) ;
-
- if( write(dos_file, (char *) &block_ptr->num_keys, BLOCK_SIZE) != BLOCK_SIZE)
- {
- u4error( E_WRITE, name, (char *) 0) ;
- return( -1) ;
- }
- key_ptr->file_block = file_block++ ;
- if( add_key( key_ptr) < 0) return( -1) ;
-
- block_ptr->num_keys = -1 ;
- }
- else
- memmove( (char *)&block_ptr->key + block_ptr->num_keys*group_len,
- (char *) key_ptr, group_len) ;
-
- block_on -- ;
- return(0) ;
- }
-
- #endif
-
-
- static int index_info( index_ref )
- int index_ref ;
- {
- INDEX *index_ptr ;
- char *eval_ptr, *expr_ptr ;
-
- index_ptr = v4index + index_ref ;
- memset( d4ptr()->buffer, (int) ' ', (size_t) d4ptr()->buffer_len) ;
-
- eval_ptr = i4eval( index_ref) ;
- if ( eval_ptr == (char *) 0) return( -1) ;
-
- index_ptr->i_type = e4type() ;
-
- #ifndef CLIPPER
- if ( e4type() == 'D' || e4type() == 'N' || e4type() == 'F' )
- index_ptr->int_or_date = 1 ;
- else
- index_ptr->int_or_date = 0 ;
- #endif
-
- key_len = 0 ;
-
- if ( e4type() == 'D' )
- key_len = 8 ;
-
- if ( e4type() == 'C' )
- key_len = strlen( eval_ptr ) ;
-
- if ( e4type() == 'N' || e4type() == 'F' )
- {
- #ifdef CLIPPER
- char buf[20] ;
- int cur_digits, cur_dec, on_digits, i_buf ;
- long f_ref ;
- int i_expr ;
-
- /* Analyse the Expression to Determine the Maximum Decimals and Len */
- i_buf = cur_digits = cur_dec = 0 ;
-
- expr_ptr = index_ptr->expression ;
- for ( i_expr = 0; ; i_expr++ )
- {
- if ( u4name_char( expr_ptr[i_expr] ) )
- {
- if ( i_buf < sizeof(buf) )
- buf[i_buf++] = expr_ptr[i_expr] ;
- }
- else
- {
- buf[i_buf] = '\0' ;
- i_buf = 0 ;
-
- f_ref = f4ref( buf ) ;
- if ( f_ref >= 0 )
- {
- on_digits = f4width(f_ref) ;
- if ( f4decimals(f_ref) > 0 )
- on_digits -= f4decimals(f_ref)+1 ;
-
- if ( on_digits > cur_digits) cur_digits = on_digits ;
-
- if ( f4decimals(f_ref) > cur_dec) cur_dec = f4decimals(f_ref);
- }
-
- if ( i_expr >= sizeof(index_ptr->expression) ) break ;
- }
- }
-
- index_ptr->key_dec = cur_dec ;
- key_len = cur_digits ;
- if ( cur_dec > 0 ) key_len += cur_dec+1 ;
- #else
- key_len = sizeof(double) ;
- #endif
- }
-
- if ( key_len > MAX_KEY_SIZE || key_len <= 0 )
- {
- u4error( E_KEY_LEN, name, "Expression:", index_ptr->expression, (char *) 0) ;
- return( -1) ;
- }
-
- index_ptr->key_len = key_len ;
- index_ptr->version = index_ptr->old_version + 1 ;
-
- #ifdef CLIPPER
- index_ptr->sign = 6 ;
- index_ptr->group_len = group_len = key_len+8 ;
- index_ptr->keys_half = (1020/ (group_len+2) - 1)/ 2;
- index_ptr->keys_max = keys_max = index_ptr->keys_half *2 ;
- if ( keys_max < 2 )
- {
- u4error( E_BAD_NDX, index_ptr->name, (char *) 0 ) ;
- return( -1 ) ;
- }
- #else
- index_ptr->group_len = group_len = key_len+8 + key_len%2 ;
- index_ptr->keys_max = keys_max = 504/ index_ptr->group_len ;
- if ( keys_max < 4 )
- {
- u4error( E_BAD_NDX, index_ptr->name, (char *) 0 ) ;
- return( -1 ) ;
- }
- #endif
-
- dos_file = index_ptr->dos_file ;
- name = index_ptr->name ;
- key_len_4 = key_len+4 ;
-
- return 0 ;
- }
-
- static int write_header(void) ;
- static int write_header()
- {
- char buffer[BLOCK_SIZE] ;
- int header_len ;
-
- memset( buffer, 0, BLOCK_SIZE ) ;
-
- lseek( dos_file, 0L, 0) ;
- #ifdef CLIPPER
- chsize(dos_file, index_ptr->root+ (long) BLOCK_SIZE ) ;
- header_len = (char *)(v4index+1) - (char *) &v4index->sign ;
- memcpy( buffer, &index_ptr->sign, header_len ) ;
- #else
- chsize(dos_file, index_ptr->eof*BLOCK_SIZE) ;
- header_len = (char *)(v4index+1) - (char *) &v4index->root ;
- memcpy( buffer, (char *) &index_ptr->root, header_len ) ;
- #endif
- if ( write( dos_file, (char *) buffer, BLOCK_SIZE) != BLOCK_SIZE )
- {
- u4error( E_WRITE, name, (char *) 0 ) ;
- return( -1 ) ;
- }
-
- return 0 ;
- }
-
-
- static int reindex( index_ref )
- int index_ref ;
- {
- long minimum, lrc ;
- int rc ;
- unsigned int allocated ;
-
- index_ptr = v4index + index_ref ;
-
- if ( index_ptr->compile != (char *) 0 ) h4free_memory( index_ptr->compile) ;
- if ( e4parse( index_ptr->expression, &index_ptr->compile) < 0) return(-1) ;
-
- if ( index_info(index_ref) < 0 ) return -1 ;
-
- minimum = key_len_4 ;
- while (1)
- {
- /* Allocate at least the minumim memory */
-
- large = h4alloc_try( allocated = 0xFFDE ) ;
- while ( large == (char *) 0)
- {
- allocated = allocated/2 ;
- if ( allocated <= (unsigned int) minimum )
- {
- allocated = (unsigned int) minimum ;
- large = h4alloc_try( allocated) ;
- if ( large == (char *) 0)
- {
- u4error( E_INDEX, name, "More Memory Needed.", (char *) 0) ;
- return( -1) ;
- }
- }
- else
- large = h4alloc_try( allocated) ;
- }
-
- keys_mem = allocated/key_len_4 ;
-
- /* Raise the minimum if necessary */
-
- if ( (long)keys_mem * ( (keys_mem < NUM_BUFFER) ? keys_mem : NUM_BUFFER) < d4reccount() )
- {
- /* Raise the minimum as it is not enough */
- minimum = (keys_mem+1)*key_len_4 ;
- if ( minimum >= 0xFFE0 )
- {
- u4error( E_INDEX, name, "Too Many Records to Index.", (char *) 0) ;
- return( -1) ;
- }
- h4free_memory( large ) ;
- }
- else
- break ;
- }
-
- temp_ptr[0] = '\0' ;
-
- /* Sort the Information */
- if ( build_sort(index_ref) < 0)
- {
- if ( temp_ptr[0] != '\0')
- {
- close( temp_file ) ;
- #ifdef TURBO
- unlink(temp_ptr) ;
- #else
- remove(temp_ptr) ;
- #endif
- }
- return( -1 ) ;
- }
-
- chsize(dos_file, (long) BLOCK_SIZE ) ;
-
- /* Build the Index File */
- lrc = build_index() ;
- if ( temp_ptr[0] != '\0')
- {
- close( temp_file ) ;
- #ifdef TURBO
- rc = unlink(temp_ptr) ;
- #else
- rc = remove(temp_ptr) ;
- #endif
- if ( rc != 0 )
- {
- u4error( E_CLOSE, "Temporary Working File", temp_ptr, (char *) 0 ) ;
- return( -1 ) ;
- }
- }
- if ( lrc < 0) return( -1) ;
-
- /* Write the Index File Control Information (Block 0) */
- if ( write_header() < 0 ) return -1 ;
-
- #ifdef CLIPPER
- if ( key_value.rec_num != 0 )
- {
- if ( i4add( index_ref, key_value.value, key_value.rec_num ) < 0 )
- return -1 ;
- }
- #endif
-
- if ( lrc != d4reccount() && ! index_ptr->unique )
- {
- char buffer[10] ;
- c4ltoa( lrc, buffer, 8 ) ;
- buffer[8] = '\0' ;
- u4error( E_INTERNAL, "NUMBER REINDEXED:", buffer, (char *) 0 ) ;
- return( -1 ) ;
- }
-
- return( 0 ) ;
- }
-
-
- i4reindex( index_ref )
- int index_ref ;
- {
- int rc, index_on, old_base ;
-
- /* First Reclaim Block Memory */
- old_base = v4cur_base ;
- for ( v4cur_base = v4last_base; v4cur_base >= 0; v4cur_base = v4base[v4cur_base].prev )
- for ( index_on = v4base[v4cur_base].index_ref; index_on >= 0; index_on = v4index[index_on].prev )
- if ( i4free( index_on ) < 0 ) return -1 ;
-
- if ( index_ref >= 0)
- v4cur_base = v4index[index_ref].base_ref ;
- else
- v4cur_base = old_base ;
-
- if ( d4lock( -1L, 1) < 0) return( -1) ;
-
- rc = 0 ;
- buffer_ptr = (BUFFER *) h4alloc( NUM_BUFFER * sizeof(BUFFER) ) ;
-
- if ( index_ref >= 0 )
- {
- large = (char *) 0 ;
- if ( i4lock( index_ref, 1) < 0) return( -1) ;
- if ( reindex( index_ref ) < 0) rc = -1 ;
-
- if ( large != (char *) 0) h4free_memory( large ) ;
- i4unlock( index_ref ) ;
- }
- else
- {
- index_on = d4ptr()->index_ref ;
- while (index_on >= 0)
- {
- large = (char *) 0 ;
-
- if ( i4lock( index_on, 1) < 0) return( -1) ;
- if ( reindex( index_on) < 0) rc = -1 ;
-
- if ( large != (char *) 0) h4free_memory( large ) ;
- i4unlock( index_on ) ;
-
- index_on = v4index[index_on].prev ;
- }
- }
- memset( v4base[v4cur_base].buffer, (int) ' ', v4base[v4cur_base].buffer_len);
- v4cur_base = old_base ;
- h4free_memory( (char *) buffer_ptr ) ;
-
- return( rc) ;
- }
-