Fix testcase unsupportedcheck1 for --disable-backend-remote
[xapian.git] / xapian-core / api / document.cc
blobedce7a5d2fb4dd62c92e02ac52da936bd753ec0d
1 /** @file
2 * @brief Class representing a document
3 */
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
21 #include <config.h>
23 #include "xapian/document.h"
25 #include <string>
26 #include <string_view>
28 #include "backends/documentinternal.h"
29 #include "net/serialise.h"
30 #include "str.h"
32 #include "xapian/error.h"
34 using namespace std;
36 [[noreturn]]
37 static void
38 throw_invalid_arg_empty_term()
40 throw Xapian::InvalidArgumentError("Empty termnames are invalid");
43 namespace Xapian {
45 Document::Document(Document::Internal* internal_)
46 : internal(internal_)
50 Document::Document(const Document&) = default;
52 Document&
53 Document::operator=(const Document&) = default;
55 Document::Document(Document&&) = default;
57 Document&
58 Document::operator=(Document&&) = default;
60 Document::Document() : internal(new Xapian::Document::Internal)
64 Document::~Document()
68 Xapian::docid
69 Document::get_docid() const
71 return internal->get_docid();
74 string
75 Document::get_data() const
77 return internal->get_data();
80 void
81 Document::set_data(string_view data)
83 internal->set_data(data);
86 void
87 Document::add_term(string_view term, Xapian::termcount wdf_inc)
89 if (term.empty()) {
90 throw_invalid_arg_empty_term();
92 internal->add_term(term, wdf_inc);
95 void
96 Document::remove_term(string_view term)
98 if (term.empty()) {
99 throw_invalid_arg_empty_term();
101 if (!internal->remove_term(term)) {
102 string m;
103 m = "Document::remove_term() failed - term '";
104 m += term;
105 m += "' not present";
106 throw Xapian::InvalidArgumentError(m);
110 void
111 Document::add_posting(string_view term,
112 Xapian::termpos term_pos,
113 Xapian::termcount wdf_inc)
115 if (term.empty()) {
116 throw_invalid_arg_empty_term();
118 internal->add_posting(term, term_pos, wdf_inc);
121 void
122 Document::remove_posting(string_view term,
123 Xapian::termpos term_pos,
124 Xapian::termcount wdf_dec)
126 if (term.empty()) {
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 '";
132 m += term;
133 if (res == Document::Internal::NO_TERM) {
134 m += "' not present";
135 } else {
136 m += "' not present at position ";
137 m += str(term_pos);
139 throw Xapian::InvalidArgumentError(m);
143 Xapian::termpos
144 Document::remove_postings(string_view term,
145 Xapian::termpos term_pos_first,
146 Xapian::termpos term_pos_last,
147 Xapian::termcount wdf_dec)
149 if (term.empty()) {
150 throw_invalid_arg_empty_term();
152 if (rare(term_pos_first > term_pos_last)) {
153 return 0;
155 Xapian::termpos n_removed;
156 auto res = internal->remove_postings(term, term_pos_first, term_pos_last,
157 wdf_dec, n_removed);
158 if (res != Document::Internal::OK) {
159 string m = "Document::remove_postings() failed - term '";
160 m += term;
161 m += "' not present";
162 throw Xapian::InvalidArgumentError(m);
164 return n_removed;
167 void
168 Document::clear_terms()
170 internal->clear_terms();
173 Xapian::termcount
174 Document::termlist_count() const {
175 return internal->termlist_count();
178 TermIterator
179 Document::termlist_begin() const
181 return TermIterator(internal->open_term_list());
184 string
185 Document::get_value(Xapian::valueno slot) const
187 return internal->get_value(slot);
190 void
191 Document::add_value(Xapian::valueno slot, string_view value)
193 internal->add_value(slot, value);
196 void
197 Document::clear_values()
199 internal->clear_values();
202 Xapian::valueno
203 Document::values_count() const {
204 return internal->values_count();
207 ValueIterator
208 Document::values_begin() const
210 return internal->values_begin();
213 string
214 Document::serialise() const
216 return serialise_document(*this);
219 Document
220 Document::unserialise(string_view serialised)
222 return unserialise_document(serialised);
225 string
226 Document::get_description() const
228 return internal->get_description();