~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/discrete_interval.h

  • Committer: Brian Aker
  • Date: 2010-01-27 18:58:12 UTC
  • Revision ID: brian@gaz-20100127185812-n62n0vwetnx8jrjy
Remove dead code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_INTERVAL_H
 
22
#define DRIZZLED_DISCRETE_INTERVAL_H
 
23
 
 
24
#include <cstdlib>
 
25
 
 
26
#include "drizzled/definitions.h"
 
27
 
 
28
/*
 
29
  Such interval is "discrete": it is the set of
 
30
  { auto_inc_interval_min + k * increment,
 
31
    0 <= k <= (auto_inc_interval_values-1) }
 
32
  Where "increment" is maintained separately by the user of this class (and is
 
33
  currently only session->variables.auto_increment_increment).
 
34
  It mustn't derive from drizzled::memory::SqlAlloc, because SET INSERT_ID needs to
 
35
  allocate memory which must stay allocated for use by the next statement.
 
36
*/
 
37
class Discrete_interval {
 
38
private:
 
39
  uint64_t interval_min;
 
40
  uint64_t interval_values;
 
41
  uint64_t  interval_max;    // excluded bound. Redundant.
 
42
public:
 
43
  Discrete_interval *next;    // used when linked into Discrete_intervals_list
 
44
  void replace(uint64_t start, uint64_t val, uint64_t incr)
 
45
  {
 
46
    interval_min=    start;
 
47
    interval_values= val;
 
48
    interval_max=    (val == UINT64_MAX) ? val : start + val * incr;
 
49
  }
 
50
  Discrete_interval(uint64_t start, uint64_t val, uint64_t incr) :
 
51
    interval_min(start), interval_values(val),
 
52
    interval_max((val == UINT64_MAX) ? val : start + val * incr),
 
53
    next(NULL)
 
54
  {};
 
55
  Discrete_interval() :
 
56
    interval_min(0), interval_values(0),
 
57
    interval_max(0), next(NULL)
 
58
  {};
 
59
  uint64_t minimum() const { return interval_min;    };
 
60
  uint64_t values()  const { return interval_values; };
 
61
  uint64_t maximum() const { return interval_max;    };
 
62
  /*
 
63
    If appending [3,5] to [1,2], we merge both in [1,5] (they should have the
 
64
    same increment for that, user of the class has to ensure that). That is
 
65
    just a space optimization. Returns 0 if merge succeeded.
 
66
  */
 
67
  bool merge_if_contiguous(uint64_t start, uint64_t val, uint64_t incr)
 
68
  {
 
69
    if (interval_max == start)
 
70
    {
 
71
      if (val == UINT64_MAX)
 
72
      {
 
73
        interval_values=   interval_max= val;
 
74
      }
 
75
      else
 
76
      {
 
77
        interval_values+=  val;
 
78
        interval_max=      start + val * incr;
 
79
      }
 
80
      return 0;
 
81
    }
 
82
    return 1;
 
83
  };
 
84
};
 
85
 
 
86
 
 
87
 
 
88
/* List of Discrete_interval objects */
 
89
class Discrete_intervals_list {
 
90
private:
 
91
  Discrete_interval        *head;
 
92
  Discrete_interval        *tail;
 
93
  /*
 
94
    When many intervals are provided at the beginning of the execution of a
 
95
    statement (in a replication slave or SET INSERT_ID), "current" points to
 
96
    the interval being consumed by the thread now (so "current" goes from
 
97
    "head" to "tail" then to NULL).
 
98
  */
 
99
  Discrete_interval        *current;
 
100
  uint32_t                  elements; // number of elements
 
101
 
 
102
  /* helper function for copy construct and assignment operator */
 
103
  void copy_(const Discrete_intervals_list& from)
 
104
  {
 
105
    for (Discrete_interval *i= from.head; i; i= i->next)
 
106
    {
 
107
      Discrete_interval j= *i;
 
108
      append(&j);
 
109
    }
 
110
  }
 
111
public:
 
112
  Discrete_intervals_list() :
 
113
    head(NULL), tail(NULL),
 
114
    current(NULL), elements(0) {};
 
115
  Discrete_intervals_list(const Discrete_intervals_list& from) :
 
116
    head(NULL), tail(NULL),
 
117
    current(NULL), elements(0)
 
118
  {
 
119
    copy_(from);
 
120
  }
 
121
  Discrete_intervals_list& operator=(const Discrete_intervals_list& from)
 
122
  {
 
123
    empty();
 
124
    copy_(from);
 
125
    return *this;
 
126
  }
 
127
  void empty_no_free()
 
128
  {
 
129
    head= current= NULL;
 
130
    elements= 0;
 
131
  }
 
132
  void empty()
 
133
  {
 
134
    for (Discrete_interval *i= head; i;)
 
135
    {
 
136
      Discrete_interval *next= i->next;
 
137
      delete i;
 
138
      i= next;
 
139
    }
 
140
    empty_no_free();
 
141
  }
 
142
 
 
143
  const Discrete_interval* get_next()
 
144
  {
 
145
    Discrete_interval *tmp= current;
 
146
    if (current != NULL)
 
147
      current= current->next;
 
148
    return tmp;
 
149
  }
 
150
  ~Discrete_intervals_list() { empty(); };
 
151
  uint64_t minimum()     const { return (head ? head->minimum() : 0); };
 
152
  uint64_t maximum()     const { return (head ? tail->maximum() : 0); };
 
153
  uint32_t      nb_elements() const { return elements; }
 
154
 
 
155
  bool append(uint64_t start, uint64_t val, uint64_t incr)
 
156
  {
 
157
    /* first, see if this can be merged with previous */
 
158
    if ((head == NULL) || tail->merge_if_contiguous(start, val, incr))
 
159
    {
 
160
      /* it cannot, so need to add a new interval */
 
161
      Discrete_interval *new_interval= new Discrete_interval(start, val, incr);
 
162
      return(append(new_interval));
 
163
    }
 
164
    return(0);
 
165
  }
 
166
 
 
167
  bool append(Discrete_interval *new_interval)
 
168
  {
 
169
    if (unlikely(new_interval == NULL))
 
170
      return(1);
 
171
    if (head == NULL)
 
172
      head= current= new_interval;
 
173
    else
 
174
      tail->next= new_interval;
 
175
    tail= new_interval;
 
176
    elements++;
 
177
    return(0);
 
178
  }
 
179
 
 
180
};
 
181
 
 
182
#endif /* DRIZZLED_DISCRETE_INTERVAL_H */