Skip to content

Commit

Permalink
wip: find() returns Mapped *
Browse files Browse the repository at this point in the history
  • Loading branch information
akuzm committed Sep 6, 2019
1 parent 88cb396 commit 9e90828
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 121 deletions.
8 changes: 4 additions & 4 deletions dbms/programs/obfuscator/Obfuscator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,21 +670,21 @@ class MarkovModel

while (pos < end)
{
Table::iterator it = table.end();
Table::MappedPtr it;

size_t context_size = params.order;
while (true)
{
it = table.find(hashContext(code_points.data() + code_points.size() - context_size, code_points.data() + code_points.size()));
if (table.end() != it && it->getSecond().total + it->getSecond().count_end != 0)
if (it && it->total + it->count_end != 0)
break;

if (context_size == 0)
break;
--context_size;
}

if (table.end() == it)
if (!it)
throw Exception("Logical error in markov model", ErrorCodes::LOGICAL_ERROR);

size_t offset_from_begin_of_string = pos - data;
Expand All @@ -710,7 +710,7 @@ class MarkovModel
if (num_bytes_after_desired_size > 0)
end_probability_multiplier = std::pow(1.25, num_bytes_after_desired_size);

CodePoint code = it->getSecond().sample(determinator, end_probability_multiplier);
CodePoint code = it->sample(determinator, end_probability_multiplier);

if (code == END)
break;
Expand Down
13 changes: 6 additions & 7 deletions dbms/src/Common/ColumnsHashingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,19 @@ class HashMethodBase
}
}

auto it = data.find(key);
bool found = it != data.end();
auto mapped_ptr = data.find(key);

if constexpr (consecutive_keys_optimization)
{
cache.found = found;
cache.found = mapped_ptr != nullptr;
cache.empty = false;

if constexpr (has_mapped)
{
cache.value.first = key;
if (found)
if (mapped_ptr)
{
cache.value.second = it->getSecond();
cache.value.second = *mapped_ptr;
}
}
else
Expand All @@ -244,9 +243,9 @@ class HashMethodBase
}

if constexpr (has_mapped)
return FindResult(found ? &it->getSecond() : nullptr, found);
return FindResult(mapped_ptr, mapped_ptr != nullptr);
else
return FindResult(found);
return FindResult(mapped_ptr != nullptr);
}
};

Expand Down
6 changes: 3 additions & 3 deletions dbms/src/Common/HashTable/FixedHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ class FixedHashMap : public FixedHashTable<Key, FixedHashMapCell<Key, Mapped>, A
{
for (auto it = this->begin(), end = this->end(); it != end; ++it)
{
decltype(it) res_it = that.find(it->getFirst(), it.getHash());
if (res_it == that.end())
auto res_it = that.find(it->getFirst(), it.getHash());
if (!res_it)
func(it->getSecond(), it->getSecond(), false);
else
func(res_it->getSecond(), it->getSecond(), true);
func(*res_it, it->getSecond(), true);
}
}

Expand Down
17 changes: 9 additions & 8 deletions dbms/src/Common/HashTable/FixedHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class FixedHashTable : private boost::noncopyable, protected Allocator, protecte
using key_type = Key;
using value_type = typename Cell::value_type;
using MappedPtr = typename Cell::mapped_type *;
using ConstMappedPtr = const typename Cell::mapped_type *;

size_t hash(const Key & x) const { return x; }

Expand Down Expand Up @@ -289,24 +290,24 @@ class FixedHashTable : private boost::noncopyable, protected Allocator, protecte
return res;
}

iterator ALWAYS_INLINE find(Key x)
MappedPtr ALWAYS_INLINE find(Key x)
{
return !buf[x].isZero(*this) ? iterator(this, &buf[x]) : end();
return !buf[x].isZero(*this) ? buf[x].getMapped() : nullptr;
}

const_iterator ALWAYS_INLINE find(Key x) const
ConstMappedPtr ALWAYS_INLINE find(Key x) const
{
return !buf[x].isZero(*this) ? const_iterator(this, &buf[x]) : end();
return const_cast<std::decay_t<decltype(*this)> *>(this)->find(x);
}

iterator ALWAYS_INLINE find(Key, size_t hash_value)
MappedPtr ALWAYS_INLINE find(Key, size_t hash_value)
{
return !buf[hash_value].isZero(*this) ? iterator(this, &buf[hash_value]) : end();
return !buf[hash_value].isZero(*this) ? buf[hash_value].getMapped() : nullptr;
}

const_iterator ALWAYS_INLINE find(Key, size_t hash_value) const
ConstMappedPtr ALWAYS_INLINE find(Key key, size_t hash_value) const
{
return !buf[hash_value].isZero(*this) ? const_iterator(this, &buf[hash_value]) : end();
return const_cast<std::decay_t<decltype(*this)> *>(this)->find(key, hash_value);
}

bool ALWAYS_INLINE has(Key x) const { return !buf[x].isZero(*this); }
Expand Down
6 changes: 3 additions & 3 deletions dbms/src/Common/HashTable/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ class HashMapTable : public HashTable<Key, Cell, Hash, Grower, Allocator>
{
for (auto it = this->begin(), end = this->end(); it != end; ++it)
{
decltype(it) res_it = that.find(it->getFirst(), it.getHash());
if (res_it == that.end())
auto res_it = that.find(it->getFirst(), it.getHash());
if (!res_it)
func(it->getSecond(), it->getSecond(), false);
else
func(res_it->getSecond(), it->getSecond(), true);
func(*res_it, it->getSecond(), true);
}
}

Expand Down
38 changes: 13 additions & 25 deletions dbms/src/Common/HashTable/HashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ class HashTable :
using key_type = Key;
using value_type = typename Cell::value_type;
using MappedPtr = typename Cell::mapped_type *;
using ConstMappedPtr = const typename Cell::mapped_type *;

size_t hash(const Key & x) const { return Hash::operator()(x); }

Expand Down Expand Up @@ -796,48 +797,35 @@ class HashTable :
resize();
}

iterator ALWAYS_INLINE find(Key x)
{
if (Cell::isZero(x, *this))
return this->hasZero() ? iteratorToZero() : end();

size_t hash_value = hash(x);
size_t place_value = findCell(x, hash_value, grower.place(hash_value));
return !buf[place_value].isZero(*this) ? iterator(this, &buf[place_value]) : end();
}


const_iterator ALWAYS_INLINE find(Key x) const
MappedPtr ALWAYS_INLINE find(Key x)
{
if (Cell::isZero(x, *this))
return this->hasZero() ? iteratorToZero() : end();
return this->hasZero() ? this->zeroValue()->getMapped() : nullptr;

size_t hash_value = hash(x);
size_t place_value = findCell(x, hash_value, grower.place(hash_value));
return !buf[place_value].isZero(*this) ? const_iterator(this, &buf[place_value]) : end();
return !buf[place_value].isZero(*this)
? buf[place_value].getMapped()
: nullptr;
}


iterator ALWAYS_INLINE find(Key x, size_t hash_value)
ConstMappedPtr ALWAYS_INLINE find(Key x) const
{
if (Cell::isZero(x, *this))
return this->hasZero() ? iteratorToZero() : end();

size_t place_value = findCell(x, hash_value, grower.place(hash_value));
return !buf[place_value].isZero(*this) ? iterator(this, &buf[place_value]) : end();
return const_cast<std::decay_t<decltype(*this)> *>(this)->find(x);
}


const_iterator ALWAYS_INLINE find(Key x, size_t hash_value) const
MappedPtr ALWAYS_INLINE find(Key x, size_t hash_value)
{
if (Cell::isZero(x, *this))
return this->hasZero() ? iteratorToZero() : end();
return this->hasZero() ? this->zeroValue()->getMapped() : nullptr;

size_t place_value = findCell(x, hash_value, grower.place(hash_value));
return !buf[place_value].isZero(*this) ? const_iterator(this, &buf[place_value]) : end();
return !buf[place_value].isZero(*this)
? buf[place_value].getMapped()
: nullptr;
}


bool ALWAYS_INLINE has(Key x) const
{
if (Cell::isZero(x, *this))
Expand Down
25 changes: 7 additions & 18 deletions dbms/src/Common/HashTable/TwoLevelHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class TwoLevelHashTable :
using key_type = typename Impl::key_type;
using value_type = typename Impl::value_type;
using MappedPtr = typename Impl::MappedPtr;
using ConstMappedPtr = typename Impl::ConstMappedPtr;

Impl impls[NUM_BUCKETS];

Expand Down Expand Up @@ -255,31 +256,19 @@ class TwoLevelHashTable :
it = impl_it;
}


iterator ALWAYS_INLINE find(Key x, size_t hash_value)
MappedPtr ALWAYS_INLINE find(Key x, size_t hash_value)
{
size_t buck = getBucketFromHash(hash_value);

typename Impl::iterator found = impls[buck].find(x, hash_value);
return found != impls[buck].end()
? iterator(this, buck, found)
: end();
return impls[buck].find(x, hash_value);
}


const_iterator ALWAYS_INLINE find(Key x, size_t hash_value) const
ConstMappedPtr ALWAYS_INLINE find(Key x, size_t hash_value) const
{
size_t buck = getBucketFromHash(hash_value);

typename Impl::const_iterator found = impls[buck].find(x, hash_value);
return found != impls[buck].end()
? const_iterator(this, buck, found)
: end();
return const_cast<std::decay_t<decltype(*this)> *>(this)->find(x, hash_value);
}


iterator ALWAYS_INLINE find(Key x) { return find(x, hash(x)); }
const_iterator ALWAYS_INLINE find(Key x) const { return find(x, hash(x)); }
MappedPtr ALWAYS_INLINE find(Key x) { return find(x, hash(x)); }
ConstMappedPtr ALWAYS_INLINE find(Key x) const { return find(x, hash(x)); }


void write(DB::WriteBuffer & wb) const
Expand Down
4 changes: 2 additions & 2 deletions dbms/src/Common/SpaceSaving.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ class SpaceSaving
Counter * findCounter(const TKey & key, size_t hash)
{
auto it = counter_map.find(key, hash);
if (it == counter_map.end())
if (!it)
return nullptr;

return it->getSecond();
return *it;
}

void rebuildCounterMap()
Expand Down
12 changes: 6 additions & 6 deletions dbms/src/Common/tests/parallel_aggregation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ void aggregate3(Map & local_map, Map & global_map, Mutex & mutex, Source::const_

for (auto it = begin; it != end; ++it)
{
Map::iterator found = local_map.find(*it);
auto found = local_map.find(*it);

if (found != local_map.end())
++found->getSecond();
if (found)
++*found;
else if (local_map.size() < threshold)
++local_map[*it]; /// TODO You could do one lookup, not two.
else
Expand Down Expand Up @@ -195,10 +195,10 @@ void aggregate4(Map & local_map, MapTwoLevel & global_map, Mutex * mutexes, Sour
{
for (; it != block_end; ++it)
{
Map::iterator found = local_map.find(*it);
auto found = local_map.find(*it);

if (found != local_map.end())
++found->getSecond();
if (found)
++*found;
else
{
size_t hash_value = global_map.hash(*it);
Expand Down
6 changes: 3 additions & 3 deletions dbms/src/DataTypes/DataTypeEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ class DataTypeEnum final : public IDataTypeEnum

FieldType getValue(StringRef field_name) const
{
const auto it = name_to_value_map.find(field_name);
if (it == std::end(name_to_value_map))
const auto value = name_to_value_map.find(field_name);
if (!value)
throw Exception{"Unknown element '" + field_name.toString() + "' for type " + getName(), ErrorCodes::LOGICAL_ERROR};

return it->getSecond();
return *value;
}

Field castToName(const Field & value_or_name) const override;
Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Dictionaries/ComplexKeyCacheDictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ class ComplexKeyCacheDictionary final : public IDictionaryBase
{
const StringRef key = keys_array[row];
const auto it = map.find(key);
const auto string_ref = it != std::end(map) ? it->getSecond() : get_default(row);
const auto string_ref = it ? *it : get_default(row);
out->insertData(string_ref.data, string_ref.size);
}
}
Expand Down
6 changes: 3 additions & 3 deletions dbms/src/Dictionaries/ComplexKeyHashedDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ void ComplexKeyHashedDictionary::updateData()
{
const auto s_key = placeKeysInPool(i, saved_key_column_ptrs, keys, temp_key_pool);
auto it = update_key_hash.find(s_key);
if (it != std::end(update_key_hash))
if (it)
filter[i] = 0;
else
filter[i] = 1;
Expand Down Expand Up @@ -561,7 +561,7 @@ void ComplexKeyHashedDictionary::getItemsImpl(
const auto key = placeKeysInPool(i, key_columns, keys, temporary_keys_pool);

const auto it = attr.find(key);
set_value(i, it != attr.end() ? static_cast<OutputType>(it->getSecond()) : get_default(i));
set_value(i, it ? static_cast<OutputType>(*it) : get_default(i));

/// free memory allocated for the key
temporary_keys_pool.rollback(key.size);
Expand Down Expand Up @@ -672,7 +672,7 @@ void ComplexKeyHashedDictionary::has(const Attribute & attribute, const Columns
const auto key = placeKeysInPool(i, key_columns, keys, temporary_keys_pool);

const auto it = attr.find(key);
out[i] = it != attr.end();
out[i] = static_cast<bool>(it);

/// free memory allocated for the key
temporary_keys_pool.rollback(key.size);
Expand Down
8 changes: 4 additions & 4 deletions dbms/src/Dictionaries/HashedDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ void HashedDictionary::isInImpl(const ChildType & child_ids, const AncestorType
while (id != null_value && id != ancestor_id)
{
auto it = attr.find(id);
if (it != std::end(attr))
id = it->getSecond();
if (it)
id = *it;
else
break;
}
Expand Down Expand Up @@ -555,7 +555,7 @@ void HashedDictionary::getItemsImpl(
for (const auto i : ext::range(0, rows))
{
const auto it = attr.find(ids[i]);
set_value(i, it != attr.end() ? static_cast<OutputType>(it->getSecond()) : get_default(i));
set_value(i, it ? static_cast<OutputType>(*it) : get_default(i));
}

query_count.fetch_add(rows, std::memory_order_relaxed);
Expand Down Expand Up @@ -631,7 +631,7 @@ void HashedDictionary::has(const Attribute & attribute, const PaddedPODArray<Key
const auto rows = ext::size(ids);

for (const auto i : ext::range(0, rows))
out[i] = attr.find(ids[i]) != std::end(attr);
out[i] = static_cast<bool>(attr.find(ids[i]));

query_count.fetch_add(rows, std::memory_order_relaxed);
}
Expand Down

0 comments on commit 9e90828

Please sign in to comment.