FreeTDS API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
buffering.h
1 typedef struct dblib_buffer_row {
5  unsigned char *row_data;
7  DBINT row;
9  TDS_INT *sizes;
11 
12 static void buffer_struct_print(const DBPROC_ROWBUF *buf);
13 static int buffer_save_row(DBPROCESS *dbproc);
14 static DBLIB_BUFFER_ROW* buffer_row_address(const DBPROC_ROWBUF * buf, int idx);
15 
16 #if ENABLE_EXTRA_CHECKS
17 static void buffer_check(const DBPROC_ROWBUF *buf)
18 {
19  int i;
20 
21  /* no buffering */
22  if (buf->capacity == 0 || buf->capacity == 1) {
23  assert(buf->head == 0);
24  assert(buf->tail == 0 || buf->tail == 1);
25  assert(buf->capacity == 1 || buf->rows == NULL);
26  return;
27  }
28 
29  assert(buf->capacity > 0);
30  assert(buf->head >= 0);
31  assert(buf->tail >= 0);
32  assert(buf->head < buf->capacity);
33  assert(buf->tail <= buf->capacity);
34 
35  /* check empty */
36  if (buf->tail == buf->capacity) {
37  assert(buf->head == 0);
38  for (i = 0; buf->rows && i < buf->capacity; ++i) {
39  assert(buf->rows[i].resinfo == NULL);
40  assert(buf->rows[i].row_data == NULL);
41  assert(buf->rows[i].sizes == NULL);
42  assert(buf->rows[i].row == 0);
43  }
44  return;
45  }
46 
47  if (buf->rows == NULL)
48  return;
49 
50  /* check filled part */
51  i = buf->tail;
52  do {
53  assert(i >= 0 && i < buf->capacity);
54  assert(buf->rows[i].resinfo != NULL);
55  assert(buf->rows[i].row > 0);
56  assert(buf->rows[i].row <= buf->received);
57  ++i;
58  if (i == buf->capacity)
59  i = 0;
60  } while (i != buf->head);
61 
62  /* check empty part */
63  if (buf->head != buf->tail) {
64  i = buf->head;
65  do {
66  assert(i >= 0 && i < buf->capacity);
67  assert(buf->rows[i].resinfo == NULL);
68  assert(buf->rows[i].row_data == NULL);
69  assert(buf->rows[i].sizes == NULL);
70  assert(buf->rows[i].row == 0);
71  ++i;
72  if (i == buf->capacity)
73  i = 0;
74  } while (i != buf->tail);
75  }
76 }
77 #define BUFFER_CHECK(buf) buffer_check(buf)
78 #else
79 #define BUFFER_CHECK(buf) do {} while(0)
80 #endif
81 
110 static int
111 buffer_count(const DBPROC_ROWBUF *buf)
112 {
113  BUFFER_CHECK(buf);
114  return (buf->head > buf->tail) ?
115  buf->head - buf->tail : /* |...TddddH....| */
116  buf->capacity - (buf->tail - buf->head); /* |ddddH....Tddd| */
117 }
118 
122 static int
123 buffer_is_full(const DBPROC_ROWBUF *buf)
124 {
125  BUFFER_CHECK(buf);
126  return buf->capacity == buffer_count(buf) && buf->capacity > 1;
127 }
128 
129 #ifndef NDEBUG
130 static int
131 buffer_index_valid(const DBPROC_ROWBUF *buf, int idx)
132 {
133  BUFFER_CHECK(buf);
134  if (buf->tail <= buf->head)
135  if (buf->head <= idx && idx <= buf->tail)
136  return 1;
137 
138  if (0 <= idx && idx <= buf->head)
139  return 1;
140 
141  if (buf->tail <= idx && idx < buf->capacity)
142  return 1;
143 #if 0
144  printf("buffer_index_valid: idx = %d\n", idx);
145  buffer_struct_print(buf);
146 #endif
147  return 0;
148 }
149 #endif
150 
151 static void
152 buffer_free_row(DBLIB_BUFFER_ROW *row)
153 {
154  if (row->sizes)
155  TDS_ZERO_FREE(row->sizes);
156  if (row->row_data) {
157  tds_free_row(row->resinfo, row->row_data);
158  row->row_data = NULL;
159  }
160  tds_free_results(row->resinfo);
161  row->resinfo = NULL;
162  row->row = 0;
163 }
164 
165 /*
166  * Buffer is freed at slightly odd points, whenever
167  * capacity changes:
168  *
169  * 1. When setting capacity, to release prior buffer.
170  * 2. By dbresults. When called the second time, it has to
171  * release prior storage because the new resultset will have
172  * a different width.
173  * 3. By dbclose(), else open/close/open would leak.
174  */
175 static void
176 buffer_free(DBPROC_ROWBUF *buf)
177 {
178  BUFFER_CHECK(buf);
179  if (buf->rows != NULL) {
180  int i;
181  for (i = 0; i < buf->capacity; ++i)
182  buffer_free_row(&buf->rows[i]);
183  TDS_ZERO_FREE(buf->rows);
184  }
185  BUFFER_CHECK(buf);
186 }
187 
188 /*
189  * When no rows are currently buffered (and the buffer is allocated)
190  * set the indices to their initial positions.
191  */
192 static void
193 buffer_reset(DBPROC_ROWBUF *buf)
194 {
195  buf->head = 0;
196  buf->current = buf->tail = buf->capacity;
197  BUFFER_CHECK(buf);
198 }
199 
200 static int
201 buffer_idx_increment(const DBPROC_ROWBUF *buf, int idx)
202 {
203  if (++idx >= buf->capacity) {
204  idx = 0;
205  }
206  return idx;
207 }
208 
213 static DBLIB_BUFFER_ROW*
214 buffer_row_address(const DBPROC_ROWBUF * buf, int idx)
215 {
216  BUFFER_CHECK(buf);
217  if (idx < 0 || idx >= buf->capacity) {
218  printf("idx is %d:\n", idx);
219  buffer_struct_print(buf);
220  return NULL;
221  }
222 
223  return &(buf->rows[idx]);
224 }
225 
229 static DBINT
230 buffer_idx2row(const DBPROC_ROWBUF *buf, int idx)
231 {
232  BUFFER_CHECK(buf);
233  return buffer_row_address(buf, idx)->row;
234 }
235 
239 static int
240 buffer_row2idx(const DBPROC_ROWBUF *buf, int row_number)
241 {
242  int i, ii, idx = -1;
243 
244  BUFFER_CHECK(buf);
245  if (buf->tail == buf->capacity) {
246  assert (buf->head == 0);
247  return -1; /* no rows buffered */
248  }
249 
250  /*
251  * March through the buffers from tail to head, stop if we find our row.
252  * A full queue is indicated by tail == head (which means we can't write).
253  */
254  for (ii=0, i = buf->tail; i != buf->head || ii == 0; i = buffer_idx_increment(buf, i)) {
255  if( buffer_idx2row(buf, i) == row_number) {
256  idx = i;
257  break;
258  }
259  assert(ii++ < buf->capacity); /* prevent infinite loop */
260  }
261 
262  return idx;
263 }
264 
269 static void
270 buffer_delete_rows(DBPROC_ROWBUF * buf, int count)
271 {
272  int i;
273 
274  BUFFER_CHECK(buf);
275  if (count < 0 || count > buffer_count(buf)) {
276  count = buffer_count(buf);
277  }
278 
279  for (i=0; i < count; i++) {
280  if (buf->tail < buf->capacity)
281  buffer_free_row(&buf->rows[buf->tail]);
282  buf->tail = buffer_idx_increment(buf, buf->tail);
283  /*
284  * If deleting rows from the buffer catches the tail to the head,
285  * return to the initial position. Otherwise, it will look full.
286  */
287  if (buf->tail == buf->head) {
288  buffer_reset(buf);
289  break;
290  }
291  }
292 #if 0
293  buffer_struct_print(buf);
294 #endif
295  BUFFER_CHECK(buf);
296 }
297 
298 static void
299 buffer_transfer_bound_data(DBPROC_ROWBUF *buf, TDS_INT res_type, TDS_INT compute_id, DBPROCESS * dbproc, int idx)
300 {
301  int i;
302  int srctype, desttype;
303  BYTE *src;
304  const DBLIB_BUFFER_ROW *row;
305 
306  tdsdump_log(TDS_DBG_FUNC, "buffer_transfer_bound_data(%p %d %d %p %d)\n", buf, res_type, compute_id, dbproc, idx);
307  BUFFER_CHECK(buf);
308  assert(buffer_index_valid(buf, idx));
309 
310  row = buffer_row_address(buf, idx);
311  assert(row->resinfo);
312 
313  for (i = 0; i < row->resinfo->num_cols; i++) {
314  DBINT srclen;
315  TDSCOLUMN *curcol = row->resinfo->columns[i];
316 
317  if (row->sizes)
318  curcol->column_cur_size = row->sizes[i];
319 
320  if (curcol->column_nullbind) {
321  if (curcol->column_cur_size < 0) {
322  *(DBINT *)(curcol->column_nullbind) = -1;
323  } else {
324  *(DBINT *)(curcol->column_nullbind) = 0;
325  }
326  }
327  if (!curcol->column_varaddr)
328  continue;
329 
330  if (row->row_data)
331  src = &row->row_data[curcol->column_data - row->resinfo->current_row];
332  else
333  src = curcol->column_data;
334  srclen = curcol->column_cur_size;
335  if (is_blob_col(curcol))
336  src = (BYTE *) ((TDSBLOB *) src)->textvalue;
337  desttype = _db_get_server_type(curcol->column_bindtype);
338  srctype = tds_get_conversion_type(curcol->column_type, curcol->column_size);
339 
340  if (srclen <= 0) {
341  if (srclen == 0 || !curcol->column_nullbind)
342  dbgetnull(dbproc, curcol->column_bindtype, curcol->column_bindlen,
343  (BYTE *) curcol->column_varaddr);
344  } else {
345  copy_data_to_host_var(dbproc, srctype, src, srclen, desttype,
346  (BYTE *) curcol->column_varaddr, curcol->column_bindlen,
347  curcol->column_bindtype, (DBINT*) curcol->column_nullbind);
348  }
349  }
350 
351  /*
352  * This function always bumps current. Usually, it's called
353  * by dbnextrow(), so bumping current is a pretty obvious choice.
354  * It can also be called by dbgetrow(), but that function also
355  * causes the bump. If you call dbgetrow() for row N, a subsequent
356  * call to dbnextrow() yields N+1.
357  */
358  buf->current = buffer_idx_increment(buf, buf->current);
359 
360 } /* end buffer_transfer_bound_data() */
361 
362 static void
363 buffer_struct_print(const DBPROC_ROWBUF *buf)
364 {
365  assert(buf);
366 
367  printf("\t%d rows in buffer\n", buffer_count(buf));
368 
369  printf("\thead = %d\t", buf->head);
370  printf("\ttail = %d\t", buf->tail);
371  printf("\tcurrent = %d\n", buf->current);
372  printf("\tcapacity = %d\t", buf->capacity);
373  printf("\thead row number = %d\n", buf->received);
374 }
375 
376 /* * * Functions called only by public db-lib API take DBPROCESS* * */
377 
394 static int
395 buffer_current_index(const DBPROCESS *dbproc)
396 {
397  const DBPROC_ROWBUF *buf = &dbproc->row_buf;
398 #if 0
399  buffer_struct_print(buf);
400 #endif
401  if (buf->capacity <= 1) /* no buffering */
402  return -1;
403  if (buf->current == buf->head || buf->current == buf->capacity)
404  return -1;
405 
406  assert(buf->current >= 0);
407  assert(buf->current < buf->capacity);
408 
409  if( buf->tail < buf->head) {
410  assert(buf->tail < buf->current);
411  assert(buf->current < buf->head);
412  } else {
413  if (buf->current > buf->head)
414  assert(buf->current > buf->tail);
415  }
416  return buf->current;
417 }
418 
419 /*
420  * Normally called by dbsetopt() to prepare for buffering
421  * Called with nrows == 0 by dbopen to safely set buf->rows to NULL.
422  */
423 static void
424 buffer_set_capacity(DBPROCESS *dbproc, int nrows)
425 {
426  DBPROC_ROWBUF *buf = &dbproc->row_buf;
427 
428  buffer_free(buf);
429 
430  memset(buf, 0, sizeof(DBPROC_ROWBUF));
431 
432  if (0 == nrows) {
433  buf->capacity = 1;
434  BUFFER_CHECK(buf);
435  return;
436  }
437 
438  assert(0 < nrows);
439 
440  buf->capacity = nrows;
441  BUFFER_CHECK(buf);
442 }
443 
444 /*
445  * Called only by dbresults(); capacity must be >= 1.
446  * Sybase's documents say dbresults() cannot return FAIL if the prior calls worked,
447  * which is a little strange, because (for FreeTDS, at least), dbresults
448  * is when we learn about the result set's width. Without that information, we
449  * can't allocate memory for the buffer. But if we *fail* to allocate memory,
450  * we're not to communicate it back to the caller?
451  */
452 static void
453 buffer_alloc(DBPROCESS *dbproc)
454 {
455  DBPROC_ROWBUF *buf = &dbproc->row_buf;
456 
457  /* Call this function only after setting capacity. */
458 
459  assert(buf);
460  assert(buf->capacity > 0);
461  assert(buf->rows == NULL);
462 
463  buf->rows = (DBLIB_BUFFER_ROW *) calloc(buf->capacity, sizeof(DBLIB_BUFFER_ROW));
464 
465  assert(buf->rows);
466 
467  buffer_reset(buf);
468 
469  buf->received = 0;
470 }
471 
476 static int
477 buffer_add_row(DBPROCESS *dbproc, TDSRESULTINFO *resinfo)
478 {
479  DBPROC_ROWBUF *buf = &dbproc->row_buf;
480  DBLIB_BUFFER_ROW *row;
481  int i;
482 
483  assert(buf->capacity >= 0);
484 
485  if (buffer_is_full(buf))
486  return -1;
487 
488  row = buffer_row_address(buf, buf->head);
489 
490  /* bump the row number, write it, and move the data to head */
491  if (row->resinfo) {
492  tds_free_row(row->resinfo, row->row_data);
493  tds_free_results(row->resinfo);
494  }
495  row->row = ++buf->received;
496  ++resinfo->ref_count;
497  row->resinfo = resinfo;
498  row->row_data = NULL;
499  if (row->sizes)
500  free(row->sizes);
501  row->sizes = (TDS_INT *) calloc(resinfo->num_cols, sizeof(TDS_INT));
502  for (i = 0; i < resinfo->num_cols; ++i)
503  row->sizes[i] = resinfo->columns[i]->column_cur_size;
504 
505  /* initial condition is head == 0 and tail == capacity */
506  if (buf->tail == buf->capacity) {
507  /* bumping this tail will set it to zero */
508  assert(buf->head == 0);
509  buf->tail = 0;
510  }
511 
512  /* update current, bump the head */
513  buf->current = buf->head;
514  buf->head = buffer_idx_increment(buf, buf->head);
515 
516  return buf->current;
517 }
518 
519 static int
520 buffer_save_row(DBPROCESS *dbproc)
521 {
522  DBPROC_ROWBUF *buf = &dbproc->row_buf;
523  DBLIB_BUFFER_ROW *row;
524  int idx = buf->head - 1;
525 
526  if (buf->capacity <= 1)
527  return SUCCEED;
528 
529  if (idx < 0)
530  idx = buf->capacity - 1;
531  if (idx >= 0 && idx < buf->capacity) {
532  row = &buf->rows[idx];
533 
534  if (row->resinfo && !row->row_data) {
535  row->row_data = row->resinfo->current_row;
536  tds_alloc_row(row->resinfo);
537  }
538  }
539 
540  return SUCCEED;
541 }
542