xapian-core  2.0.0
glass_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 "glass_metadata.h"
25 
26 #include "glass_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_view>
36 
37 using namespace std;
38 using namespace std::string_literals;
40 
43  GlassCursor * cursor_,
44  string_view prefix_)
45  : database(database_),
46  cursor(cursor_),
47  prefix("\x00\xc0"s.append(prefix_))
48 {
49  LOGCALL_CTOR(DB, "GlassMetadataTermList", database_ | cursor_ | prefix_);
50  Assert(cursor);
51  // Seek to the first key before the first metadata key.
53 }
54 
56 {
57  LOGCALL_DTOR(DB, "GlassMetadataTermList");
58  delete cursor;
59 }
60 
63 {
64  // Very approximate! This is only used to build a balanced or tree, so
65  // at least we'll get an even tree by returning a constant answer.
66  return 1;
67 }
68 
71 {
72  throw Xapian::InvalidOperationError("GlassMetadataTermList::get_termfreq() not meaningful");
73 }
74 
75 TermList *
77 {
78  LOGCALL(DB, TermList *, "GlassMetadataTermList::next", NO_ARGS);
79  Assert(!cursor->after_end());
80 
81  if (!cursor->next() || !startswith(cursor->current_key, prefix)) {
82  // We've reached the end of the prefixed terms.
83  RETURN(this);
84  }
85  current_term.assign(cursor->current_key, 2);
86  RETURN(NULL);
87 }
88 
89 TermList*
91 {
92  LOGCALL(DB, TermList *, "GlassMetadataTermList::skip_to", key);
93  Assert(!cursor->after_end());
94 
95  if (cursor->find_entry_ge(string("\x00\xc0", 2).append(key))) {
96  // Exact match.
97  current_term = key;
98  } else {
99  // The exact term we asked for isn't there, so check if the next
100  // term after it also has the right prefix.
102  // We've reached the end of the prefixed terms.
103  RETURN(this);
104  }
105  current_term.assign(cursor->current_key, 2);
106  }
107  RETURN(NULL);
108 }
A cursor pointing to a position in a Btree table, for reading several entries in order,...
Definition: glass_cursor.h:148
string current_key
Current key pointed to by cursor.
Definition: glass_cursor.h:239
bool after_end() const
Determine whether cursor is off the end of table.
Definition: glass_cursor.h:337
bool next()
Advance to the next key.
bool find_entry_ge(std::string_view key)
Position the cursor on the lowest entry with key >= key.
void find_entry_lt(const string &key)
Position the cursor on the highest entry with key < key.
Xapian::termcount get_approx_size() const
Return approximate size of this termlist.
std::string prefix
The prefix that all returned keys must have.
GlassMetadataTermList(const GlassMetadataTermList &)
Copying is not allowed.
GlassCursor * cursor
A cursor which runs through the postlist table reading metadata keys.
TermList * skip_to(std::string_view key)
Advance to the first key which is >= key.
TermList * next()
Advance to the next term in the list.
Xapian::doccount get_termfreq() const
Return the term frequency for the term at the current position.
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.
Interface to Btree cursors.
Access to metadata for a glass 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