/*************************************************************************************************
 * gdbmtest
 * Writing test of GDBM
 *************************************************************************************************/


#include <gdbm.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define LOOPNUM        1000000
#define CACHESIZE      1021
#define DBNAME         "casket.gdbm"
#define BLOCKSIZE      4096

/* function prototypes */
int main(int argc, char **argv);


/* main routine */
int main(int argc, char **argv){
  GDBM_FILE dbf;
  int i, optval, len;
  char buf[32];
  datum key, val;
  if(!(dbf = gdbm_open(DBNAME, BLOCKSIZE, GDBM_NEWDB, 00644, NULL))){
    fprintf(stderr, "%s: %s: %s\n", argv[0], DBNAME, gdbm_strerror(gdbm_errno));
    exit(1);
  }
  optval = CACHESIZE;
  if(gdbm_setopt(dbf, GDBM_CACHESIZE, &optval, sizeof(int)) != 0){
    fprintf(stderr, "%s: %s: %s\n", argv[0], DBNAME, gdbm_strerror(gdbm_errno));
  }
  for(i = 1; i <= LOOPNUM; i++){
    len = sprintf(buf, "%08d", i);
    key.dptr = buf;
    key.dsize = len;
    val.dptr = buf;
    val.dsize = len;
    if(gdbm_store(dbf, key, val, GDBM_REPLACE) != 0){
      fprintf(stderr, "%s: %s: %s\n", argv[0], DBNAME, gdbm_strerror(gdbm_errno));
      break;
    }
    if(LOOPNUM > 250 && i % (LOOPNUM / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == LOOPNUM || i % (LOOPNUM / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  gdbm_close(dbf);
  return 0;
}



/* END OF FILE */
