home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / AIAT 1.0.1 / Examples / Sources / DemoIndexing.cpp < prev   
Encoding:
C/C++ Source or Header  |  1997-09-11  |  8.6 KB  |  370 lines  |  [TEXT/CWIE]

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