Zero  0.1.0
scan.h
Go to the documentation of this file.
1 #ifndef __SCAN_H
2 #define __SCAN_H
3 
4 #include "table_man.h"
5 
6 class base_scan_t {
7 protected:
9 
11 
12 public:
14  : _pindex(pindex),
15  btcursor(nullptr) {
16  w_assert1(_pindex);
17  }
18 
19  virtual ~base_scan_t() {
20  if (btcursor) {
21  delete btcursor;
22  }
23  };
24 
25  w_rc_t open_scan(bool forward = true) {
26  if (!btcursor) {
27  btcursor = new bt_cursor_t(_pindex->stid(), forward);
28  }
29  return (RCOK);
30  }
31 
32  w_rc_t open_scan(char* bound, int bsz, bool incl, bool forward = true) {
33  if (!btcursor) {
34  w_keystr_t kstr;
35  kstr.construct_regularkey(bound, bsz);
36  btcursor = new bt_cursor_t(_pindex->stid(), kstr, incl, forward);
37  }
38 
39  return (RCOK);
40  }
41 
42  w_rc_t open_scan(char* lower, int lowsz, bool lower_incl,
43  char* upper, int upsz, bool upper_incl,
44  bool forward = true) {
45  if (!btcursor) {
46  w_keystr_t kup, klow;
47  kup.construct_regularkey(upper, upsz);
48  klow.construct_regularkey(lower, lowsz);
49  btcursor = new bt_cursor_t(
50  _pindex->stid(),
51  klow, lower_incl, kup, upper_incl, forward);
52  }
53 
54  return (RCOK);
55  }
56 
57  virtual w_rc_t next(bool& eof, table_row_t& tuple) = 0;
58 };
59 
60 template<class T>
62 public:
63 
65  : base_scan_t(pmanager->table()->primary_idx()) {}
66 
67  virtual ~table_scan_iter_impl() {}
68 
69  virtual w_rc_t next(bool& eof, table_row_t& tuple) {
70  if (!btcursor) {
71  open_scan();
72  }
73 
74  W_DO(btcursor->next());
75 
76  eof = btcursor->eof();
77  if (eof) {
78  return RCOK;
79  }
80 
81  // Load key
83  tuple.load_key(tuple._rep_key->_dest, _pindex);
84 
85  // Load element
86  char* elem = btcursor->elem();
87  tuple.load_value(elem, _pindex);
88 
89  return (RCOK);
90  }
91 };
92 
93 template<class T>
95 private:
97 
99 
100 public:
102  table_man_t<T>* pmanager,
103  bool need_tuple = false)
104  : base_scan_t(pindex),
105  _need_tuple(need_tuple) {
106  assert (_pindex);
107  assert (pmanager);
108  _primary_idx = pmanager->table()->primary_idx();
109  }
110 
111  virtual ~index_scan_iter_impl() {};
112 
113  virtual w_rc_t next(bool& eof, table_row_t& tuple) {
114  if (!btcursor) {
115  open_scan();
116  }
117 
118  assert (btcursor);
119 
120  W_DO(btcursor->next());
121 
122  eof = btcursor->eof();
123  if (eof) {
124  return RCOK;
125  }
126 
127  bool loaded = false;
128 
129  if (!_need_tuple) {
130  // Load only fields of secondary key (index key)
132  tuple.load_key(tuple._rep_key->_dest, _pindex);
133  } else {
134  // Fetch complete tuple from primary index
135  index_desc_t* prim_idx = _primary_idx;
136  char* pkey = btcursor->elem();
137  smsize_t elen = btcursor->elen();
138 
139  // load primary key fields
140  tuple.load_key(pkey, prim_idx);
141 
142  // fetch and load other fields
143  w_keystr_t pkeystr;
144  pkeystr.construct_regularkey(pkey, elen);
145  ss_m::find_assoc(prim_idx->stid(), pkeystr, tuple._rep->_dest,
146  elen, loaded);
147  w_assert0(loaded);
148 
149  tuple.load_value(tuple._rep->_dest, prim_idx);
150  }
151  return (RCOK);
152  }
153 };
154 
155 #endif // __SCAN_H
A cursor object to sequentially read BTree.
Definition: btcursor.h:64
index_desc_t * _pindex
Definition: scan.h:8
bool construct_regularkey(const void *nonkeystr, w_keystr_len_t length)
Definition: w_key.h:281
const w_rc_t RCOK
Definition: w_rc.h:239
w_rc_t open_scan(char *bound, int bsz, bool incl, bool forward=true)
Definition: scan.h:32
#define w_assert1(x)
Level 1 should not add significant extra time.
Definition: w_base.h:198
uint32_t smsize_t
Definition: basics.h:41
static rc_t find_assoc(StoreID stid, const w_keystr_t &key, void *el, smsize_t &elen, bool &found)
Find an entry associated with a key in a B+-Tree index.
Definition: smindex.cpp:88
T * table()
Definition: table_man.h:140
index_scan_iter_impl(index_desc_t *pindex, table_man_t< T > *pmanager, bool need_tuple=false)
Definition: scan.h:101
Definition: scan.h:61
w_rc_t open_scan(bool forward=true)
Definition: scan.h:25
virtual w_rc_t next(bool &eof, table_row_t &tuple)
Definition: scan.h:69
void load_value(char *data, index_desc_t *pindex=nullptr)
Definition: row.cpp:339
Key string class which can represent a few special values.
Definition: w_key.h:47
Definition: scan.h:94
virtual ~table_scan_iter_impl()
Definition: scan.h:67
bool _need_tuple
Definition: scan.h:98
base_scan_t(index_desc_t *pindex)
Definition: scan.h:13
const w_keystr_t & key()
Definition: btcursor.h:122
#define w_assert0(x)
Default assert/debug level is 0.
Definition: w_base.h:175
virtual ~index_scan_iter_impl()
Definition: scan.h:111
w_rc_t open_scan(char *lower, int lowsz, bool lower_incl, char *upper, int upsz, bool upper_incl, bool forward=true)
Definition: scan.h:42
Definition: scan.h:6
bt_cursor_t * btcursor
Definition: scan.h:10
Return code for most functions and methods.
Definition: w_rc.h:87
Definition: index_desc.h:60
int elen() const
Definition: btcursor.h:135
void serialize_as_nonkeystr(void *buffer) const
Definition: w_key.h:478
StoreID & stid()
Definition: index_desc.h:117
rep_row_t * _rep
Definition: row.h:171
rc_t next()
Definition: btcursor.cpp:247
#define W_DO(x)
Call a method or function x. This macro is the normal idiom for calling a method or function...
Definition: w_rc.h:304
: Base class for tables stored in Shore
table_scan_iter_impl(table_man_t< T > *pmanager)
Definition: scan.h:64
rep_row_t * _rep_key
Definition: row.h:172
char * _dest
Definition: row.h:118
Definition: row.h:147
virtual w_rc_t next(bool &eof, table_row_t &tuple)=0
virtual ~base_scan_t()
Definition: scan.h:19
bool eof()
Definition: btcursor.h:131
virtual w_rc_t next(bool &eof, table_row_t &tuple)
Definition: scan.h:113
Definition: table_man.h:117
index_desc_t * _primary_idx
Definition: scan.h:96
char * elem()
Definition: btcursor.h:139
void load_key(char *data, index_desc_t *pindex=nullptr)
Definition: row.cpp:249