xapian-core  1.4.21
glass_valuelist.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2008,2009 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 "glass_valuelist.h"
24 
25 #include "glass_cursor.h"
26 #include "glass_database.h"
27 #include "omassert.h"
28 #include "str.h"
29 
30 using namespace Glass;
31 using namespace std;
32 
33 bool
35 {
36  Xapian::docid first_did = docid_from_key(slot, cursor->current_key);
37  if (!first_did) return false;
38 
39  cursor->read_tag();
40  const string & tag = cursor->current_tag;
41  reader.assign(tag.data(), tag.size(), first_did);
42  return true;
43 }
44 
46 {
47  delete cursor;
48 }
49 
52 {
53  Assert(!at_end());
54  return reader.get_docid();
55 }
56 
59 {
60  return slot;
61 }
62 
63 std::string
65 {
66  Assert(!at_end());
67  return reader.get_value();
68 }
69 
70 bool
72 {
73  return cursor == NULL;
74 }
75 
76 void
78 {
79  if (!cursor) {
80  cursor = db->get_postlist_cursor();
81  if (!cursor) return;
82  cursor->find_entry_ge(make_valuechunk_key(slot, 1));
83  } else if (!reader.at_end()) {
84  reader.next();
85  if (!reader.at_end()) return;
86  cursor->next();
87  }
88 
89  if (!cursor->after_end()) {
90  if (update_reader()) {
91  if (!reader.at_end()) return;
92  }
93  }
94 
95  // We've reached the end.
96  delete cursor;
97  cursor = NULL;
98 }
99 
100 void
102 {
103  if (!cursor) {
104  cursor = db->get_postlist_cursor();
105  if (!cursor) return;
106  } else if (!reader.at_end()) {
107  reader.skip_to(did);
108  if (!reader.at_end()) return;
109  }
110 
111  if (!cursor->find_entry(make_valuechunk_key(slot, did))) {
112  if (update_reader()) {
113  reader.skip_to(did);
114  if (!reader.at_end()) return;
115  }
116  // The requested docid is between two chunks.
117  cursor->next();
118  }
119 
120  // Either an exact match, or in a gap before the start of a chunk.
121  if (!cursor->after_end()) {
122  if (update_reader()) {
123  if (!reader.at_end()) return;
124  }
125  }
126 
127  // We've reached the end.
128  delete cursor;
129  cursor = NULL;
130 }
131 
132 bool
134 {
135  if (!cursor) {
136  cursor = db->get_postlist_cursor();
137  if (!cursor) return true;
138  } else if (!reader.at_end()) {
139  // Check for the requested docid in the current block.
140  reader.skip_to(did);
141  if (!reader.at_end()) return true;
142  }
143 
144  // Try moving to the appropriate chunk.
145  if (!cursor->find_entry(make_valuechunk_key(slot, did))) {
146  // We're in a chunk which might contain the docid.
147  if (update_reader()) {
148  reader.skip_to(did);
149  if (!reader.at_end()) return true;
150  }
151  return false;
152  }
153 
154  // We had an exact match for a chunk starting with specified docid.
155  Assert(!cursor->after_end());
156  if (!update_reader()) {
157  // We found the exact key we built, so it must match the slot.
158  // Therefore update_reader() "can't possibly fail".
159  Assert(false);
160  }
161 
162  return true;
163 }
164 
165 string
167 {
168  string desc("GlassValueList(slot=");
169  desc += str(slot);
170  desc += ')';
171  return desc;
172 }
void skip_to(Xapian::docid)
Skip forward to the specified docid.
#define Assert(COND)
Definition: omassert.h:122
void next()
Advance the current position to the next document in the value stream.
Xapian::docid docid_from_key(Xapian::valueno required_slot, const std::string &key)
Definition: glass_values.h:50
Glass class for value streams.
STL namespace.
std::string make_valuechunk_key(Xapian::valueno slot, Xapian::docid did)
Generate a key for a value stream chunk.
Definition: glass_values.h:41
Convert types to std::string.
bool update_reader()
Update reader to use the chunk currently pointed to by cursor.
string str(int value)
Convert int to std::string.
Definition: str.cc:90
C++ class definition for glass database.
std::string get_value() const
Return the value at the current position.
std::string get_description() const
Return a string description of this object.
Interface to Btree cursors.
Xapian::docid get_docid() const
Return the docid at the current position.
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
bool at_end() const
Return true if the current position is past the last entry in this list.
Various assertion macros.
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
bool check(Xapian::docid did)
Check if the specified docid occurs in this valuestream.
Xapian::valueno get_valueno() const
Return the value slot for the current position/this iterator.