~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


#ifndef DRIZZLED_DISCRETE_INTERVAL_H
#define DRIZZLED_DISCRETE_INTERVAL_H

#include <cstdlib>

#include <drizzled/definitions.h>

namespace drizzled
{

/*
  Such interval is "discrete": it is the set of
  { auto_inc_interval_min + k * increment,
    0 <= k <= (auto_inc_interval_values-1) }
  Where "increment" is maintained separately by the user of this class (and is
  currently only session->variables.auto_increment_increment).
  It mustn't derive from memory::SqlAlloc, because SET INSERT_ID needs to
  allocate memory which must stay allocated for use by the next statement.
*/
class Discrete_interval {
private:
  uint64_t interval_min;
  uint64_t interval_values;
  uint64_t  interval_max;    // excluded bound. Redundant.
public:
  Discrete_interval *next;    // used when linked into Discrete_intervals_list
  void replace(uint64_t start, uint64_t val, uint64_t incr)
  {
    interval_min=    start;
    interval_values= val;
    interval_max=    (val == UINT64_MAX) ? val : start + val * incr;
  }
  Discrete_interval(uint64_t start, uint64_t val, uint64_t incr) :
    interval_min(start), interval_values(val),
    interval_max((val == UINT64_MAX) ? val : start + val * incr),
    next(NULL)
  {}
  Discrete_interval() :
    interval_min(0), interval_values(0),
    interval_max(0), next(NULL)
  {}
  uint64_t minimum() const { return interval_min;    }
  uint64_t values()  const { return interval_values; }
  uint64_t maximum() const { return interval_max;    }
  /*
    If appending [3,5] to [1,2], we merge both in [1,5] (they should have the
    same increment for that, user of the class has to ensure that). That is
    just a space optimization. Returns 0 if merge succeeded.
  */
  bool merge_if_contiguous(uint64_t start, uint64_t val, uint64_t incr)
  {
    if (interval_max == start)
    {
      if (val == UINT64_MAX)
      {
        interval_values=   interval_max= val;
      }
      else
      {
        interval_values+=  val;
        interval_max=      start + val * incr;
      }
      return 0;
    }
    return 1;
  }
};



/* List of Discrete_interval objects */
class Discrete_intervals_list {
private:
  Discrete_interval        *head;
  Discrete_interval        *tail;
  /*
    When many intervals are provided at the beginning of the execution of a
    statement (in a replication slave or SET INSERT_ID), "current" points to
    the interval being consumed by the thread now (so "current" goes from
    "head" to "tail" then to NULL).
  */
  Discrete_interval        *current;
  uint32_t                  elements; // number of elements

  /* helper function for copy construct and assignment operator */
  void copy_(const Discrete_intervals_list& from)
  {
    for (Discrete_interval *i= from.head; i; i= i->next)
    {
      Discrete_interval j= *i;
      append(&j);
    }
  }
public:
  Discrete_intervals_list() :
    head(NULL), tail(NULL),
    current(NULL), elements(0) {}
  Discrete_intervals_list(const Discrete_intervals_list& from) :
    head(NULL), tail(NULL),
    current(NULL), elements(0)
  {
    copy_(from);
  }
  Discrete_intervals_list& operator=(const Discrete_intervals_list& from)
  {
    empty();
    copy_(from);
    return *this;
  }
  void empty_no_free()
  {
    head= current= NULL;
    elements= 0;
  }
  void empty()
  {
    for (Discrete_interval *i= head; i;)
    {
      Discrete_interval *next= i->next;
      delete i;
      i= next;
    }
    empty_no_free();
  }

  const Discrete_interval* get_next()
  {
    Discrete_interval *tmp= current;
    if (current != NULL)
      current= current->next;
    return tmp;
  }
  ~Discrete_intervals_list() { empty(); }
  uint64_t minimum()     const { return (head ? head->minimum() : 0); }
  uint64_t maximum()     const { return (head ? tail->maximum() : 0); }
  uint32_t      nb_elements() const { return elements; }

  bool append(uint64_t start, uint64_t val, uint64_t incr)
  {
    /* first, see if this can be merged with previous */
    if ((head == NULL) || tail->merge_if_contiguous(start, val, incr))
    {
      /* it cannot, so need to add a new interval */
      Discrete_interval *new_interval= new Discrete_interval(start, val, incr);
      return(append(new_interval));
    }
    return(0);
  }

  bool append(Discrete_interval *new_interval)
  {
    if (unlikely(new_interval == NULL))
      return(1);
    if (head == NULL)
      head= current= new_interval;
    else
      tail->next= new_interval;
    tail= new_interval;
    elements++;
    return(0);
  }

};

} /* namespace drizzled */

#endif /* DRIZZLED_DISCRETE_INTERVAL_H */