home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / AIAT / Examples / Sources / DemoIndexing.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-16  |  9.2 KB  |  413 lines  |  [TEXT/CWIE]

  1. /// DemoIndexing.cpp
  2. //    Copyright:    © 1994 - 1998 by Apple Computer, Inc., all rights reserved.
  3.  
  4.  
  5. #include "DemoIndex.h"
  6.  
  7. // include some utilities
  8. #include "HighFreqTerms.h"
  9. #include <stdio.h>
  10.  
  11. #ifndef __mac_os
  12. typedef char* StringPtr;
  13. #endif
  14.  
  15. #include <string.h>
  16.  
  17. #ifdef DEBUG_NEW
  18. #include <DebugNew.h>
  19. #endif
  20.  
  21. #if __profile__
  22. #include <profiler.h>
  23. #endif
  24.  
  25. const char* gtoken = "product";
  26.  
  27. //// IAIndex construction
  28.  
  29. void DemoUpdate(StringPtr folder, bool initialize = true);
  30. void DemoUpdate(StringPtr folder, bool initialize) {
  31.     // make a storage
  32.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  33.     if (initialize) {
  34.       storage->Initialize();
  35.     } else {
  36.       storage->Open(true);
  37.     }
  38.     // make an index in this storage
  39.     DemoIndex* index = new DemoIndex(storage, folder);
  40.     if (initialize) {
  41.       index->Initialize();
  42.     } else {
  43.       index->Open();
  44.     }
  45.     time_t start = time(NULL);
  46.  
  47.     index->Update();                // update the index
  48.     
  49.     storage->Commit();                // commit the changes 
  50.  
  51.     time_t duration = time(NULL) - start;
  52.     struct tm* time = localtime(&duration);
  53.     char buffer[80];
  54.     strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
  55.     printf(buffer);
  56.  
  57.     delete index;
  58.     delete storage;
  59. }
  60.  
  61.  
  62. void DemoHighFreqTerms();
  63. void DemoHighFreqTerms() {
  64.     // make a storage
  65.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  66.     IADeleteOnUnwind delStorage(storage);
  67.     storage->Open(false);
  68.     // make an index in this storage
  69.     DemoIndex index(storage);
  70.     index.Open();
  71.  
  72.     const uint32 maxResultCount = 10;
  73.  
  74.     printf("finding %lu highest frequency terms...\n", maxResultCount);
  75.     time_t start = time(NULL);
  76.  
  77.     uint32 resultCount = maxResultCount;
  78.     FreqTerm* results = GetHighFreqTerms(&index, &resultCount);
  79.  
  80.     time_t duration = time(NULL) - start;
  81.     struct tm* time = localtime(&duration);
  82.     char buffer[80];
  83.     strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
  84.     printf(buffer);
  85.  
  86.     for (uint32 i = 0; i < resultCount; i++) {
  87.       FreqTerm* ft = results + i;
  88.       printf("%lu %s\n", ft->freq, ft->term->GetData());
  89.       delete ft->term;
  90.     }
  91.     IAFreeArray(results);
  92. }
  93.  
  94. //// IAIndex compaction
  95.  
  96. void DemoCompact ();
  97. void DemoCompact () {
  98.     // make a storage
  99.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  100.     IADeleteOnUnwind delStorage(storage);
  101.     storage->Open(true);
  102.     // make an index in this storage
  103.     DemoIndex index(storage);
  104.     index.Open();
  105.  
  106.     printf("compacting...\n");
  107.     time_t start = time(NULL);
  108.  
  109.     index.Compact();
  110.     storage->Compact();
  111.  
  112.     time_t duration = time(NULL) - start;
  113.     struct tm* time = localtime(&duration);
  114.     char buffer[80];
  115.     strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
  116.     printf(buffer);
  117. }
  118.  
  119. //void DemoMerge(StringPtr folder, bool initialize = true);
  120. void DemoMerge(StringPtr folder, bool initialize = false);
  121. void DemoMerge(StringPtr folder, bool initialize) {
  122.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  123.     IADeleteOnUnwind delStorage(storage);
  124.     DemoIndex* index = new DemoIndex(storage, folder);
  125.     IADeleteOnUnwind delIndex(index);
  126.     if (initialize) {
  127.         storage->Initialize();
  128.         index->Initialize();
  129.     } else {
  130.         storage->Open(true);
  131.         index->Open();
  132.     }
  133.     
  134.     IAStorage* storage1 = DEMOSTORAGE("\p1.index");
  135.     IADeleteOnUnwind delStorage1(storage1);
  136.     storage1->Open(false);
  137.     DemoIndex index1(storage1);
  138.     index1.Open();
  139. /*
  140.     IAStorage* storage2 = DEMOSTORAGE("\p2.index");
  141.     IADeleteOnUnwind delStorage2(storage2);
  142.     storage2->Open(false);
  143.     DemoIndex index2(storage2);
  144.     index2.Open();
  145. */
  146. /*
  147.     IAStorage* storage3 = DEMOSTORAGE("\p3.index");
  148.     IADeleteOnUnwind delStorage3(storage3);
  149.     storage3->Open(false);
  150.     DemoIndex index3(storage3);
  151.     index3.Open();
  152. */
  153.  
  154. /*    IAStorage* storage4 = DEMOSTORAGE("\p4.index");
  155.     IADeleteOnUnwind delStorage4(storage4);
  156.     storage4->Open(false);
  157.     DemoIndex index4(storage4);
  158.     index4.Open();
  159. */
  160.     IAIndex* indices[3];
  161.     indices[0] = &index1;
  162.     //indices[1] = &index2;
  163.     //indices[2] = &index3;
  164.     //indices[3] = &index4;
  165.  
  166.     time_t start = time(NULL);
  167.  
  168.     index->Merge(indices, 1);
  169.     
  170.     storage->Commit();
  171.     storage->Compact();
  172.  
  173.     time_t duration = time(NULL) - start;
  174.     struct tm* time = localtime(&duration);
  175.     char buffer[80];
  176.     strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
  177.     printf(buffer);
  178. }
  179.  
  180. void DemoTermInfo ();
  181. void DemoTermInfo () {
  182.     // make a storage
  183.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  184.     IADeleteOnUnwind delStorage(storage);
  185.     storage->Open(true);
  186.     // make an index in this storage
  187.     DemoIndex index(storage);
  188.     index.Open();
  189.     IATerm*  term = new IATerm((unsigned char*)gtoken, strlen(gtoken));
  190.     TermInfo* terminfo = index.GetTermInfo(term);
  191.     printf ("Term Info before delete %d\n", terminfo->GetDocumentCount());
  192.     DocID maxID = index.GetMaxDocID();
  193.  
  194.     if (maxID > 1) {
  195.       IADoc* doc = index.GetIDDoc(maxID / 2);
  196.       IADeleteOnUnwind delDoc(doc);
  197.           time_t start = time(NULL);
  198.  
  199.           index.DeleteDoc(doc);
  200.           
  201.           time_t duration = time(NULL) - start;
  202.         struct tm* time = localtime(&duration);
  203.         char buffer[80];
  204.         strftime(buffer, 80, "time: %H hours, %M minutes and %S seconds.\n", time);
  205.         printf(buffer);
  206.  
  207.         index.Flush();
  208.         storage->Commit();            // commit the changes
  209.     }
  210.     TermInfo* terminfo2 = index.GetTermInfo(term);
  211.     printf ("Term Info after delete %d\n", terminfo2->GetDocumentCount());
  212.     
  213.     index.Flush();
  214.     storage->Commit();            // commit the changes
  215.     
  216.     delete terminfo;
  217.     delete terminfo2;
  218.     
  219.     delete term;
  220. }
  221.  
  222. /// the main procedure
  223. void* MyAllocator(size_t size);
  224. void* MyAllocator(size_t size)
  225. {
  226. //    printf ("called the local allocator for %d bytes\n", size);
  227.     void* l = (void*)malloc(size);
  228.     return l;
  229. }
  230.  
  231. void MyDeAllocator(void* obj);
  232. void MyDeAllocator(void* obj)
  233. {
  234. //    printf ("called the local Deallocator\n");
  235.     free(obj);
  236. }
  237.  
  238. void DemoOneStep(StringPtr folder, bool initialize = true);
  239. void DemoOneStep(StringPtr folder, bool initialize) {
  240.     // make a storage
  241.     IAStorage* storage = DEMOSTORAGE(DEMOINDEXFILENAME);
  242.     IADeleteOnUnwind delStorage(storage);
  243.     if (initialize) {
  244.       storage->Initialize();
  245.     } else {
  246.       storage->Open(true);
  247.     }
  248.     // make an index in this storage
  249.     DemoIndex* index = new DemoIndex(storage, folder);
  250.     IADeleteOnUnwind delIndex(index);
  251.     if (initialize) {
  252.       index->Initialize();
  253.     } else {
  254.       index->Open();
  255.     }
  256.  
  257.     index->Update();                // update the index
  258.     index->Flush();
  259.     index->Compact();
  260.     storage->Commit();                // commit the changes 
  261.     storage->Compact();
  262.     
  263.     
  264.     const uint32 maxResultCount = 10;
  265.  
  266.     uint32 resultCount = maxResultCount;
  267.     FreqTerm* results = GetHighFreqTerms(index, &resultCount);
  268.  
  269.     for (uint32 i = 0; i < resultCount; i++) {
  270.         char buffer[256];
  271.  
  272.       FreqTerm* ft = results + i;
  273.       sprintf(buffer, "%lu %s\r", ft->freq, ft->term->GetData());
  274.       delete ft->term;
  275.     }
  276.     IAFreeArray(results);
  277. }
  278.  
  279. #if 0
  280. void DemoAdd();
  281. void DemoAdd()
  282. {
  283.     StringPtr name = "\ptest.index";
  284.     short vRefNum = 0;
  285.     long dirID = 0;
  286.     StringPtr docName = "\pEagle";
  287.     short docVRefNum = 0;
  288.     long docDirID = 0;
  289. // create storage for the index
  290.     IAStorage * aStorage = MakeHFSStorage(vRefNum, dirID, name);
  291.     IADeleteOnUnwind delInxStorage(aStorage);
  292.     aStorage->Initialize();
  293. // create the corpus
  294.     HFSCorpus* anHFSCorpus = new HFSCorpus(HFSCorpusType);
  295. // create the HFS Doc
  296.     HFSDoc *anHFSDoc = new HFSDoc(anHFSCorpus, docVRefNum, docDirID, docName);
  297. // create index for folder (creates analysis)
  298.     InVecIndex anInVecIndex(aStorage, anHFSCorpus, new SimpleAnalysis());
  299.     anInVecIndex.Initialize();
  300.     anInVecIndex.AddDoc(anHFSDoc);
  301. // complete the update
  302.     anInVecIndex.Flush();
  303. // commit the storage to disk
  304.     aStorage->Commit ();
  305.     printf ("Successful Completion\n");
  306.  
  307. }
  308. #endif
  309.  
  310. StringPtr GetFolderPath();
  311. StringPtr GetFolderPath()
  312. {
  313.     char lpath[1024];
  314.     int i = 0;
  315.     printf ("Enter Folder Path >> ");
  316.     while (!feof(stdin)) {
  317.          char cc = fgetc(stdin);
  318.          if (cc == '\n') break;
  319.          lpath[i++] = cc;
  320.     }
  321.      lpath[i] = '\0';
  322.      if (i == 0) return NULL;
  323.      StringPtr    folder = (StringPtr)IAMallocArraySized(byte, i + 1);
  324.      
  325. #ifdef __mac_os
  326.      folder[0] = i;
  327.      memcpy(folder + 1, lpath, i);
  328. #else
  329.      memcpy(folder, lpath, i);
  330.      folder[i] = '\0';
  331. #endif
  332.      return folder;    
  333. }
  334.  
  335. void main() {
  336.  
  337.  
  338. #if __profile__
  339. #ifdef powerc
  340.     ProfilerInit(collectDetailed, PPCTimeBase, 1500, 50);
  341. #else
  342.     ProfilerInit(collectSummary, microsecondsTimeBase, 1500, 50);
  343. #endif    
  344. #endif
  345.  
  346.     // make this bigger to use more memory and index faster
  347.     IADiskBlockSize = 4096;
  348.     
  349. //    IAAllocationFunc = &MyAllocator;
  350. //    IADeAllocationFunc = &MyDeAllocator;
  351.  
  352.     StringPtr    folder = GetFolderPath();
  353.     
  354.     IATry {
  355.     
  356.         // enter the name of the folder to index here
  357.     
  358. //        StringPtr folder = "\pMacintoshHD:Finder:AIAT:Examples:Projects:Test Collection";
  359.  
  360.           
  361.         // uncomment this section to build a new index
  362. //        for (int i = 0; i < 3; i++) {
  363. //            if (i == 0) {
  364. //                DemoOneStep(folder);
  365. //            }
  366. //            else {
  367. //                DemoOneStep(folder, false);
  368. //            }
  369. //        }
  370. //        DemoAdd();
  371.  
  372.         if (folder != NULL) {
  373.              DemoUpdate(folder);
  374.         }
  375.  
  376. //         DemoTermInfo();
  377. //         DemoCompact();
  378.  
  379. //        DemoHighFreqTerms();
  380.  
  381.         // uncomment this section to update an existing index
  382. //        DemoUpdate(folder, false);
  383. //        DemoCompact();
  384. //        DemoHighFreqTerms();
  385.  
  386.  
  387. //        DemoMerge(folder);
  388.     }
  389.     IACatch (const IAException& exception) {
  390.         printf("Caught exception: %s\n", exception.What());
  391.     }
  392.     if (folder != NULL) {
  393. #ifdef __mac_os    
  394.         IAFreeArraySized(folder, byte, folder[0] + 1);
  395. #else    
  396.         IAFreeArraySized(folder, byte, strlen(folder) + 1);
  397. #endif
  398.     }
  399.     
  400.     IAReportMemoryUsage();
  401.  
  402.  
  403. #if __profile__
  404.     ProfilerDump("\pDemoIndex.prof");
  405.     ProfilerTerm();
  406. #endif
  407.  
  408. #ifdef DEBUG_NEW
  409.     DebugNewReportLeaks();
  410. #endif
  411. }
  412.  
  413.