1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/** @file
* @brief Class representing a document
*/
/* Copyright 2008,2017,2018,2024 Olly Betts
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see
* <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "xapian/document.h"
#include <string>
#include <string_view>
#include "backends/documentinternal.h"
#include "net/serialise.h"
#include "str.h"
#include "xapian/error.h"
using namespace std;
[[noreturn]]
static void
throw_invalid_arg_empty_term()
{
throw Xapian::InvalidArgumentError("Empty termnames are invalid");
}
namespace Xapian {
Document::Document(Document::Internal* internal_)
: internal(internal_)
{
}
Document::Document(const Document&) = default;
Document&
Document::operator=(const Document&) = default;
Document::Document(Document&&) = default;
Document&
Document::operator=(Document&&) = default;
Document::Document() : internal(new Xapian::Document::Internal)
{
}
Document::~Document()
{
}
Xapian::docid
Document::get_docid() const
{
return internal->get_docid();
}
string
Document::get_data() const
{
return internal->get_data();
}
void
Document::set_data(string_view data)
{
internal->set_data(data);
}
void
Document::add_term(string_view term, Xapian::termcount wdf_inc)
{
if (term.empty()) {
throw_invalid_arg_empty_term();
}
internal->add_term(term, wdf_inc);
}
void
Document::remove_term(string_view term)
{
if (term.empty()) {
throw_invalid_arg_empty_term();
}
if (!internal->remove_term(term)) {
string m;
m = "Document::remove_term() failed - term '";
m += term;
m += "' not present";
throw Xapian::InvalidArgumentError(m);
}
}
void
Document::add_posting(string_view term,
Xapian::termpos term_pos,
Xapian::termcount wdf_inc)
{
if (term.empty()) {
throw_invalid_arg_empty_term();
}
internal->add_posting(term, term_pos, wdf_inc);
}
void
Document::remove_posting(string_view term,
Xapian::termpos term_pos,
Xapian::termcount wdf_dec)
{
if (term.empty()) {
throw_invalid_arg_empty_term();
}
auto res = internal->remove_posting(term, term_pos, wdf_dec);
if (res != Document::Internal::OK) {
string m = "Document::remove_posting() failed - term '";
m += term;
if (res == Document::Internal::NO_TERM) {
m += "' not present";
} else {
m += "' not present at position ";
m += str(term_pos);
}
throw Xapian::InvalidArgumentError(m);
}
}
Xapian::termpos
Document::remove_postings(string_view term,
Xapian::termpos term_pos_first,
Xapian::termpos term_pos_last,
Xapian::termcount wdf_dec)
{
if (term.empty()) {
throw_invalid_arg_empty_term();
}
if (rare(term_pos_first > term_pos_last)) {
return 0;
}
Xapian::termpos n_removed;
auto res = internal->remove_postings(term, term_pos_first, term_pos_last,
wdf_dec, n_removed);
if (res != Document::Internal::OK) {
string m = "Document::remove_postings() failed - term '";
m += term;
m += "' not present";
throw Xapian::InvalidArgumentError(m);
}
return n_removed;
}
void
Document::clear_terms()
{
internal->clear_terms();
}
Xapian::termcount
Document::termlist_count() const {
return internal->termlist_count();
}
TermIterator
Document::termlist_begin() const
{
return TermIterator(internal->open_term_list());
}
string
Document::get_value(Xapian::valueno slot) const
{
return internal->get_value(slot);
}
void
Document::add_value(Xapian::valueno slot, string_view value)
{
internal->add_value(slot, value);
}
void
Document::clear_values()
{
internal->clear_values();
}
Xapian::valueno
Document::values_count() const {
return internal->values_count();
}
ValueIterator
Document::values_begin() const
{
return internal->values_begin();
}
string
Document::serialise() const
{
return serialise_document(*this);
}
Document
Document::unserialise(string_view serialised)
{
return unserialise_document(serialised);
}
string
Document::get_description() const
{
return internal->get_description();
}
}
|