1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/** @file chert_valuelist.cc
* @brief Chert class for value streams.
*/
/* Copyright (C) 2007,2008 Olly Betts
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include "chert_valuelist.h"
#include "chert_cursor.h"
#include "chert_database.h"
#include "omassert.h"
#include "str.h"
using namespace std;
bool
ChertValueList::update_reader()
{
Xapian::docid first_did = docid_from_key(slot, cursor->current_key);
if (!first_did) return false;
cursor->read_tag();
const string & tag = cursor->current_tag;
reader.assign(tag.data(), tag.size(), first_did);
return true;
}
ChertValueList::~ChertValueList()
{
delete cursor;
}
Xapian::docid
ChertValueList::get_docid() const
{
Assert(!at_end());
return reader.get_docid();
}
Xapian::valueno
ChertValueList::get_valueno() const
{
return slot;
}
std::string
ChertValueList::get_value() const
{
Assert(!at_end());
return reader.get_value();
}
bool
ChertValueList::at_end() const
{
return cursor == NULL;
}
void
ChertValueList::next()
{
if (!cursor) {
cursor = db->get_postlist_cursor();
if (!cursor) return;
cursor->find_entry_ge(make_valuechunk_key(slot, 1));
} else if (!reader.at_end()) {
reader.next();
if (!reader.at_end()) return;
cursor->next();
}
if (!cursor->after_end()) {
if (update_reader()) {
if (!reader.at_end()) return;
}
}
// We've reached the end.
delete cursor;
cursor = NULL;
}
void
ChertValueList::skip_to(Xapian::docid did)
{
if (!cursor) {
cursor = db->get_postlist_cursor();
if (!cursor) return;
} else if (!reader.at_end()) {
reader.skip_to(did);
if (!reader.at_end()) return;
}
if (!cursor->find_entry(make_valuechunk_key(slot, did))) {
if (update_reader()) {
reader.skip_to(did);
if (!reader.at_end()) return;
}
// The requested docid is between two chunks.
cursor->next();
}
// Either an exact match, or in a gap before the start of a chunk.
if (!cursor->after_end()) {
if (update_reader()) {
if (!reader.at_end()) return;
}
}
// We've reached the end.
delete cursor;
cursor = NULL;
}
bool
ChertValueList::check(Xapian::docid did)
{
if (!cursor) {
cursor = db->get_postlist_cursor();
if (!cursor) return true;
} else if (!reader.at_end()) {
// Check for the requested docid in the current block.
reader.skip_to(did);
if (!reader.at_end()) return true;
}
// Try moving to the appropriate chunk.
if (!cursor->find_entry(make_valuechunk_key(slot, did))) {
// We're in a chunk which might contain the docid.
if (update_reader()) {
reader.skip_to(did);
if (!reader.at_end()) return true;
}
return false;
}
// We had an exact match for a chunk starting with specified docid.
Assert(!cursor->after_end());
if (!update_reader()) {
// We found the exact key we built, so it must match the slot.
// Therefore update_reader() "can't possibly fail".
Assert(false);
}
return true;
}
string
ChertValueList::get_description() const
{
string desc("ChertValueList(slot=");
desc += str(slot);
desc += ')';
return desc;
}
|