2012-08-23 23:49:28 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <city.h>
|
2012-08-23 20:22:44 +00:00
|
|
|
|
#include <openssl/md5.h>
|
|
|
|
|
|
2012-10-07 06:30:10 +00:00
|
|
|
|
#include <DB/Common/SipHash.h>
|
2013-06-30 16:56:00 +00:00
|
|
|
|
#include <DB/Common/Arena.h>
|
2014-04-28 01:48:24 +00:00
|
|
|
|
#include <DB/Common/UInt128.h>
|
2014-10-06 22:48:20 +00:00
|
|
|
|
#include <DB/Core/Defines.h>
|
2012-08-23 23:49:28 +00:00
|
|
|
|
#include <DB/Core/StringRef.h>
|
|
|
|
|
#include <DB/Columns/IColumn.h>
|
2012-08-23 20:22:44 +00:00
|
|
|
|
|
|
|
|
|
|
2014-04-29 00:28:18 +00:00
|
|
|
|
template <>
|
|
|
|
|
struct DefaultHash<StringRef> : public StringRefHash {};
|
|
|
|
|
|
|
|
|
|
|
2012-08-23 20:22:44 +00:00
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2012-08-23 23:49:28 +00:00
|
|
|
|
typedef std::vector<size_t> Sizes;
|
|
|
|
|
|
|
|
|
|
|
2013-06-30 16:56:00 +00:00
|
|
|
|
/// Записать набор ключей фиксированной длины в UInt128, уложив их подряд (при допущении, что они помещаются).
|
2014-10-06 22:48:20 +00:00
|
|
|
|
static inline UInt128 ALWAYS_INLINE pack128(
|
2013-06-30 16:56:00 +00:00
|
|
|
|
size_t i, size_t keys_size, const ConstColumnPlainPtrs & key_columns, const Sizes & key_sizes)
|
2012-08-23 23:49:28 +00:00
|
|
|
|
{
|
|
|
|
|
union
|
|
|
|
|
{
|
2013-06-30 16:56:00 +00:00
|
|
|
|
UInt128 key;
|
2012-10-07 06:30:10 +00:00
|
|
|
|
char bytes[16];
|
2013-06-30 16:56:00 +00:00
|
|
|
|
};
|
2012-08-23 23:49:28 +00:00
|
|
|
|
|
2013-06-30 16:56:00 +00:00
|
|
|
|
memset(bytes, 0, 16);
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
for (size_t j = 0; j < keys_size; ++j)
|
2012-08-23 23:49:28 +00:00
|
|
|
|
{
|
2013-06-30 16:56:00 +00:00
|
|
|
|
StringRef key_data = key_columns[j]->getDataAt(i);
|
|
|
|
|
memcpy(bytes + offset, key_data.data, key_sizes[j]);
|
|
|
|
|
offset += key_sizes[j];
|
2012-08-23 23:49:28 +00:00
|
|
|
|
}
|
2013-06-30 16:56:00 +00:00
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Хэшировать набор ключей в UInt128.
|
2014-10-06 22:48:20 +00:00
|
|
|
|
static inline UInt128 ALWAYS_INLINE hash128(
|
2013-06-30 16:56:00 +00:00
|
|
|
|
size_t i, size_t keys_size, const ConstColumnPlainPtrs & key_columns, StringRefs & keys)
|
|
|
|
|
{
|
2013-08-28 17:13:43 +00:00
|
|
|
|
UInt128 key;
|
2013-06-30 16:56:00 +00:00
|
|
|
|
SipHash hash;
|
2012-08-23 23:49:28 +00:00
|
|
|
|
|
2013-06-30 16:56:00 +00:00
|
|
|
|
for (size_t j = 0; j < keys_size; ++j)
|
|
|
|
|
{
|
|
|
|
|
/// Хэшируем ключ.
|
|
|
|
|
keys[j] = key_columns[j]->getDataAtWithTerminatingZero(i);
|
|
|
|
|
hash.update(keys[j].data, keys[j].size);
|
2012-08-23 23:49:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 17:13:43 +00:00
|
|
|
|
hash.get128(key.first, key.second);
|
2013-06-30 16:56:00 +00:00
|
|
|
|
|
|
|
|
|
return key;
|
2012-08-23 23:49:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-30 16:56:00 +00:00
|
|
|
|
|
|
|
|
|
/// То же самое, но без возврата ссылок на данные ключей.
|
2014-10-06 22:48:20 +00:00
|
|
|
|
static inline UInt128 ALWAYS_INLINE hash128(
|
2013-06-30 16:56:00 +00:00
|
|
|
|
size_t i, size_t keys_size, const ConstColumnPlainPtrs & key_columns)
|
2013-02-17 23:28:52 +00:00
|
|
|
|
{
|
2013-08-28 17:13:43 +00:00
|
|
|
|
UInt128 key;
|
2013-06-30 16:56:00 +00:00
|
|
|
|
SipHash hash;
|
2013-02-17 23:28:52 +00:00
|
|
|
|
|
2013-06-30 16:56:00 +00:00
|
|
|
|
for (size_t j = 0; j < keys_size; ++j)
|
2013-02-17 23:28:52 +00:00
|
|
|
|
{
|
2013-06-30 16:56:00 +00:00
|
|
|
|
/// Хэшируем ключ.
|
|
|
|
|
StringRef key = key_columns[j]->getDataAtWithTerminatingZero(i);
|
|
|
|
|
hash.update(key.data, key.size);
|
2013-02-17 23:28:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 17:13:43 +00:00
|
|
|
|
hash.get128(key.first, key.second);
|
2013-06-30 16:56:00 +00:00
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-17 23:28:52 +00:00
|
|
|
|
|
2013-06-30 16:56:00 +00:00
|
|
|
|
/// Скопировать ключи в пул. Потом разместить в пуле StringRef-ы на них и вернуть указатель на первый.
|
2014-10-06 22:48:20 +00:00
|
|
|
|
static inline StringRef * ALWAYS_INLINE placeKeysInPool(
|
2013-06-30 16:56:00 +00:00
|
|
|
|
size_t i, size_t keys_size, StringRefs & keys, Arena & pool)
|
|
|
|
|
{
|
|
|
|
|
for (size_t j = 0; j < keys_size; ++j)
|
|
|
|
|
{
|
|
|
|
|
char * place = pool.alloc(keys[j].size);
|
|
|
|
|
memcpy(place, keys[j].data, keys[j].size);
|
|
|
|
|
keys[j].data = place;
|
2013-02-17 23:28:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-30 16:56:00 +00:00
|
|
|
|
/// Размещаем в пуле StringRef-ы на только что скопированные ключи.
|
|
|
|
|
char * res = pool.alloc(keys_size * sizeof(StringRef));
|
|
|
|
|
memcpy(res, &keys[0], keys_size * sizeof(StringRef));
|
|
|
|
|
|
|
|
|
|
return reinterpret_cast<StringRef *>(res);
|
2013-02-17 23:28:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-23 23:49:28 +00:00
|
|
|
|
|
2013-09-15 10:53:10 +00:00
|
|
|
|
/// Скопировать ключи в пул. Потом разместить в пуле StringRef-ы на них и вернуть указатель на первый.
|
2014-10-06 22:48:20 +00:00
|
|
|
|
static inline StringRef * ALWAYS_INLINE extractKeysAndPlaceInPool(
|
2013-09-15 10:53:10 +00:00
|
|
|
|
size_t i, size_t keys_size, const ConstColumnPlainPtrs & key_columns, StringRefs & keys, Arena & pool)
|
|
|
|
|
{
|
|
|
|
|
for (size_t j = 0; j < keys_size; ++j)
|
|
|
|
|
{
|
|
|
|
|
keys[j] = key_columns[j]->getDataAtWithTerminatingZero(i);
|
|
|
|
|
char * place = pool.alloc(keys[j].size);
|
|
|
|
|
memcpy(place, keys[j].data, keys[j].size);
|
|
|
|
|
keys[j].data = place;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Размещаем в пуле StringRef-ы на только что скопированные ключи.
|
|
|
|
|
char * res = pool.alloc(keys_size * sizeof(StringRef));
|
|
|
|
|
memcpy(res, &keys[0], keys_size * sizeof(StringRef));
|
|
|
|
|
|
|
|
|
|
return reinterpret_cast<StringRef *>(res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-23 20:22:44 +00:00
|
|
|
|
}
|