2014-02-25 16:04:35 +01:00
|
|
|
/* Copyright (c) 2003, 2013, Oracle and/or its affiliates
|
2013-04-07 14:00:16 +02:00
|
|
|
Copyright (c) 2009, 2013, Monty Program Ab.
|
2003-11-02 13:00:25 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2003-11-02 13:00:25 +01:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2019-05-11 20:29:06 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
|
2003-11-02 13:00:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Implementation of a bitmap type.
|
|
|
|
The idea with this is to be able to handle any constant number of bits but
|
|
|
|
also be able to use 32 or 64 bits bitmaps very efficiently
|
|
|
|
*/
|
|
|
|
|
2010-03-31 16:05:33 +02:00
|
|
|
#ifndef SQL_BITMAP_INCLUDED
|
|
|
|
#define SQL_BITMAP_INCLUDED
|
|
|
|
|
2010-05-25 22:01:38 +02:00
|
|
|
#include <my_sys.h>
|
2003-10-24 22:44:48 +02:00
|
|
|
#include <my_bitmap.h>
|
2019-05-09 17:38:22 +02:00
|
|
|
#include <my_bit.h>
|
2003-10-24 22:44:48 +02:00
|
|
|
|
2019-06-17 16:50:59 +02:00
|
|
|
|
|
|
|
/* An iterator to quickly walk over bits in ulonglong bitmap. */
|
2019-06-15 16:04:49 +02:00
|
|
|
class Table_map_iterator
|
|
|
|
{
|
|
|
|
ulonglong bmp;
|
|
|
|
public:
|
2019-06-17 16:50:59 +02:00
|
|
|
Table_map_iterator(ulonglong t): bmp(t){}
|
|
|
|
uint next_bit()
|
2019-06-15 16:04:49 +02:00
|
|
|
{
|
2019-06-17 16:50:59 +02:00
|
|
|
if (!bmp)
|
|
|
|
return BITMAP_END;
|
|
|
|
uint bit= my_find_first_bit(bmp);
|
|
|
|
bmp &= ~(1ULL << bit);
|
|
|
|
return bit;
|
2019-06-15 16:04:49 +02:00
|
|
|
}
|
|
|
|
int operator++(int) { return next_bit(); }
|
|
|
|
enum { BITMAP_END= 64 };
|
|
|
|
};
|
2019-06-12 01:08:09 +02:00
|
|
|
|
|
|
|
template <uint width> class Bitmap
|
|
|
|
{
|
2019-06-07 11:41:18 +02:00
|
|
|
/*
|
|
|
|
Workaround GCC optimizer bug (generating SSE instuctions on unaligned data)
|
|
|
|
*/
|
2019-06-11 12:52:20 +02:00
|
|
|
#if defined (__GNUC__) && defined(__x86_64__) && (__GNUC__ < 6) && !defined(__clang__)
|
2019-06-07 11:41:18 +02:00
|
|
|
#define NEED_GCC_NO_SSE_WORKAROUND
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NEED_GCC_NO_SSE_WORKAROUND
|
|
|
|
#pragma GCC push_options
|
|
|
|
#pragma GCC target ("no-sse")
|
|
|
|
#endif
|
|
|
|
|
2019-06-15 16:04:49 +02:00
|
|
|
private:
|
|
|
|
static const int BITS_PER_ELEMENT= sizeof(ulonglong) * 8;
|
|
|
|
static const int ARRAY_ELEMENTS= (width + BITS_PER_ELEMENT - 1) / BITS_PER_ELEMENT;
|
|
|
|
static const ulonglong ALL_BITS_SET= ULLONG_MAX;
|
|
|
|
|
|
|
|
ulonglong buffer[ARRAY_ELEMENTS];
|
|
|
|
|
|
|
|
uint bit_index(uint n) const
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(n < width);
|
|
|
|
return ARRAY_ELEMENTS == 1 ? 0 : n / BITS_PER_ELEMENT;
|
|
|
|
}
|
|
|
|
ulonglong bit_mask(uint n) const
|
2019-05-09 17:38:22 +02:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
DBUG_ASSERT(n < width);
|
|
|
|
return ARRAY_ELEMENTS == 1 ? 1ULL << n : 1ULL << (n % BITS_PER_ELEMENT);
|
2019-05-09 17:38:22 +02:00
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
ulonglong last_element_mask(int n) const
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(n % BITS_PER_ELEMENT != 0);
|
|
|
|
return bit_mask(n) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
The default constructor does nothing.
|
|
|
|
The caller is supposed to either zero the memory
|
|
|
|
or to call set_all()/clear_all()/set_prefix()
|
|
|
|
to initialize bitmap.
|
|
|
|
*/
|
2023-02-07 12:57:20 +01:00
|
|
|
Bitmap() = default;
|
2019-06-15 16:04:49 +02:00
|
|
|
|
2019-05-09 17:38:22 +02:00
|
|
|
explicit Bitmap(uint prefix)
|
|
|
|
{
|
|
|
|
set_prefix(prefix);
|
|
|
|
}
|
|
|
|
void init(uint prefix)
|
2003-10-24 22:44:48 +02:00
|
|
|
{
|
2019-05-09 17:38:22 +02:00
|
|
|
set_prefix(prefix);
|
|
|
|
}
|
|
|
|
uint length() const
|
|
|
|
{
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
void set_bit(uint n)
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
buffer[bit_index(n)] |= bit_mask(n);
|
2019-05-09 17:38:22 +02:00
|
|
|
}
|
|
|
|
void clear_bit(uint n)
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
buffer[bit_index(n)] &= ~bit_mask(n);
|
|
|
|
}
|
|
|
|
bool is_set(uint n) const
|
|
|
|
{
|
|
|
|
return buffer[bit_index(n)] & bit_mask(n);
|
2019-05-09 17:38:22 +02:00
|
|
|
}
|
|
|
|
void set_prefix(uint prefix_size)
|
|
|
|
{
|
|
|
|
set_if_smaller(prefix_size, width);
|
2015-11-06 20:38:03 +01:00
|
|
|
|
2019-06-15 16:04:49 +02:00
|
|
|
size_t idx= prefix_size / BITS_PER_ELEMENT;
|
|
|
|
|
|
|
|
for (size_t i= 0; i < idx; i++)
|
|
|
|
buffer[i]= ALL_BITS_SET;
|
|
|
|
|
|
|
|
if (prefix_size % BITS_PER_ELEMENT)
|
|
|
|
buffer[idx++]= last_element_mask(prefix_size);
|
|
|
|
|
|
|
|
for (size_t i= idx; i < ARRAY_ELEMENTS; i++)
|
|
|
|
buffer[i]= 0;
|
|
|
|
}
|
|
|
|
bool is_prefix(uint prefix_size) const
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(prefix_size <= width);
|
|
|
|
|
|
|
|
size_t idx= prefix_size / BITS_PER_ELEMENT;
|
|
|
|
|
|
|
|
for (size_t i= 0; i < idx; i++)
|
|
|
|
if (buffer[i] != ALL_BITS_SET)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (prefix_size % BITS_PER_ELEMENT)
|
|
|
|
if (buffer[idx++] != last_element_mask(prefix_size))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i= idx; i < ARRAY_ELEMENTS; i++)
|
|
|
|
if (buffer[i] != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2019-05-09 17:38:22 +02:00
|
|
|
}
|
|
|
|
void set_all()
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
if (width % BITS_PER_ELEMENT)
|
|
|
|
set_prefix(width);
|
|
|
|
else if (ARRAY_ELEMENTS > 1)
|
|
|
|
memset(buffer, 0xff, sizeof(buffer));
|
|
|
|
else
|
|
|
|
buffer[0] = ALL_BITS_SET;
|
2019-05-09 17:38:22 +02:00
|
|
|
}
|
|
|
|
void clear_all()
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
if (ARRAY_ELEMENTS > 1)
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
else
|
|
|
|
buffer[0]= 0;
|
2019-05-09 17:38:22 +02:00
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
void intersect(const Bitmap& map2)
|
2019-05-09 17:38:22 +02:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
|
2019-05-09 17:38:22 +02:00
|
|
|
buffer[i] &= map2.buffer[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/*
|
|
|
|
Intersect with a bitmap represented as as longlong.
|
|
|
|
In addition, pad the rest of the bitmap with 0 or 1 bits
|
|
|
|
depending on pad_with_ones parameter.
|
|
|
|
*/
|
|
|
|
void intersect_and_pad(ulonglong map2buff, bool pad_with_ones)
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
buffer[0] &= map2buff;
|
2019-05-09 17:38:22 +02:00
|
|
|
|
2019-06-15 16:04:49 +02:00
|
|
|
for (size_t i= 1; i < ARRAY_ELEMENTS; i++)
|
|
|
|
buffer[i]= pad_with_ones ? ALL_BITS_SET : 0;
|
2019-05-09 17:38:22 +02:00
|
|
|
|
2019-06-15 16:04:49 +02:00
|
|
|
if (ARRAY_ELEMENTS > 1 && (width % BITS_PER_ELEMENT) && pad_with_ones)
|
|
|
|
buffer[ARRAY_ELEMENTS - 1]= last_element_mask(width);
|
2019-05-09 17:38:22 +02:00
|
|
|
}
|
2015-11-06 20:38:03 +01:00
|
|
|
|
2019-05-09 17:38:22 +02:00
|
|
|
public:
|
|
|
|
void intersect(ulonglong map2buff)
|
|
|
|
{
|
|
|
|
intersect_and_pad(map2buff, 0);
|
2003-10-24 22:44:48 +02:00
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
/* Use highest bit for all bits above first element. */
|
Bug#10932 - Building server with key limit of 128, makes test cases fail
This patch allows to configure MyISAM for 128 indexes per table.
The main problem is the key_map, wich is implemented as an ulonglong.
To get rid of the limit and keep the efficient and flexible
implementation, the highest bit is now used for all upper keys.
This means that the lower keys can be disabled and enabled
individually as usual and the high keys can only be disabled and
enabled as a block. That way the existing test suite is still
applicable, while more keys work, though slightly less efficient.
To really get more than 64 keys, some defines need to be changed.
Another patch will address this.
include/my_bitmap.h:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Added the declaration for a function that extends the highest bit value
to all upper bits of a bigger bitmap.
include/myisam.h:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed MI_MAX_POSSIBLE_KEY to what it was meant for.
Added a bunch of macros to handle the MyISAM key_map.
myisam/mi_check.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_create.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_delete.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_extra.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_open.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
Changed pointer types from signed char* to unsigned char*.
myisam/mi_preload.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_rsame.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_rsamepos.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_search.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_update.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/mi_write.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/myisamchk.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/myisamdef.h:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed pointer types from signed char* to unsigned char*.
myisam/myisamlog.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/myisampack.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
myisam/sort.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
mysys/my_bitmap.c:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Added a function that extends the highest bit value
to all upper bits of a bigger bitmap.
sql/ha_myisam.cc:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Changed key_map access to use the new macros.
sql/sql_bitmap.h:
Bug#10932 - Building server with key limit of 128, makes test cases fail
Added a method that extends the highest bit value
to all upper bits of a bigger bitmap.
2005-07-19 14:13:56 +02:00
|
|
|
void intersect_extended(ulonglong map2buff)
|
|
|
|
{
|
2019-05-09 17:38:22 +02:00
|
|
|
intersect_and_pad(map2buff, (map2buff & (1ULL << 63)));
|
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
void subtract(const Bitmap& map2)
|
2019-05-09 17:38:22 +02:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
|
2019-05-09 17:38:22 +02:00
|
|
|
buffer[i] &= ~(map2.buffer[i]);
|
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
void merge(const Bitmap& map2)
|
2019-05-09 17:38:22 +02:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
|
2019-05-09 17:38:22 +02:00
|
|
|
buffer[i] |= map2.buffer[i];
|
|
|
|
}
|
|
|
|
bool is_clear_all() const
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
|
2019-05-09 17:38:22 +02:00
|
|
|
if (buffer[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
bool is_subset(const Bitmap& map2) const
|
2019-05-09 17:38:22 +02:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
|
2019-05-09 17:38:22 +02:00
|
|
|
if (buffer[i] & ~(map2.buffer[i]))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
bool is_overlapping(const Bitmap& map2) const
|
2019-05-09 17:38:22 +02:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
|
2019-05-09 17:38:22 +02:00
|
|
|
if (buffer[i] & map2.buffer[i])
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
bool operator==(const Bitmap& map2) const
|
2019-05-09 17:38:22 +02:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
if (ARRAY_ELEMENTS > 1)
|
|
|
|
return !memcmp(buffer,map2.buffer,sizeof(buffer));
|
|
|
|
return buffer[0] == map2.buffer[0];
|
2019-05-09 17:38:22 +02:00
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
bool operator!=(const Bitmap& map2) const
|
2019-05-09 17:38:22 +02:00
|
|
|
{
|
|
|
|
return !(*this == map2);
|
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
/*
|
|
|
|
Print hexadecimal representation of bitmap.
|
|
|
|
Truncate trailing zeros.
|
|
|
|
*/
|
2003-10-24 22:44:48 +02:00
|
|
|
char *print(char *buf) const
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
size_t last; /*index of the last non-zero element, or 0. */
|
|
|
|
|
|
|
|
for (last= ARRAY_ELEMENTS - 1; last && !buffer[last]; last--){}
|
|
|
|
|
|
|
|
const int HEX_DIGITS_PER_ELEMENT= BITS_PER_ELEMENT / 4;
|
|
|
|
for (size_t i= 0; i < last; i++)
|
2003-10-24 22:44:48 +02:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
ulonglong num = buffer[i];
|
|
|
|
uint shift = BITS_PER_ELEMENT - 4;
|
|
|
|
size_t pos= i * HEX_DIGITS_PER_ELEMENT;
|
|
|
|
for (size_t j= 0; j < HEX_DIGITS_PER_ELEMENT; j++)
|
|
|
|
{
|
|
|
|
buf[pos + j]= _dig_vec_upper[(num >> shift) & 0xf];
|
|
|
|
shift += 4;
|
|
|
|
}
|
2003-10-24 22:44:48 +02:00
|
|
|
}
|
2019-06-15 16:04:49 +02:00
|
|
|
longlong2str(buffer[last], buf, 16);
|
2003-10-24 22:44:48 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
ulonglong to_ulonglong() const
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
return buffer[0];
|
2003-10-24 22:44:48 +02:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
uint bits_set()
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
uint res= 0;
|
|
|
|
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
|
|
|
|
res += my_count_bits(buffer[i]);
|
2019-05-09 17:38:22 +02:00
|
|
|
return res;
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
2015-11-06 20:38:03 +01:00
|
|
|
class Iterator
|
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
const Bitmap& map;
|
|
|
|
uint offset;
|
|
|
|
Table_map_iterator tmi;
|
2015-11-06 20:38:03 +01:00
|
|
|
public:
|
2019-06-15 16:04:49 +02:00
|
|
|
Iterator(const Bitmap<width>& map2) : map(map2), offset(0), tmi(map2.buffer[0]) {}
|
|
|
|
int operator++(int)
|
|
|
|
{
|
|
|
|
for (;;)
|
2015-11-06 20:38:03 +01:00
|
|
|
{
|
2019-06-15 16:04:49 +02:00
|
|
|
int nextbit= tmi++;
|
|
|
|
|
|
|
|
if (nextbit != Table_map_iterator::BITMAP_END)
|
|
|
|
return offset + nextbit;
|
|
|
|
|
|
|
|
if (offset + BITS_PER_ELEMENT >= map.length())
|
|
|
|
return BITMAP_END;
|
|
|
|
|
|
|
|
offset += BITS_PER_ELEMENT;
|
|
|
|
tmi= Table_map_iterator(map.buffer[offset / BITS_PER_ELEMENT]);
|
2015-11-06 20:38:03 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 17:38:22 +02:00
|
|
|
enum { BITMAP_END = width };
|
2015-11-06 20:38:03 +01:00
|
|
|
};
|
2003-10-24 22:44:48 +02:00
|
|
|
|
2019-06-07 11:41:18 +02:00
|
|
|
#ifdef NEED_GCC_NO_SSE_WORKAROUND
|
|
|
|
#pragma GCC pop_options
|
|
|
|
#undef NEED_GCC_NO_SSE_WORKAROUND
|
|
|
|
#endif
|
2009-08-13 00:34:21 +02:00
|
|
|
};
|
|
|
|
|
2019-06-15 16:04:49 +02:00
|
|
|
typedef Bitmap<MAX_INDEXES> key_map; /* Used for finding keys */
|
2010-03-31 16:05:33 +02:00
|
|
|
|
|
|
|
#endif /* SQL_BITMAP_INCLUDED */
|