~drizzle-trunk/drizzle/development

1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1239.3.37 by Toru Maesaka
Added a slotted lock mechanism to BlitzLock. This is used to atomically update the index file(s) and the data dictionary with concurrency in mind.
4
 *  Copyright (C) 2009 - 2010 Toru Maesaka
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
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
1800.3.1 by Vijay Samuel
Merge change of <config.h> to "config.h"
20
#include "config.h"
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
21
#include "ha_blitz.h"
22
1239.3.42 by Toru Maesaka
Merged Drizzle's Trunk.
23
using namespace drizzled;
24
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
25
BlitzLock::BlitzLock() : scanner_count(0), updater_count(0) {
26
  pthread_cond_init(&condition, NULL);
27
  pthread_mutex_init(&mutex, NULL);
1239.3.37 by Toru Maesaka
Added a slotted lock mechanism to BlitzLock. This is used to atomically update the index file(s) and the data dictionary with concurrency in mind.
28
29
  for (int i = 0; i < BLITZ_LOCK_SLOTS; i++)
30
    pthread_mutex_init(&slots[i], NULL);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
31
}
32
33
BlitzLock::~BlitzLock() {
34
  pthread_cond_destroy(&condition);
35
  pthread_mutex_destroy(&mutex);
1239.3.37 by Toru Maesaka
Added a slotted lock mechanism to BlitzLock. This is used to atomically update the index file(s) and the data dictionary with concurrency in mind.
36
37
  for (int i = 0; i < BLITZ_LOCK_SLOTS; i++)
38
    pthread_mutex_destroy(&slots[i]);
39
}
40
41
uint32_t BlitzLock::slot_id(const void *data, size_t len) {
42
  uint64_t hash = 14695981039346656037ULL;
43
  const unsigned char *rp = (unsigned char *)data;
44
  const unsigned char *ep = rp + len;
45
46
  while (rp < ep)
47
    hash = (hash ^ *(rp++)) * 109951162811ULL;
48
49
  return (uint32_t)(hash % BLITZ_LOCK_SLOTS);
50
}
51
52
int BlitzLock::slotted_lock(const uint32_t id) {
53
  return pthread_mutex_lock(&slots[id]);
54
}
55
56
int BlitzLock::slotted_unlock(const uint32_t id) {
57
  return pthread_mutex_unlock(&slots[id]);
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
58
}
59
60
void BlitzLock::update_begin() {
61
  pthread_mutex_lock(&mutex);
62
  while (true) {
63
    if (scanner_count < 1) {
64
      updater_count++;
65
      pthread_mutex_unlock(&mutex);
66
      return;
67
    }
68
    pthread_cond_wait(&condition, &mutex);
69
  }
70
  pthread_mutex_unlock(&mutex);
71
}
72
73
void BlitzLock::update_end() {
74
  pthread_mutex_lock(&mutex);
75
  updater_count--;
76
  assert(updater_count >= 0);
77
  if (updater_count == 0)
78
    pthread_cond_broadcast(&condition);
79
  pthread_mutex_unlock(&mutex);
80
}
81
82
void BlitzLock::scan_begin() {
83
  pthread_mutex_lock(&mutex);
84
  while (true) {
85
    if (updater_count == 0) {
86
      scanner_count++;
87
      pthread_mutex_unlock(&mutex);
88
      return;
89
    }
90
    pthread_cond_wait(&condition, &mutex);
91
  }
92
  pthread_mutex_unlock(&mutex);
93
}
94
95
void BlitzLock::scan_end() {
96
  pthread_mutex_lock(&mutex);
97
  scanner_count--;
98
  assert(scanner_count >= 0);
99
  if (scanner_count == 0)
100
    pthread_cond_broadcast(&condition);
101
  pthread_mutex_unlock(&mutex);
102
}
103
104
void BlitzLock::scan_update_begin() {
105
  pthread_mutex_lock(&mutex);
106
  while (true) {
107
    if (scanner_count == 0 && updater_count == 0) {
108
      scanner_count++;
109
      updater_count++;
110
      pthread_mutex_unlock(&mutex);
111
      return;
112
    }
113
    pthread_cond_wait(&condition, &mutex);
114
  }
115
  pthread_mutex_unlock(&mutex);
116
}
117
118
void BlitzLock::scan_update_end() {
119
  pthread_mutex_lock(&mutex);
120
  scanner_count--;
121
  updater_count--;
122
  assert(scanner_count >= 0 && updater_count >= 0);
123
124
  /* All other threads are guaranteed to be
125
     waiting so broadcast regardless. */
126
  pthread_cond_broadcast(&condition);
127
  pthread_mutex_unlock(&mutex);
128
}
129
1239.3.127 by Toru Maesaka
Added more index related tests and renamed BlitzDB's lock functions.
130
int ha_blitz::blitz_optimal_lock() {
1239.3.11 by Toru Maesaka
Added appropriate locking for ALTER TABLE and test case for it. Record the number of indexes in BlitzShare.
131
  if (sql_command_type == SQLCOM_ALTER_TABLE ||
132
      sql_command_type == SQLCOM_UPDATE ||
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
133
      sql_command_type == SQLCOM_DELETE ||
1239.3.4 by Toru Maesaka
Reworked position() and rnd_pos(). Started reworking update_row() for UPDATES with ORDER BY.
134
      sql_command_type == SQLCOM_REPLACE ||
135
      sql_command_type == SQLCOM_REPLACE_SELECT) {
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
136
    share->blitz_lock.scan_update_begin();
137
  } else {
138
    share->blitz_lock.scan_begin();
139
  }
140
  thread_locked = true;
141
  return 0;
142
}
143
1239.3.127 by Toru Maesaka
Added more index related tests and renamed BlitzDB's lock functions.
144
int ha_blitz::blitz_optimal_unlock() {
1239.3.11 by Toru Maesaka
Added appropriate locking for ALTER TABLE and test case for it. Record the number of indexes in BlitzShare.
145
  if (sql_command_type == SQLCOM_ALTER_TABLE ||
146
      sql_command_type == SQLCOM_UPDATE ||
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
147
      sql_command_type == SQLCOM_DELETE ||
1239.3.11 by Toru Maesaka
Added appropriate locking for ALTER TABLE and test case for it. Record the number of indexes in BlitzShare.
148
      sql_command_type == SQLCOM_REPLACE ||
149
      sql_command_type == SQLCOM_REPLACE_SELECT) {
1239.3.1 by Toru Maesaka
Initial import of Project BlitzDB.
150
    share->blitz_lock.scan_update_end();
151
  } else {
152
    share->blitz_lock.scan_end();
153
  }
154
  thread_locked = false;
155
  return 0;
156
}