/*************************************************************************************************
 * tdbtest
 * Writing test of TDB
 *************************************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <tdb.h>

#define LOOPNUM        1000000
#define HASHSIZE       2000000
#define DBNAME         "casket.tdb"


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


/* main routine */
int main(int argc, char **argv){
  TDB_CONTEXT *tdb;
  int i, len;
  char buf[32];
  TDB_DATA key, val;
  if(!(tdb = tdb_open(DBNAME, HASHSIZE, 0, O_RDWR | O_CREAT | O_TRUNC, 00644))){
    fprintf(stderr, "%s: %s: tdb_open failed\n", argv[0], DBNAME);
    exit(1);
  }
  for(i = 1; i <= LOOPNUM; i++){
    len = sprintf(buf, "%08d", i);
    key.dptr = buf;
    key.dsize = len;
    val.dptr = buf;
    val.dsize = len;
    if(tdb_store(tdb, key, val, TDB_REPLACE) != 0){
      fprintf(stderr, "%s: %s: tdb_store failed\n", argv[0], DBNAME);
      break;
    }
    if(LOOPNUM > 250 && i % (LOOPNUM / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == LOOPNUM || i % (LOOPNUM / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  tdb_close(tdb);
  return 0;
}



/* END OF FILE */
