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
|
/** @file glass_values.h
* @brief GlassValueManager class
*/
/* Copyright (C) 2008,2009,2011 Olly Betts
* Copyright (C) 2008 Lemur Consulting Ltd
*
* 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
*/
#ifndef XAPIAN_INCLUDED_GLASS_VALUES_H
#define XAPIAN_INCLUDED_GLASS_VALUES_H
#include "pack.h"
#include "backends/valuestats.h"
#include "xapian/error.h"
#include "xapian/types.h"
#include "autoptr.h"
#include <map>
#include <string>
class GlassCursor;
namespace Glass {
/** Generate a key for a value stream chunk. */
inline std::string
make_valuechunk_key(Xapian::valueno slot, Xapian::docid did)
{
std::string key("\0\xd8", 2);
pack_uint(key, slot);
pack_uint_preserving_sort(key, did);
return key;
}
inline Xapian::docid
docid_from_key(Xapian::valueno required_slot, const std::string & key)
{
const char * p = key.data();
const char * end = p + key.length();
// Fail if not a value chunk key.
if (end - p < 2 || *p++ != '\0' || *p++ != '\xd8') return 0;
Xapian::valueno slot;
if (!unpack_uint(&p, end, &slot))
throw Xapian::DatabaseCorruptError("bad value key");
// Fail if for a different slot.
if (slot != required_slot) return 0;
Xapian::docid did;
if (!unpack_uint_preserving_sort(&p, end, &did))
throw Xapian::DatabaseCorruptError("bad value key");
return did;
}
}
namespace Xapian {
class Document;
}
class GlassPostListTable;
class GlassTermListTable;
struct ValueStats;
class GlassValueManager {
/** The value number for the most recently used value statistics.
*
* Set to Xapian::BAD_VALUENO if no value statistics are currently
* cached.
*/
mutable Xapian::valueno mru_slot;
/** The most recently used value statistics. */
mutable ValueStats mru_valstats;
GlassPostListTable * postlist_table;
GlassTermListTable * termlist_table;
std::map<Xapian::docid, std::string> slots;
std::map<Xapian::valueno, std::map<Xapian::docid, std::string> > changes;
mutable AutoPtr<GlassCursor> cursor;
void add_value(Xapian::docid did, Xapian::valueno slot,
const std::string & val);
void remove_value(Xapian::docid did, Xapian::valueno slot);
Xapian::docid get_chunk_containing_did(Xapian::valueno slot,
Xapian::docid did,
std::string &chunk) const;
/** Get the statistics for value slot @a slot. */
void get_value_stats(Xapian::valueno slot) const;
void get_value_stats(Xapian::valueno slot, ValueStats & stats) const;
public:
/** Create a new GlassValueManager object. */
GlassValueManager(GlassPostListTable * postlist_table_,
GlassTermListTable * termlist_table_)
: mru_slot(Xapian::BAD_VALUENO),
postlist_table(postlist_table_),
termlist_table(termlist_table_) { }
// Merge in batched-up changes.
void merge_changes();
void add_document(Xapian::docid did, const Xapian::Document &doc,
std::map<Xapian::valueno, ValueStats> & value_stats);
void delete_document(Xapian::docid did,
std::map<Xapian::valueno, ValueStats> & value_stats);
void replace_document(Xapian::docid did, const Xapian::Document &doc,
std::map<Xapian::valueno, ValueStats> & value_stats);
std::string get_value(Xapian::docid did, Xapian::valueno slot) const;
void get_all_values(std::map<Xapian::valueno, std::string> & values,
Xapian::docid did) const;
Xapian::doccount get_value_freq(Xapian::valueno slot) const {
if (mru_slot != slot) get_value_stats(slot);
return mru_valstats.freq;
}
std::string get_value_lower_bound(Xapian::valueno slot) const {
if (mru_slot != slot) get_value_stats(slot);
return mru_valstats.lower_bound;
}
std::string get_value_upper_bound(Xapian::valueno slot) const {
if (mru_slot != slot) get_value_stats(slot);
return mru_valstats.upper_bound;
}
/** Write the updated statistics to the table.
*
* If the @a freq member of the statistics for a particular slot is 0, the
* statistics for that slot will be cleared.
*
* @param value_stats The statistics to set.
*/
void set_value_stats(std::map<Xapian::valueno, ValueStats> & value_stats);
void reset() {
/// Ignore any old cached valuestats.
mru_slot = Xapian::BAD_VALUENO;
}
bool is_modified() const {
return !changes.empty();
}
void cancel() {
// Discard batched-up changes.
slots.clear();
changes.clear();
}
};
namespace Glass {
class ValueChunkReader {
const char *p;
const char *end;
Xapian::docid did;
std::string value;
public:
/// Create a ValueChunkReader which is already at_end().
ValueChunkReader() : p(NULL) { }
ValueChunkReader(const char * p_, size_t len, Xapian::docid did_) {
assign(p_, len, did_);
}
void assign(const char * p_, size_t len, Xapian::docid did_);
bool at_end() const { return p == NULL; }
Xapian::docid get_docid() const { return did; }
const std::string & get_value() const { return value; }
void next();
void skip_to(Xapian::docid target);
};
}
#endif // XAPIAN_INCLUDED_GLASS_VALUES_H
|