1 /** @file flint_alldocspostlist.cc
2  * @brief A PostList which iterates over all documents in a FlintDatabase.
3  */
4 /* Copyright (C) 2006,2007,2008,2009 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20 
21 #include <config.h>
22 #include "flint_alldocspostlist.h"
23 
24 #include <string>
25 
26 #include "debuglog.h"
27 #include "flint_database.h"
28 #include "str.h"
29 
30 using namespace std;
31 
32 Xapian::doccount
get_termfreq() const33 FlintAllDocsPostList::get_termfreq() const
34 {
35     return doccount;
36 }
37 
38 Xapian::docid
get_docid() const39 FlintAllDocsPostList::get_docid() const
40 {
41     return current_did;
42 }
43 
44 Xapian::termcount
get_doclength() const45 FlintAllDocsPostList::get_doclength() const
46 {
47     LOGCALL(DB, Xapian::termcount, "FlintAllDocsPostList::get_doclength", NO_ARGS);
48     Assert(current_did);
49 
50     cursor->read_tag();
51 
52     if (cursor->current_tag.empty()) RETURN(0);
53 
54     const char * pos = cursor->current_tag.data();
55     const char * end = pos + cursor->current_tag.size();
56 
57     flint_doclen_t doclen;
58     if (!F_unpack_uint(&pos, end, &doclen)) {
59 	const char *msg;
60 	if (pos == 0) {
61 	    msg = "Too little data for doclen in termlist";
62 	} else {
63 	    msg = "Overflowed value for doclen in termlist";
64 	}
65 	throw Xapian::DatabaseCorruptError(msg);
66     }
67 
68     RETURN(doclen);
69 }
70 
71 Xapian::termcount
get_wdf() const72 FlintAllDocsPostList::get_wdf() const
73 {
74     LOGCALL(DB, Xapian::termcount, "FlintAllDocsPostList::get_wdf", NO_ARGS);
75     Assert(current_did);
76     RETURN(1);
77 }
78 
79 PostList *
read_did_from_current_key()80 FlintAllDocsPostList::read_did_from_current_key()
81 {
82     LOGCALL(DB, PostList *, "FlintAllDocsPostList::read_did_from_current_key", NO_ARGS);
83     const string & key = cursor->current_key;
84     const char * pos = key.data();
85     const char * end = pos + key.size();
86     if (!F_unpack_uint_preserving_sort(&pos, end, &current_did)) {
87 	const char *msg;
88 	if (pos == 0) {
89 	    msg = "Too little data in termlist key";
90 	} else {
91 	    msg = "Overflowed value in termlist key";
92 	}
93 	throw Xapian::DatabaseCorruptError(msg);
94     }
95 
96     // Return NULL to help the compiler tail-call optimise our callers.
97     RETURN(NULL);
98 }
99 
100 PostList *
next(Xapian::weight)101 FlintAllDocsPostList::next(Xapian::weight /*w_min*/)
102 {
103     LOGCALL(DB, PostList *, "FlintAllDocsPostList::next", Literal("/*w_min*/"));
104     Assert(!at_end());
105     if (!cursor->next()) RETURN(NULL);
106     RETURN(read_did_from_current_key());
107 }
108 
109 PostList *
skip_to(Xapian::docid did,Xapian::weight)110 FlintAllDocsPostList::skip_to(Xapian::docid did, Xapian::weight /*w_min*/)
111 {
112     LOGCALL(DB, PostList *, "FlintAllDocsPostList::skip_to", did | Literal("/*w_min*/"));
113 
114     if (did <= current_did || at_end()) RETURN(NULL);
115 
116     if (cursor->find_entry_ge(F_pack_uint_preserving_sort(did))) {
117 	// The exact docid that was asked for exists.
118 	current_did = did;
119 	RETURN(NULL);
120     }
121     if (cursor->after_end()) RETURN(NULL);
122 
123     RETURN(read_did_from_current_key());
124 }
125 
126 bool
at_end() const127 FlintAllDocsPostList::at_end() const {
128     LOGCALL(DB, bool, "FlintAllDocsPostList::at_end", NO_ARGS);
129     RETURN(cursor->after_end());
130 }
131 
132 string
get_description() const133 FlintAllDocsPostList::get_description() const
134 {
135     string desc = "FlintAllDocsPostList(did=";
136     desc += str(current_did);
137     desc += ",doccount=";
138     desc += str(doccount);
139     desc += ')';
140     return desc;
141 }
142