xapian-core  1.4.27
chert_values.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2008,2011 Olly Betts
5  * Copyright (C) 2008 Lemur Consulting Ltd
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef XAPIAN_INCLUDED_CHERT_VALUES_H
23 #define XAPIAN_INCLUDED_CHERT_VALUES_H
24 
25 #include "pack.h"
26 #include "backends/valuestats.h"
27 
28 #include "xapian/error.h"
29 #include "xapian/types.h"
30 
31 #include "autoptr.h"
32 #include <map>
33 #include <string>
34 
35 class ChertCursor;
36 
38 inline std::string
40 {
41  std::string key("\0\xd8", 2);
42  pack_uint(key, slot);
44  return key;
45 }
46 
47 inline Xapian::docid
48 docid_from_key(Xapian::valueno required_slot, const std::string & key)
49 {
50  const char * p = key.data();
51  const char * end = p + key.length();
52  // Fail if not a value chunk key.
53  if (end - p < 2 || *p++ != '\0' || *p++ != '\xd8') return 0;
54  Xapian::valueno slot;
55  if (!unpack_uint(&p, end, &slot))
56  throw Xapian::DatabaseCorruptError("bad value key");
57  // Fail if for a different slot.
58  if (slot != required_slot) return 0;
59  Xapian::docid did;
60  if (!C_unpack_uint_preserving_sort(&p, end, &did))
61  throw Xapian::DatabaseCorruptError("bad value key");
62  return did;
63 }
64 
65 namespace Xapian {
66  class Document;
67 }
68 
69 class ChertPostListTable;
70 class ChertTermListTable;
71 struct ValueStats;
72 
80 
83 
85 
87 
88  std::map<Xapian::docid, std::string> slots;
89 
90  std::map<Xapian::valueno, std::map<Xapian::docid, std::string>> changes;
91 
92  mutable AutoPtr<ChertCursor> cursor;
93 
94  void add_value(Xapian::docid did, Xapian::valueno slot,
95  const std::string & val);
96 
97  void remove_value(Xapian::docid did, Xapian::valueno slot);
98 
99  Xapian::docid get_chunk_containing_did(Xapian::valueno slot,
100  Xapian::docid did,
101  std::string &chunk) const;
102 
104  void get_value_stats(Xapian::valueno slot) const;
105 
106  void get_value_stats(Xapian::valueno slot, ValueStats & stats) const;
107 
108  public:
111  ChertTermListTable * termlist_table_)
112  : mru_slot(Xapian::BAD_VALUENO),
113  postlist_table(postlist_table_),
114  termlist_table(termlist_table_) { }
115 
116  // Merge in batched-up changes.
117  void merge_changes();
118 
119  void add_document(Xapian::docid did, const Xapian::Document &doc,
120  std::map<Xapian::valueno, ValueStats> & value_stats);
121 
122  void delete_document(Xapian::docid did,
123  std::map<Xapian::valueno, ValueStats> & value_stats);
124 
125  void replace_document(Xapian::docid did, const Xapian::Document &doc,
126  std::map<Xapian::valueno, ValueStats> & value_stats);
127 
128  std::string get_value(Xapian::docid did, Xapian::valueno slot) const;
129 
130  void get_all_values(std::map<Xapian::valueno, std::string> & values,
131  Xapian::docid did) const;
132 
134  if (mru_slot != slot) get_value_stats(slot);
135  return mru_valstats.freq;
136  }
137 
138  std::string get_value_lower_bound(Xapian::valueno slot) const {
139  if (mru_slot != slot) get_value_stats(slot);
140  return mru_valstats.lower_bound;
141  }
142 
143  std::string get_value_upper_bound(Xapian::valueno slot) const {
144  if (mru_slot != slot) get_value_stats(slot);
145  return mru_valstats.upper_bound;
146  }
147 
155  void set_value_stats(std::map<Xapian::valueno, ValueStats> & value_stats);
156 
157  void reset() {
159  mru_slot = Xapian::BAD_VALUENO;
160  }
161 
162  bool is_modified() const {
163  return !changes.empty();
164  }
165 
166  void cancel() {
167  // Discard batched-up changes.
168  slots.clear();
169  changes.clear();
170  }
171 };
172 
174  const char *p;
175  const char *end;
176 
178 
179  std::string value;
180 
181  public:
183  ValueChunkReader() : p(NULL) { }
184 
185  ValueChunkReader(const char * p_, size_t len, Xapian::docid did_) {
186  assign(p_, len, did_);
187  }
188 
189  void assign(const char * p_, size_t len, Xapian::docid did_);
190 
191  bool at_end() const { return p == NULL; }
192 
193  Xapian::docid get_docid() const { return did; }
194 
195  const std::string & get_value() const { return value; }
196 
197  void next();
198 
199  void skip_to(Xapian::docid target);
200 };
201 
202 #endif // XAPIAN_INCLUDED_CHERT_VALUES_H
std::map< Xapian::docid, std::string > slots
Definition: chert_values.h:88
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
ChertPostListTable * postlist_table
Definition: chert_values.h:84
Class to hold statistics for a given slot.
Definition: valuestats.h:29
typedefs for Xapian
A cursor pointing to a position in a Btree table, for reading several entries in order, or finding approximate matches.
Definition: chert_cursor.h:66
Statistics about values.
ValueChunkReader()
Create a ValueChunkReader which is already at_end().
Definition: chert_values.h:183
std::string get_value_upper_bound(Xapian::valueno slot) const
Definition: chert_values.h:143
const char * p
Definition: chert_values.h:174
AutoPtr< ChertCursor > cursor
Definition: chert_values.h:92
std::string make_valuechunk_key(Xapian::valueno slot, Xapian::docid did)
Generate a key for a value stream chunk.
Definition: chert_values.h:39
bool is_modified() const
Definition: chert_values.h:162
Xapian::doccount get_value_freq(Xapian::valueno slot) const
Definition: chert_values.h:133
std::string upper_bound
An upper bound on the values stored in the given value slot.
Definition: valuestats.h:41
Xapian::docid did
Definition: chert_values.h:177
ValueStats mru_valstats
The most recently used value statistics.
Definition: chert_values.h:82
Xapian::doccount freq
The number of documents which have a (non-empty) value stored in the slot.
Definition: valuestats.h:33
std::string lower_bound
A lower bound on the values stored in the given value slot.
Definition: valuestats.h:37
Hierarchy of classes which Xapian can throw as exceptions.
const std::string & get_value() const
Definition: chert_values.h:195
ChertValueManager(ChertPostListTable *postlist_table_, ChertTermListTable *termlist_table_)
Create a new ChertValueManager object.
Definition: chert_values.h:110
void C_pack_uint_preserving_sort(std::string &s, U value)
Append an encoded unsigned integer to a string, preserving the sort order.
Definition: pack.h:149
bool C_unpack_uint_preserving_sort(const char **p, const char *end, U *result)
Decode an "sort preserved" unsigned integer from a string.
Definition: pack.h:185
ChertTermListTable * termlist_table
Definition: chert_values.h:86
Xapian::docid docid_from_key(Xapian::valueno required_slot, const std::string &key)
Definition: chert_values.h:48
DatabaseCorruptError indicates database corruption was detected.
Definition: error.h:409
std::map< Xapian::valueno, std::map< Xapian::docid, std::string > > changes
Definition: chert_values.h:90
std::string value
Definition: chert_values.h:179
void pack_uint(std::string &s, U value)
Append an encoded unsigned integer to a string.
Definition: pack.h:382
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
Pack types into strings and unpack them again.
unsigned valueno
The number for a value slot in a document.
Definition: types.h:108
bool unpack_uint(const char **p, const char *end, U *result)
Decode an unsigned integer from a string.
Definition: pack.h:413
ValueChunkReader(const char *p_, size_t len, Xapian::docid did_)
Definition: chert_values.h:185
std::string get_value_lower_bound(Xapian::valueno slot) const
Definition: chert_values.h:138
bool at_end() const
Definition: chert_values.h:191
Xapian::valueno mru_slot
The value number for the most recently used value statistics.
Definition: chert_values.h:79
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
Xapian::docid get_docid() const
Definition: chert_values.h:193
const valueno BAD_VALUENO
Reserved value to indicate "no valueno".
Definition: types.h:125
const char * end
Definition: chert_values.h:175
A handle representing a document in a Xapian database.
Definition: document.h:61
Wrapper around standard unique_ptr template.