xapian-core  2.0.0
document.cc
Go to the documentation of this file.
1 
4 /* Copyright 2008,2017,2018,2024 Olly Betts
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "xapian/document.h"
24 
25 #include <string>
26 #include <string_view>
27 
29 #include "net/serialise.h"
30 #include "str.h"
31 
32 #include "xapian/error.h"
33 
34 using namespace std;
35 
36 [[noreturn]]
37 static void
39 {
40  throw Xapian::InvalidArgumentError("Empty termnames are invalid");
41 }
42 
43 namespace Xapian {
44 
45 Document::Document(Document::Internal* internal_)
46  : internal(internal_)
47 {
48 }
49 
50 Document::Document(const Document&) = default;
51 
52 Document&
53 Document::operator=(const Document&) = default;
54 
55 Document::Document(Document&&) = default;
56 
57 Document&
58 Document::operator=(Document&&) = default;
59 
61 {
62 }
63 
65 {
66 }
67 
70 {
71  return internal->get_docid();
72 }
73 
74 string
76 {
77  return internal->get_data();
78 }
79 
80 void
81 Document::set_data(string_view data)
82 {
83  internal->set_data(data);
84 }
85 
86 void
88 {
89  if (term.empty()) {
91  }
92  internal->add_term(term, wdf_inc);
93 }
94 
95 void
97 {
98  if (term.empty()) {
100  }
101  if (!internal->remove_term(term)) {
102  string m;
103  m = "Document::remove_term() failed - term '";
104  m += term;
105  m += "' not present";
107  }
108 }
109 
110 void
112  Xapian::termpos term_pos,
113  Xapian::termcount wdf_inc)
114 {
115  if (term.empty()) {
117  }
118  internal->add_posting(term, term_pos, wdf_inc);
119 }
120 
121 void
123  Xapian::termpos term_pos,
124  Xapian::termcount wdf_dec)
125 {
126  if (term.empty()) {
128  }
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);
138  }
140  }
141 }
142 
145  Xapian::termpos term_pos_first,
146  Xapian::termpos term_pos_last,
147  Xapian::termcount wdf_dec)
148 {
149  if (term.empty()) {
151  }
152  if (rare(term_pos_first > term_pos_last)) {
153  return 0;
154  }
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";
163  }
164  return n_removed;
165 }
166 
167 void
169 {
170  internal->clear_terms();
171 }
172 
175  return internal->termlist_count();
176 }
177 
180 {
182 }
183 
184 string
186 {
187  return internal->get_value(slot);
188 }
189 
190 void
191 Document::add_value(Xapian::valueno slot, string_view value)
192 {
193  internal->add_value(slot, value);
194 }
195 
196 void
198 {
199  internal->clear_values();
200 }
201 
204  return internal->values_count();
205 }
206 
209 {
210  return internal->values_begin();
211 }
212 
213 string
215 {
216  return serialise_document(*this);
217 }
218 
219 Document
220 Document::unserialise(string_view serialised)
221 {
222  return unserialise_document(serialised);
223 }
224 
225 string
227 {
228  return internal->get_description();
229 }
230 
231 }
Abstract base class for a document.
bool remove_term(std::string_view term)
Remove a term from this document.
TermList * open_term_list() const
Start iterating the terms in this document.
Class representing a document.
Definition: document.h:64
Document & operator=(const Document &o)
Assignment operator.
Xapian::docid get_docid() const
Get the document ID this document came from.
Definition: document.cc:69
void set_data(std::string_view data)
Set the document data.
Definition: document.cc:81
std::string get_data() const
Get the document data.
Definition: document.cc:75
void add_term(std::string_view term, Xapian::termcount wdf_inc=1)
Add a term to this document.
Definition: document.cc:87
Xapian::valueno values_count() const
Count the value slots used in this document.
Definition: document.cc:203
Xapian::Internal::intrusive_ptr_nonnull< Internal > internal
Definition: document.h:67
ValueIterator values_begin() const
Start iterating the values in this document.
Definition: document.cc:208
std::string serialise() const
Serialise document into a string.
Definition: document.cc:214
std::string get_value(Xapian::valueno slot) const
Read a value slot in this document.
Definition: document.cc:185
void remove_term(std::string_view term)
Remove a term from this document.
Definition: document.cc:96
void remove_posting(std::string_view term, Xapian::termpos term_pos, Xapian::termcount wdf_dec=1)
Remove posting for a term.
Definition: document.cc:122
Xapian::termcount termlist_count() const
Return the number of distinct terms in this document.
Definition: document.cc:174
TermIterator termlist_begin() const
Start iterating the terms in this document.
Definition: document.cc:179
~Document()
Destructor.
Definition: document.cc:64
void clear_values()
Clear all value slots in this document.
Definition: document.cc:197
std::string get_description() const
Return a string describing this object.
Definition: document.cc:226
void clear_terms()
Clear all terms from the document.
Definition: document.cc:168
Xapian::termpos remove_postings(std::string_view term, Xapian::termpos term_pos_first, Xapian::termpos term_pos_last, Xapian::termcount wdf_dec=1)
Remove a range of postings for a term.
Definition: document.cc:144
static Document unserialise(std::string_view serialised)
Unserialise a document from a string produced by serialise().
Definition: document.cc:220
Document()
Default constructor.
Definition: document.cc:60
void add_value(Xapian::valueno slot, std::string_view value)
Add a value to a slot in this document.
Definition: document.cc:191
void add_posting(std::string_view term, Xapian::termpos term_pos, Xapian::termcount wdf_inc=1)
Add a posting for a term.
Definition: document.cc:111
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:229
Class for iterating over a list of terms.
Definition: termiterator.h:41
Class for iterating over document values.
Definition: valueiterator.h:39
#define rare(COND)
Definition: config.h:607
string term
static void throw_invalid_arg_empty_term()
Definition: document.cc:38
Class representing a document.
Abstract base class for a document.
Hierarchy of classes which Xapian can throw as exceptions.
string str(int value)
Convert int to std::string.
Definition: str.cc:91
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
unsigned valueno
The number for a value slot in a document.
Definition: types.h:90
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
unsigned XAPIAN_TERMPOS_BASE_TYPE termpos
A term position within a document or query.
Definition: types.h:75
string serialise_document(const Xapian::Document &doc)
Serialise a Xapian::Document object.
Definition: serialise.cc:183
Xapian::Document unserialise_document(string_view s)
Unserialise a serialised Xapian::Document object.
Definition: serialise.cc:223
functions to convert classes to strings and back
Convert types to std::string.