~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/xid.cc

  • Committer: Mark Atwood
  • Date: 2011-08-11 03:05:03 UTC
  • mfrom: (2385.1.12 refactor4)
  • Revision ID: me@mark.atwood.name-20110811030503-rp9xjihc5x3y0x4q
mergeĀ lp:~olafvdspek/drizzle/refactor4

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
#include <string.h>
 
20
#include <config.h>
 
21
#include <cstring>
22
22
 
23
 
#include <drizzled/my_hash.h>
24
23
#include <drizzled/xid.h>
25
 
#include "drizzled/charset.h"
26
 
#include "drizzled/global_charset_info.h"
27
 
#include "drizzled/charset_info.h"
28
 
 
29
 
#include <boost/thread/mutex.hpp>
30
 
 
31
 
namespace drizzled
32
 
{
33
 
 
34
 
bool XID::eq(XID *xid)
35
 
{
36
 
  return eq(xid->gtrid_length, xid->bqual_length, xid->data);
37
 
}
38
 
 
39
 
bool XID::eq(long g, long b, const char *d)
40
 
{
41
 
  return g == gtrid_length && b == bqual_length && !memcmp(d, data, g+b);
42
 
}
43
 
 
44
 
void XID::set(XID *xid)
45
 
{
46
 
  memcpy(this, xid, xid->length());
47
 
}
48
 
 
49
 
void XID::set(long f, const char *g, long gl, const char *b, long bl)
50
 
{
51
 
  formatID= f;
52
 
  memcpy(data, g, gtrid_length= gl);
53
 
  memcpy(data+gl, b, bqual_length= bl);
54
 
}
 
24
#include <drizzled/charset.h>
 
25
 
 
26
namespace drizzled {
55
27
 
56
28
void XID::set(uint64_t xid)
57
29
{
58
 
  my_xid tmp;
59
30
  formatID= 1;
60
31
  set(DRIZZLE_XID_PREFIX_LEN, 0, DRIZZLE_XID_PREFIX);
61
 
  memcpy(data+DRIZZLE_XID_PREFIX_LEN, &server_id, sizeof(server_id));
62
 
  tmp= xid;
63
 
  memcpy(data+DRIZZLE_XID_OFFSET, &tmp, sizeof(tmp));
64
 
  gtrid_length=DRIZZLE_XID_GTRID_LEN;
 
32
  memcpy(data + DRIZZLE_XID_PREFIX_LEN, &server_id, sizeof(server_id));
 
33
  my_xid tmp= xid;
 
34
  memcpy(data + DRIZZLE_XID_OFFSET, &tmp, sizeof(tmp));
 
35
  gtrid_length= DRIZZLE_XID_GTRID_LEN;
65
36
}
66
37
 
67
38
void XID::set(long g, long b, const char *d)
72
43
  memcpy(data, d, g+b);
73
44
}
74
45
 
75
 
bool XID::is_null()
 
46
bool XID::is_null() const
76
47
{
77
48
  return formatID == -1;
78
49
}
79
50
 
80
 
void XID::null()
 
51
void XID::set_null()
81
52
{
82
53
  formatID= -1;
83
54
}
85
56
my_xid XID::quick_get_my_xid()
86
57
{
87
58
  my_xid tmp;
88
 
  memcpy(&tmp, data+DRIZZLE_XID_OFFSET, sizeof(tmp));
 
59
  memcpy(&tmp, data + DRIZZLE_XID_OFFSET, sizeof(tmp));
89
60
  return tmp;
90
61
}
91
62
 
97
68
    quick_get_my_xid() : 0;
98
69
}
99
70
 
100
 
uint32_t XID::length()
 
71
uint32_t XID::length() const
101
72
{
102
73
  return sizeof(formatID)+sizeof(gtrid_length)+sizeof(bqual_length)+
103
74
    gtrid_length+bqual_length;
104
75
}
105
76
 
106
 
unsigned char *XID::key()
107
 
{
108
 
  return (unsigned char *)&gtrid_length;
109
 
}
110
 
 
111
 
uint32_t XID::key_length()
112
 
{
113
 
  return sizeof(gtrid_length)+sizeof(bqual_length)+gtrid_length+bqual_length;
114
 
}
115
 
 
116
 
/***************************************************************************
117
 
  Handling of XA id cacheing
118
 
***************************************************************************/
119
 
boost::mutex LOCK_xid_cache;
120
 
HASH xid_cache;
121
 
 
122
 
unsigned char *xid_get_hash_key(const unsigned char *, size_t *, bool);
123
 
void xid_free_hash(void *);
124
 
 
125
 
unsigned char *xid_get_hash_key(const unsigned char *ptr, size_t *length,
126
 
                        bool )
127
 
{
128
 
  *length=((XID_STATE*)ptr)->xid.key_length();
129
 
  return ((XID_STATE*)ptr)->xid.key();
130
 
}
131
 
 
132
 
void xid_free_hash(void *ptr)
133
 
{
134
 
  XID_STATE *state= (XID_STATE *)ptr;
135
 
  if (state->in_session == false)
136
 
    delete state;
137
 
}
138
 
 
139
 
bool xid_cache_init()
140
 
{
141
 
  return hash_init(&xid_cache, &my_charset_bin, 100, 0, 0,
142
 
                   xid_get_hash_key, xid_free_hash, 0) != 0;
143
 
}
144
 
 
145
 
void xid_cache_free()
146
 
{
147
 
  if (hash_inited(&xid_cache))
148
 
  {
149
 
    hash_free(&xid_cache);
150
 
  }
151
 
}
152
 
 
153
 
XID_STATE *xid_cache_search(XID *xid)
154
 
{
155
 
  LOCK_xid_cache.lock();
156
 
  XID_STATE *res=(XID_STATE *)hash_search(&xid_cache, xid->key(), xid->key_length());
157
 
  LOCK_xid_cache.unlock();
158
 
  return res;
159
 
}
160
 
 
161
 
bool xid_cache_insert(XID *xid, enum xa_states xa_state)
162
 
{
163
 
  XID_STATE *xs;
164
 
  bool res;
165
 
  LOCK_xid_cache.lock();
166
 
  if (hash_search(&xid_cache, xid->key(), xid->key_length()))
167
 
  {
168
 
    res= false;
169
 
  }
170
 
  else if ((xs = new XID_STATE) == NULL)
171
 
  {
172
 
    res= true;
173
 
  }
174
 
  else
175
 
  {
176
 
    xs->xa_state=xa_state;
177
 
    xs->xid.set(xid);
178
 
    xs->in_session=0;
179
 
    res= my_hash_insert(&xid_cache, (unsigned char*)xs);
180
 
  }
181
 
  LOCK_xid_cache.unlock();
182
 
  return res;
183
 
}
184
 
 
185
 
bool xid_cache_insert(XID_STATE *xid_state)
186
 
{
187
 
  LOCK_xid_cache.lock();
188
 
  bool res=my_hash_insert(&xid_cache, (unsigned char*)xid_state);
189
 
  LOCK_xid_cache.unlock();
190
 
  return res;
191
 
}
192
 
 
193
 
void xid_cache_delete(XID_STATE *xid_state)
194
 
{
195
 
  LOCK_xid_cache.lock();
196
 
  hash_delete(&xid_cache, (unsigned char *)xid_state);
197
 
  LOCK_xid_cache.unlock();
198
 
}
199
 
 
200
77
} /* namespace drizzled */