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