xapian-core  2.0.0
honey_metadata.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2004,2005,2006,2007,2008,2009,2010,2011,2017,2024 Olly Betts
5  * Copyright (C) 2008 Lemur Consulting Ltd
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 
24 #include "honey_metadata.h"
25 
26 #include "honey_cursor.h"
27 
29 #include "debuglog.h"
30 #include "omassert.h"
31 #include "stringutils.h"
32 
33 #include "xapian/error.h"
34 
35 #include <string>
36 #include <string_view>
37 
38 using namespace std;
39 using namespace std::string_literals;
41 
43  const Xapian::Database::Internal* database_,
44  HoneyCursor* cursor_,
45  string_view prefix_)
46  : database(database_), cursor(cursor_), prefix("\0\0"s.append(prefix_))
47 {
48  LOGCALL_CTOR(DB, "HoneyMetadataTermList", database_ | cursor_ | prefix_);
49  Assert(cursor);
50  // Set the cursor to its end to signal we haven't started yet.
51  cursor->to_end();
52 }
53 
55 {
56  LOGCALL_DTOR(DB, "HoneyMetadataTermList");
57  delete cursor;
58 }
59 
62 {
63  // Very approximate! This is only used to build a balanced or tree, so
64  // at least we'll get an even tree by returning a constant answer.
65  return 1;
66 }
67 
70 {
71  throw Xapian::InvalidOperationError("HoneyMetadataTermList::get_termfreq() "
72  "not meaningful");
73 }
74 
75 TermList*
77 {
78  LOGCALL(DB, TermList*, "HoneyMetadataTermList::next", NO_ARGS);
79  Assert(cursor != NULL);
80 
81  if (cursor->after_end()) {
82  // This is the first action on a new HoneyMetadataTermList.
84  RETURN(NULL);
85  } else {
86  cursor->next();
87  }
88 
90  // We've reached the end of the prefixed terms.
91  RETURN(this);
92  }
93  current_term.assign(cursor->current_key, 2);
94  RETURN(NULL);
95 }
96 
97 TermList*
99 {
100  LOGCALL(DB, TermList*, "HoneyMetadataTermList::skip_to", key);
101  Assert(cursor != NULL);
102 
103  // k is the table key (key is the user metadata key).
104  string k(2, '\0');
105  k += key;
106  if (cursor->after_end() && prefix > k) {
107  // This is the first action on a new HoneySynonymTermList and we were
108  // asked to skip to a key before the prefix - this ought to leave us
109  // on the first key with the specified prefix.
110  k = prefix;
111  }
112 
113  if (cursor->find_entry_ge(k)) {
114  // Exact match.
115  current_term = key;
116  } else {
117  // The exact key we asked for isn't there, so check if the next
118  // key after it also has the right prefix.
120  // We've reached the end of the prefixed keys.
121  RETURN(this);
122  }
123  current_term.assign(cursor->current_key, 2);
124  }
125  RETURN(NULL);
126 }
bool after_end() const
Definition: honey_cursor.h:94
bool find_entry_ge(std::string_view key)
Definition: honey_cursor.h:110
void to_end()
Definition: honey_cursor.h:92
bool next()
Definition: honey_cursor.h:96
std::string current_key
Definition: honey_cursor.h:43
Xapian::doccount get_termfreq() const
Return the term frequency for the term at the current position.
Xapian::termcount get_approx_size() const
Return approximate size of this termlist.
HoneyCursor * cursor
A cursor which runs through the postlist table reading metadata keys.
TermList * next()
Advance to the next term in the list.
HoneyMetadataTermList(const HoneyMetadataTermList &)
Copying is not allowed.
std::string prefix
The prefix that all returned keys must have.
TermList * skip_to(std::string_view key)
Advance to the first key which is >= key.
Virtual base class for Database internals.
A smart pointer that uses intrusive reference counting.
Definition: intrusive_ptr.h:83
InvalidOperationError indicates the API was used in an invalid way.
Definition: error.h:271
Abstract base class for termlists.
Definition: termlist.h:42
std::string current_term
The current term.
Definition: termlist.h:54
Virtual base class for Database internals.
Debug logging macros.
#define RETURN(...)
Definition: debuglog.h:484
#define LOGCALL(CATEGORY, TYPE, FUNC, PARAMS)
Definition: debuglog.h:478
#define LOGCALL_CTOR(CATEGORY, CLASS, PARAMS)
Definition: debuglog.h:480
#define LOGCALL_DTOR(CATEGORY, CLASS)
Definition: debuglog.h:481
Hierarchy of classes which Xapian can throw as exceptions.
HoneyCursor class.
Access to metadata for a honey database.
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
Various assertion macros.
#define Assert(COND)
Definition: omassert.h:122
Various handy string-related helpers.
bool startswith(std::string_view s, char pfx)
Definition: stringutils.h:56