1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
23
#include <drizzled/my_hash.h>
24
#include <drizzled/xid.h>
25
#include "drizzled/internal/my_pthread.h"
26
#include "drizzled/charset.h"
27
#include "drizzled/global_charset_info.h"
28
#include "drizzled/charset_info.h"
33
bool XID::eq(XID *xid)
35
return eq(xid->gtrid_length, xid->bqual_length, xid->data);
38
bool XID::eq(long g, long b, const char *d)
40
return g == gtrid_length && b == bqual_length && !memcmp(d, data, g+b);
43
void XID::set(XID *xid)
45
memcpy(this, xid, xid->length());
48
void XID::set(long f, const char *g, long gl, const char *b, long bl)
51
memcpy(data, g, gtrid_length= gl);
52
memcpy(data+gl, b, bqual_length= bl);
55
void XID::set(uint64_t xid)
59
set(DRIZZLE_XID_PREFIX_LEN, 0, DRIZZLE_XID_PREFIX);
60
memcpy(data+DRIZZLE_XID_PREFIX_LEN, &server_id, sizeof(server_id));
62
memcpy(data+DRIZZLE_XID_OFFSET, &tmp, sizeof(tmp));
63
gtrid_length=DRIZZLE_XID_GTRID_LEN;
66
void XID::set(long g, long b, const char *d)
76
return formatID == -1;
84
my_xid XID::quick_get_my_xid()
87
memcpy(&tmp, data+DRIZZLE_XID_OFFSET, sizeof(tmp));
91
my_xid XID::get_my_xid()
93
return gtrid_length == DRIZZLE_XID_GTRID_LEN && bqual_length == 0 &&
94
!memcmp(data+DRIZZLE_XID_PREFIX_LEN, &server_id, sizeof(server_id)) &&
95
!memcmp(data, DRIZZLE_XID_PREFIX, DRIZZLE_XID_PREFIX_LEN) ?
96
quick_get_my_xid() : 0;
99
uint32_t XID::length()
101
return sizeof(formatID)+sizeof(gtrid_length)+sizeof(bqual_length)+
102
gtrid_length+bqual_length;
105
unsigned char *XID::key()
107
return (unsigned char *)>rid_length;
110
uint32_t XID::key_length()
112
return sizeof(gtrid_length)+sizeof(bqual_length)+gtrid_length+bqual_length;
115
/***************************************************************************
116
Handling of XA id cacheing
117
***************************************************************************/
118
pthread_mutex_t LOCK_xid_cache;
121
unsigned char *xid_get_hash_key(const unsigned char *, size_t *, bool);
122
void xid_free_hash(void *);
124
unsigned char *xid_get_hash_key(const unsigned char *ptr, size_t *length,
127
*length=((XID_STATE*)ptr)->xid.key_length();
128
return ((XID_STATE*)ptr)->xid.key();
131
void xid_free_hash(void *ptr)
133
XID_STATE *state= (XID_STATE *)ptr;
134
if (state->in_session == false)
138
bool xid_cache_init()
140
pthread_mutex_init(&LOCK_xid_cache, MY_MUTEX_INIT_FAST);
141
return hash_init(&xid_cache, &my_charset_bin, 100, 0, 0,
142
xid_get_hash_key, xid_free_hash, 0) != 0;
145
void xid_cache_free()
147
if (hash_inited(&xid_cache))
149
hash_free(&xid_cache);
150
pthread_mutex_destroy(&LOCK_xid_cache);
154
XID_STATE *xid_cache_search(XID *xid)
156
pthread_mutex_lock(&LOCK_xid_cache);
157
XID_STATE *res=(XID_STATE *)hash_search(&xid_cache, xid->key(), xid->key_length());
158
pthread_mutex_unlock(&LOCK_xid_cache);
162
bool xid_cache_insert(XID *xid, enum xa_states xa_state)
166
pthread_mutex_lock(&LOCK_xid_cache);
167
if (hash_search(&xid_cache, xid->key(), xid->key_length()))
169
else if ((xs = new XID_STATE) == NULL)
173
xs->xa_state=xa_state;
176
res= my_hash_insert(&xid_cache, (unsigned char*)xs);
178
pthread_mutex_unlock(&LOCK_xid_cache);
182
bool xid_cache_insert(XID_STATE *xid_state)
184
pthread_mutex_lock(&LOCK_xid_cache);
185
bool res=my_hash_insert(&xid_cache, (unsigned char*)xid_state);
186
pthread_mutex_unlock(&LOCK_xid_cache);
190
void xid_cache_delete(XID_STATE *xid_state)
192
pthread_mutex_lock(&LOCK_xid_cache);
193
hash_delete(&xid_cache, (unsigned char *)xid_state);
194
pthread_mutex_unlock(&LOCK_xid_cache);
197
} /* namespace drizzled */