1 /** @file glass_values.h
2 * @brief GlassValueManager class
4 /* Copyright (C) 2008,2009,2011 Olly Betts
5 * Copyright (C) 2008 Lemur Consulting Ltd
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.
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.
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
22 #ifndef XAPIAN_INCLUDED_GLASS_VALUES_H
23 #define XAPIAN_INCLUDED_GLASS_VALUES_H
26 #include "backends/valuestats.h"
28 #include "xapian/error.h"
29 #include "xapian/types.h"
39 /** Generate a key for a value stream chunk. */
41 make_valuechunk_key(Xapian::valueno slot
, Xapian::docid did
)
43 std::string
key("\0\xd8", 2);
45 pack_uint_preserving_sort(key
, did
);
50 docid_from_key(Xapian::valueno required_slot
, const std::string
& key
)
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;
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;
62 if (!unpack_uint_preserving_sort(&p
, end
, &did
))
63 throw Xapian::DatabaseCorruptError("bad value key");
73 class GlassPostListTable
;
74 class GlassTermListTable
;
77 class GlassValueManager
{
78 /** The value number for the most recently used value statistics.
80 * Set to Xapian::BAD_VALUENO if no value statistics are currently
83 mutable Xapian::valueno mru_slot
;
85 /** The most recently used value statistics. */
86 mutable ValueStats mru_valstats
;
88 GlassPostListTable
* postlist_table
;
90 GlassTermListTable
* termlist_table
;
92 std::map
<Xapian::docid
, std::string
> slots
;
94 std::map
<Xapian::valueno
, std::map
<Xapian::docid
, std::string
> > changes
;
96 mutable std::unique_ptr
<GlassCursor
> cursor
;
98 void add_value(Xapian::docid did
, Xapian::valueno slot
,
99 const std::string
& val
);
101 void remove_value(Xapian::docid did
, Xapian::valueno slot
);
103 Xapian::docid
get_chunk_containing_did(Xapian::valueno slot
,
105 std::string
&chunk
) const;
107 /** Get the statistics for value slot @a slot. */
108 void get_value_stats(Xapian::valueno slot
) const;
110 void get_value_stats(Xapian::valueno slot
, ValueStats
& stats
) const;
113 /** Create a new GlassValueManager object. */
114 GlassValueManager(GlassPostListTable
* postlist_table_
,
115 GlassTermListTable
* termlist_table_
)
116 : mru_slot(Xapian::BAD_VALUENO
),
117 postlist_table(postlist_table_
),
118 termlist_table(termlist_table_
) { }
120 // Merge in batched-up changes.
121 void merge_changes();
123 void add_document(Xapian::docid did
, const Xapian::Document
&doc
,
124 std::map
<Xapian::valueno
, ValueStats
> & value_stats
);
126 void delete_document(Xapian::docid did
,
127 std::map
<Xapian::valueno
, ValueStats
> & value_stats
);
129 void replace_document(Xapian::docid did
, const Xapian::Document
&doc
,
130 std::map
<Xapian::valueno
, ValueStats
> & value_stats
);
132 std::string
get_value(Xapian::docid did
, Xapian::valueno slot
) const;
134 void get_all_values(std::map
<Xapian::valueno
, std::string
> & values
,
135 Xapian::docid did
) const;
137 Xapian::doccount
get_value_freq(Xapian::valueno slot
) const {
138 if (mru_slot
!= slot
) get_value_stats(slot
);
139 return mru_valstats
.freq
;
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
;
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
;
152 /** Write the updated statistics to the table.
154 * If the @a freq member of the statistics for a particular slot is 0, the
155 * statistics for that slot will be cleared.
157 * @param value_stats The statistics to set.
159 void set_value_stats(std::map
<Xapian::valueno
, ValueStats
> & value_stats
);
162 /// Ignore any old cached valuestats.
163 mru_slot
= Xapian::BAD_VALUENO
;
166 bool is_modified() const {
167 return !changes
.empty();
171 // Discard batched-up changes.
179 class ValueChunkReader
{
188 /// Create a ValueChunkReader which is already at_end().
189 ValueChunkReader() : p(NULL
) { }
191 ValueChunkReader(const char * p_
, size_t len
, Xapian::docid did_
) {
192 assign(p_
, len
, did_
);
195 void assign(const char * p_
, size_t len
, Xapian::docid did_
);
197 bool at_end() const { return p
== NULL
; }
199 Xapian::docid
get_docid() const { return did
; }
201 const std::string
& get_value() const { return value
; }
205 void skip_to(Xapian::docid target
);
210 #endif // XAPIAN_INCLUDED_GLASS_VALUES_H