C DATABASE (source!)

Dave Brower daveb at gonzo.UUCP
Tue Dec 6 15:01:49 AEST 1988


Save yourself 30 minutes of work.  Here are brute force patchable difs
to the posted SHAREWARE programs for UNIX.  It does seem to run the
vtest program OK on my 7300 at home.  (I gave up on the names.c program,
since it seemed to use MSC specific i/o, and I didn't want to figure it
out.)

Adding locking to this code is likely to be more complicated than people
think, unless they take the sleazy way out.  That is, you need to do
whole-file locks.  Otherwise, you will nedd to take into account index
rotation, splitting and the like, and this is enought of a pain that you
don't want to do it unless you absolutely need the conccurrency.

Speaking of concurrency, locking should be done external to the
provided routines.  When someone is doing a scan on a file, you don't
want the data moving underneath, so you need to hold a read lock the
whole time.  That means you need to do something like:

	lock( file, SHARABLE_LOCK );
	open...()
	while( get a record )
		...;
	close...()
	unlock( file );

Ditto writing, where you need to get an EXCLUSIVE lock on the whole file.

Anyway, good luck.

-dB

*** orig/bplus.c	Mon Dec  5 20:08:36 1988
--- bplus.c	Mon Dec  5 20:28:27 1988
***************
*** 17,22
  
  #include <stdio.h>
  #include <fcntl.h>
  #include <io.h>
  #include <sys\types.h>            /*  delete this line for Turbo C  */
  #include <sys\stat.h>

--- 17,23 -----
  
  #include <stdio.h>
  #include <fcntl.h>
+ # ifdef MSC
  #include <io.h>
  # else
  # define SEEK_SET	0
***************
*** 18,25
  #include <stdio.h>
  #include <fcntl.h>
  #include <io.h>
! #include <sys\types.h>            /*  delete this line for Turbo C  */
! #include <sys\stat.h>
  #include <string.h>
  #include "bplus.h"
  

--- 19,30 -----
  #include <fcntl.h>
  # ifdef MSC
  #include <io.h>
! # else
! # define SEEK_SET	0
! # define O_BINARY	0
! # endif
! #include <sys/types.h>            /*  delete this line for Turbo C  */
! #include <sys/stat.h>
  #include <string.h>
  #include "bplus.h"
  
***************
*** 23,29
  #include <string.h>
  #include "bplus.h"
  
- 
  /*  macros, constants, data types  */
  
  #define  NULLREC      (-1L)

--- 28,33 -----
  #include <string.h>
  #include "bplus.h"
  
  /*  macros, constants, data types  */
  
  #define  NULLREC      (-1L)
***************
*** 60,97
  int          split_size = IXB_SPACE;
  int          comb_size  = (IXB_SPACE/2);
  
! void pascal error(int, long);
! void pascal read_if(long, char *, int);
! void pascal write_if(int, long, char *, int);
! int  pascal creat_if(char *);
! int  pascal open_if(char *);
! void pascal close_if(int);
! void pascal update_block(void);
! void pascal init_cache(void);
! int  pascal find_cache(RECPOS);
! int  pascal new_cache(void);
! void pascal load_cache(RECPOS);
! void pascal get_cache(RECPOS);
! void pascal retrieve_block(int, RECPOS);
! int  pascal prev_entry(int);
! int  pascal next_entry(int);
! int  pascal copy_entry(ENTRY *, ENTRY *);
! int  pascal scan_blk(int);
! int  pascal last_entry(void);
! int  pascal write_free( RECPOS, BLOCK *);
! RECPOS pascal get_free(void);
! int  pascal find_block(ENTRY *, int *);
! void pascal movedown(BLOCK *, int, int);
! void pascal moveup(BLOCK *, int, int);
! void pascal ins_block(BLOCK *, ENTRY *, int);
! void pascal del_block(BLOCK *, int);
! int  pascal split(int, ENTRY *, ENTRY *);
! void pascal ins_level(int, ENTRY *);
! int  pascal insert_ix(ENTRY *, IX_DESC *);
! int  pascal find_ix(ENTRY *, IX_DESC *, int);
! int  pascal combineblk(RECPOS, int);
! void pascal replace_entry(ENTRY *);
! void print_blk(BLOCK *);
  
  
  /*  file I/O for B-PLUS module  */

--- 64,101 -----
  int          split_size = IXB_SPACE;
  int          comb_size  = (IXB_SPACE/2);
  
! void pascal error P((int, long));
! void pascal read_if P((long, char *, int));
! void pascal write_if P((int, long, char *, int));
! int  pascal creat_if P((char *));
! int  pascal open_if P((char *));
! void pascal close_if P((int));
! void pascal update_block P((void));
! void pascal init_cache P((void));
! int  pascal find_cache P((RECPOS));
! int  pascal new_cache P((void));
! void pascal load_cache P((RECPOS));
! void pascal get_cache P((RECPOS));
! void pascal retrieve_block P((int, RECPOS));
! int  pascal prev_entry P((int));
! int  pascal next_entry P((int));
! int  pascal copy_entry P((ENTRY *, ENTRY *));
! int  pascal scan_blk P((int));
! int  pascal last_entry P((void));
! int  pascal write_free P(( RECPOS, BLOCK *));
! RECPOS pascal get_free P((void));
! int  pascal find_block P((ENTRY *, int *));
! void pascal movedown P((BLOCK *, int, int));
! void pascal moveup P((BLOCK *, int, int));
! void pascal ins_block P((BLOCK *, ENTRY *, int));
! void pascal del_block P((BLOCK *, int));
! int  pascal split P((int, ENTRY *, ENTRY *));
! void pascal ins_level P((int, ENTRY *));
! int  pascal insert_ix P((ENTRY *, IX_DESC *));
! int  pascal find_ix P((ENTRY *, IX_DESC *, int));
! int  pascal combineblk P((RECPOS, int));
! void pascal replace_entry P((ENTRY *));
! void print_blk P((BLOCK *));
  
  # ifdef unix
  RECPOS filelength( fd )
***************
*** 93,98
  void pascal replace_entry(ENTRY *);
  void print_blk(BLOCK *);
  
  
  /*  file I/O for B-PLUS module  */
  

--- 97,111 -----
  void pascal replace_entry P((ENTRY *));
  void print_blk P((BLOCK *));
  
+ # ifdef unix
+ RECPOS filelength( fd )
+   int fd;
+   {
+     struct stat sb;
+     fstat( fd, &sb );
+     return( sb.st_size );
+   }
+ # endif
  
  
  /*  file I/O for B-PLUS module  */
***************
*** 94,99
  void print_blk(BLOCK *);
  
  
  /*  file I/O for B-PLUS module  */
  
  void pascal error(j, l)

--- 107,113 -----
    }
  # endif
  
+ 
  /*  file I/O for B-PLUS module  */
  
  void pascal error(j, l)
***************
*** 392,398
           pci->dx.ff = rt;
        }
      else
!       r = filelength (pci->ixfile);
      return (r);
    } /* get_free */
  

--- 406,412 -----
           pci->dx.ff = rt;
        }
      else
!       r = filelength(pci->ixfile);
      return (r);
    } /* get_free */
  
***************
*** 948,951
      insert_ix(pe, pci);
    } /* replace_entry */
  
! \032

--- 962,965 -----
      insert_ix(pe, pci);
    } /* replace_entry */
  
! 

*** orig/bplus.h	Mon Dec  5 20:08:36 1988
--- bplus.h	Mon Dec  5 20:13:39 1988
***************
*** 1,6
  
  /*  bplus.h - data structures and constants  */
  
  
  #define IX_OK       1
  #define IX_FAIL     0

--- 1,11 -----
  
  /*  bplus.h - data structures and constants  */
  
+ # ifdef _STDC_
+ # define	P(x)	x
+ # else
+ # define	P(x)	()
+ # endif
  
  # ifdef unix
  # define pascal
***************
*** 2,7
  /*  bplus.h - data structures and constants  */
  
  
  #define IX_OK       1
  #define IX_FAIL     0
  #define EOIX       (-2)

--- 7,17 -----
  # define	P(x)	()
  # endif
  
+ # ifdef unix
+ # define pascal
+ # define cdecl
+ # endif
+ 
  #define IX_OK       1
  #define IX_FAIL     0
  #define EOIX       (-2)
***************
*** 55,71
       IX_DISK  dx;
    }  IX_DESC;
  
! int cdecl open_index(char *,IX_DESC *, int);
! int cdecl close_index(IX_DESC *);
! int cdecl make_index(char *,IX_DESC *, int);
! int cdecl first_key(IX_DESC *);
! int cdecl last_key(IX_DESC *);
! int cdecl next_key(ENTRY *, IX_DESC *);
! int cdecl prev_key(ENTRY *, IX_DESC *);
! int cdecl find_key(ENTRY *, IX_DESC *);
! int cdecl add_key(ENTRY *, IX_DESC *);
! int cdecl locate_key(ENTRY *, IX_DESC *);
! int cdecl delete_key(ENTRY *, IX_DESC *);
! int cdecl find_exact(ENTRY *, IX_DESC *);
! 
! \032

--- 65,79 -----
       IX_DISK  dx;
    }  IX_DESC;
  
! int cdecl open_index P((char *,IX_DESC *, int));
! int cdecl close_index P((IX_DESC *));
! int cdecl make_index P((char *,IX_DESC *, int));
! int cdecl first_key P((IX_DESC *));
! int cdecl last_key P((IX_DESC *));
! int cdecl next_key P((ENTRY *, IX_DESC *));
! int cdecl prev_key P((ENTRY *, IX_DESC *));
! int cdecl find_key P((ENTRY *, IX_DESC *));
! int cdecl add_key P((ENTRY *, IX_DESC *));
! int cdecl locate_key P((ENTRY *, IX_DESC *));
! int cdecl delete_key P((ENTRY *, IX_DESC *));
! int cdecl find_exact P((ENTRY *, IX_DESC *));

*** orig/vtest.c	Mon Dec  5 20:08:37 1988
--- vtest.c	Mon Dec  5 20:17:25 1988
***************
*** 7,12
  #include "bplus.h"
  #include <stdio.h>
  #include <time.h>
  #include <conio.h>
  
  IX_DESC name1, name2;        /* index file variables */

--- 7,13 -----
  #include "bplus.h"
  #include <stdio.h>
  #include <time.h>
+ #ifdef MSC
  #include <conio.h>
  #endif
  
***************
*** 8,13
  #include <stdio.h>
  #include <time.h>
  #include <conio.h>
  
  IX_DESC name1, name2;        /* index file variables */
  

--- 9,15 -----
  #include <time.h>
  #ifdef MSC
  #include <conio.h>
+ #endif
  
  IX_DESC name1, name2;        /* index file variables */
  
***************
*** 100,103
      close_index(&name1);
      close_index(&name2);
    }
! \032

--- 102,106 -----
      close_index(&name1);
      close_index(&name2);
    }
! 
! 

-- 
If life was like the movies, the music would match the picture.

{sun,mtxinu,hoptoad}!rtech!gonzo!daveb		daveb at gonzo.uucp



More information about the Comp.lang.c mailing list