Handle removing doc term at current TermIterator position
[xapian.git] / xapian-core / backends / documentinternal.cc
blobe540f87e201ba3063a82c4f97d4fb80b618c9899
1 /** @file documentinternal.cc
2 * @brief Abstract base class for a document
3 */
4 /* Copyright 2017 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 "backends/documentinternal.h"
25 #include "api/documenttermlist.h"
26 #include "api/documentvaluelist.h"
27 #include "str.h"
28 #include "unicode/description_append.h"
30 #include "xapian/valueiterator.h"
32 using namespace std;
34 namespace Xapian {
36 void
37 Document::Internal::ensure_terms_fetched() const
39 if (terms)
40 return;
42 terms.reset(new map<string, TermInfo>());
43 termlist_size = 0;
44 if (!database.get())
45 return;
47 unique_ptr<TermList> t(database->open_term_list(did));
48 while (t->next(), !t->at_end()) {
49 ++termlist_size;
50 auto&& r = terms->emplace(make_pair(t->get_termname(),
51 TermInfo(t->get_wdf())));
52 TermInfo& term = r.first->second;
53 for (auto p = t->positionlist_begin(); p != PositionIterator(); ++p) {
54 term.append_position(*p);
59 void
60 Document::Internal::ensure_values_fetched() const
62 if (values)
63 return;
65 values.reset(new map<Xapian::valueno, string>());
66 if (database.get()) {
67 fetch_all_values(*values);
71 string
72 Document::Internal::fetch_data() const
74 return string();
77 void
78 Document::Internal::fetch_all_values(map<Xapian::valueno,
79 string>& values_) const
81 values_.clear();
84 string
85 Document::Internal::fetch_value(Xapian::valueno) const
87 return string();
90 Document::Internal::~Internal()
92 if (database.get())
93 database->invalidate_doc_object(this);
96 TermList*
97 Document::Internal::open_term_list() const
99 if (terms)
100 return new DocumentTermList(this);
102 if (!database.get())
103 return NULL;
105 return database->open_term_list(did);
108 Xapian::ValueIterator
109 Document::Internal::values_begin() const
111 if (!values && database.get()) {
112 values.reset(new map<Xapian::valueno, string>());
113 fetch_all_values(*values);
116 if (!values || values->empty())
117 return Xapian::ValueIterator();
119 return Xapian::ValueIterator(new DocumentValueList(this));
122 string
123 Document::Internal::get_description() const
125 string desc = "Document(docid=";
126 desc += str(did);
128 if (data) {
129 desc += ", data=";
130 description_append(desc, *data);
133 if (terms) {
134 desc += ", terms[";
135 desc += str(terms->size());
136 desc += ']';
139 if (values) {
140 desc += ", values[";
141 desc += str(values->size());
142 desc += ']';
145 if (database.get()) {
146 desc += ", db=";
147 desc += database->get_description();
150 desc += ')';
152 return desc;