xapian-core  2.0.0
honey_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, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "honey_valuelist.h"
24 
25 #include "honey_cursor.h"
26 #include "honey_database.h"
27 #include "omassert.h"
28 #include "str.h"
29 
30 using namespace Honey;
31 using namespace std;
32 
33 bool
35 {
36  Xapian::docid last_did = docid_from_key(slot, cursor->current_key);
37  if (!last_did) return false;
38 
39  cursor->read_tag();
40  const string& tag = cursor->current_tag;
41  reader.assign(tag.data(), tag.size(), last_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_ge(make_valuechunk_key(slot, did))) {
112  // Exact match.
113  if (rare(!update_reader())) {
114  // Shouldn't be possible.
115  Assert(false);
116  }
117  reader.skip_to(did);
118  if (!at_end()) return;
119  // The chunk's last docid is did, so skip_to() should always succeed.
120  Assert(false);
121  } else if (!cursor->after_end()) {
122  if (update_reader()) {
123  reader.skip_to(did);
124  if (!reader.at_end()) return;
125  // The chunk's last docid is >= did, so skip_to() shouldn't reach
126  // the end.
127  Assert(false);
128  }
129  }
130 
131  // We've reached the end.
132  delete cursor;
133  cursor = NULL;
134 }
135 
136 string
138 {
139  string desc("HoneyValueList(slot=");
140  desc += str(slot);
141  desc += ')';
142  return desc;
143 }
std::string get_value() const
Return the value at the current position.
Xapian::valueno get_valueno() const
Return the value slot for the current position/this iterator.
void skip_to(Xapian::docid)
Skip forward to the specified docid.
bool at_end() const
Return true if the current position is past the last entry in this list.
void next()
Advance the current position to the next document in the value stream.
Xapian::docid get_docid() const
Return the docid at the current position.
std::string get_description() const
Return a string description of this object.
bool update_reader()
Update reader to use the chunk currently pointed to by cursor.
#define rare(COND)
Definition: config.h:607
HoneyCursor class.
Database using honey backend.
Honey class for value streams.
std::string make_valuechunk_key(Xapian::valueno slot, Xapian::docid last_did)
Generate a key for a value stream chunk.
Definition: honey_values.h:39
Xapian::docid docid_from_key(const std::string &key)
string str(int value)
Convert int to std::string.
Definition: str.cc:91
unsigned valueno
The number for a value slot in a document.
Definition: types.h:90
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
Various assertion macros.
#define Assert(COND)
Definition: omassert.h:122
Convert types to std::string.