1 /** @file flint_spellingwordslist.cc
2  * @brief Iterator for the spelling correction words in a flint database.
3  */
4 /* Copyright (C) 2004,2005,2006,2007,2008 Olly Betts
5  * Copyright (C) 2007 Lemur Consulting Ltd
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21 
22 
23 #include <config.h>
24 #include "flint_spellingwordslist.h"
25 
26 #include "xapian/error.h"
27 #include "xapian/types.h"
28 
29 #include "debuglog.h"
30 #include "flint_utils.h"
31 #include "stringutils.h"
32 
~FlintSpellingWordsList()33 FlintSpellingWordsList::~FlintSpellingWordsList()
34 {
35     LOGCALL_DTOR(DB, "~FlintSpellingWordsList");
36     delete cursor;
37 }
38 
39 string
get_termname() const40 FlintSpellingWordsList::get_termname() const
41 {
42     LOGCALL(DB, string, "FlintSpellingWordsList::get_termname", NO_ARGS);
43     Assert(cursor);
44     Assert(!at_end());
45     Assert(!cursor->current_key.empty());
46     Assert(cursor->current_key[0] == 'W');
47     RETURN(cursor->current_key.substr(1));
48 }
49 
50 Xapian::doccount
get_termfreq() const51 FlintSpellingWordsList::get_termfreq() const
52 {
53     LOGCALL(DB, string, "FlintSpellingWordsList::get_termfreq", NO_ARGS);
54     Assert(cursor);
55     Assert(!at_end());
56     Assert(!cursor->current_key.empty());
57     Assert(cursor->current_key[0] == 'W');
58     cursor->read_tag();
59 
60     Xapian::termcount freq;
61     const char *p = cursor->current_tag.data();
62     if (!F_unpack_uint_last(&p, p + cursor->current_tag.size(), &freq)) {
63 	throw Xapian::DatabaseCorruptError("Bad spelling word freq");
64     }
65     return freq;
66 }
67 
68 Xapian::termcount
get_collection_freq() const69 FlintSpellingWordsList::get_collection_freq() const
70 {
71     throw Xapian::InvalidOperationError("FlintSpellingWordsList::get_collection_freq() not meaningful");
72 }
73 
74 TermList *
next()75 FlintSpellingWordsList::next()
76 {
77     LOGCALL(DB, TermList *, "FlintSpellingWordsList::next", NO_ARGS);
78     Assert(!at_end());
79 
80     cursor->next();
81     if (!cursor->after_end() && !startswith(cursor->current_key, 'W')) {
82 	// We've reached the end of the prefixed terms.
83 	cursor->to_end();
84     }
85 
86     RETURN(NULL);
87 }
88 
89 TermList *
skip_to(const string & tname)90 FlintSpellingWordsList::skip_to(const string &tname)
91 {
92     LOGCALL(DB, TermList *, "FlintSpellingWordsList::skip_to", tname);
93     Assert(!at_end());
94 
95     if (!cursor->find_entry_ge("W" + tname)) {
96 	// The exact term we asked for isn't there, so check if the next
97 	// term after it also has a W prefix.
98 	if (!cursor->after_end() && !startswith(cursor->current_key, 'W')) {
99 	    // We've reached the end of the prefixed terms.
100 	    cursor->to_end();
101 	}
102     }
103     RETURN(NULL);
104 }
105 
106 bool
at_end() const107 FlintSpellingWordsList::at_end() const
108 {
109     LOGCALL(DB, bool, "FlintSpellingWordsList::at_end", NO_ARGS);
110     RETURN(cursor->after_end());
111 }
112