xapian-core  2.0.0
honey_cursor.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2017,2018,2024 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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 #ifndef XAPIAN_INCLUDED_HONEY_CURSOR_H
22 #define XAPIAN_INCLUDED_HONEY_CURSOR_H
23 
24 #include "honey_table.h"
25 
26 class HoneyCursor {
33  bool do_find(std::string_view key, bool greater_than);
34 
35  bool do_next();
36 
38  bool next_from_index();
39 
41 
42  public:
43  std::string current_key, current_tag;
44  mutable size_t val_size = 0;
45  bool current_compressed = false;
47  bool is_at_end = false;
48  mutable std::string last_key;
49 
50  // File offset to start of index.
51  off_t root;
52 
53  // File offset to start of table (zero except for single-file DB).
54  off_t offset;
55 
56  // Forward to next constructor form.
57  explicit HoneyCursor(const HoneyTable* table)
58  : store(table->store),
59  comp_stream(Z_DEFAULT_STRATEGY),
60  root(table->get_root()),
61  offset(table->get_offset())
62  {
63  store.set_pos(offset); // FIXME root
64  }
65 
67  : store(o.store),
69  current_tag(o.current_tag), // FIXME really copy?
70  val_size(o.val_size),
72  comp_stream(Z_DEFAULT_STRATEGY),
74  last_key(o.last_key),
75  root(o.root),
76  offset(o.offset)
77  {
79  }
80 
85  void rewind() {
86  store.set_pos(offset); // FIXME root
87  current_key = last_key = std::string();
88  is_at_end = false;
89  val_size = 0;
90  }
91 
92  void to_end() { is_at_end = true; }
93 
94  bool after_end() const { return is_at_end; }
95 
96  bool next() {
97  if (store.was_forced_closed()) {
99  }
100 
101  return do_next();
102  }
103 
104  bool read_tag(bool keep_compressed = false);
105 
106  bool find_exact(std::string_view key) {
107  return do_find(key, false);
108  }
109 
110  bool find_entry_ge(std::string_view key) {
111  return do_find(key, true);
112  }
113 
123  bool prev();
124 };
125 
128 
129  public:
131  : HoneyCursor(table_),
132  table(table_)
133  { }
134 
135  bool del() {
136  Assert(!is_at_end);
137  std::string key_to_del = current_key;
138  bool res = next();
139  table->del(key_to_del);
140  return res;
141  }
142 };
143 
144 #endif // XAPIAN_INCLUDED_HONEY_CURSOR_H
off_t get_pos() const
Definition: honey_table.h:171
void set_pos(off_t pos_)
Definition: honey_table.h:175
bool was_forced_closed() const
Definition: honey_table.h:150
bool read_tag(bool keep_compressed=false)
HoneyCursor(const HoneyTable *table)
Definition: honey_cursor.h:57
bool find_exact(std::string_view key)
Definition: honey_cursor.h:106
bool current_compressed
Definition: honey_cursor.h:45
bool after_end() const
Definition: honey_cursor.h:94
HoneyCursor(const HoneyCursor &o)
Definition: honey_cursor.h:66
bool do_find(std::string_view key, bool greater_than)
Search for key.
bool next_from_index()
Handle the value part of the (key,value).
Definition: honey_cursor.cc:90
CompressionStream comp_stream
Definition: honey_cursor.h:46
void rewind()
Position cursor on the dummy empty key.
Definition: honey_cursor.h:85
bool prev()
Move to the item before the current one.
bool find_entry_ge(std::string_view key)
Definition: honey_cursor.h:110
void to_end()
Definition: honey_cursor.h:92
std::string last_key
Definition: honey_cursor.h:48
bool is_at_end
Definition: honey_cursor.h:47
std::string current_tag
Definition: honey_cursor.h:43
bool do_next()
Definition: honey_cursor.cc:37
bool next()
Definition: honey_cursor.h:96
BufferedFile store
Definition: honey_cursor.h:40
size_t val_size
Definition: honey_cursor.h:44
off_t offset
Definition: honey_cursor.h:54
std::string current_key
Definition: honey_cursor.h:43
bool del(const std::string &)
Definition: honey_table.h:671
static void throw_database_closed()
Definition: honey_table.h:689
HoneyTable * table
Definition: honey_cursor.h:127
MutableHoneyCursor(HoneyTable *table_)
Definition: honey_cursor.h:130
HoneyTable class.
#define Assert(COND)
Definition: omassert.h:122