xapian-core  1.4.25
glass_cursor.h
Go to the documentation of this file.
1 
4 /* Copyright 1999,2000,2001 BrightStation PLC
5  * Copyright 2002,2003,2004,2006,2007,2008,2009,2010,2012,2013,2014,2016 Olly Betts
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22 
23 #ifndef OM_HGUARD_GLASS_CURSOR_H
24 #define OM_HGUARD_GLASS_CURSOR_H
25 
26 #include "glass_defs.h"
27 
28 #include "alignment_cast.h"
29 #include "omassert.h"
30 
31 #include <algorithm>
32 #include <cstring>
33 #include <string>
34 using std::string;
35 
36 #define BLK_UNUSED uint4(-1)
37 
38 namespace Glass {
39 
40 class Cursor {
41  // Prevent copying
42  Cursor(const Cursor &);
43  Cursor & operator=(const Cursor &);
44 
46  char * data;
47 
48  public:
50  Cursor() : data(0), c(-1), rewrite(false) { }
51 
52  ~Cursor() { destroy(); }
53 
54  uint8_t * init(unsigned block_size) {
55  if (data && refs() > 1) {
56  --refs();
57  data = NULL;
58  }
59  if (!data)
60  data = new char[block_size + 8];
61  refs() = 1;
63  rewrite = false;
64  c = -1;
65  return reinterpret_cast<uint8_t*>(data + 8);
66  }
67 
68  const uint8_t * clone(const Cursor & o) {
69  if (data != o.data) {
70  destroy();
71  data = o.data;
72  ++refs();
73  }
74  return reinterpret_cast<uint8_t*>(data + 8);
75  }
76 
77  void swap(Cursor & o) {
78  std::swap(data, o.data);
79  std::swap(c, o.c);
80  std::swap(rewrite, o.rewrite);
81  }
82 
83  void destroy() {
84  if (data) {
85  if (--refs() == 0)
86  delete [] data;
87  data = NULL;
88  rewrite = false;
89  }
90  }
91 
92  uint4 & refs() const {
93  Assert(data);
94  return *alignment_cast<uint4*>(data);
95  }
96 
101  uint4 get_n() const {
102  Assert(data);
103  return *alignment_cast<uint4*>(data + 4);
104  }
105 
106  void set_n(uint4 n) {
107  Assert(data);
108  // Assert(refs() == 1);
109  *alignment_cast<uint4*>(data + 4) = n;
110  }
111 
116  const uint8_t * get_p() const {
117  if (rare(!data)) return NULL;
118  return reinterpret_cast<uint8_t*>(data + 8);
119  }
120 
121  uint8_t * get_modifiable_p(unsigned block_size) {
122  if (rare(!data)) return NULL;
123  if (refs() > 1) {
124  char * new_data = new char[block_size + 8];
125  std::memcpy(new_data, data, block_size + 8);
126  --refs();
127  data = new_data;
128  refs() = 1;
129  }
130  return reinterpret_cast<uint8_t*>(data + 8);
131  }
132 
134  int c;
135 
137  bool rewrite;
138 };
139 
140 }
141 
142 class GlassTable;
143 
147 class GlassCursor {
149  GlassCursor(const GlassCursor &);
150 
152  GlassCursor & operator=(const GlassCursor &);
153 
158  void rebuild();
159 
160  protected:
167 
171 
172  private:
174  enum { UNREAD, UNREAD_ON_LAST_CHUNK, UNCOMPRESSED, COMPRESSED } tag_status;
175 
176  protected:
178  const GlassTable * B;
179 
180  private:
183 
184  unsigned long version;
185 
187  int level;
188 
208  void get_key(string * key) const;
209 
210  public:
222  explicit GlassCursor(const GlassTable *B,
223  const Glass::Cursor * C_ = NULL);
224 
230  GlassCursor * clone() const {
231  return new GlassCursor(B, C);
232  }
233 
235  ~GlassCursor();
236 
239  string current_key;
240 
244  string current_tag;
245 
258  bool read_tag(bool keep_compressed = false);
259 
274  bool next();
275 
298  bool find_entry(const string &key);
299 
308  bool find_exact(const string &key);
309 
311  void find_entry_lt(const string &key);
312 
318  bool find_entry_ge(const string &key);
319 
322  void to_end() { is_after_end = true; }
323 
329  bool after_end() const { return is_after_end; }
330 
332  const GlassTable * get_table() const { return B; }
333 };
334 
336  public:
350  explicit MutableGlassCursor(GlassTable *B_) : GlassCursor(B_) { }
351 
357  bool del();
358 };
359 
360 #endif /* OM_HGUARD_GLASS_CURSOR_H */
#define Assert(COND)
Definition: omassert.h:122
bool is_after_end
Whether the cursor is off the end of the table.
Definition: glass_cursor.h:170
Class managing a Btree table in a Glass database.
Definition: glass_table.h:425
const GlassTable * B
The Btree table.
Definition: glass_cursor.h:178
Cast a pointer we know is suitably aligned.
Definitions, types, etc for use inside glass.
bool after_end() const
Determine whether cursor is off the end of table.
Definition: glass_cursor.h:329
int level
The value of level in the Btree structure.
Definition: glass_cursor.h:187
std::enable_if< std::is_const< typename std::remove_pointer< U >::type >::value, T >::type alignment_cast(U ptr)
Cast a pointer we know is suitably aligned.
#define false
Definition: header.h:9
#define rare(COND)
Definition: config.h:565
void set_n(uint4 n)
Definition: glass_cursor.h:106
uint32_t uint4
Definition: internaltypes.h:32
uint4 & refs() const
Definition: glass_cursor.h:92
string current_key
Current key pointed to by cursor.
Definition: glass_cursor.h:239
string current_tag
Current tag pointed to by cursor.
Definition: glass_cursor.h:244
unsigned long version
Definition: glass_cursor.h:184
const uint8_t * clone(const Cursor &o)
Definition: glass_cursor.h:68
uint8_t * get_modifiable_p(unsigned block_size)
Definition: glass_cursor.h:121
#define BLK_UNUSED
Definition: glass_cursor.h:36
A cursor pointing to a position in a Btree table, for reading several entries in order, or finding approximate matches.
Definition: glass_cursor.h:147
GlassCursor * clone() const
Clone a cursor.
Definition: glass_cursor.h:230
Cursor()
Constructor.
Definition: glass_cursor.h:50
const GlassTable * get_table() const
Return a pointer to the GlassTable we&#39;re a cursor for.
Definition: glass_cursor.h:332
int c
offset in the block&#39;s directory
Definition: glass_cursor.h:134
char * data
Pointer to reference counted data.
Definition: glass_cursor.h:46
void swap(Cursor &o)
Definition: glass_cursor.h:77
MutableGlassCursor(GlassTable *B_)
Create a mutable cursor attached to a Btree.
Definition: glass_cursor.h:350
bool is_positioned
Whether the cursor is positioned at a valid entry.
Definition: glass_cursor.h:166
const uint8_t * get_p() const
Get pointer to block.
Definition: glass_cursor.h:116
Various assertion macros.
Glass::Cursor * C
Pointer to an array of Cursors.
Definition: glass_cursor.h:182
uint4 get_n() const
Get the block number.
Definition: glass_cursor.h:101
Cursor & operator=(const Cursor &)
uint8_t * init(unsigned block_size)
Definition: glass_cursor.h:54
bool rewrite
true if the block is not the same as on disk, and so needs rewriting
Definition: glass_cursor.h:137
void to_end()
Set the cursor to be off the end of the table.
Definition: glass_cursor.h:322
void destroy()
Definition: glass_cursor.h:83