1 //
2 // Part of the ht://Dig package   <http://www.htdig.org/>
3 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group
4 // For copyright details, see the file COPYING in your distribution
5 // or the GNU General Public License version 2 or later
6 // <http://www.gnu.org/copyleft/gpl.html>
7 //
8 // $Id: WordSearch.h,v 1.4 2001/06/29 14:14:08 loic Exp $
9 //
10 // ************************* WordSearch implementation ********************
11 //
12 // NAME
13 //
14 // Solve a query from a WordTree syntax tree
15 //
16 // SYNOPSIS
17 //
18 // #include <WordSearch.h>
19 //
20 // WordTree* expr = get_query();
21 // WordSearch search;
22 // search.limit_count = NUMBER_OF_RESULTS;
23 // WordMatch* search.Search(expr);
24 // ...
25 //
26 // DESCRIPTION
27 //
28 // The WordSearch class is a wrapper to query an inverted index
29 // using a WordTree syntax tree.
30 //
31 // END
32 //
33 
34 #ifndef _WordSearch_h
35 #define _WordSearch_h
36 
37 #include <WordList.h>
38 #include <WordMatch.h>
39 #include <WordTree.h>
40 
41 class WordSearch {
42 public:
43   WordSearch(WordList* words);
44 
~WordSearch()45   ~WordSearch() {
46     if(expr) delete expr;
47   }
48 
49   //-
50   // Set the list of documents that must be ignored.
51   //
SetResults(WordResults * nresults)52   inline int SetResults(WordResults* nresults) { results = nresults; return OK; }
53   //-
54   // Get the list of documents that must be ignored.
55   //
GetResults()56   inline WordResults* GetResults() { return results; }
57 
58   //-
59   // Save the context of the last document retrieved in the context_out
60   // data member.
61   //
62   int ContextSave(int status);
63   //-
64   // Restore a search context from the context_in data member.
65   //
66   int ContextRestore();
67 
68   //-
69   // First call SearchFromCache and then SearchFromIndex if SearchFromCache
70   // returned 0.
71   //
72   WordMatches *Search();
73   //-
74   // Attempt to retrieve the results from the cache. Returns 0 if
75   // the search cannot be resolved from the cache. Returns the list
76   // of matches if the search can be resolved from the cache. If the
77   // cache only contains part of the desired results, call SearchFromIndex
78   // to get the others. If the cache does not contain any of the desired
79   // results, return 0.
80   //
81   int SearchFromCache();
82   //-
83   // Perform a search from the <b>expr</b> specifications.
84   // Restore the context from <i>context_in</i> on <b>expr</b>.
85   // Then skip (using WalkNext) <i>limit_bottom</i> entries.
86   // Then collect in a WordMatch array of size <i>limit_count</i>
87   // each match returned by WalkNext. When finished store
88   // the context (ContextSave) in <i>context_out</i>.
89   // It is the responsibility of the caller to free the WordMatch
90   // array. If no match are found a null pointer is returned.
91   //
92   int SearchFromIndex(unsigned int length);
93 
94   //-
95   // Search backend, only run the WalkNext loop but does not
96   // allocate/deallocate data. If limit_bottom is above all matches
97   // return the last valid limit_count range and reset limit_bottom
98   // accordingly.
99   //
100   int SearchLoop(WordTree *expr, unsigned int length);
101 
Verbose(int verbosity)102   inline int Verbose(int verbosity) { return verbose = verbosity; }
103 
104   //
105   // Internal
106   //
107   WordList* words;
108   //-
109   // A list of documents that must be ignored
110   //
111   WordResults *results;
112   int verbose;
113 
114   //
115   // Input/Output
116   //
117   //
118   // Input: Absolute position of the document pointed by context_in. The
119   //        limit_bottom position is relative to limit_base.
120   // Output: Absolute position of the first document returned.
121   //
122   unsigned int limit_base;
123 
124   //
125   // Input
126   //
127   //
128   // Maximum number of matches returned
129   //
130   unsigned int limit_count;
131   //
132   // Query tree
133   //
134   WordTree* expr;
135 
136   //
137   // Output
138   //
139   //
140   // Array of at most limit_count matches. The number of valid elements in
141   // the array is matches_length;
142   //
143   WordMatches* matches;
144   //
145   // Estimated number of matches.
146   //
147   unsigned int matches_total;
148 };
149 
150 #endif /* _WordSearch_h */
151