Zero  0.1.0
sort.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 
33 #ifndef __SORT_H
34 #define __SORT_H
35 
36 #include "table_man.h"
37 
38 /**********************************************************************
39  *
40  * This file contains the in-memory sort buffer structure definition.
41  * The sort buffer is defined as a subclass of shore_table (defined in
42  * shore_table.h) to take advantage of the schema and tuple value
43  * operations. The data waiting to be sorted is stored in a memory
44  * buffer (sort_asc_buffer_t::_sort_asc_buf).
45  *
46  * @note: To simplify the memory management, the sort buffer only works
47  * on fixed length fields.
48  * Supported sql_types_t: SQL_INT, SQL_SMALLINT.
49  * Under test: SQL_BIT
50  *
51  **********************************************************************/
52 
53 class asc_sort_iter_impl;
54 
55 /**********************************************************************
56  *
57  * @class: sort_asc_buffer_t
58  *
59  * @brief: Description of a sort buffer
60  *
61  **********************************************************************/
62 
64 public:
65 
67  : table_desc_t("ASC_SORT_BUF", field_count) {}
68 
70 
71  /* set the schema - accepts only fixed length */
72  void setup(const size_t index, sqltype_t type, const int len = 0) {
73  assert(index < _field_count);
74  _desc[index].setup(type, "", len);
75  assert(!_desc[index].is_variable_length());
76  assert(!_desc[index].allow_null());
77  }
78 }; // EOF: sort_asc_t
79 
80 
81 
82 /**********************************************************************
83  *
84  * @class: asc_sort_man_impl
85  *
86  * @warning: NO THREAD-SAFE the caller should make sure that only one
87  * thread is accessing objects of this class
88  *
89  **********************************************************************/
90 
91 class asc_sort_man_impl : public table_man_t<asc_sort_buffer_t> {
92  friend class asc_sort_iter_impl;
93 
94 protected:
95 
96  char* _sort_buf; /* memory buffer */
97  int _tuple_size; /* tuple size */
98  int _tuple_count; /* # of tuples in buffer */
99  int _buf_size; /* size of the buffer (in # of tuples) */
100  bool _is_sorted; /* shows if sorted */
102 
103  rep_row_t* _preprow; /* used for the tuple->format() */
104 
105  /* count _tuple_size and allocate buffer */
106  void init();
107 
108  /* retrieve a tuple */
109  bool get_sorted(const int index, table_row_t* ptuple);
110 
111 public:
112 
114  : table_man_t<asc_sort_buffer_t>(aSortBufferAsc, false),
115  _sort_buf(nullptr),
116  _tuple_size(0),
117  _tuple_count(0),
118  _buf_size(0),
119  _is_sorted(false),
120  _preprow(aprow) {}
121 
123  if (_sort_buf) {
124  delete[] _sort_buf;
125  }
126  }
127 
128  /* add current tuple to the sort buffer */
129  void add_tuple(table_row_t& atuple);
130 
131  /* return a sort iterator */
132  w_rc_t get_asc_sort_iter(asc_sort_iter_impl*& asc_sort_iter);
133 
134  w_rc_t get_sort_iter(asc_sort_iter_impl*& sort_iter);
135 
136  /* sort tuples on the first field value */
137  void sort();
138 
139  inline int count() {
140  return (_tuple_count);
141  }
142 
143  void reset();
144 }; // EOF: asc_sort_man_impl
145 
146 
147 /**********************************************************************
148  *
149  * @class: asc_sort_iter_impl
150  *
151  * @brief: Iterator over a sorted buffer
152  *
153  * @note: Iterator that does not need a db handle, since the sorting
154  * takes place only in memory
155  *
156  **********************************************************************/
157 
159 private:
161 
163 
164  int _index;
165 
166 public:
167 
169  : psortbuf(psortbuf),
170  _manager(psortman),
171  _index(0) {
172  assert (_manager);
173  assert (psortbuf);
174  W_COERCE(open_scan());
175  }
176 
177 
178  /* ------------------------------ */
179  /* --- sorted iter operations --- */
180  /* ------------------------------ */
181 
182  w_rc_t open_scan();
183 
185  return (RCOK);
186  };
187 
188  w_rc_t next(bool& eof, table_row_t& tuple);
189 
190  void reset();
191 }; // EOF: asc_sort_iter_impl
192 
193 
194 
195 /**********************************************************************
196  *
197  * asc_sort_iter_impl methods
198  *
199  **********************************************************************/
200 
201 
202 /*********************************************************************
203  *
204  * @fn: open_scan
205  *
206  * @brief: Opens a scan operator
207  *
208  * @note: If the sorted buffer is not sorted, it sorts it
209  *
210  *********************************************************************/
211 
213  _manager->sort();
214 
215  _index = 0;
216  return (RCOK);
217 }
218 
219 /*********************************************************************
220  *
221  * @fn: next
222  *
223  * @brief: Gets the next tuple pointed by the index
224  *
225  *********************************************************************/
226 
227 inline w_rc_t asc_sort_iter_impl::next(bool& eof, table_row_t& tuple) {
228  _manager->get_sorted(_index, &tuple);
229  eof = (++_index > _manager->_tuple_count);
230  return (RCOK);
231 }
232 
233 /*********************************************************************
234  *
235  * @fn: reset
236  *
237  * @brief: Clear the fields and prepares for re-use
238  *
239  *********************************************************************/
240 
242  // the sorter_manager should already be set
243  assert (_manager);
244  _index = 0;
245 }
246 
247 #endif // __SORT_H
void reset()
Definition: sort.h:241
w_rc_t next(bool &eof, table_row_t &tuple)
Definition: sort.h:227
unsigned _field_count
Definition: table_desc.h:127
Definition: table_desc.h:122
bool _is_sorted
Definition: sort.h:100
asc_sort_buffer_t(const size_t field_count)
Definition: sort.h:66
const w_rc_t RCOK
Definition: w_rc.h:239
Definition: sort.h:158
char * _sort_buf
Definition: sort.h:96
field_desc_t * _desc
Definition: table_desc.h:135
asc_sort_iter_impl(asc_sort_buffer_t *psortbuf, asc_sort_man_impl *psortman)
Definition: sort.h:168
asc_sort_man_impl * _manager
Definition: sort.h:162
void setup(const size_t index, sqltype_t type, const int len=0)
Definition: sort.h:72
tatas_lock _sorted_lock
Definition: sort.h:101
unsigned field_count() const
Definition: table_desc.h:247
int count()
Definition: sort.h:139
asc_sort_man_impl(asc_sort_buffer_t *aSortBufferAsc, rep_row_t *aprow)
Definition: sort.h:113
Return code for most functions and methods.
Definition: w_rc.h:87
int _index
Definition: sort.h:164
asc_sort_buffer_t * psortbuf
Definition: sort.h:160
Definition: sort.h:63
int _buf_size
Definition: sort.h:99
w_rc_t open_scan()
Definition: sort.h:212
A test-and-test-and-set spinlock.
Definition: tatas.h:25
Definition: row.h:117
#define W_COERCE(x)
Call a function or method x, fail catastrophically if error is returned.
Definition: w_rc.h:349
~asc_sort_buffer_t()
Definition: sort.h:69
Definition: sort.h:91
int _tuple_count
Definition: sort.h:98
void setup(const sqltype_t type, const char *name, const short size=0, const bool allow_null=false)
Definition: field.h:418
~asc_sort_man_impl()
Definition: sort.h:122
sqltype_t
Definition: field.h:99
: Base class for tables stored in Shore
rep_row_t * _preprow
Definition: sort.h:103
Definition: row.h:147
w_rc_t close_scan()
Definition: sort.h:184
Definition: table_man.h:117
int _tuple_size
Definition: sort.h:97