xapian-core  1.4.19
glass_values.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2008,2009,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_GLASS_VALUES_H
23 #define XAPIAN_INCLUDED_GLASS_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 GlassCursor;
36 
37 namespace Glass {
38 
40 inline std::string
42 {
43  std::string key("\0\xd8", 2);
44  pack_uint(key, slot);
45  pack_uint_preserving_sort(key, did);
46  return key;
47 }
48 
49 inline Xapian::docid
50 docid_from_key(Xapian::valueno required_slot, const std::string & key)
51 {
52  const char * p = key.data();
53  const char * end = p + key.length();
54  // Fail if not a value chunk key.
55  if (end - p < 2 || *p++ != '\0' || *p++ != '\xd8') return 0;
56  Xapian::valueno slot;
57  if (!unpack_uint(&p, end, &slot))
58  throw Xapian::DatabaseCorruptError("bad value key");
59  // Fail if for a different slot.
60  if (slot != required_slot) return 0;
61  Xapian::docid did;
62  if (!unpack_uint_preserving_sort(&p, end, &did))
63  throw Xapian::DatabaseCorruptError("bad value key");
64  return did;
65 }
66 
67 }
68 
69 namespace Xapian {
70  class Document;
71 }
72 
73 class GlassPostListTable;
74 class GlassTermListTable;
75 struct ValueStats;
76 
84 
87 
89 
91 
92  std::map<Xapian::docid, std::string> slots;
93 
94  std::map<Xapian::valueno, std::map<Xapian::docid, std::string>> changes;
95 
96  mutable AutoPtr<GlassCursor> cursor;
97 
98  void add_value(Xapian::docid did, Xapian::valueno slot,
99  const std::string & val);
100 
101  void remove_value(Xapian::docid did, Xapian::valueno slot);
102 
103  Xapian::docid get_chunk_containing_did(Xapian::valueno slot,
104  Xapian::docid did,
105  std::string &chunk) const;
106 
108  void get_value_stats(Xapian::valueno slot) const;
109 
110  void get_value_stats(Xapian::valueno slot, ValueStats & stats) const;
111 
112  public:
115  GlassTermListTable * termlist_table_)
116  : mru_slot(Xapian::BAD_VALUENO),
117  postlist_table(postlist_table_),
118  termlist_table(termlist_table_) { }
119 
120  // Merge in batched-up changes.
121  void merge_changes();
122 
123  void add_document(Xapian::docid did, const Xapian::Document &doc,
124  std::map<Xapian::valueno, ValueStats> & value_stats);
125 
126  void delete_document(Xapian::docid did,
127  std::map<Xapian::valueno, ValueStats> & value_stats);
128 
129  void replace_document(Xapian::docid did, const Xapian::Document &doc,
130  std::map<Xapian::valueno, ValueStats> & value_stats);
131 
132  std::string get_value(Xapian::docid did, Xapian::valueno slot) const;
133 
134  void get_all_values(std::map<Xapian::valueno, std::string> & values,
135  Xapian::docid did) const;
136 
138  if (mru_slot != slot) get_value_stats(slot);
139  return mru_valstats.freq;
140  }
141 
142  std::string get_value_lower_bound(Xapian::valueno slot) const {
143  if (mru_slot != slot) get_value_stats(slot);
144  return mru_valstats.lower_bound;
145  }
146 
147  std::string get_value_upper_bound(Xapian::valueno slot) const {
148  if (mru_slot != slot) get_value_stats(slot);
149  return mru_valstats.upper_bound;
150  }
151 
159  void set_value_stats(std::map<Xapian::valueno, ValueStats> & value_stats);
160 
161  void reset() {
163  mru_slot = Xapian::BAD_VALUENO;
164  }
165 
166  bool is_modified() const {
167  return !changes.empty();
168  }
169 
170  void cancel() {
171  // Discard batched-up changes.
172  slots.clear();
173  changes.clear();
174  }
175 };
176 
177 namespace Glass {
178 
180  const char *p;
181  const char *end;
182 
184 
185  std::string value;
186 
187  public:
189  ValueChunkReader() : p(NULL) { }
190 
191  ValueChunkReader(const char * p_, size_t len, Xapian::docid did_) {
192  assign(p_, len, did_);
193  }
194 
195  void assign(const char * p_, size_t len, Xapian::docid did_);
196 
197  bool at_end() const { return p == NULL; }
198 
199  Xapian::docid get_docid() const { return did; }
200 
201  const std::string & get_value() const { return value; }
202 
203  void next();
204 
205  void skip_to(Xapian::docid target);
206 };
207 
208 }
209 
210 #endif // XAPIAN_INCLUDED_GLASS_VALUES_H
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
Class to hold statistics for a given slot.
Definition: valuestats.h:29
GlassTermListTable * termlist_table
Definition: glass_values.h:90
typedefs for Xapian
GlassPostListTable * postlist_table
Definition: glass_values.h:88
ValueChunkReader(const char *p_, size_t len, Xapian::docid did_)
Definition: glass_values.h:191
Statistics about values.
Xapian::docid docid_from_key(Xapian::valueno required_slot, const std::string &key)
Definition: glass_values.h:50
std::string make_valuechunk_key(Xapian::valueno slot, Xapian::docid did)
Generate a key for a value stream chunk.
Definition: glass_values.h:41
ValueStats mru_valstats
The most recently used value statistics.
Definition: glass_values.h:86
std::string upper_bound
An upper bound on the values stored in the given value slot.
Definition: valuestats.h:41
GlassValueManager(GlassPostListTable *postlist_table_, GlassTermListTable *termlist_table_)
Create a new GlassValueManager object.
Definition: glass_values.h:114
Xapian::doccount freq
The number of documents which have a (non-empty) value stored in the slot.
Definition: valuestats.h:33
const std::string & get_value() const
Definition: glass_values.h:201
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.
bool is_modified() const
Definition: glass_values.h:166
std::map< Xapian::docid, std::string > slots
Definition: glass_values.h:92
Xapian::valueno mru_slot
The value number for the most recently used value statistics.
Definition: glass_values.h:83
bool unpack_uint_preserving_sort(const char **p, const char *end, U *result)
Decode an "sort preserved" unsigned integer from a string.
Definition: pack.h:318
A cursor pointing to a position in a Btree table, for reading several entries in order, or finding approximate matches.
Definition: glass_cursor.h:147
DatabaseCorruptError indicates database corruption was detected.
Definition: error.h:409
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
Xapian::docid get_docid() const
Definition: glass_values.h:199
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
Xapian::doccount get_value_freq(Xapian::valueno slot) const
Definition: glass_values.h:137
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
ValueChunkReader()
Create a ValueChunkReader which is already at_end().
Definition: glass_values.h:189
const valueno BAD_VALUENO
Reserved value to indicate "no valueno".
Definition: types.h:125
std::map< Xapian::valueno, std::map< Xapian::docid, std::string > > changes
Definition: glass_values.h:94
std::string get_value_upper_bound(Xapian::valueno slot) const
Definition: glass_values.h:147
AutoPtr< GlassCursor > cursor
Definition: glass_values.h:96
A handle representing a document in a Xapian database.
Definition: document.h:61
std::string get_value_lower_bound(Xapian::valueno slot) const
Definition: glass_values.h:142
Wrapper around standard unique_ptr template.
void pack_uint_preserving_sort(std::string &s, U value)
Append an encoded unsigned integer to a string, preserving the sort order.
Definition: pack.h:269