~drizzle-trunk/drizzle/development

520.4.31 by Monty Taylor
Removed server_id from common_includes.
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
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
20
#include "config.h"
520.4.31 by Monty Taylor
Removed server_id from common_includes.
21
#include <string.h>
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
22
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
23
#include <drizzled/my_hash.h>
520.4.31 by Monty Taylor
Removed server_id from common_includes.
24
#include <drizzled/xid.h>
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
25
#include "drizzled/charset.h"
26
#include "drizzled/global_charset_info.h"
1241.9.61 by Monty Taylor
No more mystrings in drizzled/
27
#include "drizzled/charset_info.h"
520.4.31 by Monty Taylor
Removed server_id from common_includes.
28
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
29
#include <boost/thread/mutex.hpp>
30
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
31
namespace drizzled
32
{
33
520.4.31 by Monty Taylor
Removed server_id from common_includes.
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
}
55
56
void XID::set(uint64_t xid)
57
{
58
  my_xid tmp;
59
  formatID= 1;
60
  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;
65
}
66
67
void XID::set(long g, long b, const char *d)
68
{
69
  formatID= 1;
70
  gtrid_length= g;
71
  bqual_length= b;
72
  memcpy(data, d, g+b);
73
}
74
75
bool XID::is_null()
76
{
77
  return formatID == -1;
78
}
79
80
void XID::null()
81
{
82
  formatID= -1;
83
}
84
85
my_xid XID::quick_get_my_xid()
86
{
87
  my_xid tmp;
88
  memcpy(&tmp, data+DRIZZLE_XID_OFFSET, sizeof(tmp));
89
  return tmp;
90
}
91
92
my_xid XID::get_my_xid()
93
{
94
  return gtrid_length == DRIZZLE_XID_GTRID_LEN && bqual_length == 0 &&
95
    !memcmp(data+DRIZZLE_XID_PREFIX_LEN, &server_id, sizeof(server_id)) &&
96
    !memcmp(data, DRIZZLE_XID_PREFIX, DRIZZLE_XID_PREFIX_LEN) ?
97
    quick_get_my_xid() : 0;
98
}
99
100
uint32_t XID::length()
101
{
102
  return sizeof(formatID)+sizeof(gtrid_length)+sizeof(bqual_length)+
103
    gtrid_length+bqual_length;
104
}
105
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
}
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
115
116
/***************************************************************************
117
  Handling of XA id cacheing
118
***************************************************************************/
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
119
boost::mutex LOCK_xid_cache;
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
120
HASH xid_cache;
121
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
122
unsigned char *xid_get_hash_key(const unsigned char *, size_t *, bool);
123
void xid_free_hash(void *);
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
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
{
1003.1.10 by Brian Aker
Cleanup to use new/delete
134
  XID_STATE *state= (XID_STATE *)ptr;
135
  if (state->in_session == false)
136
    delete state;
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
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
{
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
155
  LOCK_xid_cache.lock();
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
156
  XID_STATE *res=(XID_STATE *)hash_search(&xid_cache, xid->key(), xid->key_length());
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
157
  LOCK_xid_cache.unlock();
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
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;
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
165
  LOCK_xid_cache.lock();
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
166
  if (hash_search(&xid_cache, xid->key(), xid->key_length()))
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
167
  {
1003.1.10 by Brian Aker
Cleanup to use new/delete
168
    res= false;
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
169
  }
1003.1.10 by Brian Aker
Cleanup to use new/delete
170
  else if ((xs = new XID_STATE) == NULL)
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
171
  {
1003.1.10 by Brian Aker
Cleanup to use new/delete
172
    res= true;
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
173
  }
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
174
  else
175
  {
176
    xs->xa_state=xa_state;
177
    xs->xid.set(xid);
178
    xs->in_session=0;
1003.1.10 by Brian Aker
Cleanup to use new/delete
179
    res= my_hash_insert(&xid_cache, (unsigned char*)xs);
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
180
  }
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
181
  LOCK_xid_cache.unlock();
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
182
  return res;
183
}
184
185
bool xid_cache_insert(XID_STATE *xid_state)
186
{
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
187
  LOCK_xid_cache.lock();
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
188
  bool res=my_hash_insert(&xid_cache, (unsigned char*)xid_state);
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
189
  LOCK_xid_cache.unlock();
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
190
  return res;
191
}
192
193
void xid_cache_delete(XID_STATE *xid_state)
194
{
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
195
  LOCK_xid_cache.lock();
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
196
  hash_delete(&xid_cache, (unsigned char *)xid_state);
1689.2.14 by Brian Aker
LOCK_xid_cache now converted to boost.
197
  LOCK_xid_cache.unlock();
934.2.15 by Jay Pipes
Pulls remainder of XID and xid_cache implementation into xid.cc and xid.h from drizzled/session.cc.
198
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
199
200
} /* namespace drizzled */