~drizzle-trunk/drizzle/development

844 by Brian Aker
Added drizzled/discrete_interval.h
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
21
#ifndef DRIZZLED_DISCRETE_INTERVALS_H
22
#define DRIZZLED_DISCRETE_INTERVALS_H
23
24
/*
25
  Such interval is "discrete": it is the set of
26
  { auto_inc_interval_min + k * increment,
27
    0 <= k <= (auto_inc_interval_values-1) }
28
  Where "increment" is maintained separately by the user of this class (and is
29
  currently only session->variables.auto_increment_increment).
30
  It mustn't derive from Sql_alloc, because SET INSERT_ID needs to
31
  allocate memory which must stay allocated for use by the next statement.
32
*/
33
class Discrete_interval {
34
private:
35
  uint64_t interval_min;
36
  uint64_t interval_values;
37
  uint64_t  interval_max;    // excluded bound. Redundant.
38
public:
39
  Discrete_interval *next;    // used when linked into Discrete_intervals_list
40
  void replace(uint64_t start, uint64_t val, uint64_t incr)
41
  {
42
    interval_min=    start;
43
    interval_values= val;
44
    interval_max=    (val == UINT64_MAX) ? val : start + val * incr;
45
  }
46
  Discrete_interval(uint64_t start, uint64_t val, uint64_t incr) :
47
    interval_min(start), interval_values(val),
48
    interval_max((val == UINT64_MAX) ? val : start + val * incr),
49
    next(NULL)
50
  {};
51
  Discrete_interval() :
52
    interval_min(0), interval_values(0),
53
    interval_max(0), next(NULL)
54
  {};
55
  uint64_t minimum() const { return interval_min;    };
56
  uint64_t values()  const { return interval_values; };
57
  uint64_t maximum() const { return interval_max;    };
58
  /*
59
    If appending [3,5] to [1,2], we merge both in [1,5] (they should have the
60
    same increment for that, user of the class has to ensure that). That is
61
    just a space optimization. Returns 0 if merge succeeded.
62
  */
63
  bool merge_if_contiguous(uint64_t start, uint64_t val, uint64_t incr)
64
  {
65
    if (interval_max == start)
66
    {
67
      if (val == UINT64_MAX)
68
      {
69
        interval_values=   interval_max= val;
70
      }
71
      else
72
      {
73
        interval_values+=  val;
74
        interval_max=      start + val * incr;
75
      }
76
      return 0;
77
    }
78
    return 1;
79
  };
80
};
81
82
83
84
/* List of Discrete_interval objects */
85
class Discrete_intervals_list {
86
private:
87
  Discrete_interval        *head;
88
  Discrete_interval        *tail;
89
  /*
90
    When many intervals are provided at the beginning of the execution of a
91
    statement (in a replication slave or SET INSERT_ID), "current" points to
92
    the interval being consumed by the thread now (so "current" goes from
93
    "head" to "tail" then to NULL).
94
  */
95
  Discrete_interval        *current;
96
  uint32_t                  elements; // number of elements
97
98
  /* helper function for copy construct and assignment operator */
99
  void copy_(const Discrete_intervals_list& from)
100
  {
101
    for (Discrete_interval *i= from.head; i; i= i->next)
102
    {
103
      Discrete_interval j= *i;
104
      append(&j);
105
    }
106
  }
107
public:
108
  Discrete_intervals_list() :
109
    head(NULL), tail(NULL),
110
    current(NULL), elements(0) {};
111
  Discrete_intervals_list(const Discrete_intervals_list& from) :
112
    head(NULL), tail(NULL),
113
    current(NULL), elements(0)
114
  {
115
    copy_(from);
116
  }
117
  Discrete_intervals_list& operator=(const Discrete_intervals_list& from)
118
  {
119
    empty();
120
    copy_(from);
121
    return *this;
122
  }
123
  void empty_no_free()
124
  {
125
    head= current= NULL;
126
    elements= 0;
127
  }
128
  void empty()
129
  {
130
    for (Discrete_interval *i= head; i;)
131
    {
132
      Discrete_interval *next= i->next;
133
      delete i;
134
      i= next;
135
    }
136
    empty_no_free();
137
  }
138
139
  const Discrete_interval* get_next()
140
  {
141
    Discrete_interval *tmp= current;
142
    if (current != NULL)
143
      current= current->next;
144
    return tmp;
145
  }
146
  ~Discrete_intervals_list() { empty(); };
147
  uint64_t minimum()     const { return (head ? head->minimum() : 0); };
148
  uint64_t maximum()     const { return (head ? tail->maximum() : 0); };
149
  uint32_t      nb_elements() const { return elements; }
882 by Brian Aker
Minor refactoring (we will need to disconnect the code from the include
150
151
  bool append(uint64_t start, uint64_t val, uint64_t incr)
152
  {
153
    /* first, see if this can be merged with previous */
154
    if ((head == NULL) || tail->merge_if_contiguous(start, val, incr))
155
    {
156
      /* it cannot, so need to add a new interval */
157
      Discrete_interval *new_interval= new Discrete_interval(start, val, incr);
158
      return(append(new_interval));
159
    }
160
    return(0);
161
  }
162
163
  bool append(Discrete_interval *new_interval)
164
  {
165
    if (unlikely(new_interval == NULL))
166
      return(1);
167
    if (head == NULL)
168
      head= current= new_interval;
169
    else
170
      tail->next= new_interval;
171
    tail= new_interval;
172
    elements++;
173
    return(0);
174
  }
175
844 by Brian Aker
Added drizzled/discrete_interval.h
176
};
177
178
#endif /* DRIZZLED_DISCRETE_INTERVALS_H */