Zero  0.1.0
table_man.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-kits -- Benchmark implementations for Shore-MT
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
35 /* shore_table.h contains the base class (table_desc_t) for tables stored in
36  * Shore. Each table consists of several parts:
37  *
38  * 1. An array of field_desc, which contains the decription of the
39  * fields. The number of fields is set by the constructor. The schema
40  * of the table is not written to the disk.
41  *
42  * 2. The primary index of the table.
43  *
44  * 3. Secondary indices on the table. All the secondary indices created
45  * on the table are stored as a linked list.
46  *
47  *
48  * FUNCTIONALITY
49  *
50  * There are methods in (table_desc_t) for creating, the table
51  * and indexes.
52  *
53  *
54  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
55  * @note Modifications to the schema need rebuilding the whole
56  * database.
57  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
58  *
59  *
60  * USAGE:
61  *
62  * To create a new table, create a class for the table by inheriting
63  * publicly from class tuple_desc_t to take advantage of all the
64  * built-in tools. The schema of the table should be set at the
65  * constructor of the table. (See shore_tpcc_schema.h for examples.)
66  *
67  *
68  * NOTE:
69  *
70  * Due to limitation of Shore implementation, only the last field
71  * in indexes can be variable length.
72  *
73  *
74  * BUGS:
75  *
76  * If a new index is created on an existing table, explicit call to
77  * load the index is needed.
78  *
79  * Timestamp field is not fully implemented: no set function.
80  *
81  *
82  * EXTENSIONS:
83  *
84  * The mapping between SQL types and C++ types are defined in
85  * (field_desc_t). Modify the class to support more SQL types or
86  * change the mapping. The NUMERIC type is currently stored as string;
87  * no further understanding is provided yet.
88  *
89  */
90 
91 #ifndef __TABLE_MAN_H
92 #define __TABLE_MAN_H
93 
94 #include "tls.h"
95 #include "sm_vas.h"
96 #include "latches.h"
97 #include "btcursor.h"
98 
99 //#include "shore_msg.h"
100 #include "util/guard.h"
101 
102 #include "table_desc.h"
103 #include "field.h"
104 #include "index_desc.h"
105 #include "row.h"
106 #include "row_cache.h"
107 
108 /* ---------------------------------------------------------------
109  *
110  * @class: table_man_t
111  *
112  * @brief: Base class for operations on a Shore table.
113  *
114  * --------------------------------------------------------------- */
115 
116 template<class T> // T is a table_desc_t implementation
117 class table_man_t {
118 protected:
119 
120  T* _ptable; /* pointer back to the table description */
121 
122  guard<blob_pool> _pts; /* trash stack */
123 
124 public:
125 
126  table_man_t(T* aTableDesc,
127  bool construct_cache = true)
128  : _ptable(aTableDesc) {
129  // init tuple cache
130  if (construct_cache) {
131  // init trash stack
132  _pts = new blob_pool(_ptable->maxsize());
133  // CS TODO: implement new row cache!
135  }
136  }
137 
138  virtual ~table_man_t() {}
139 
140  T* table() {
141  return (_ptable);
142  }
143 
144  // loads store id values in fid field for this table and its indexes
146 
147  /* ------------------------------ */
148  /* --- trash stack operations --- */
149  /* ------------------------------ */
150 
152  assert (_pts);
153  return (_pts);
154  }
155 
156 
157  /* ---------------------------- */
158  /* --- access through index --- */
159  /* ---------------------------- */
160 
161  // idx probe
162  w_rc_t index_probe(ss_m* db,
163  index_desc_t* pidx,
164  table_row_t* ptuple,
165  const lock_mode_t lock_mode = okvl_mode::S, /* One of: N, S, X */
166  const PageID& root = 0); /* Start of the search */
167 
168  // probe idx in X (& LATCH_EX) mode
170  index_desc_t* pidx,
171  table_row_t* ptuple,
172  const PageID& root = 0) {
173  return (index_probe(db, pidx, ptuple, okvl_mode::X, root));
174  }
175 
176  // probe idx in N (& LATCH_SH) mode
178  index_desc_t* pidx,
179  table_row_t* ptuple,
180  const PageID& root = 0) {
181  return (index_probe(db, pidx, ptuple, okvl_mode::N, root));
182  }
183 
184  // probe primary idx
186  table_row_t* ptuple,
187  lock_mode_t lock_mode = okvl_mode::S,
188  const PageID& root = 0) {
189  assert (_ptable && _ptable->primary_idx());
190  return (index_probe(db, _ptable->primary_idx(), ptuple, lock_mode, root));
191  }
192 
193  // idx probe - based on idx name //
195  const char* idx_name,
196  table_row_t* ptuple,
197  lock_mode_t lock_mode = okvl_mode::S,
198  const PageID& root = 0) {
199  index_desc_t* pindex = _ptable->find_index(idx_name);
200  return (index_probe(db, pindex, ptuple, lock_mode, root));
201  }
202 
203  // probe idx in EX (& LATCH_EX) mode - based on idx name //
205  const char* idx_name,
206  table_row_t* ptuple,
207  const PageID& root = 0) {
208  index_desc_t* pindex = _ptable->find_index(idx_name);
209  w_assert0(pindex);
210  return (index_probe_forupdate(db, pindex, ptuple, root));
211  }
212 
213  // probe idx in N (& LATCH_NL) mode - based on idx name //
215  const char* idx_name,
216  table_row_t* ptuple,
217  const PageID& root = 0) {
218  index_desc_t* pindex = _ptable->find_index(idx_name);
219  return (index_probe_nl(db, pindex, ptuple, root));
220  }
221 
222 
223  /* -------------------------- */
224  /* --- tuple manipulation --- */
225  /* -------------------------- */
226 
227  w_rc_t add_tuple(ss_m* db,
228  table_row_t* ptuple,
229  const lock_mode_t lock_mode = okvl_mode::X,
230  const PageID& primary_root = 0);
231 
233  const char* idx_name,
234  table_row_t* ptuple,
235  const lock_mode_t lock_mode = okvl_mode::X,
236  const PageID& primary_root = 0);
237 
239  table_row_t* ptuple,
240  const lock_mode_t lock_mode = okvl_mode::X,
241  const PageID& primary_root = 0);
242 
244  const char* idx_name,
245  table_row_t* ptuple,
246  const lock_mode_t lock_mode = okvl_mode::X,
247  const PageID& primary_root = 0);
248 
250  table_row_t* ptuple,
251  const lock_mode_t lock_mode = okvl_mode::X);
252 
253  // set indexed fields of the row to minimum
254  int min_key(index_desc_t* pindex,
255  table_row_t* ptuple,
256  rep_row_t& arep);
257 
258  // set indexed fields of the row to maximum
259  int max_key(index_desc_t* pindex,
260  table_row_t* ptuple,
261  rep_row_t& arep);
262 
263  // length of the formatted key
264  int key_size(index_desc_t* pindex) const;
265 
266 
267 
268 
269 
270  /* ------------------------------------------------------- */
271  /* --- check consistency between the indexes and table --- */
272  /* ------------------------------------------------------- */
273 
274  // CS TODO
275  // virtual w_rc_t check_all_indexes_together(ss_m* db)=0;
276  // virtual bool check_all_indexes(ss_m* db)=0;
277  // virtual w_rc_t check_index(ss_m* db, index_desc_t* pidx)=0;
278  // virtual w_rc_t scan_all_indexes(ss_m* db)=0;
279  // virtual w_rc_t scan_index(ss_m* db, index_desc_t* pidx)=0;
280 
281 
282  /* -------------------------------- */
283  /* - population related if needed - */
284  /* -------------------------------- */
285  virtual w_rc_t populate(ss_m* /* db */, bool& /* hasNext */) {
286  return (RCOK);
287  }
288 
289 
290  /* ----------------- */
291  /* --- debugging --- */
292  /* ----------------- */
293 
294  /*
295  * print the table on screen or to files
296  */
297  virtual w_rc_t print_table(ostream& os, unsigned int num_lines = 0);
298 
299  virtual w_rc_t print_index(unsigned i, ostream& os,
300  unsigned int num_lines = 0, bool need_tuple = false);
301 
302 
303  /* --------------- */
304  /* --- caching --- */
305  /* --------------- */
306 
307  /* fetch the pages of the table and its indexes to buffer pool */
308  virtual w_rc_t fetch_table(ss_m* db, lock_mode_t alm = okvl_mode::S);
309 
310 // Row cache
311 protected:
312 
313  /* Place-holder until we clean up the code
314 
315  WARNING: forward decl only... must be specialized manually for
316  each instance we create
317  */
318  struct pcache_link {
319  static row_cache_t<T>* tls_get();
320 
321  operator row_cache_t<T>*() {
322  return tls_get();
323  }
324 
326  return tls_get();
327  }
328  };
329 
330  pcache_link _pcache; /* pointer to a tuple cache */
331 
332 public:
333 
335  assert (_pcache);
336  return (_pcache);
337  }
338 
339  inline table_row_t* get_tuple() {
340  return (_pcache->borrow());
341  }
342 
343  inline void give_tuple(table_row_t* ptt) {
344  _pcache->giveback(ptt);
345  }
346 }; // EOF: table_man_t
347 
348 #define DEFINE_ROW_CACHE_TLS(bench, table) \
349  DECLARE_TLS(row_cache_t<bench::table##_t>, bench##_##table##_cache); \
350  template<> row_cache_t<bench::table##_t>* \
351  table_man_t<bench::table##_t>::pcache_link::tls_get() \
352  { return bench##_##table##_cache; }
353 
354 #if 0 // CS: disabled for now -- should be moved to other file anyway
355 
356 /* ---------------------------------------------------------------
357  *
358  * @class: table_printer_t
359  *
360  * @brief: Thread to print the table contents
361  *
362  * --------------------------------------------------------------- */
363 
364 class table_printer_t : public thread_t
365 {
366 private:
367 
368  ShoreEnv* _env;
369  int _lines;
370 
371 public:
372 
373  table_printer_t(ShoreEnv* _env, int lines);
374  ~table_printer_t();
375  void work();
376 
377 }; // EOF: table_printer_t
378 
379 
380 
381 
382 
383 
384 
385 /* ---------------------------------------------------------------
386  *
387  * @class: table_fetcher_t
388  *
389  * @brief: Thread to fetch the pages of the table and its indexes
390  *
391  * --------------------------------------------------------------- */
392 
393 class table_fetcher_t : public thread_t
394 {
395 private:
396 
397  ShoreEnv* _env;
398 
399 public:
400 
401  table_fetcher_t(ShoreEnv* _env);
402  ~table_fetcher_t();
403  void work();
404 
405 }; // EOF: table_fetcher_t
406 #endif // 0
407 
408 #endif // __TABLE_MAN_H
blob_pool * ts()
Definition: table_man.h:151
w_rc_t add_index_entry(ss_m *db, const char *idx_name, table_row_t *ptuple, const lock_mode_t lock_mode=okvl_mode::X, const PageID &primary_root=0)
Definition: table_man.cpp:223
virtual w_rc_t print_table(ostream &os, unsigned int num_lines=0)
Definition: table_man.cpp:469
const w_rc_t RCOK
Definition: w_rc.h:239
T * table()
Definition: table_man.h:140
Definition: row_cache.h:41
virtual w_rc_t fetch_table(ss_m *db, lock_mode_t alm=okvl_mode::S)
Definition: table_man.cpp:550
w_rc_t add_tuple(ss_m *db, table_row_t *ptuple, const lock_mode_t lock_mode=okvl_mode::X, const PageID &primary_root=0)
Definition: table_man.cpp:166
Definition: w_okvl.h:113
T * _ptable
Definition: table_man.h:120
: Description of an index.
w_rc_t index_probe_by_name(ss_m *db, const char *idx_name, table_row_t *ptuple, lock_mode_t lock_mode=okvl_mode::S, const PageID &root=0)
Definition: table_man.h:194
Definition: kits_thread.h:134
w_rc_t update_tuple(ss_m *db, table_row_t *ptuple, const lock_mode_t lock_mode=okvl_mode::X)
Definition: table_man.cpp:381
element_lock_mode
Lock mode for one OKVL component (key, partition, or gap).
Definition: w_okvl.h:107
: Base class for tables stored in Shore
pcache_link _pcache
Definition: table_man.h:330
table_row_t * get_tuple()
Definition: table_man.h:339
: Cache for tuples (row_impl<>) used in Shore
Definition: w_okvl.h:108
uint32_t PageID
Definition: basics.h:45
#define w_assert0(x)
Default assert/debug level is 0.
Definition: w_base.h:175
This is the SHORE Storage Manager API.
Definition: sm.h:405
void give_tuple(table_row_t *ptt)
Definition: table_man.h:343
w_rc_t index_probe_forupdate(ss_m *db, index_desc_t *pidx, table_row_t *ptuple, const PageID &root=0)
Definition: table_man.h:169
virtual w_rc_t populate(ss_m *, bool &)
Definition: table_man.h:285
static TableDesc *& ptable()
Definition: row_cache.h:46
w_rc_t load_and_register_fid(ss_m *db)
Definition: table_man.cpp:49
Return code for most functions and methods.
Definition: w_rc.h:87
Definition: index_desc.h:60
w_rc_t index_probe_forupdate_by_name(ss_m *db, const char *idx_name, table_row_t *ptuple, const PageID &root=0)
Definition: table_man.h:204
w_rc_t index_probe_primary(ss_m *db, table_row_t *ptuple, lock_mode_t lock_mode=okvl_mode::S, const PageID &root=0)
Definition: table_man.h:185
guard< blob_pool > _pts
Definition: table_man.h:122
virtual w_rc_t print_index(unsigned i, ostream &os, unsigned int num_lines=0, bool need_tuple=false)
Definition: table_man.cpp:500
Definition: row.h:117
int key_size(index_desc_t *pindex) const
w_rc_t delete_index_entry(ss_m *db, const char *idx_name, table_row_t *ptuple, const lock_mode_t lock_mode=okvl_mode::X, const PageID &primary_root=0)
Definition: table_man.cpp:330
table_man_t(T *aTableDesc, bool construct_cache=true)
Definition: table_man.h:126
w_rc_t index_probe_nl_by_name(ss_m *db, const char *idx_name, table_row_t *ptuple, const PageID &root=0)
Definition: table_man.h:214
: Description and current value of a field (column)
: Base class for records (rows) of tables in Shore
w_rc_t delete_tuple(ss_m *db, table_row_t *ptuple, const lock_mode_t lock_mode=okvl_mode::X, const PageID &primary_root=0)
Definition: table_man.cpp:275
virtual ~table_man_t()
Definition: table_man.h:138
w_rc_t index_probe(ss_m *db, index_desc_t *pidx, table_row_t *ptuple, const lock_mode_t lock_mode=okvl_mode::S, const PageID &root=0)
Definition: table_man.cpp:71
Definition: shore_env.h:349
int min_key(index_desc_t *pindex, table_row_t *ptuple, rep_row_t &arep)
Definition: row.h:147
w_rc_t index_probe_nl(ss_m *db, index_desc_t *pidx, table_row_t *ptuple, const PageID &root=0)
Definition: table_man.h:177
#define T
Definition: w_okvl_inl.h:45
Definition: w_okvl.h:111
Definition: table_man.h:117
row_cache_t< T > * get_cache()
Definition: table_man.h:334
int max_key(index_desc_t *pindex, table_row_t *ptuple, rep_row_t &arep)