xapian-core  2.0.0
documentinternal.cc
Go to the documentation of this file.
1 
4 /* Copyright 2017,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 
24 
25 #include "api/documenttermlist.h"
26 #include "api/documentvaluelist.h"
27 #include "str.h"
29 
30 #include "xapian/valueiterator.h"
31 
32 #include <memory>
33 
34 using namespace std;
35 
36 namespace Xapian {
37 
38 void
39 Document::Internal::ensure_terms_fetched() const
40 {
41  if (terms)
42  return;
43 
44  terms.reset(new map<string, TermInfo, std::less<>>());
45  termlist_size = 0;
46  if (!database)
47  return;
48 
49  unique_ptr<TermList> t(database->open_term_list(did));
50  while (t->next() == NULL) {
51  ++termlist_size;
52  auto&& r = terms->emplace_hint(terms->end(),
53  t->get_termname(),
54  TermInfo(t->get_wdf()));
55  TermInfo& term = r->second;
56  unique_ptr<PositionList> p(t->positionlist_begin());
57  if (p) {
58  while (p->next()) {
59  term.append_position(p->get_position());
60  }
61  }
62  }
63 }
64 
65 void
66 Document::Internal::ensure_values_fetched() const
67 {
68  if (values)
69  return;
70 
71  values.reset(new map<Xapian::valueno, string>());
72  if (database) {
73  fetch_all_values(*values);
74  }
75 }
76 
77 string
78 Document::Internal::fetch_data() const
79 {
80  return string();
81 }
82 
83 void
84 Document::Internal::fetch_all_values(map<Xapian::valueno,
85  string>& values_) const
86 {
87  values_.clear();
88 }
89 
90 string
91 Document::Internal::fetch_value(Xapian::valueno) const
92 {
93  return string();
94 }
95 
96 Document::Internal::~Internal()
97 {
98  if (database)
99  database->invalidate_doc_object(this);
100 }
101 
102 TermList*
103 Document::Internal::open_term_list() const
104 {
105  if (terms)
106  return new DocumentTermList(this);
107 
108  if (!database)
109  return NULL;
110 
111  return database->open_term_list(did);
112 }
113 
115 Document::Internal::values_begin() const
116 {
117  if (!values && database) {
118  values.reset(new map<Xapian::valueno, string>());
119  fetch_all_values(*values);
120  }
121 
122  if (!values || values->empty())
123  return Xapian::ValueIterator();
124 
125  return Xapian::ValueIterator(new DocumentValueList(this));
126 }
127 
128 string
129 Document::Internal::get_description() const
130 {
131  string desc = "Document(docid=";
132  desc += str(did);
133 
134  if (data) {
135  desc += ", data=";
136  description_append(desc, *data);
137  }
138 
139  if (terms) {
140  desc += ", terms[";
141  desc += str(terms->size());
142  desc += ']';
143  }
144 
145  if (values) {
146  desc += ", values[";
147  desc += str(values->size());
148  desc += ']';
149  }
150 
151  if (database) {
152  desc += ", db=";
153  desc += database->get_description();
154  }
155 
156  desc += ')';
157 
158  return desc;
159 }
160 
161 }
Iteration over terms in a document.
Iteration over values in a document.
Metadata for a term in a document.
Definition: terminfo.h:28
virtual bool next()=0
Advance to the next entry in the positionlist.
virtual Xapian::termpos get_position() const =0
Return the current position.
Abstract base class for termlists.
Definition: termlist.h:42
Class for iterating over document values.
Definition: valueiterator.h:39
string term
PositionList * p
Append a string to an object description, escaping invalid UTF-8.
Abstract base class for a document.
Iteration over terms in a document.
Iteration over values in a document.
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 valueno
The number for a value slot in a document.
Definition: types.h:90
Convert types to std::string.
void description_append(std::string &desc, std::string_view s)
Definition: unittest.cc:105
Class for iterating over document values.