2 * @brief Class representing a document
4 /* Copyright 2008,2017,2018,2024 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "xapian/document.h"
26 #include <string_view>
28 #include "backends/documentinternal.h"
29 #include "net/serialise.h"
32 #include "xapian/error.h"
38 throw_invalid_arg_empty_term()
40 throw Xapian::InvalidArgumentError("Empty termnames are invalid");
45 Document::Document(Document::Internal
* internal_
)
50 Document::Document(const Document
&) = default;
53 Document::operator=(const Document
&) = default;
55 Document::Document(Document
&&) = default;
58 Document::operator=(Document
&&) = default;
60 Document::Document() : internal(new Xapian::Document::Internal
)
69 Document::get_docid() const
71 return internal
->get_docid();
75 Document::get_data() const
77 return internal
->get_data();
81 Document::set_data(string_view data
)
83 internal
->set_data(data
);
87 Document::add_term(string_view term
, Xapian::termcount wdf_inc
)
90 throw_invalid_arg_empty_term();
92 internal
->add_term(term
, wdf_inc
);
96 Document::remove_term(string_view term
)
99 throw_invalid_arg_empty_term();
101 if (!internal
->remove_term(term
)) {
103 m
= "Document::remove_term() failed - term '";
105 m
+= "' not present";
106 throw Xapian::InvalidArgumentError(m
);
111 Document::add_posting(string_view term
,
112 Xapian::termpos term_pos
,
113 Xapian::termcount wdf_inc
)
116 throw_invalid_arg_empty_term();
118 internal
->add_posting(term
, term_pos
, wdf_inc
);
122 Document::remove_posting(string_view term
,
123 Xapian::termpos term_pos
,
124 Xapian::termcount wdf_dec
)
127 throw_invalid_arg_empty_term();
129 auto res
= internal
->remove_posting(term
, term_pos
, wdf_dec
);
130 if (res
!= Document::Internal::OK
) {
131 string m
= "Document::remove_posting() failed - term '";
133 if (res
== Document::Internal::NO_TERM
) {
134 m
+= "' not present";
136 m
+= "' not present at position ";
139 throw Xapian::InvalidArgumentError(m
);
144 Document::remove_postings(string_view term
,
145 Xapian::termpos term_pos_first
,
146 Xapian::termpos term_pos_last
,
147 Xapian::termcount wdf_dec
)
150 throw_invalid_arg_empty_term();
152 if (rare(term_pos_first
> term_pos_last
)) {
155 Xapian::termpos n_removed
;
156 auto res
= internal
->remove_postings(term
, term_pos_first
, term_pos_last
,
158 if (res
!= Document::Internal::OK
) {
159 string m
= "Document::remove_postings() failed - term '";
161 m
+= "' not present";
162 throw Xapian::InvalidArgumentError(m
);
168 Document::clear_terms()
170 internal
->clear_terms();
174 Document::termlist_count() const {
175 return internal
->termlist_count();
179 Document::termlist_begin() const
181 return TermIterator(internal
->open_term_list());
185 Document::get_value(Xapian::valueno slot
) const
187 return internal
->get_value(slot
);
191 Document::add_value(Xapian::valueno slot
, string_view value
)
193 internal
->add_value(slot
, value
);
197 Document::clear_values()
199 internal
->clear_values();
203 Document::values_count() const {
204 return internal
->values_count();
208 Document::values_begin() const
210 return internal
->values_begin();
214 Document::serialise() const
216 return serialise_document(*this);
220 Document::unserialise(string_view serialised
)
222 return unserialise_document(serialised
);
226 Document::get_description() const
228 return internal
->get_description();