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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/* db_database.cc: C++ class for dbtype access routines
*
* Copyright 1999,2000,2001 BrightStation PLC
* Copyright 2002 Ananova Ltd
* Copyright 2002,2003,2004,2006 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 "safeerrno.h"
#include <string.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using std::bad_alloc;
using std::pair;
#include "utils.h"
#include "database.h"
#include "leafpostlist.h"
#include "termlist.h"
#include "db_database.h"
#include "db_document.h"
#include "dbread.h"
#include "omdebug.h"
#include <xapian/document.h>
#include <xapian/error.h>
DBPostList::DBPostList(const string & tname_,
struct DB_postings * postlist_,
Xapian::doccount termfreq_,
Xapian::Internal::RefCntPtr<const DBDatabase> this_db_)
: postlist(postlist_), currdoc(0), tname(tname_), termfreq(termfreq_),
this_db(this_db_)
{
}
DBPostList::~DBPostList()
{
DB_close_postings(postlist);
}
PostList * DBPostList::next(Xapian::weight /*w_min*/)
{
Assert(currdoc == 0 || !at_end());
DEBUGLINE(DB, "DBPostList::next(/*w_min*/): current docid = " << currdoc);
if (currdoc && currdoc < Xapian::docid(postlist->E)) {
currdoc++;
} else {
DB_read_postings(postlist, 1, 0);
currdoc = Xapian::docid(postlist->Doc);
}
DEBUGLINE(DB, "DBPostList::next(" << w_min <<
"): new docid = " << currdoc);
return NULL;
}
PostList * DBPostList::skip_to(Xapian::docid did, Xapian::weight /*w_min*/)
{
Assert(currdoc == 0 || !at_end());
Assert(did >= currdoc);
DEBUGLINE(DB, "DBPostList::skip_to(" << did <<
", /*w_min*/): current docid = " << currdoc);
if (currdoc && did <= Xapian::docid(postlist->E)) {
DEBUGLINE(DB, "skip within range (end of range is " <<
Xapian::docid(postlist->E) << ")");
currdoc = did;
} else {
DB_read_postings(postlist, 1, did);
DEBUGLINE(DB, "reading more postings");
currdoc = Xapian::docid(postlist->Doc);
}
DEBUGLINE(DB, "DBPostList::skip_to(" << did <<
", /*w_min*/): new docid = " << currdoc);
return NULL;
}
PositionList *
DBPostList::read_position_list()
{
return NULL;
}
PositionList *
DBPostList::open_position_list() const
{
// This tells the level above to return begin() = end()
return NULL;
}
DBTermList::DBTermList(struct termvec *tv, Xapian::doccount dbsize_,
Xapian::Internal::RefCntPtr<const DBDatabase> this_db_)
: have_started(false), dbsize(dbsize_), this_db(this_db_)
{
// FIXME - read terms as we require them, rather than all at beginning?
M_read_terms(tv);
while(tv->term != 0) {
char *term = reinterpret_cast<char *>(tv->term);
Xapian::doccount freq = tv->freq;
terms.push_back(DBTermListItem(string(term + 1, unsigned(term[0]) - 1),
tv->wdf, freq));
M_read_terms(tv);
}
M_lose_termvec(tv);
pos = terms.begin();
}
Xapian::doccount
DBTermList::get_termfreq() const
{
Assert(!at_end());
Assert(have_started);
// FIXME: really icky cast
if (pos->termfreq == Xapian::termcount(-1)) {
// Not available - read from database
DEBUGLINE(DB, "DBTermList::get_termfreq - termfreq for `" << pos->tname
<< "' not available, reading from database");
pos->termfreq = this_db->get_termfreq(pos->tname);
}
return pos->termfreq;
}
OmExpandBits
DBTermList::get_weighting() const
{
Assert(!at_end());
Assert(have_started);
Assert(wt != NULL);
// FIXME: want to use document length
return wt->get_bits(pos->wdf, 1, get_termfreq(), dbsize);
}
DBDatabase::DBDatabase(const string &filename, const string &filename_v,
int cache_size) : DB(0), valuefile(0)
{
// Actually open
DB = DB_open(filename.c_str(), cache_size);
if (DB == 0) {
throw Xapian::DatabaseOpeningError(string("When opening ") + filename + ": " + strerror(errno));
}
if (filename_v.empty()) return;
// Open valuefile
valuefile = fopen(filename_v.c_str(), "rb");
if (valuefile == 0) {
throw Xapian::DatabaseOpeningError(string("When opening ") + filename_v +
": " + strerror(errno));
}
// Check for magic string at beginning of file.
try {
char input[8];
size_t bytes_read = fread(input, 1, 8, valuefile);
if (bytes_read < 8) {
throw Xapian::DatabaseOpeningError(string("When opening ") + filename_v +
": couldn't read magic - " + strerror(errno));
}
if (memcmp(input, "omrocks!", 8)) {
throw Xapian::DatabaseOpeningError(string("When opening ") + filename_v +
": couldn't read magic - got `" +
input + "'");
}
}
catch (...) {
fclose(valuefile);
DB_close(DB);
DB = 0;
throw;
}
}
DBDatabase::~DBDatabase()
{
if (valuefile != 0) {
fclose(valuefile);
valuefile = 0;
}
if (DB != 0) {
DB_close(DB);
DB = 0;
}
}
Xapian::doccount
DBDatabase::get_doccount() const
{
return DB->doc_count;
}
Xapian::doclength
DBDatabase::get_avlength() const
{
// FIXME - actually want to return real avlength.
return 1;
}
Xapian::doclength
DBDatabase::get_doclength(Xapian::docid /*did*/) const
{
// FIXME: should return actual length.
return get_avlength();
}
Xapian::doccount
DBDatabase::get_termfreq(const string & tname) const
{
if (!term_exists(tname)) return 0;
LeafPostList *pl = open_post_list_internal(tname);
Xapian::doccount freq = 0;
if (pl) freq = pl->get_termfreq();
delete pl;
return freq;
}
bool
DBDatabase::term_exists(const string & tname) const
{
Assert(tname.size() != 0);
return (term_lookup(tname).get() != 0);
}
bool
DBDatabase::has_positions() const
{
return false;
}
// Returns a new posting list, for the postings in this database for given term
LeafPostList *
DBDatabase::open_post_list_internal(const string & tname) const
{
// Make sure the term has been looked up
Xapian::Internal::RefCntPtr<const DBTerm> the_term = term_lookup(tname);
Assert(the_term.get() != 0);
struct DB_postings * postlist;
postlist = DB_open_postings(the_term->get_ti(), DB);
return new DBPostList(tname, postlist, the_term->get_ti()->freq,
Xapian::Internal::RefCntPtr<const DBDatabase>(this));
}
LeafPostList *
DBDatabase::do_open_post_list(const string & tname) const
{
return open_post_list_internal(tname);
}
// Returns a new term list, for the terms in this database for given document
LeafTermList *
DBDatabase::open_term_list(Xapian::docid did) const
{
if (did == 0) throw Xapian::InvalidArgumentError("Docid 0 invalid");
struct termvec *tv = M_make_termvec();
if (DB_get_termvec(DB, did, tv) == 0) {
M_lose_termvec(tv);
throw Xapian::DocNotFoundError(string("Docid ") + om_tostring(did) +
string(" not found"));
}
M_open_terms(tv);
return new DBTermList(tv, DBDatabase::get_doccount(),
Xapian::Internal::RefCntPtr<const DBDatabase>(this));
}
struct record *
DBDatabase::get_record(Xapian::docid did) const
{
if (did == 0) throw Xapian::InvalidArgumentError("Docid 0 invalid");
struct record *r = M_make_record();
int found = DB_get_record(DB, did, r);
if (found == 0) {
M_lose_record(r);
throw Xapian::DocNotFoundError(string("Docid ") + om_tostring(did) +
string(" not found"));
}
return r;
}
/// Get the specified value for given document from the fast lookup file.
string
DBDatabase::get_value(Xapian::docid did, Xapian::valueno valueid) const
{
string value;
DEBUGLINE(DB, "Looking in valuefile for valueno " << valueid << " in document " << did);
if (valueid) return value;
if (valuefile == 0) {
DEBUGLINE(DB, ": don't have valuefile - using record");
} else {
int seekok = fseek(valuefile, long(did) * 8, SEEK_SET);
if (seekok == -1) {
DEBUGLINE(DB, ": seek off end of valuefile - using record");
} else {
char input[9];
size_t bytes_read = fread(input, sizeof(char), 8, valuefile);
if (bytes_read < 8) {
DEBUGLINE(DB, ": read off end of valuefile - using record");
} else {
value = string(input, 8);
DEBUGLINE(DB, ": found - value is `" << value << "'");
}
}
}
return value;
}
Xapian::Document::Internal *
DBDatabase::open_document(Xapian::docid did, bool lazy) const
{
return new DBDocument(this, did, DB->heavy_duty, lazy);
}
PositionList *
DBDatabase::open_position_list(Xapian::docid /*did*/,
const string & /*tname*/) const
{
// This tells the level above to return begin() = end()
return NULL;
}
Xapian::Internal::RefCntPtr<const DBTerm>
DBDatabase::term_lookup(const string & tname) const
{
DEBUGLINE(DB, "DBDatabase::term_lookup(`" << tname << "'): ");
map<string, Xapian::Internal::RefCntPtr<const DBTerm> >::const_iterator p;
p = termmap.find(tname);
Xapian::Internal::RefCntPtr<const DBTerm> the_term;
if (p == termmap.end()) {
string::size_type len = tname.length();
if (len > 255) return 0;
byte * k = reinterpret_cast<byte *>(malloc(len + 1));
if (k == NULL) throw bad_alloc();
k[0] = len + 1;
tname.copy(reinterpret_cast<char*>(k + 1), len, 0);
struct DB_term_info ti;
int found = DB_term(k, &ti, DB);
free(k);
if (found == 0) {
DEBUGLINE(DB, "Not in collection");
} else {
// FIXME: be a bit nicer on the cache than this
if (termmap.size() > 500) {
DEBUGLINE(DB, "cache full, wiping");
termmap.clear();
}
DEBUGLINE(DB, "found, adding to cache");
pair<string, Xapian::Internal::RefCntPtr<const DBTerm> > termpair(tname, new DBTerm(&ti, tname));
termmap.insert(termpair);
the_term = termmap.find(tname)->second;
}
} else {
the_term = (*p).second;
DEBUGLINE(DB, "found in cache");
}
return the_term;
}
TermList *
DBDatabase::open_allterms() const
{
throw Xapian::UnimplementedError("open_allterms() not implemented yet");
}
|