home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / blt2_214.zip / src / bd_ko.c < prev    next >
C/C++ Source or Header  |  1996-10-16  |  7KB  |  239 lines

  1.  
  2. /* bd_ko.c - 30-Sep-1996 Cornel Huth
  3.  * This module is called by bd_main.c
  4.  * Key-only (uses external data (non-BULLET data file) key store)
  5.  */
  6.  
  7. #include "platform.h"
  8.  
  9. // external to this module
  10.  
  11. void PutMsg(CHAR *strg);
  12. void GetMsg(CHAR *strg);
  13.  
  14. extern CHAR *collateTable;
  15.  
  16. #if FOR_WINDOWS == 1
  17.  void DoWinThing(int waitFlag);
  18. #endif
  19.  
  20. // public in this module
  21.  
  22. int bd_ko(void);
  23.  
  24.  
  25. // GO
  26.  
  27. int bd_ko(void) {
  28.  
  29. #pragma pack(1)
  30.  
  31.  ACCESSPACK AP;
  32.  DOSFILEPACK DFP;
  33.  CREATEINDEXPACK CIP;
  34.  HANDLEPACK HP;
  35.  OPENPACK OP;
  36.  
  37. #pragma pack()
  38.  
  39.  time_t startTime, endTime;
  40.  int display = 0;                // display results or not flag
  41.  int sameSequence = 1;           // use same sequence for keys or new flag
  42.  int binaryKey = 0;              // store key as binary 32-bit flag
  43.  
  44.  LONG rndSeqVar = 0;             // used to construct on-the-fly key
  45.  
  46.  CHAR nameIX3[] = ".\\$bd_ko.ix3";  // name of index file created
  47.  ULONG indexID=0;                // handle of index file
  48.  CHAR keyExpression[128];        // key expression string buffer (159 max)
  49.  CHAR keyBuffer[68];             // buffer used to store/receive key values
  50.  
  51.  LONG keys2add;                  // keys to store
  52.  LONG i;                         // counter
  53.  CHAR tmpStr[128];               // misc stuff, non-Bullet related
  54.  CHAR putStr[128];               // message output string
  55.  LONG rez;                       // return value from Bullet
  56.  LONG dupHits = 0;               // duplicate keys generated from rand()
  57.  
  58.  
  59.  // Delete previous files from any previous run (disregard any error return)
  60.  
  61.  DFP.func = DELETE_FILE_DOS;
  62.  DFP.filenamePtr = nameIX3;
  63.  rez = BULLET(&DFP);
  64.  
  65.  strcpy(keyExpression,"EXTERNAL DATA SO NOT PARSED BUT IS STORED");
  66.  
  67.  CIP.func = CREATE_INDEX_XB;
  68.  CIP.filenamePtr = nameIX3;
  69.  CIP.keyExpPtr = keyExpression;
  70.  CIP.xbLink = -1;                // must be -1 to use external data
  71.                                  // sort by ASCII (or any valid sort-cmp func)
  72.  CIP.sortFunction = (8 << 8) | ASCII_SORT;  // the 8 is the key size
  73.  
  74.  CIP.codePage = CODEPAGE;        // from bullet_2.h
  75.  CIP.countryCode = CTRYCODE;     //
  76.  CIP.collatePtr = NULL;          // since ASCII_SORT, no collate table is used
  77.  CIP.nodeSize = 512;             // 512-byte node size (or 1024, 2048 bytes)
  78.  rez = BULLET(&CIP);
  79.  if (rez) {
  80.     sprintf(putStr,"Failed index file create.  Err: %d\n",rez);
  81.     PutMsg(putStr);
  82.     goto Abend;
  83.  }
  84.  
  85.  OP.func = OPEN_INDEX_XB;
  86.  OP.filenamePtr = nameIX3;
  87.  OP.asMode = READWRITE | DENYNONE; // must always specify a sharing mode
  88.  OP.xbLink = -1;                   // must be -1 to use external data
  89.  rez = BULLET(&OP);
  90.  if (rez) {
  91.     sprintf(putStr,"Failed index file open.  Err: %d\n",rez);
  92.     PutMsg(putStr);
  93.     goto Abend;
  94.  }
  95.  indexID = OP.handle;
  96.  
  97.  
  98.  PutMsg("Display keys and key data (slower results)? (y/N) ");
  99.  GetMsg(tmpStr);
  100.  if (*tmpStr=='y') display = 1;
  101.  
  102.  PutMsg("           Repeat same random key sequence? (Y/n) ");
  103.  GetMsg(tmpStr);
  104.  if (*tmpStr=='n') sameSequence = 0;
  105.  
  106.  PutMsg("                          How many keys to store? ");
  107.  GetMsg(tmpStr);
  108.  keys2add = atol(tmpStr);
  109.  if (keys2add < 1) keys2add = 1;       // would you rather end the test?
  110.  if (keys2add > 9999999) keys2add = 1; // why wait around for so many?
  111.  
  112.  if (sameSequence)
  113.     srand(1);
  114.  else
  115.     srand((unsigned)time(0));
  116.  
  117.  sprintf(putStr,"  Storing %d keys... ",keys2add);
  118.  PutMsg(putStr);
  119.  if (display) PutMsg("\n");
  120.  
  121.  time(&startTime);
  122.  
  123.  AP.func = STORE_KEY_XB;
  124.  AP.handle = indexID;
  125.  AP.keyPtr = keyBuffer;
  126.  AP.recNo = 1;           // 32-bit data attached, start at 1 (why not)
  127.  
  128.  if (binaryKey) {
  129.     PutMsg("binaryKey version is not coded\n");
  130.  }
  131.  else {
  132.     for (i = 1; i <= keys2add; i++) {
  133.        rndSeqVar = (rand() >> 3) * (rand() >> 3);   // 4095*4095=16769025 max
  134.        sprintf(tmpStr,"%8.8i",rndSeqVar);
  135.        strncpy(keyBuffer,tmpStr,8);
  136.  
  137.        rez = BULLET(&AP);
  138.        if (rez) {
  139.  
  140.           // tried to insert a 'random' key that was already in the index; since
  141.           // this is not INSERT_XB, dups are not automatically handled -- here
  142.           // they're just skipped and another store is done to cover the total
  143.  
  144.           if (rez==EXB_KEY_EXISTS) {
  145.              dupHits++;
  146.              i--;                // do another key store (reuse AP.recNo)
  147.           }
  148.           else {
  149.              sprintf(putStr,"Failed storing key %s  %d.  Err: %d\n",keyBuffer,i,rez);
  150.              PutMsg(putStr);
  151.              goto Abend;
  152.           }
  153.        }
  154.        else {
  155.            if (display) {
  156.               sprintf(putStr,"%8.8s  %9.9lu\r", keyBuffer, AP.recNo);
  157.               PutMsg(putStr);
  158.            }
  159.           AP.recNo++;
  160.        }
  161.  
  162. #if FOR_WINDOWS == 1                      // deal with message queue every now and then
  163.        if (i % Q_INS == 0) DoWinThing(0); // 0=no waiting around
  164. #endif
  165.  
  166.     }
  167.  
  168.  }
  169.  time(&endTime);
  170.  if (display) PutMsg("\n...");
  171.  
  172.  sprintf(putStr,"took %u secs.",(endTime - startTime));
  173.  PutMsg(putStr);
  174.  
  175.  if (dupHits) {
  176.     sprintf(putStr," (had %d duplicates--ignored)\n",dupHits);
  177.     PutMsg(putStr);
  178.  }
  179.  else {
  180.     PutMsg("\n");
  181.  }
  182.  
  183.  memset(keyBuffer,0,sizeof(keyBuffer)); // gives the \0 to the returned key
  184.  
  185.  sprintf(putStr,"Accessing %d keys in order... ",keys2add);
  186.  PutMsg(putStr);
  187.  if (display) PutMsg("\n");
  188.  time(&startTime);
  189.  AP.func = FIRST_KEY_XB;
  190.  AP.handle = indexID;
  191.  AP.keyPtr = keyBuffer;
  192.  rez = BULLET(&AP);
  193.  i=0;
  194.  while (rez==0) {
  195.     i++;
  196.     if (display) {
  197.        sprintf(putStr,"%8.8s  %9.9lu\r", keyBuffer, AP.recNo);
  198.        PutMsg(putStr);
  199.     }
  200.     AP.func = NEXT_KEY_XB;
  201.     rez = BULLET(&AP);
  202.  
  203. #if FOR_WINDOWS == 1                   // deal with message queue every now and then
  204.     if (i % Q_KEY == 0) DoWinThing(0); // 0=no waiting around
  205. #endif
  206.  
  207.  }
  208.  time(&endTime);
  209.  if (display) PutMsg("\n...");
  210.  
  211.  // expected rez is EXB_END_OF_FILE
  212.  
  213.  if (rez == EXB_END_OF_FILE) rez=0;
  214.  if (rez) {
  215.     sprintf(putStr,"Failed KEY access.  Err: %d\n",rez);
  216.     PutMsg(putStr);
  217.     goto Abend;
  218.  }
  219.  sprintf(putStr,"took %u secs. for %d keys\n",(endTime - startTime),i);
  220.  PutMsg(putStr);
  221.  
  222.  
  223.  // Fatal errors above come straight to here
  224.  Abend:
  225.  
  226.  // Close file
  227.  
  228.  if (indexID) {
  229.     HP.func = CLOSE_INDEX_XB;
  230.     HP.handle = indexID;
  231.     rez = BULLET(&HP);
  232.     if (rez) {
  233.        sprintf(putStr,"Failed index file close.  Err: %d\n",rez);
  234.        PutMsg(putStr);
  235.     }
  236.  }
  237.  return rez;  // module exit
  238. }
  239.