~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/xid.cc

  • Committer: Brian Aker
  • Date: 2009-07-11 19:23:04 UTC
  • mfrom: (1089.1.14 merge)
  • Revision ID: brian@gaz-20090711192304-ootijyl5yf9jq9kd
Merge Brian

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