xapian-core  1.4.25
slowvaluelist.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2008,2011,2013,2014,2018 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (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 
23 #include "slowvaluelist.h"
24 
25 #include "document.h"
26 #include "str.h"
28 
29 #include "xapian/error.h"
30 
31 #include "autoptr.h"
32 
33 using namespace std;
34 
37 {
38  return current_did;
39 }
40 
41 string
43 {
44  return current_value;
45 }
46 
49 {
50  return slot;
51 }
52 
53 bool
55 {
56  return last_docid == 0;
57 }
58 
59 void
61 {
62  // Compare before incrementing, since last_docid could be the largest
63  // representable value in which case current_did will wrap to zero before
64  // exceeding it.
65  while (current_did++ < last_docid) {
66  try {
67  // Open document lazily so that we don't waste time checking for
68  // its existence.
69  void * d = db->open_document(current_did, true);
70  if (!d)
71  continue;
72  AutoPtr<Xapian::Document::Internal>
73  doc(static_cast<Xapian::Document::Internal*>(d));
74  string value = doc->get_value(slot);
75  if (!value.empty()) {
76  swap(current_value, value);
77  return;
78  }
79  } catch (const Xapian::DocNotFoundError &) {
80  }
81  }
82 
83  // Indicate that we're at_end().
84  last_docid = 0;
85 }
86 
87 void
89 {
90  if (did <= current_did) return;
91  current_did = did - 1;
92  next();
93 }
94 
95 bool
97 {
98  if (did <= current_did) {
99  return !current_value.empty();
100  }
101 
102  if (did > last_docid) {
103  // Indicate that we're at_end().
104  last_docid = 0;
105  return true;
106  }
107 
108  current_did = did;
109  try {
110  void * d = db->open_document(current_did, true);
111  if (d) {
112  AutoPtr<Xapian::Document::Internal>
113  doc(static_cast<Xapian::Document::Internal*>(d));
114  current_value = doc->get_value(slot);
115  if (!current_value.empty()) return true;
116  }
117  } catch (const Xapian::DocNotFoundError &) {
118  }
119  current_value = string();
120  return false;
121 }
122 
123 string
125 {
126  string desc = "SlowValueList(slot=";
127  desc += str(slot);
128  if (last_docid != 0) {
129  desc += ", docid=";
130  desc += str(current_did);
131  desc += ", value=\"";
132  description_append(desc, current_value);
133  desc += "\")";
134  } else {
135  desc += ", atend)";
136  }
137  return desc;
138 }
bool at_end() const
Return true if the current position is past the last entry in this list.
Xapian::docid get_docid() const
Return the docid at the current position.
STL namespace.
Convert types to std::string.
bool check(Xapian::docid did)
Check if the specified docid occurs in this valuestream.
Hierarchy of classes which Xapian can throw as exceptions.
void description_append(std::string &desc, const std::string &s)
Definition: unittest.cc:102
string str(int value)
Convert int to std::string.
Definition: str.cc:90
std::string get_description() const
Return a string description of this object.
Indicates an attempt to access a document not present in the database.
Definition: error.h:674
std::string get_value() const
Return the value at the current position.
Append a string to an object description, escaping invalid UTF-8.
Slow implementation for backends which don&#39;t streamed values.
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
void next()
Advance the current position to the next document in the value stream.
void skip_to(Xapian::docid)
Skip forward to the specified docid.
Xapian::valueno get_valueno() const
Return the value slot for the current position/this iterator.
Wrapper around standard unique_ptr template.